Skip to content

Commit db4f3f8

Browse files
vendor: go get github.com/hashicorp/go-tfe@v0.8.0
This includes a new TargetAddrs field on both Run and RunCreateOptions which we'll use to send resource addresses that were specified using -target on the CLI command line when using the remote backend. There were some unrelated upstream breaking changes compared to the last version we had vendored, so this commit also includes some changes to the backend/remote package to work with this new API, which now requires the remote backend to be aware of the remote system's opaque workspace id.
1 parent 1d834fb commit db4f3f8

20 files changed

Lines changed: 985 additions & 122 deletions

backend/remote/backend_context.go

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,17 @@ func (b *Remote) Context(op *backend.Operation) (*terraform.Context, statemgr.Fu
3030
}
3131

3232
// Get the remote workspace name.
33-
workspace := op.Workspace
34-
switch {
35-
case op.Workspace == backend.DefaultStateName:
36-
workspace = b.workspace
37-
case b.prefix != "" && !strings.HasPrefix(op.Workspace, b.prefix):
38-
workspace = b.prefix + op.Workspace
39-
}
33+
remoteWorkspaceName := b.getRemoteWorkspaceName(op.Workspace)
4034

4135
// Get the latest state.
42-
log.Printf("[TRACE] backend/remote: requesting state manager for workspace %q", workspace)
36+
log.Printf("[TRACE] backend/remote: requesting state manager for workspace %q", remoteWorkspaceName)
4337
stateMgr, err := b.StateMgr(op.Workspace)
4438
if err != nil {
4539
diags = diags.Append(errwrap.Wrapf("Error loading state: {{err}}", err))
4640
return nil, nil, diags
4741
}
4842

49-
log.Printf("[TRACE] backend/remote: requesting state lock for workspace %q", workspace)
43+
log.Printf("[TRACE] backend/remote: requesting state lock for workspace %q", remoteWorkspaceName)
5044
if err := op.StateLocker.Lock(stateMgr, op.Type.String()); err != nil {
5145
diags = diags.Append(errwrap.Wrapf("Error locking state: {{err}}", err))
5246
return nil, nil, diags
@@ -63,7 +57,7 @@ func (b *Remote) Context(op *backend.Operation) (*terraform.Context, statemgr.Fu
6357
}
6458
}()
6559

