You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: Fail an apply command if the plan file was generated for a workspace that isn't the selected workspace (#37955)
* fix: Fail apply command if the plan file was generated for a workspace that isn't the selected workspace.
* Add change file
* test: Update test helper to include Workspace name in plan representation
* fix: Make error message more generic, so is applicable to backend and cloud blocks.
* fix: Make error message specific to backend or cloud block
* test: Add separate tests for backend/cloud usage
* test: Update remaining tests to include a value for Workspace in mocked plans
* Apply suggestions from code review
Co-authored-by: Radek Simko <radeksimko@users.noreply.github.com>
* fix: Panic when a plan file has missing workspace data
* test: Update test to match changes in error text
---------
Co-authored-by: Radek Simko <radeksimko@users.noreply.github.com>
// Check the workspace name in the plan matches the current workspace
340
+
currentWorkspace, err:=m.Workspace()
341
+
iferr!=nil {
342
+
diags=diags.Append(fmt.Errorf("error determining current workspace when initializing a backend from the plan file: %w", err))
343
+
returnnil, diags
344
+
}
345
+
varplannedWorkspacestring
346
+
varisCloudbool
347
+
switch {
348
+
caseplan.StateStore!=nil:
349
+
plannedWorkspace=plan.StateStore.Workspace
350
+
isCloud=false
351
+
caseplan.Backend!=nil:
352
+
plannedWorkspace=plan.Backend.Workspace
353
+
isCloud=plan.Backend.Type=="cloud"
354
+
default:
355
+
panic(fmt.Sprintf("Workspace data missing from plan file. Current workspace is %q. This is a bug in Terraform and should be reported.", currentWorkspace))
msg:=fmt.Sprintf(`The plan file describes changes to the %q workspace, but the %q workspace is currently in use.
26
+
27
+
Applying this plan with the incorrect workspace selected could result in state being stored in an unexpected location, or a downstream error when Terraform attempts apply a plan using the other workspace's state.`,
28
+
e.plannedWorkspace,
29
+
e.currentWorkspace,
30
+
)
31
+
32
+
// For users to understand what's happened and how to correct it we'll give some guidance,
33
+
// but that guidance depends on whether a cloud backend is in use or not.
34
+
ife.isCloud {
35
+
// When using the cloud backend the solution is to focus on the cloud block and running init
36
+
msg=msg+fmt.Sprintf(` If you'd like to continue to use the plan file, make sure the cloud block in your configuration contains the workspace name %q.
37
+
In future, make sure your cloud block is correct and unchanged since the last time you performed "terraform init" before creating a plan.`, e.plannedWorkspace)
38
+
} else {
39
+
// When using the backend block the solution is to not select a different workspace
40
+
// between plan and apply operations.
41
+
msg=msg+fmt.Sprintf(` If you'd like to continue to use the plan file, you must run "terraform workspace select %s" to select the matching workspace.
42
+
In future make sure the selected workspace is not changed between creating and applying a plan file.
43
+
`, e.plannedWorkspace)
44
+
}
45
+
46
+
returnmsg
47
+
}
48
+
12
49
// errBackendLocalRead is a custom error used to alert users that state
13
50
// files on their local filesystem were not erased successfully after
14
51
// migrating that state to a remote-state backend.
t.Fatalf("expected an error but got none: %s", diags.ErrWithWarnings())
1998
+
}
1999
+
expectedMsgs:= []string{
2000
+
fmt.Sprintf("The plan file describes changes to the %q workspace, but the %q workspace is currently in use",
2001
+
planWorkspace,
2002
+
otherWorkspace,
2003
+
),
2004
+
fmt.Sprintf(`If you'd like to continue to use the plan file, make sure the cloud block in your configuration contains the workspace name %q`, planWorkspace),
2005
+
}
2006
+
for_, msg:=rangeexpectedMsgs {
2007
+
if!strings.Contains(diags.Err().Error(), msg) {
2008
+
t.Fatalf("expected error to include `%s`, but got:\n%s",
2009
+
msg,
2010
+
diags.Err())
2011
+
}
2012
+
}
2013
+
})
2014
+
}
2015
+
1911
2016
// init a backend using -backend-config options multiple times
0 commit comments