update flow of schema checking

This commit is contained in:
Megan Bang 2022-08-26 16:00:20 -05:00
parent 12e3560d21
commit d5decc2407
6 changed files with 81 additions and 56 deletions

View File

@ -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))

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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)