66-
log.Printf("[TRACE] backend/remote: reading remote state for workspace %q", workspace)
60+
log.Printf("[TRACE] backend/remote: reading remote state for workspace %q", remoteWorkspaceName)
6761
if err := stateMgr.RefreshState(); err != nil {
6862
diags = diags.Append(errwrap.Wrapf("Error loading state: {{err}}", err))
6963
return nil, nil, diags
@@ -83,7 +77,7 @@ func (b *Remote) Context(op *backend.Operation) (*terraform.Context, statemgr.Fu
8377
// Load the latest state. If we enter contextFromPlanFile below then the
8478
// state snapshot in the plan file must match this, or else it'll return
8579
// error diagnostics.
86-
log.Printf("[TRACE] backend/remote: retrieving remote state snapshot for workspace %q", workspace)
80+
log.Printf("[TRACE] backend/remote: retrieving remote state snapshot for workspace %q", remoteWorkspaceName)
8781
opts.State = stateMgr.State()
8882

8983
log.Printf("[TRACE] backend/remote: loading configuration for the current working directory")
@@ -94,11 +88,17 @@ func (b *Remote) Context(op *backend.Operation) (*terraform.Context, statemgr.Fu
9488
}
9589
opts.Config = config
9690

97-
log.Printf("[TRACE] backend/remote: retrieving variables from workspace %q", workspace)
98-
tfeVariables, err := b.client.Variables.List(context.Background(), tfe.VariableListOptions{
99-
Organization: tfe.String(b.organization),
100-
Workspace: tfe.String(workspace),
101-
})
91+
// The underlying API expects us to use the opaque workspace id to request
92+
// variables, so we'll need to look that up using our organization name
93+
// and workspace name.
94+
remoteWorkspaceID, err := b.getRemoteWorkspaceID(context.Background(), op.Workspace)
95+
if err != nil {
96+
diags = diags.Append(errwrap.Wrapf("Error finding remote workspace: {{err}}", err))
97+
return nil, nil, diags
98+
}
99+
100+
log.Printf("[TRACE] backend/remote: retrieving variables from workspace %s/%s (%s)", remoteWorkspaceName, b.organization, remoteWorkspaceID)
101+
tfeVariables, err := b.client.Variables.List(context.Background(), remoteWorkspaceID, tfe.VariableListOptions{})
102102
if err != nil && err != tfe.ErrResourceNotFound {
103103
diags = diags.Append(errwrap.Wrapf("Error loading variables: {{err}}", err))
104104
return nil, nil, diags
@@ -142,6 +142,32 @@ func (b *Remote) Context(op *backend.Operation) (*terraform.Context, statemgr.Fu
142142
return tfCtx, stateMgr, diags
143143
}
144144

145+
func (b *Remote) getRemoteWorkspaceName(localWorkspaceName string) string {
146+
switch {
147+
case localWorkspaceName == backend.DefaultStateName:
148+
// The default workspace name is a special case, for when the backend
149+
// is configured to with to an exact remote workspace rather than with
150+
// a remote workspace _prefix_.
151+
return b.workspace
152+
case b.prefix != "" && !strings.HasPrefix(localWorkspaceName, b.prefix):
153+
return b.prefix + localWorkspaceName
154+
default:
155+
return localWorkspaceName
156+
}
157+
}
158+
159+
func (b *Remote) getRemoteWorkspaceID(ctx context.Context, localWorkspaceName string) (string, error) {
160+
remoteWorkspaceName := b.getRemoteWorkspaceName(localWorkspaceName)
161+
162+
log.Printf("[TRACE] backend/remote: looking up workspace id for %s/%s", b.organization, remoteWorkspaceName)
163+
remoteWorkspace, err := b.client.Workspaces.Read(ctx, b.organization, remoteWorkspaceName)
164+
if err != nil {
165+
return "", err
166+
}
167+
168+
return remoteWorkspace.ID, nil
169+
}
170+
145171
func stubAllVariables(vv map[string]backend.UnparsedVariableValue, decls map[string]*configs.Variable) terraform.InputValues {
146172
ret := make(terraform.InputValues, len(decls))
147173

backend/remote/backend_context_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package remote
22

33
import (
4+
"context"
45
"testing"
56

67
tfe "github.com/hashicorp/go-tfe"
@@ -176,6 +177,11 @@ func TestRemoteContextWithVars(t *testing.T) {
176177
_, configLoader, configCleanup := initwd.MustLoadConfigForTests(t, configDir)
177178
defer configCleanup()
178179

180+
workspaceID, err := b.getRemoteWorkspaceID(context.Background(), backend.DefaultStateName)
181+
if err != nil {
182+
t.Fatal(err)
183+
}
184+
179185
op := &backend.Operation{
180186
ConfigDir: configDir,
181187
ConfigLoader: configLoader,
@@ -187,12 +193,7 @@ func TestRemoteContextWithVars(t *testing.T) {
187193
key := "key"
188194
v.Key = &key
189195
}
190-
if v.Workspace == nil {
191-
v.Workspace = &tfe.Workspace{
192-
Name: b.workspace,
193-
}
194-
}
195-
b.client.Variables.Create(nil, *v)
196+
b.client.Variables.Create(nil, workspaceID, *v)
196197

197198
_, _, diags := b.Context(op)
198199

backend/remote/backend_mock.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -952,19 +952,21 @@ type mockVariables struct {
952952
workspaces map[string]*tfe.VariableList
953953
}
954954

955+
var _ tfe.Variables = (*mockVariables)(nil)
956+
955957
func newMockVariables(client *mockClient) *mockVariables {
956958
return &mockVariables{
957959
client: client,
958960
workspaces: make(map[string]*tfe.VariableList),
959961
}
960962
}
961963

962-
func (m *mockVariables) List(ctx context.Context, options tfe.VariableListOptions) (*tfe.VariableList, error) {
963-
vl := m.workspaces[*options.Workspace]
964+
func (m *mockVariables) List(ctx context.Context, workspaceID string, options tfe.VariableListOptions) (*tfe.VariableList, error) {
965+
vl := m.workspaces[workspaceID]
964966
return vl, nil
965967
}
966968

967-
func (m *mockVariables) Create(ctx context.Context, options tfe.VariableCreateOptions) (*tfe.Variable, error) {
969+
func (m *mockVariables) Create(ctx context.Context, workspaceID string, options tfe.VariableCreateOptions) (*tfe.Variable, error) {
968970
v := &tfe.Variable{
969971
ID: generateID("var-"),
970972
Key: *options.Key,
@@ -980,7 +982,7 @@ func (m *mockVariables) Create(ctx context.Context, options tfe.VariableCreateOp
980982
v.Sensitive = *options.Sensitive
981983
}
982984

983-
workspace := options.Workspace.Name
985+
workspace := workspaceID
984986

985987
if m.workspaces[workspace] == nil {
986988
m.workspaces[workspace] = &tfe.VariableList{}
@@ -992,15 +994,15 @@ func (m *mockVariables) Create(ctx context.Context, options tfe.VariableCreateOp
992994
return v, nil
993995
}
994996

995-
func (m *mockVariables) Read(ctx context.Context, variableID string) (*tfe.Variable, error) {
997+
func (m *mockVariables) Read(ctx context.Context, workspaceID string, variableID string) (*tfe.Variable, error) {
996998
panic("not implemented")
997999
}
9981000

999-
func (m *mockVariables) Update(ctx context.Context, variableID string, options tfe.VariableUpdateOptions) (*tfe.Variable, error) {
1001+
func (m *mockVariables) Update(ctx context.Context, workspaceID string, variableID string, options tfe.VariableUpdateOptions) (*tfe.Variable, error) {
10001002
panic("not implemented")
10011003
}
10021004

1003-
func (m *mockVariables) Delete(ctx context.Context, variableID string) error {
1005+
func (m *mockVariables) Delete(ctx context.Context, workspaceID string, variableID string) error {
10041006
panic("not implemented")
10051007
}
10061008

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ require (
6464
github.com/hashicorp/go-retryablehttp v0.5.2
6565
github.com/hashicorp/go-rootcerts v1.0.0
6666
github.com/hashicorp/go-sockaddr v0.0.0-20180320115054-6d291a969b86 // indirect
67-
github.com/hashicorp/go-tfe v0.3.27
67+
github.com/hashicorp/go-tfe v0.8.0
6868
github.com/hashicorp/go-uuid v1.0.1
6969
github.com/hashicorp/go-version v1.2.0
7070
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ github.com/hashicorp/go-slug v0.4.1 h1:/jAo8dNuLgSImoLXaX7Od7QB4TfYCVPam+OpAt5bZ
231231
github.com/hashicorp/go-slug v0.4.1/go.mod h1:I5tq5Lv0E2xcNXNkmx7BSfzi1PsJ2cNjs3cC3LwyhK8=
232232
github.com/hashicorp/go-sockaddr v0.0.0-20180320115054-6d291a969b86 h1:7YOlAIO2YWnJZkQp7B5eFykaIY7C9JndqAFQyVV5BhM=
233233
github.com/hashicorp/go-sockaddr v0.0.0-20180320115054-6d291a969b86/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
234-
github.com/hashicorp/go-tfe v0.3.27 h1:7XZ/ZoPyYoeuNXaWWW0mJOq016y0qb7I4Q0P/cagyu8=
235-
github.com/hashicorp/go-tfe v0.3.27/go.mod h1:DVPSW2ogH+M9W1/i50ASgMht8cHP7NxxK0nrY9aFikQ=
234+
github.com/hashicorp/go-tfe v0.8.0 h1:kz3x3tbIKRkEAzKg05P/qbFY88fkEU7TiSX3w8xUrmE=
235+
github.com/hashicorp/go-tfe v0.8.0/go.mod h1:XAV72S4O1iP8BDaqiaPLmL2B4EE6almocnOn8E8stHc=
236236
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
237237
github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1BE=
238238
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=

vendor/github.com/hashicorp/go-tfe/README.md

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hashicorp/go-tfe/go.mod

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hashicorp/go-tfe/go.sum

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hashicorp/go-tfe/oauth_client.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)