2018-11-26 14:06:27 +01:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
2019-03-28 17:23:48 +01:00
|
|
|
"strings"
|
2018-11-26 14:06:27 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/errwrap"
|
|
|
|
tfe "github.com/hashicorp/go-tfe"
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/command/clistate"
|
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Context implements backend.Enhanced.
|
|
|
|
func (b *Remote) Context(op *backend.Operation) (*terraform.Context, statemgr.Full, tfdiags.Diagnostics) {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
|
|
|
if op.LockState {
|
|
|
|
op.StateLocker = clistate.NewLocker(context.Background(), op.StateLockTimeout, b.CLI, b.cliColorize())
|
|
|
|
} else {
|
|
|
|
op.StateLocker = clistate.NewNoopLocker()
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:23:48 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-11-26 14:06:27 +01:00
|
|
|
// Get the latest state.
|
2019-03-28 17:23:48 +01:00
|
|
|
log.Printf("[TRACE] backend/remote: requesting state manager for workspace %q", workspace)
|
2018-11-26 14:06:27 +01:00
|
|
|
stateMgr, err := b.StateMgr(op.Workspace)
|
|
|
|
if err != nil {
|
|
|
|
diags = diags.Append(errwrap.Wrapf("Error loading state: {{err}}", err))
|
|
|
|
return nil, nil, diags
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:23:48 +01:00
|
|
|
log.Printf("[TRACE] backend/remote: requesting state lock for workspace %q", workspace)
|
2018-11-26 14:06:27 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:23:48 +01:00
|
|
|
log.Printf("[TRACE] backend/remote: reading remote state for workspace %q", workspace)
|
2018-11-26 14:06:27 +01:00
|
|
|
if err := stateMgr.RefreshState(); err != nil {
|
|
|
|
diags = diags.Append(errwrap.Wrapf("Error loading state: {{err}}", err))
|
|
|
|
return nil, nil, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize our context options
|
|
|
|
var opts terraform.ContextOpts
|
|
|
|
if v := b.ContextOpts; v != nil {
|
|
|
|
opts = *v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy set options from the operation
|
|
|
|
opts.Destroy = op.Destroy
|
|
|
|
opts.Targets = op.Targets
|
|
|
|
opts.UIInput = op.UIIn
|
|
|
|
|
|
|
|
// 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.
|
2019-03-28 17:23:48 +01:00
|
|
|
log.Printf("[TRACE] backend/remote: retrieving remote state snapshot for workspace %q", workspace)
|
2018-11-26 14:06:27 +01:00
|
|
|
opts.State = stateMgr.State()
|
|
|
|
|
|
|
|
log.Printf("[TRACE] backend/remote: loading configuration for the current working directory")
|
|
|
|
config, configDiags := op.ConfigLoader.LoadConfig(op.ConfigDir)
|
|
|
|
diags = diags.Append(configDiags)
|
|
|
|
if configDiags.HasErrors() {
|
|
|
|
return nil, nil, diags
|
|
|
|
}
|
|
|
|
opts.Config = config
|
|
|
|
|
2019-03-28 17:23:48 +01:00
|
|
|
log.Printf("[TRACE] backend/remote: retrieving variables from workspace %q", workspace)
|
2018-11-26 14:06:27 +01:00
|
|
|
tfeVariables, err := b.client.Variables.List(context.Background(), tfe.VariableListOptions{
|
|
|
|
Organization: tfe.String(b.organization),
|
2019-03-28 17:23:48 +01:00
|
|
|
Workspace: tfe.String(workspace),
|
2018-11-26 14:06:27 +01:00
|
|
|
})
|
|
|
|
if err != nil && err != tfe.ErrResourceNotFound {
|
|
|
|
diags = diags.Append(errwrap.Wrapf("Error loading variables: {{err}}", err))
|
|
|
|
return nil, nil, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
if tfeVariables != nil {
|
|
|
|
for _, v := range tfeVariables.Items {
|
|
|
|
if v.Sensitive {
|
|
|
|
v.Value = "<sensitive>"
|
|
|
|
}
|
|
|
|
op.Variables[v.Key] = &unparsedVariableValue{
|
|
|
|
value: v.Value,
|
2019-03-28 17:23:48 +01:00
|
|
|
source: terraform.ValueFromEnvVar,
|
2018-11-26 14:06:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
variables, varDiags := backend.ParseVariableValues(op.Variables, config.Module.Variables)
|
|
|
|
diags = diags.Append(varDiags)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
return nil, nil, diags
|
|
|
|
}
|
|
|
|
if op.Variables != nil {
|
|
|
|
opts.Variables = variables
|
|
|
|
}
|
|
|
|
|
|
|
|
tfCtx, ctxDiags := terraform.NewContext(&opts)
|
|
|
|
diags = diags.Append(ctxDiags)
|
|
|
|
|
|
|
|
log.Printf("[TRACE] backend/remote: finished building terraform.Context")
|
|
|
|
|
|
|
|
return tfCtx, stateMgr, diags
|
|
|
|
}
|