From f63b6198ca78a16f2ebd895d7b40e1670e14dc31 Mon Sep 17 00:00:00 2001 From: Luces Huayhuaca <21225410+uturunku1@users.noreply.github.com> Date: Mon, 22 Nov 2021 13:32:34 -0800 Subject: [PATCH] Create a function for logic that assigns value to initReason var after changing backend configuration (#29967) * Create a function for logic that assigns value to initReason var after changing backend configuration Create func determineInitReason() for logic block that assigns value to initReason var after changing backend/cloud configuration block or migrating to a different type of backend configuration. Also clarify 'cloud' configuration block message to say 'Terraform Cloud configuration block has changed' instead of 'Terraform Cloud configuration has changed'. --- internal/command/meta_backend.go | 83 ++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/internal/command/meta_backend.go b/internal/command/meta_backend.go index ecc00a386..4e0b09555 100644 --- a/internal/command/meta_backend.go +++ b/internal/command/meta_backend.go @@ -672,44 +672,10 @@ func (m *Meta) backendFromConfig(opts *BackendOpts) (backend.Backend, tfdiags.Di cloudMode := cloud.DetectConfigChangeType(s.Backend, c, false) - initReason := "" - switch cloudMode { - case cloud.ConfigMigrationIn: - initReason = fmt.Sprintf("Changed from backend %q to Terraform Cloud", s.Backend.Type) - case cloud.ConfigMigrationOut: - initReason = fmt.Sprintf("Changed from Terraform Cloud to backend %q", s.Backend.Type) - case cloud.ConfigChangeInPlace: - initReason = "Terraform Cloud configuration has changed" - default: - switch { - case s.Backend.Type != c.Type: - initReason = fmt.Sprintf("Backend type changed from %q to %q", s.Backend.Type, c.Type) - default: - initReason = fmt.Sprintf("Configuration changed for backend %q", c.Type) - } - } - if !opts.Init { - switch cloudMode { - case cloud.ConfigChangeInPlace: - diags = diags.Append(tfdiags.Sourceless( - tfdiags.Error, - "Terraform Cloud initialization required: please run \"terraform init\"", - fmt.Sprintf(strings.TrimSpace(errBackendInitCloud), initReason), - )) - case cloud.ConfigMigrationIn: - diags = diags.Append(tfdiags.Sourceless( - tfdiags.Error, - "Terraform Cloud initialization required: please run \"terraform init\"", - fmt.Sprintf(strings.TrimSpace(errBackendInitCloudMigration), initReason), - )) - default: - diags = diags.Append(tfdiags.Sourceless( - tfdiags.Error, - "Backend initialization required: please run \"terraform init\"", - fmt.Sprintf(strings.TrimSpace(errBackendInit), initReason), - )) - } + //user ran another cmd that is not init but they are required to initialize because of a potential relevant change to their backend configuration + initDiag := m.determineInitReason(s.Backend.Type, c.Type, cloudMode) + diags = diags.Append(initDiag) return nil, diags } @@ -733,6 +699,49 @@ func (m *Meta) backendFromConfig(opts *BackendOpts) (backend.Backend, tfdiags.Di } } +func (m *Meta) determineInitReason(previousBackendType string, currentBackendType string, cloudMode cloud.ConfigChangeMode) tfdiags.Diagnostics { + initReason := "" + switch cloudMode { + case cloud.ConfigMigrationIn: + initReason = fmt.Sprintf("Changed from backend %q to Terraform Cloud", previousBackendType) + case cloud.ConfigMigrationOut: + initReason = fmt.Sprintf("Changed from Terraform Cloud to backend %q", previousBackendType) + case cloud.ConfigChangeInPlace: + initReason = "Terraform Cloud configuration block has changed" + default: + switch { + case previousBackendType != currentBackendType: + initReason = fmt.Sprintf("Backend type changed from %q to %q", previousBackendType, currentBackendType) + default: + initReason = "Backend configuration block has changed" + } + } + + var diags tfdiags.Diagnostics + switch cloudMode { + case cloud.ConfigChangeInPlace: + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Error, + "Terraform Cloud initialization required: please run \"terraform init\"", + fmt.Sprintf(strings.TrimSpace(errBackendInitCloud), initReason), + )) + case cloud.ConfigMigrationIn: + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Error, + "Terraform Cloud initialization required: please run \"terraform init\"", + fmt.Sprintf(strings.TrimSpace(errBackendInitCloudMigration), initReason), + )) + default: + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Error, + "Backend initialization required: please run \"terraform init\"", + fmt.Sprintf(strings.TrimSpace(errBackendInit), initReason), + )) + } + + return diags +} + // backendFromState returns the initialized (not configured) backend directly // from the state. This should be used only when a user runs `terraform init // -backend=false`. This function returns a local backend if there is no state