Merge pull request #20857 from hashicorp/svh/b-console-vars

backend/remote: correctly load remote variables
This commit is contained in:
Sander van Harmelen 2019-03-29 08:34:01 +01:00 committed by GitHub
commit b94ddab3a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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,
}
}
}