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'.
This commit is contained in:
Luces Huayhuaca 2021-11-22 13:32:34 -08:00 committed by GitHub
parent a8972d82e9
commit f63b6198ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 46 additions and 37 deletions

View File

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