Skip to content

Commit

Permalink
command: Various updates for the new backend package API
Browse files Browse the repository at this point in the history
This is a rather-messy, complex change to get the "command" package
building again against the new backend API that was updated for
the new configuration loader.

A lot of this is mechanical rewriting to the new API, but
meta_config.go and meta_backend.go in particular saw some major
changes to interface with the new loader APIs and to deal with
the change in order of steps in the backend API.
  • Loading branch information
apparentlymart committed Oct 17, 2018
1 parent 5782357 commit ebafa51
Show file tree
Hide file tree
Showing 36 changed files with 1,324 additions and 1,150 deletions.
4 changes: 4 additions & 0 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,7 @@ const (
// performed at all.
OperationFailure OperationResult = 1
)

func (r OperationResult) ExitStatus() int {
return int(r)
}
74 changes: 42 additions & 32 deletions command/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"sort"
"strings"

"github.com/hashicorp/terraform/configs"

"github.com/hashicorp/terraform/tfdiags"

"github.com/hashicorp/go-getter"
"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/terraform"
)

Expand Down Expand Up @@ -118,66 +119,75 @@ func (c *ApplyCommand) Run(args []string) int {

var diags tfdiags.Diagnostics

// Load the module if we don't have one yet (not running from plan)
var mod *module.Tree
var backendConfig *configs.Backend
if plan == nil {
var modDiags tfdiags.Diagnostics
mod, modDiags = c.Module(configPath)
diags = diags.Append(modDiags)
if modDiags.HasErrors() {
var configDiags tfdiags.Diagnostics
backendConfig, configDiags = c.loadBackendConfig(configPath)
diags = diags.Append(configDiags)
if configDiags.HasErrors() {
c.showDiagnostics(diags)
return 1
}
}

var conf *config.Config
if mod != nil {
conf = mod.Config()
}

// Load the backend
b, err := c.Backend(&BackendOpts{
Config: conf,
b, beDiags := c.Backend(&BackendOpts{
Config: backendConfig,
Plan: plan,
})
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
diags = diags.Append(beDiags)
if beDiags.HasErrors() {
c.showDiagnostics(diags)
return 1
}

// Before we delegate to the backend, we'll print any warning diagnostics
// we've accumulated here, since the backend will start fresh with its own
// diagnostics.
c.showDiagnostics(diags)
diags = nil

// Build the operation
opReq := c.Operation()
opReq.AutoApprove = autoApprove
opReq.Destroy = c.Destroy
opReq.DestroyForce = destroyForce
opReq.Module = mod
opReq.ConfigDir = configPath
opReq.Plan = plan
opReq.PlanRefresh = refresh
opReq.Type = backend.OperationTypeApply

op, err := c.RunOperation(b, opReq)
opReq.AutoApprove = autoApprove
opReq.DestroyForce = destroyForce
opReq.ConfigLoader, err = c.initConfigLoader()
if err != nil {
diags = diags.Append(err)
c.showDiagnostics(err)
return 1
}

c.showDiagnostics(diags)
if diags.HasErrors() {
op, err := c.RunOperation(b, opReq)
if err != nil {
c.showDiagnostics(err)
return 1
}
if op.Result != backend.OperationSuccess {
return op.Result.ExitStatus()
}

if !c.Destroy {
// Get the right module that we used. If we ran a plan, then use
// that module.
if plan != nil {
mod = plan.Module
}
// TODO: Print outputs, once this is updated to use new config types.
/*
// Get the right module that we used. If we ran a plan, then use
// that module.
if plan != nil {
mod = plan.Module
}
if outputs := outputsAsString(op.State, terraform.RootModulePath, mod.Config().Outputs, true); outputs != "" {
c.Ui.Output(c.Colorize().Color(outputs))
}
if outputs := outputsAsString(op.State, terraform.RootModulePath, mod.Config().Outputs, true); outputs != "" {
c.Ui.Output(c.Colorize().Color(outputs))
}
*/
}

return op.ExitCode
return op.Result.ExitStatus()
}

func (c *ApplyCommand) Help() string {
Expand Down
10 changes: 5 additions & 5 deletions command/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ func (m *Meta) completePredictWorkspaceName() complete.Predictor {
return nil
}

cfg, err := m.Config(configPath)
if err != nil {
backendConfig, diags := m.loadBackendConfig(configPath)
if diags.HasErrors() {
return nil
}

b, err := m.Backend(&BackendOpts{
Config: cfg,
b, diags := m.Backend(&BackendOpts{
Config: backendConfig,
})
if err != nil {
if diags.HasErrors() {
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,9 @@ func testBackendState(t *testing.T, s *terraform.State, c int) (*terraform.State

state := terraform.NewState()
state.Backend = &terraform.BackendState{
Type: "http",
Config: map[string]interface{}{"address": srv.URL},
Hash: 2529831861221416334,
Type: "http",
ConfigRaw: json.RawMessage(fmt.Sprintf(`{"address":%q}`, srv.URL)),
Hash: 2529831861221416334,
}

return state, srv
Expand Down
37 changes: 19 additions & 18 deletions command/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package command

import (
"bufio"
"fmt"
"strings"

"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/helper/wrappedstreams"
"github.com/hashicorp/terraform/repl"
"github.com/hashicorp/terraform/tfdiags"
Expand Down Expand Up @@ -41,43 +39,46 @@ func (c *ConsoleCommand) Run(args []string) int {

var diags tfdiags.Diagnostics

// Load the module
mod, diags := c.Module(configPath)
backendConfig, backendDiags := c.loadBackendConfig(configPath)
diags = diags.Append(backendDiags)
if diags.HasErrors() {
c.showDiagnostics(diags)
return 1
}

var conf *config.Config
if mod != nil {
conf = mod.Config()
}

// Load the backend
b, err := c.Backend(&BackendOpts{
Config: conf,
b, backendDiags := c.Backend(&BackendOpts{
Config: backendConfig,
})

if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
diags = diags.Append(backendDiags)
if backendDiags.HasErrors() {
c.showDiagnostics(diags)
return 1
}

// We require a local backend
local, ok := b.(backend.Local)
if !ok {
c.showDiagnostics(diags) // in case of any warnings in here
c.Ui.Error(ErrUnsupportedLocalOp)
return 1
}

// Build the operation
opReq := c.Operation()
opReq.Module = mod
opReq.ConfigDir = configPath
opReq.ConfigLoader, err = c.initConfigLoader()
if err != nil {
diags = diags.Append(err)
c.showDiagnostics(diags)
return 1
}

// Get the context
ctx, _, err := local.Context(opReq)
if err != nil {
c.Ui.Error(err.Error())
ctx, _, ctxDiags := local.Context(opReq)
diags = diags.Append(ctxDiags)
if ctxDiags.HasErrors() {
c.showDiagnostics(diags)
return 1
}

Expand Down
54 changes: 22 additions & 32 deletions command/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/hashicorp/terraform/tfdiags"

"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/dag"
"github.com/hashicorp/terraform/terraform"
)
Expand Down Expand Up @@ -60,55 +58,47 @@ func (c *GraphCommand) Run(args []string) int {

var diags tfdiags.Diagnostics

// Load the module
var mod *module.Tree
if plan == nil {
var modDiags tfdiags.Diagnostics
mod, modDiags = c.Module(configPath)
diags = diags.Append(modDiags)
if modDiags.HasErrors() {
c.showDiagnostics(diags)
return 1
}
}

var conf *config.Config
if mod != nil {
conf = mod.Config()
backendConfig, backendDiags := c.loadBackendConfig(configPath)
diags = diags.Append(backendDiags)
if diags.HasErrors() {
c.showDiagnostics(diags)
return 1
}

// Load the backend
b, err := c.Backend(&BackendOpts{
Config: conf,
Plan: plan,
b, backendDiags := c.Backend(&BackendOpts{
Config: backendConfig,
})
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
diags = diags.Append(backendDiags)
if backendDiags.HasErrors() {
c.showDiagnostics(diags)
return 1
}

// We require a local backend
local, ok := b.(backend.Local)
if !ok {
c.showDiagnostics(diags) // in case of any warnings in here
c.Ui.Error(ErrUnsupportedLocalOp)
return 1
}

// Building a graph may require config module to be present, even if it's
// empty.
if mod == nil && plan == nil {
mod = module.NewEmptyTree()
}

// Build the operation
opReq := c.Operation()
opReq.Module = mod
opReq.ConfigDir = configPath
opReq.ConfigLoader, err = c.initConfigLoader()
opReq.Plan = plan
if err != nil {
diags = diags.Append(err)
c.showDiagnostics(diags)
return 1
}

// Get the context
ctx, _, err := local.Context(opReq)
if err != nil {
c.Ui.Error(err.Error())
ctx, _, ctxDiags := local.Context(opReq)
diags = diags.Append(ctxDiags)
if ctxDiags.HasErrors() {
c.showDiagnostics(diags)
return 1
}

Expand Down
Loading

0 comments on commit ebafa51

Please sign in to comment.