Skip to content

Commit 50997d9

Browse files
committed
Restore API version checking, with internal usage accounted for
1 parent cb26cbb commit 50997d9

4 files changed

Lines changed: 68 additions & 25 deletions

File tree

‎internal/cloud/backend.go‎

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ type Cloud struct {
8585
// version. This will also cause VerifyWorkspaceTerraformVersion to return
8686
// a warning diagnostic instead of an error.
8787
ignoreVersionConflict bool
88+
89+
runningInAutomation bool
8890
}
8991

9092
var _ backend.Backend = (*Cloud)(nil)
@@ -296,15 +298,25 @@ func (b *Cloud) Configure(obj cty.Value) tfdiags.Diagnostics {
296298

297299
if parseErr != nil || currentAPIVersion.LessThan(desiredAPIVersion) {
298300
log.Printf("[TRACE] API version check failed; want: >= %s, got: %s", desiredAPIVersion.Original(), currentAPIVersion)
299-
// FIXME: Skip version checking temporarily.
300-
// diags = diags.Append(tfdiags.Sourceless(
301-
// tfdiags.Error,
302-
// "Unsupported Terraform Enterprise version",
303-
// fmt.Sprintf(
304-
// `The 'cloud' option requires Terraform Enterprise %s or later.`,
305-
// apiToMinimumTFEVersion["2.5"],
306-
// ),
307-
// ))
301+
if b.runningInAutomation {
302+
// It should never be possible for this Terraform process to be mistakenly
303+
// used internally within an unsupported Terraform Enterprise install - but
304+
// just in case it happens, give an actionable error.
305+
diags = diags.Append(
306+
tfdiags.Sourceless(
307+
tfdiags.Error,
308+
"Unsupported Terraform Enterprise version",
309+
cloudIntegrationUsedInUnsupportedTFE,
310+
),
311+
)
312+
} else {
313+
diags = diags.Append(tfdiags.Sourceless(
314+
tfdiags.Error,
315+
"Unsupported Terraform Enterprise version",
316+
`The 'cloud' option is not supported with this version of Terraform Enterprise.`,
317+
),
318+
)
319+
}
308320
}
309321

310322
// 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
967979
const unavailableTerraformVersion = `
968980
[reset][yellow]The local Terraform version (%s) is not available in Terraform Cloud, or your
969981
organization does not have access to it. The new workspace will use %s. You can
970-
change this later in the workspace settings.[reset]
971-
`
982+
change this later in the workspace settings.[reset]`
983+
984+
const cloudIntegrationUsedInUnsupportedTFE = `
985+
This version of Terraform Cloud/Enterprise does not support the state mechanism
986+
attempting to be used by the platform. This should never happen.
987+
988+
Please reach out to HashiCorp Support to resolve this issue.`
972989

973990
var (
974991
workspaceConfigurationHelp = fmt.Sprintf(

‎internal/cloud/backend_cli.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func (b *Cloud) CLIInit(opts *backend.CLIOpts) error {
1515
b.CLI = opts.CLI
1616
b.CLIColor = opts.CLIColor
1717
b.ContextOpts = opts.ContextOpts
18+
b.runningInAutomation = opts.RunningInAutomation
1819

1920
return nil
2021
}

‎internal/cloud/backend_test.go‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ func TestCloud_config(t *testing.T) {
269269
}
270270

271271
func TestCloud_configVerifyMinimumTFEVersion(t *testing.T) {
272-
t.Skip("skipping, as TFE version checking has been temporarily disabled")
273272
config := cty.ObjectVal(map[string]cty.Value{
274273
"hostname": cty.NullVal(cty.String),
275274
"organization": cty.StringVal("hashicorp"),
@@ -299,7 +298,45 @@ func TestCloud_configVerifyMinimumTFEVersion(t *testing.T) {
299298
t.Fatalf("expected configure to error")
300299
}
301300

302-
expected := "The 'cloud' option requires Terraform Enterprise v202201-1 or later."
301+
expected := `The 'cloud' option is not supported with this version of Terraform Enterprise.`
302+
if !strings.Contains(confDiags.Err().Error(), expected) {
303+
t.Fatalf("expected configure to error with %q, got %q", expected, confDiags.Err().Error())
304+
}
305+
}
306+
307+
func TestCloud_configVerifyMinimumTFEVersionInAutomation(t *testing.T) {
308+
config := cty.ObjectVal(map[string]cty.Value{
309+
"hostname": cty.NullVal(cty.String),
310+
"organization": cty.StringVal("hashicorp"),
311+
"token": cty.NullVal(cty.String),
312+
"workspaces": cty.ObjectVal(map[string]cty.Value{
313+
"name": cty.NullVal(cty.String),
314+
"tags": cty.SetVal(
315+
[]cty.Value{
316+
cty.StringVal("billing"),
317+
},
318+
),
319+
}),
320+
})
321+
322+
handlers := map[string]func(http.ResponseWriter, *http.Request){
323+
"/api/v2/ping": func(w http.ResponseWriter, r *http.Request) {
324+
w.Header().Set("Content-Type", "application/json")
325+
w.Header().Set("TFP-API-Version", "2.4")
326+
},
327+
}
328+
s := testServerWithHandlers(handlers)
329+
330+
b := New(testDisco(s))
331+
b.runningInAutomation = true
332+
333+
confDiags := b.Configure(config)
334+
if confDiags.Err() == nil {
335+
t.Fatalf("expected configure to error")
336+
}
337+
338+
expected := `This version of Terraform Cloud/Enterprise does not support the state mechanism
339+
attempting to be used by the platform. This should never happen.`
303340
if !strings.Contains(confDiags.Err().Error(), expected) {
304341
t.Fatalf("expected configure to error with %q, got %q", expected, confDiags.Err().Error())
305342
}

‎internal/cloud/versioning.go‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)