mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
refactor getSchemas
This commit is contained in:
parent
344379f5c7
commit
00cc1ea26d
@ -766,62 +766,6 @@ func testBackendState(t *testing.T, s *states.State, c int) (*legacy.State, *htt
|
||||
return state, srv
|
||||
}
|
||||
|
||||
// testCloudBackendState is used to make a cloud configured
|
||||
// backend.
|
||||
//
|
||||
// When using this function, the configuration fixture for the test must
|
||||
// include an empty configuration block for the HTTP backend, like this:
|
||||
//
|
||||
// terraform {
|
||||
// cloud {
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// If such a block isn't present, then an error will
|
||||
// be returned about the backend configuration having changed and that
|
||||
// "terraform init" must be run, since the test backend config cache created
|
||||
// by this function contains the hash for an empty configuration.
|
||||
func testCloudBackendState(t *testing.T, s *states.State, c int) (*legacy.State, *httptest.Server) {
|
||||
t.Helper()
|
||||
|
||||
var b64md5 string
|
||||
buf := bytes.NewBuffer(nil)
|
||||
|
||||
cb := func(resp http.ResponseWriter, req *http.Request) {
|
||||
if req.Method == "PUT" {
|
||||
resp.WriteHeader(c)
|
||||
return
|
||||
}
|
||||
if s == nil {
|
||||
resp.WriteHeader(404)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Header().Set("Content-MD5", b64md5)
|
||||
resp.Write(buf.Bytes())
|
||||
}
|
||||
|
||||
// If a state was given, make sure we calculate the proper b64md5
|
||||
if s != nil {
|
||||
err := statefile.Write(&statefile.File{State: s}, buf)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
md5 := md5.Sum(buf.Bytes())
|
||||
b64md5 = base64.StdEncoding.EncodeToString(md5[:16])
|
||||
}
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(cb))
|
||||
|
||||
state := legacy.NewState()
|
||||
state.Backend = &legacy.BackendState{
|
||||
Type: "cloud",
|
||||
ConfigRaw: json.RawMessage(fmt.Sprintf(`{"address":%q}`, srv.URL)),
|
||||
}
|
||||
|
||||
return state, srv
|
||||
}
|
||||
|
||||
// testRemoteState is used to make a test HTTP server to return a given
|
||||
// state file that can be used for testing legacy remote state.
|
||||
//
|
||||
|
@ -249,10 +249,13 @@ func (c *ImportCommand) Run(args []string) int {
|
||||
}
|
||||
|
||||
// Get schemas, if possible, before writing state
|
||||
schemas, diags := getSchemas(&c.Meta, newState, config)
|
||||
if diags.HasErrors() && isCloudMode(b) {
|
||||
var schemas *terraform.Schemas
|
||||
if isCloudMode(b) {
|
||||
schemas, diags = c.GetSchemas(newState)
|
||||
if diags.HasErrors() {
|
||||
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
||||
}
|
||||
}
|
||||
|
||||
// Persist the final state
|
||||
log.Printf("[INFO] Writing state output to: %s", c.Meta.StateOutPath())
|
||||
|
@ -6,6 +6,8 @@ import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform/internal/configs"
|
||||
"github.com/hashicorp/terraform/internal/states"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
@ -779,3 +781,43 @@ func (m *Meta) checkRequiredVersion() tfdiags.Diagnostics {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSchemas loads and returns the schemas
|
||||
func (c *Meta) GetSchemas(state *states.State) (*terraform.Schemas, tfdiags.Diagnostics) {
|
||||
var diags tfdiags.Diagnostics
|
||||
var config *configs.Config
|
||||
|
||||
path, err := os.Getwd()
|
||||
if err != nil {
|
||||
diags.Append(err)
|
||||
return nil, diags
|
||||
}
|
||||
|
||||
config, diags = c.loadConfig(path)
|
||||
if diags.HasErrors() {
|
||||
diags.Append(diags)
|
||||
return nil, diags
|
||||
}
|
||||
|
||||
if config != nil || state != nil {
|
||||
opts, err := c.contextOpts()
|
||||
if err != nil {
|
||||
diags = diags.Append(err)
|
||||
return nil, diags
|
||||
}
|
||||
tfCtx, ctxDiags := terraform.NewContext(opts)
|
||||
diags = diags.Append(ctxDiags)
|
||||
if ctxDiags.HasErrors() {
|
||||
return nil, diags
|
||||
}
|
||||
var schemaDiags tfdiags.Diagnostics
|
||||
schemas, schemaDiags := tfCtx.Schemas(config, state)
|
||||
diags = diags.Append(schemaDiags)
|
||||
if schemaDiags.HasErrors() {
|
||||
return nil, diags
|
||||
}
|
||||
return schemas, diags
|
||||
|
||||
}
|
||||
return nil, diags
|
||||
}
|
||||
|
@ -435,26 +435,14 @@ func (m *Meta) backendMigrateState_s_s(opts *backendMigrateOpts) error {
|
||||
// both managers support such metadata.
|
||||
log.Print("[TRACE] backendMigrateState: migration confirmed, so migrating")
|
||||
|
||||
path, err := os.Getwd()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not get working directory")
|
||||
}
|
||||
|
||||
config, diags := m.loadConfig(path)
|
||||
if diags.HasErrors() {
|
||||
return diags.Err()
|
||||
}
|
||||
|
||||
schemas, diags := getSchemas(m, destination, config)
|
||||
if diags.HasErrors() {
|
||||
return diags.Err()
|
||||
}
|
||||
|
||||
if err := statemgr.Migrate(destinationState, sourceState); err != nil {
|
||||
return fmt.Errorf(strings.TrimSpace(errBackendStateCopy),
|
||||
opts.SourceType, opts.DestinationType, err)
|
||||
}
|
||||
if err := destinationState.PersistState(schemas); err != nil {
|
||||
// Fetching schemas during init might be more of a hassle than we want to attempt
|
||||
// in the case that we're migrating to TFC backend, the initial JSON state won't
|
||||
// be generated and stored.
|
||||
if err := destinationState.PersistState(nil); err != nil {
|
||||
return fmt.Errorf(strings.TrimSpace(errBackendStateCopy),
|
||||
opts.SourceType, opts.DestinationType, err)
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package command
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform/internal/terraform"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform/internal/addrs"
|
||||
@ -387,7 +386,7 @@ func (c *StateMvCommand) Run(args []string) int {
|
||||
return 0 // This is as far as we go in dry-run mode
|
||||
}
|
||||
|
||||
b, backendDiags := c.Backend(&BackendOpts{})
|
||||
b, backendDiags := c.Backend(nil)
|
||||
diags = diags.Append(backendDiags)
|
||||
if backendDiags.HasErrors() {
|
||||
c.showDiagnostics(diags)
|
||||
@ -395,23 +394,13 @@ func (c *StateMvCommand) Run(args []string) int {
|
||||
}
|
||||
|
||||
// Get schemas, if possible, before writing state
|
||||
schemas := &terraform.Schemas{}
|
||||
path, err := os.Getwd()
|
||||
schemaErr := err != nil
|
||||
|
||||
if !schemaErr {
|
||||
config, diags := c.loadConfig(path)
|
||||
schemaErr = diags.HasErrors()
|
||||
|
||||
if !schemaErr {
|
||||
schemas, diags = getSchemas(&c.Meta, stateTo, config)
|
||||
schemaErr = diags.HasErrors()
|
||||
}
|
||||
}
|
||||
|
||||
if schemaErr && isCloudMode(b) {
|
||||
var schemas *terraform.Schemas
|
||||
if isCloudMode(b) {
|
||||
schemas, diags = c.GetSchemas(stateTo)
|
||||
if diags.HasErrors() {
|
||||
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
||||
}
|
||||
}
|
||||
|
||||
// Write the new state
|
||||
if err := stateToMgr.WriteState(stateTo); err != nil {
|
||||
|
@ -3,6 +3,7 @@ package command
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform/internal/terraform"
|
||||
"github.com/hashicorp/terraform/internal/tfdiags"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
@ -129,23 +130,14 @@ func (c *StatePushCommand) Run(args []string) int {
|
||||
}
|
||||
|
||||
// Get schemas, if possible, before writing state
|
||||
schemas := &terraform.Schemas{}
|
||||
path, err := os.Getwd()
|
||||
schemaErr := err != nil
|
||||
|
||||
if !schemaErr {
|
||||
config, diags := c.loadConfig(path)
|
||||
schemaErr = diags.HasErrors()
|
||||
|
||||
if !schemaErr {
|
||||
schemas, diags = getSchemas(&c.Meta, srcStateFile.State, config)
|
||||
schemaErr = diags.HasErrors()
|
||||
}
|
||||
}
|
||||
|
||||
if schemaErr && isCloudMode(b) {
|
||||
var schemas *terraform.Schemas
|
||||
if isCloudMode(b) {
|
||||
var diags tfdiags.Diagnostics
|
||||
schemas, diags = c.GetSchemas(srcStateFile.State)
|
||||
if diags.HasErrors() {
|
||||
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
||||
}
|
||||
}
|
||||
|
||||
if err := stateMgr.WriteState(srcStateFile.State); err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Failed to write state: %s", err))
|
||||
|
@ -3,7 +3,6 @@ package command
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform/internal/terraform"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform/internal/addrs"
|
||||
@ -162,7 +161,7 @@ func (c *StateReplaceProviderCommand) Run(args []string) int {
|
||||
resource.ProviderConfig.Provider = to
|
||||
}
|
||||
|
||||
b, backendDiags := c.Backend(&BackendOpts{})
|
||||
b, backendDiags := c.Backend(nil)
|
||||
diags = diags.Append(backendDiags)
|
||||
if backendDiags.HasErrors() {
|
||||
c.showDiagnostics(diags)
|
||||
@ -170,23 +169,13 @@ func (c *StateReplaceProviderCommand) Run(args []string) int {
|
||||
}
|
||||
|
||||
// Get schemas, if possible, before writing state
|
||||
schemas := &terraform.Schemas{}
|
||||
path, err := os.Getwd()
|
||||
schemaErr := err != nil
|
||||
|
||||
if !schemaErr {
|
||||
config, diags := c.loadConfig(path)
|
||||
schemaErr = diags.HasErrors()
|
||||
|
||||
if !schemaErr {
|
||||
schemas, diags = getSchemas(&c.Meta, state, config)
|
||||
schemaErr = diags.HasErrors()
|
||||
}
|
||||
}
|
||||
|
||||
if schemaErr && isCloudMode(b) {
|
||||
var schemas *terraform.Schemas
|
||||
if isCloudMode(b) {
|
||||
schemas, diags = c.GetSchemas(state)
|
||||
if diags.HasErrors() {
|
||||
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
||||
}
|
||||
}
|
||||
|
||||
// Write the updated state
|
||||
if err := stateMgr.WriteState(state); err != nil {
|
||||
|
@ -3,7 +3,6 @@ package command
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform/internal/terraform"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform/internal/addrs"
|
||||
@ -112,7 +111,7 @@ func (c *StateRmCommand) Run(args []string) int {
|
||||
return 0 // This is as far as we go in dry-run mode
|
||||
}
|
||||
|
||||
b, backendDiags := c.Backend(&BackendOpts{})
|
||||
b, backendDiags := c.Backend(nil)
|
||||
diags = diags.Append(backendDiags)
|
||||
if backendDiags.HasErrors() {
|
||||
c.showDiagnostics(diags)
|
||||
@ -120,23 +119,13 @@ func (c *StateRmCommand) Run(args []string) int {
|
||||
}
|
||||
|
||||
// Get schemas, if possible, before writing state
|
||||
schemas := &terraform.Schemas{}
|
||||
path, err := os.Getwd()
|
||||
schemaErr := err != nil
|
||||
|
||||
if !schemaErr {
|
||||
config, diags := c.loadConfig(path)
|
||||
schemaErr = diags.HasErrors()
|
||||
|
||||
if !schemaErr {
|
||||
schemas, diags = getSchemas(&c.Meta, state, config)
|
||||
schemaErr = diags.HasErrors()
|
||||
}
|
||||
}
|
||||
|
||||
if schemaErr && isCloudMode(b) {
|
||||
var schemas *terraform.Schemas
|
||||
if isCloudMode(b) {
|
||||
schemas, diags = c.GetSchemas(state)
|
||||
if diags.HasErrors() {
|
||||
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
||||
}
|
||||
}
|
||||
|
||||
if err := stateMgr.WriteState(state); err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
|
||||
|
@ -3,7 +3,6 @@ package command
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform/internal/terraform"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform/internal/addrs"
|
||||
@ -128,23 +127,13 @@ func (c *TaintCommand) Run(args []string) int {
|
||||
}
|
||||
|
||||
// Get schemas, if possible, before writing state
|
||||
schemas := &terraform.Schemas{}
|
||||
path, err := os.Getwd()
|
||||
schemaErr := err != nil
|
||||
|
||||
if !schemaErr {
|
||||
config, diags := c.loadConfig(path)
|
||||
schemaErr = diags.HasErrors()
|
||||
|
||||
if !schemaErr {
|
||||
schemas, diags = getSchemas(&c.Meta, state, config)
|
||||
schemaErr = diags.HasErrors()
|
||||
}
|
||||
}
|
||||
|
||||
if schemaErr && isCloudMode(b) {
|
||||
var schemas *terraform.Schemas
|
||||
if isCloudMode(b) {
|
||||
schemas, diags = c.GetSchemas(state)
|
||||
if diags.HasErrors() {
|
||||
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
||||
}
|
||||
}
|
||||
|
||||
ss := state.SyncWrapper()
|
||||
|
||||
|
@ -3,7 +3,6 @@ package command
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform/internal/terraform"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform/internal/addrs"
|
||||
@ -167,23 +166,13 @@ func (c *UntaintCommand) Run(args []string) int {
|
||||
}
|
||||
|
||||
// Get schemas, if possible, before writing state
|
||||
schemas := &terraform.Schemas{}
|
||||
path, err := os.Getwd()
|
||||
schemaErr := err != nil
|
||||
|
||||
if !schemaErr {
|
||||
config, diags := c.loadConfig(path)
|
||||
schemaErr = diags.HasErrors()
|
||||
|
||||
if !schemaErr {
|
||||
schemas, diags = getSchemas(&c.Meta, state, config)
|
||||
schemaErr = diags.HasErrors()
|
||||
}
|
||||
}
|
||||
|
||||
if schemaErr && isCloudMode(b) {
|
||||
var schemas *terraform.Schemas
|
||||
if isCloudMode(b) {
|
||||
schemas, diags = c.GetSchemas(state)
|
||||
if diags.HasErrors() {
|
||||
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
||||
}
|
||||
}
|
||||
|
||||
obj.Status = states.ObjectReady
|
||||
ss.SetResourceInstanceCurrent(addr, obj, rs.ProviderConfig)
|
||||
|
Loading…
Reference in New Issue
Block a user