refactor GetSchemas to include an option to pass in a config

This commit is contained in:
Megan Bang 2022-08-29 11:32:14 -05:00
parent 40263cd861
commit b572e57fb3
11 changed files with 16 additions and 45 deletions

View File

@ -3,10 +3,6 @@ package command
import (
"github.com/hashicorp/terraform/internal/backend"
"github.com/hashicorp/terraform/internal/cloud"
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/internal/terraform"
"github.com/hashicorp/terraform/internal/tfdiags"
)
const failedToLoadSchemasMessage = `
@ -24,29 +20,3 @@ func isCloudMode(b backend.Enhanced) bool {
return ok
}
func getSchemas(c *Meta, state *states.State, config *configs.Config) (*terraform.Schemas, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
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
}

View File

@ -251,7 +251,7 @@ func (c *ImportCommand) Run(args []string) int {
// Get schemas, if possible, before writing state
var schemas *terraform.Schemas
if isCloudMode(b) {
schemas, diags = c.GetSchemas(newState)
schemas, diags = c.GetSchemas(newState, nil)
if diags.HasErrors() {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}

View File

@ -783,9 +783,8 @@ func (m *Meta) checkRequiredVersion() tfdiags.Diagnostics {
}
// GetSchemas loads and returns the schemas
func (c *Meta) GetSchemas(state *states.State) (*terraform.Schemas, tfdiags.Diagnostics) {
func (c *Meta) GetSchemas(state *states.State, config *configs.Config) (*terraform.Schemas, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
var config *configs.Config
path, err := os.Getwd()
if err != nil {
@ -793,10 +792,12 @@ func (c *Meta) GetSchemas(state *states.State) (*terraform.Schemas, tfdiags.Diag
return nil, diags
}
config, diags = c.loadConfig(path)
if diags.HasErrors() {
diags.Append(diags)
return nil, diags
if config == nil {
config, diags = c.loadConfig(path)
if diags.HasErrors() {
diags.Append(diags)
return nil, diags
}
}
if config != nil || state != nil {

View File

@ -110,7 +110,7 @@ func (c *ShowCommand) show(path string) (*plans.Plan, *statefile.File, *configs.
// Get schemas, if possible
if config != nil || stateFile != nil {
schemas, diags = getSchemas(&c.Meta, stateFile.State, config)
schemas, diags = c.GetSchemas(stateFile.State, config)
if diags.HasErrors() {
return plan, stateFile, config, schemas, diags
}

View File

@ -396,7 +396,7 @@ func (c *StateMvCommand) Run(args []string) int {
// Get schemas, if possible, before writing state
var schemas *terraform.Schemas
if isCloudMode(b) {
schemas, diags = c.GetSchemas(stateTo)
schemas, diags = c.GetSchemas(stateTo, nil)
if diags.HasErrors() {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}

View File

@ -133,7 +133,7 @@ func (c *StatePushCommand) Run(args []string) int {
var schemas *terraform.Schemas
if isCloudMode(b) {
var diags tfdiags.Diagnostics
schemas, diags = c.GetSchemas(srcStateFile.State)
schemas, diags = c.GetSchemas(srcStateFile.State, nil)
if diags.HasErrors() {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}

View File

@ -171,7 +171,7 @@ func (c *StateReplaceProviderCommand) Run(args []string) int {
// Get schemas, if possible, before writing state
var schemas *terraform.Schemas
if isCloudMode(b) {
schemas, diags = c.GetSchemas(state)
schemas, diags = c.GetSchemas(state, nil)
if diags.HasErrors() {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}

View File

@ -121,7 +121,7 @@ func (c *StateRmCommand) Run(args []string) int {
// Get schemas, if possible, before writing state
var schemas *terraform.Schemas
if isCloudMode(b) {
schemas, diags = c.GetSchemas(state)
schemas, diags = c.GetSchemas(state, nil)
if diags.HasErrors() {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}

View File

@ -129,7 +129,7 @@ func (c *TaintCommand) Run(args []string) int {
// Get schemas, if possible, before writing state
var schemas *terraform.Schemas
if isCloudMode(b) {
schemas, diags = c.GetSchemas(state)
schemas, diags = c.GetSchemas(state, nil)
if diags.HasErrors() {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}

View File

@ -1,11 +1,11 @@
package command
import (
"github.com/google/go-cmp/cmp"
"os"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/mitchellh/cli"
"github.com/hashicorp/terraform/internal/addrs"

View File

@ -168,7 +168,7 @@ func (c *UntaintCommand) Run(args []string) int {
// Get schemas, if possible, before writing state
var schemas *terraform.Schemas
if isCloudMode(b) {
schemas, diags = c.GetSchemas(state)
schemas, diags = c.GetSchemas(state, nil)
if diags.HasErrors() {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}