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
|
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
|
// testRemoteState is used to make a test HTTP server to return a given
|
||||||
// state file that can be used for testing legacy remote state.
|
// 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
|
// Get schemas, if possible, before writing state
|
||||||
schemas, diags := getSchemas(&c.Meta, newState, config)
|
var schemas *terraform.Schemas
|
||||||
if diags.HasErrors() && isCloudMode(b) {
|
if isCloudMode(b) {
|
||||||
|
schemas, diags = c.GetSchemas(newState)
|
||||||
|
if diags.HasErrors() {
|
||||||
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Persist the final state
|
// Persist the final state
|
||||||
log.Printf("[INFO] Writing state output to: %s", c.Meta.StateOutPath())
|
log.Printf("[INFO] Writing state output to: %s", c.Meta.StateOutPath())
|
||||||
|
@ -6,6 +6,8 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/hashicorp/terraform/internal/configs"
|
||||||
|
"github.com/hashicorp/terraform/internal/states"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -779,3 +781,43 @@ func (m *Meta) checkRequiredVersion() tfdiags.Diagnostics {
|
|||||||
|
|
||||||
return nil
|
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.
|
// both managers support such metadata.
|
||||||
log.Print("[TRACE] backendMigrateState: migration confirmed, so migrating")
|
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 {
|
if err := statemgr.Migrate(destinationState, sourceState); err != nil {
|
||||||
return fmt.Errorf(strings.TrimSpace(errBackendStateCopy),
|
return fmt.Errorf(strings.TrimSpace(errBackendStateCopy),
|
||||||
opts.SourceType, opts.DestinationType, err)
|
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),
|
return fmt.Errorf(strings.TrimSpace(errBackendStateCopy),
|
||||||
opts.SourceType, opts.DestinationType, err)
|
opts.SourceType, opts.DestinationType, err)
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package command
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hashicorp/terraform/internal/terraform"
|
"github.com/hashicorp/terraform/internal/terraform"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/internal/addrs"
|
"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
|
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)
|
diags = diags.Append(backendDiags)
|
||||||
if backendDiags.HasErrors() {
|
if backendDiags.HasErrors() {
|
||||||
c.showDiagnostics(diags)
|
c.showDiagnostics(diags)
|
||||||
@ -395,23 +394,13 @@ func (c *StateMvCommand) Run(args []string) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get schemas, if possible, before writing state
|
// Get schemas, if possible, before writing state
|
||||||
schemas := &terraform.Schemas{}
|
var schemas *terraform.Schemas
|
||||||
path, err := os.Getwd()
|
if isCloudMode(b) {
|
||||||
schemaErr := err != nil
|
schemas, diags = c.GetSchemas(stateTo)
|
||||||
|
if diags.HasErrors() {
|
||||||
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) {
|
|
||||||
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Write the new state
|
// Write the new state
|
||||||
if err := stateToMgr.WriteState(stateTo); err != nil {
|
if err := stateToMgr.WriteState(stateTo); err != nil {
|
||||||
|
@ -3,6 +3,7 @@ package command
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hashicorp/terraform/internal/terraform"
|
"github.com/hashicorp/terraform/internal/terraform"
|
||||||
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@ -129,23 +130,14 @@ func (c *StatePushCommand) Run(args []string) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get schemas, if possible, before writing state
|
// Get schemas, if possible, before writing state
|
||||||
schemas := &terraform.Schemas{}
|
var schemas *terraform.Schemas
|
||||||
path, err := os.Getwd()
|
if isCloudMode(b) {
|
||||||
schemaErr := err != nil
|
var diags tfdiags.Diagnostics
|
||||||
|
schemas, diags = c.GetSchemas(srcStateFile.State)
|
||||||
if !schemaErr {
|
if diags.HasErrors() {
|
||||||
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) {
|
|
||||||
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := stateMgr.WriteState(srcStateFile.State); err != nil {
|
if err := stateMgr.WriteState(srcStateFile.State); err != nil {
|
||||||
c.Ui.Error(fmt.Sprintf("Failed to write state: %s", err))
|
c.Ui.Error(fmt.Sprintf("Failed to write state: %s", err))
|
||||||
|
@ -3,7 +3,6 @@ package command
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hashicorp/terraform/internal/terraform"
|
"github.com/hashicorp/terraform/internal/terraform"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/internal/addrs"
|
"github.com/hashicorp/terraform/internal/addrs"
|
||||||
@ -162,7 +161,7 @@ func (c *StateReplaceProviderCommand) Run(args []string) int {
|
|||||||
resource.ProviderConfig.Provider = to
|
resource.ProviderConfig.Provider = to
|
||||||
}
|
}
|
||||||
|
|
||||||
b, backendDiags := c.Backend(&BackendOpts{})
|
b, backendDiags := c.Backend(nil)
|
||||||
diags = diags.Append(backendDiags)
|
diags = diags.Append(backendDiags)
|
||||||
if backendDiags.HasErrors() {
|
if backendDiags.HasErrors() {
|
||||||
c.showDiagnostics(diags)
|
c.showDiagnostics(diags)
|
||||||
@ -170,23 +169,13 @@ func (c *StateReplaceProviderCommand) Run(args []string) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get schemas, if possible, before writing state
|
// Get schemas, if possible, before writing state
|
||||||
schemas := &terraform.Schemas{}
|
var schemas *terraform.Schemas
|
||||||
path, err := os.Getwd()
|
if isCloudMode(b) {
|
||||||
schemaErr := err != nil
|
schemas, diags = c.GetSchemas(state)
|
||||||
|
if diags.HasErrors() {
|
||||||
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))
|
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Write the updated state
|
// Write the updated state
|
||||||
if err := stateMgr.WriteState(state); err != nil {
|
if err := stateMgr.WriteState(state); err != nil {
|
||||||
|
@ -3,7 +3,6 @@ package command
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hashicorp/terraform/internal/terraform"
|
"github.com/hashicorp/terraform/internal/terraform"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/internal/addrs"
|
"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
|
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)
|
diags = diags.Append(backendDiags)
|
||||||
if backendDiags.HasErrors() {
|
if backendDiags.HasErrors() {
|
||||||
c.showDiagnostics(diags)
|
c.showDiagnostics(diags)
|
||||||
@ -120,23 +119,13 @@ func (c *StateRmCommand) Run(args []string) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get schemas, if possible, before writing state
|
// Get schemas, if possible, before writing state
|
||||||
schemas := &terraform.Schemas{}
|
var schemas *terraform.Schemas
|
||||||
path, err := os.Getwd()
|
if isCloudMode(b) {
|
||||||
schemaErr := err != nil
|
schemas, diags = c.GetSchemas(state)
|
||||||
|
if diags.HasErrors() {
|
||||||
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))
|
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := stateMgr.WriteState(state); err != nil {
|
if err := stateMgr.WriteState(state); err != nil {
|
||||||
c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
|
c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
|
||||||
|
@ -3,7 +3,6 @@ package command
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hashicorp/terraform/internal/terraform"
|
"github.com/hashicorp/terraform/internal/terraform"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/internal/addrs"
|
"github.com/hashicorp/terraform/internal/addrs"
|
||||||
@ -128,23 +127,13 @@ func (c *TaintCommand) Run(args []string) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get schemas, if possible, before writing state
|
// Get schemas, if possible, before writing state
|
||||||
schemas := &terraform.Schemas{}
|
var schemas *terraform.Schemas
|
||||||
path, err := os.Getwd()
|
if isCloudMode(b) {
|
||||||
schemaErr := err != nil
|
schemas, diags = c.GetSchemas(state)
|
||||||
|
if diags.HasErrors() {
|
||||||
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))
|
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ss := state.SyncWrapper()
|
ss := state.SyncWrapper()
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@ package command
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hashicorp/terraform/internal/terraform"
|
"github.com/hashicorp/terraform/internal/terraform"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/internal/addrs"
|
"github.com/hashicorp/terraform/internal/addrs"
|
||||||
@ -167,23 +166,13 @@ func (c *UntaintCommand) Run(args []string) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get schemas, if possible, before writing state
|
// Get schemas, if possible, before writing state
|
||||||
schemas := &terraform.Schemas{}
|
var schemas *terraform.Schemas
|
||||||
path, err := os.Getwd()
|
if isCloudMode(b) {
|
||||||
schemaErr := err != nil
|
schemas, diags = c.GetSchemas(state)
|
||||||
|
if diags.HasErrors() {
|
||||||
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))
|
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
obj.Status = states.ObjectReady
|
obj.Status = states.ObjectReady
|
||||||
ss.SetResourceInstanceCurrent(addr, obj, rs.ProviderConfig)
|
ss.SetResourceInstanceCurrent(addr, obj, rs.ProviderConfig)
|
||||||
|
Loading…
Reference in New Issue
Block a user