From d5decc2407a918f0a3dec61e921c07a1a640340c Mon Sep 17 00:00:00 2001 From: Megan Bang Date: Fri, 26 Aug 2022 16:00:20 -0500 Subject: [PATCH] update flow of schema checking --- internal/command/state_mv.go | 24 ++++++++++++--------- internal/command/state_push.go | 22 +++++++++++-------- internal/command/state_replace_provider.go | 22 +++++++++++-------- internal/command/state_rm.go | 22 +++++++++++-------- internal/command/taint.go | 22 +++++++++++-------- internal/command/untaint.go | 25 +++++++++++++--------- 6 files changed, 81 insertions(+), 56 deletions(-) diff --git a/internal/command/state_mv.go b/internal/command/state_mv.go index d057834b59..93b8117ca3 100644 --- a/internal/command/state_mv.go +++ b/internal/command/state_mv.go @@ -2,6 +2,7 @@ package command import ( "fmt" + "github.com/hashicorp/terraform/internal/terraform" "os" "strings" @@ -393,22 +394,25 @@ func (c *StateMvCommand) Run(args []string) int { return 1 } + // Get schemas, if possible, before writing state + schemas := &terraform.Schemas{} path, err := os.Getwd() - if err != nil && isCloudMode(b) { - c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) + 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() + } } - config, diags := c.loadConfig(path) - if diags.HasErrors() && isCloudMode(b) { + if schemaErr && isCloudMode(b) { c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) } - schemas, diags := getSchemas(&c.Meta, stateTo, config) - if diags.HasErrors() && isCloudMode(b) { - c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) - return 1 - } - // Write the new state if err := stateToMgr.WriteState(stateTo); err != nil { c.Ui.Error(fmt.Sprintf(errStateRmPersist, err)) diff --git a/internal/command/state_push.go b/internal/command/state_push.go index 1d9dfcc52b..95dc0fb810 100644 --- a/internal/command/state_push.go +++ b/internal/command/state_push.go @@ -2,6 +2,7 @@ package command import ( "fmt" + "github.com/hashicorp/terraform/internal/terraform" "io" "os" "strings" @@ -128,18 +129,21 @@ func (c *StatePushCommand) Run(args []string) int { } // Get schemas, if possible, before writing state + schemas := &terraform.Schemas{} path, err := os.Getwd() - if err != nil && isCloudMode(b) { - c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) + 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() + } } - config, diags := c.loadConfig(path) - if diags.HasErrors() && isCloudMode(b) { - c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) - } - - schemas, diags := getSchemas(&c.Meta, srcStateFile.State, config) - if diags.HasErrors() && isCloudMode(b) { + if schemaErr && isCloudMode(b) { c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) } diff --git a/internal/command/state_replace_provider.go b/internal/command/state_replace_provider.go index 19a1f8df77..684e8d3409 100644 --- a/internal/command/state_replace_provider.go +++ b/internal/command/state_replace_provider.go @@ -2,6 +2,7 @@ package command import ( "fmt" + "github.com/hashicorp/terraform/internal/terraform" "os" "strings" @@ -169,18 +170,21 @@ func (c *StateReplaceProviderCommand) Run(args []string) int { } // Get schemas, if possible, before writing state + schemas := &terraform.Schemas{} path, err := os.Getwd() - if err != nil && isCloudMode(b) { - c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) + 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() + } } - config, diags := c.loadConfig(path) - if diags.HasErrors() && isCloudMode(b) { - c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) - } - - schemas, diags := getSchemas(&c.Meta, state, config) - if diags.HasErrors() && isCloudMode(b) { + if schemaErr && isCloudMode(b) { c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) } diff --git a/internal/command/state_rm.go b/internal/command/state_rm.go index 8dfc9f7eb5..0344d51ce5 100644 --- a/internal/command/state_rm.go +++ b/internal/command/state_rm.go @@ -2,6 +2,7 @@ package command import ( "fmt" + "github.com/hashicorp/terraform/internal/terraform" "os" "strings" @@ -119,18 +120,21 @@ func (c *StateRmCommand) Run(args []string) int { } // Get schemas, if possible, before writing state + schemas := &terraform.Schemas{} path, err := os.Getwd() - if err != nil && isCloudMode(b) { - c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) + 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() + } } - config, diags := c.loadConfig(path) - if diags.HasErrors() && isCloudMode(b) { - c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) - } - - schemas, diags := getSchemas(&c.Meta, state, config) - if diags.HasErrors() && isCloudMode(b) { + if schemaErr && isCloudMode(b) { c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) } diff --git a/internal/command/taint.go b/internal/command/taint.go index 03ddbbddd7..d2710d6b7f 100644 --- a/internal/command/taint.go +++ b/internal/command/taint.go @@ -2,6 +2,7 @@ package command import ( "fmt" + "github.com/hashicorp/terraform/internal/terraform" "os" "strings" @@ -127,18 +128,21 @@ func (c *TaintCommand) Run(args []string) int { } // Get schemas, if possible, before writing state + schemas := &terraform.Schemas{} path, err := os.Getwd() - if err != nil && isCloudMode(b) { - c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) + 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() + } } - config, diags := c.loadConfig(path) - if diags.HasErrors() && isCloudMode(b) { - c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) - } - - schemas, diags := getSchemas(&c.Meta, state, config) - if diags.HasErrors() && isCloudMode(b) { + if schemaErr && isCloudMode(b) { c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) } diff --git a/internal/command/untaint.go b/internal/command/untaint.go index 68d29d99be..d66b6d1bce 100644 --- a/internal/command/untaint.go +++ b/internal/command/untaint.go @@ -2,6 +2,7 @@ package command import ( "fmt" + "github.com/hashicorp/terraform/internal/terraform" "os" "strings" @@ -166,20 +167,24 @@ func (c *UntaintCommand) Run(args []string) int { } // Get schemas, if possible, before writing state + schemas := &terraform.Schemas{} path, err := os.Getwd() - if err != nil && isCloudMode(b) { + 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) { c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) } - config, diags := c.loadConfig(path) - if diags.HasErrors() && isCloudMode(b) { - c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) - } - - schemas, diags := getSchemas(&c.Meta, state, config) - if diags.HasErrors() && isCloudMode(b) { - c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err)) - } obj.Status = states.ObjectReady ss.SetResourceInstanceCurrent(addr, obj, rs.ProviderConfig)