Restore API version checking, with internal usage accounted for

This commit is contained in:
Chris Arcand 2021-10-22 15:08:22 -05:00
parent cb26cbba22
commit 50997d9a32
4 changed files with 68 additions and 25 deletions

View File

@ -85,6 +85,8 @@ type Cloud struct {
// version. This will also cause VerifyWorkspaceTerraformVersion to return
// a warning diagnostic instead of an error.
ignoreVersionConflict bool
runningInAutomation bool
}
var _ backend.Backend = (*Cloud)(nil)
@ -296,15 +298,25 @@ func (b *Cloud) Configure(obj cty.Value) tfdiags.Diagnostics {
if parseErr != nil || currentAPIVersion.LessThan(desiredAPIVersion) {
log.Printf("[TRACE] API version check failed; want: >= %s, got: %s", desiredAPIVersion.Original(), currentAPIVersion)
// FIXME: Skip version checking temporarily.
// diags = diags.Append(tfdiags.Sourceless(
// tfdiags.Error,
// "Unsupported Terraform Enterprise version",
// fmt.Sprintf(
// `The 'cloud' option requires Terraform Enterprise %s or later.`,
// apiToMinimumTFEVersion["2.5"],
// ),
// ))
if b.runningInAutomation {
// It should never be possible for this Terraform process to be mistakenly
// used internally within an unsupported Terraform Enterprise install - but
// just in case it happens, give an actionable error.
diags = diags.Append(
tfdiags.Sourceless(
tfdiags.Error,
"Unsupported Terraform Enterprise version",
cloudIntegrationUsedInUnsupportedTFE,
),
)
} else {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Unsupported Terraform Enterprise version",
`The 'cloud' option is not supported with this version of Terraform Enterprise.`,
),
)
}
}
// Configure a local backend for when we need to run operations locally.
@ -967,8 +979,13 @@ const refreshToApplyRefresh = `[bold][yellow]Proceeding with 'terraform apply -r
const unavailableTerraformVersion = `
[reset][yellow]The local Terraform version (%s) is not available in Terraform Cloud, or your
organization does not have access to it. The new workspace will use %s. You can
change this later in the workspace settings.[reset]
`
change this later in the workspace settings.[reset]`
const cloudIntegrationUsedInUnsupportedTFE = `
This version of Terraform Cloud/Enterprise does not support the state mechanism
attempting to be used by the platform. This should never happen.
Please reach out to HashiCorp Support to resolve this issue.`
var (
workspaceConfigurationHelp = fmt.Sprintf(

View File

@ -15,6 +15,7 @@ func (b *Cloud) CLIInit(opts *backend.CLIOpts) error {
b.CLI = opts.CLI
b.CLIColor = opts.CLIColor
b.ContextOpts = opts.ContextOpts
b.runningInAutomation = opts.RunningInAutomation
return nil
}

View File

@ -269,7 +269,6 @@ func TestCloud_config(t *testing.T) {
}
func TestCloud_configVerifyMinimumTFEVersion(t *testing.T) {
t.Skip("skipping, as TFE version checking has been temporarily disabled")
config := cty.ObjectVal(map[string]cty.Value{
"hostname": cty.NullVal(cty.String),
"organization": cty.StringVal("hashicorp"),
@ -299,7 +298,45 @@ func TestCloud_configVerifyMinimumTFEVersion(t *testing.T) {
t.Fatalf("expected configure to error")
}
expected := "The 'cloud' option requires Terraform Enterprise v202201-1 or later."
expected := `The 'cloud' option is not supported with this version of Terraform Enterprise.`
if !strings.Contains(confDiags.Err().Error(), expected) {
t.Fatalf("expected configure to error with %q, got %q", expected, confDiags.Err().Error())
}
}
func TestCloud_configVerifyMinimumTFEVersionInAutomation(t *testing.T) {
config := cty.ObjectVal(map[string]cty.Value{
"hostname": cty.NullVal(cty.String),
"organization": cty.StringVal("hashicorp"),
"token": cty.NullVal(cty.String),
"workspaces": cty.ObjectVal(map[string]cty.Value{
"name": cty.NullVal(cty.String),
"tags": cty.SetVal(
[]cty.Value{
cty.StringVal("billing"),
},
),
}),
})
handlers := map[string]func(http.ResponseWriter, *http.Request){
"/api/v2/ping": func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("TFP-API-Version", "2.4")
},
}
s := testServerWithHandlers(handlers)
b := New(testDisco(s))
b.runningInAutomation = true
confDiags := b.Configure(config)
if confDiags.Err() == nil {
t.Fatalf("expected configure to error")
}
expected := `This version of Terraform Cloud/Enterprise does not support the state mechanism
attempting to be used by the platform. This should never happen.`
if !strings.Contains(confDiags.Err().Error(), expected) {
t.Fatalf("expected configure to error with %q, got %q", expected, confDiags.Err().Error())
}

View File

@ -1,12 +0,0 @@
package cloud
// This simple map exists to translate TFP-API-Version strings to the TFE release where it was
// introduced, to provide actionable feedback on features that may be unsupported by the TFE
// installation but present in this version of Terraform.
//
// The cloud package here, introduced in Terraform 1.1.0, requires a minimum of 2.5 (v202201-1)
// The TFP-API-Version header that this refers to was introduced in 2.3 (v202006-1), so an absent
// header can be considered < 2.3.
// var apiToMinimumTFEVersion = map[string]string{
// "2.5": "v202201-1",
// }