check for cloud integration mode

This commit is contained in:
Megan Bang 2022-08-26 15:32:18 -05:00
parent 021f1f69e9
commit 12e3560d21
9 changed files with 104 additions and 19 deletions

View File

@ -766,6 +766,62 @@ 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.
//

View File

@ -1,6 +1,8 @@
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"
@ -17,6 +19,12 @@ however, historic state information may be missing if the affected integration r
%s
`
func isCloudMode(b backend.Enhanced) bool {
_, ok := b.(*cloud.Cloud)
return ok
}
func getSchemas(c *Meta, state *states.State, config *configs.Config) (*terraform.Schemas, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics

View File

@ -250,7 +250,7 @@ func (c *ImportCommand) Run(args []string) int {
// Get schemas, if possible, before writing state
schemas, diags := getSchemas(&c.Meta, newState, config)
if diags.HasErrors() {
if diags.HasErrors() && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}

View File

@ -386,18 +386,25 @@ 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{})
diags = diags.Append(backendDiags)
if backendDiags.HasErrors() {
c.showDiagnostics(diags)
return 1
}
path, err := os.Getwd()
if err != nil {
if err != nil && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}
config, diags := c.loadConfig(path)
if diags.HasErrors() {
if diags.HasErrors() && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}
schemas, diags := getSchemas(&c.Meta, stateTo, config)
if diags.HasErrors() {
if diags.HasErrors() && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
return 1
}

View File

@ -129,17 +129,17 @@ func (c *StatePushCommand) Run(args []string) int {
// Get schemas, if possible, before writing state
path, err := os.Getwd()
if err != nil {
if err != nil && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}
config, diags := c.loadConfig(path)
if diags.HasErrors() {
if diags.HasErrors() && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}
schemas, diags := getSchemas(&c.Meta, srcStateFile.State, config)
if diags.HasErrors() {
if diags.HasErrors() && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}

View File

@ -161,19 +161,26 @@ func (c *StateReplaceProviderCommand) Run(args []string) int {
resource.ProviderConfig.Provider = to
}
b, backendDiags := c.Backend(&BackendOpts{})
diags = diags.Append(backendDiags)
if backendDiags.HasErrors() {
c.showDiagnostics(diags)
return 1
}
// Get schemas, if possible, before writing state
path, err := os.Getwd()
if err != nil {
if err != nil && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}
config, diags := c.loadConfig(path)
if diags.HasErrors() {
if diags.HasErrors() && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}
schemas, diags := getSchemas(&c.Meta, state, config)
if diags.HasErrors() {
if diags.HasErrors() && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}

View File

@ -111,19 +111,26 @@ 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{})
diags = diags.Append(backendDiags)
if backendDiags.HasErrors() {
c.showDiagnostics(diags)
return 1
}
// Get schemas, if possible, before writing state
path, err := os.Getwd()
if err != nil {
if err != nil && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}
config, diags := c.loadConfig(path)
if diags.HasErrors() {
if diags.HasErrors() && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}
schemas, diags := getSchemas(&c.Meta, state, config)
if diags.HasErrors() {
if diags.HasErrors() && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}

View File

@ -128,17 +128,17 @@ func (c *TaintCommand) Run(args []string) int {
// Get schemas, if possible, before writing state
path, err := os.Getwd()
if err != nil {
if err != nil && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}
config, diags := c.loadConfig(path)
if diags.HasErrors() {
if diags.HasErrors() && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}
schemas, diags := getSchemas(&c.Meta, state, config)
if diags.HasErrors() {
if diags.HasErrors() && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}

View File

@ -167,17 +167,17 @@ func (c *UntaintCommand) Run(args []string) int {
// Get schemas, if possible, before writing state
path, err := os.Getwd()
if err != nil {
if err != nil && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}
config, diags := c.loadConfig(path)
if diags.HasErrors() {
if diags.HasErrors() && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}
schemas, diags := getSchemas(&c.Meta, state, config)
if diags.HasErrors() {
if diags.HasErrors() && isCloudMode(b) {
c.Ui.Warn(fmt.Sprintf(failedToLoadSchemasMessage, err))
}
obj.Status = states.ObjectReady