From 39a95e42222a67c8d9a97b02e9a8f8cfcf95924f Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Thu, 28 Mar 2019 17:23:48 +0100 Subject: [PATCH] backend/remote: correctly load remote variables When using `terraform console` in combination with the remote backend, variables defined in Terraform Enterprise were load loaded correctly. --- backend/remote/backend_context.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/backend/remote/backend_context.go b/backend/remote/backend_context.go index 044d24c456..dfc5783d4f 100644 --- a/backend/remote/backend_context.go +++ b/backend/remote/backend_context.go @@ -3,6 +3,7 @@ package remote import ( "context" "log" + "strings" "github.com/hashicorp/errwrap" tfe "github.com/hashicorp/go-tfe" @@ -23,21 +24,30 @@ func (b *Remote) Context(op *backend.Operation) (*terraform.Context, statemgr.Fu op.StateLocker = clistate.NewNoopLocker() } + // Get the remote workspace name. + workspace := op.Workspace + switch { + case op.Workspace == backend.DefaultStateName: + workspace = b.workspace + case b.prefix != "" && !strings.HasPrefix(op.Workspace, b.prefix): + workspace = b.prefix + op.Workspace + } + // Get the latest state. - log.Printf("[TRACE] backend/remote: requesting state manager for workspace %q", op.Workspace) + log.Printf("[TRACE] backend/remote: requesting state manager for workspace %q", workspace) stateMgr, err := b.StateMgr(op.Workspace) if err != nil { diags = diags.Append(errwrap.Wrapf("Error loading state: {{err}}", err)) return nil, nil, diags } - log.Printf("[TRACE] backend/remote: requesting state lock for workspace %q", op.Workspace) + log.Printf("[TRACE] backend/remote: requesting state lock for workspace %q", workspace) if err := op.StateLocker.Lock(stateMgr, op.Type.String()); err != nil { diags = diags.Append(errwrap.Wrapf("Error locking state: {{err}}", err)) return nil, nil, diags } - log.Printf("[TRACE] backend/remote: reading remote state for workspace %q", op.Workspace) + log.Printf("[TRACE] backend/remote: reading remote state for workspace %q", workspace) if err := stateMgr.RefreshState(); err != nil { diags = diags.Append(errwrap.Wrapf("Error loading state: {{err}}", err)) return nil, nil, diags @@ -57,7 +67,7 @@ func (b *Remote) Context(op *backend.Operation) (*terraform.Context, statemgr.Fu // Load the latest state. If we enter contextFromPlanFile below then the // state snapshot in the plan file must match this, or else it'll return // error diagnostics. - log.Printf("[TRACE] backend/remote: retrieving remote state snapshot for workspace %q", op.Workspace) + log.Printf("[TRACE] backend/remote: retrieving remote state snapshot for workspace %q", workspace) opts.State = stateMgr.State() log.Printf("[TRACE] backend/remote: loading configuration for the current working directory") @@ -68,10 +78,10 @@ func (b *Remote) Context(op *backend.Operation) (*terraform.Context, statemgr.Fu } opts.Config = config - log.Printf("[TRACE] backend/remote: retrieving variables from workspace %q", op.Workspace) + log.Printf("[TRACE] backend/remote: retrieving variables from workspace %q", workspace) tfeVariables, err := b.client.Variables.List(context.Background(), tfe.VariableListOptions{ Organization: tfe.String(b.organization), - Workspace: tfe.String(op.Workspace), + Workspace: tfe.String(workspace), }) if err != nil && err != tfe.ErrResourceNotFound { diags = diags.Append(errwrap.Wrapf("Error loading variables: {{err}}", err)) @@ -85,7 +95,7 @@ func (b *Remote) Context(op *backend.Operation) (*terraform.Context, statemgr.Fu } op.Variables[v.Key] = &unparsedVariableValue{ value: v.Value, - source: terraform.ValueFromCLIArg, + source: terraform.ValueFromEnvVar, } } }