Skip to content

Commit

Permalink
command/apply: Handle remote state in a plan file
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Jan 7, 2015
1 parent 14bba88 commit 408ae62
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
65 changes: 65 additions & 0 deletions command/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"testing"
"time"

"github.com/hashicorp/terraform/remote"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
)
Expand Down Expand Up @@ -368,6 +369,70 @@ func TestApply_plan(t *testing.T) {
}
}

func TestApply_plan_remoteState(t *testing.T) {
// Disable test mode so input would be asked
test = false
defer func() { test = true }()
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
if err := remote.EnsureDirectory(); err != nil {
t.Fatalf("err: %v", err)
}

// Set some default reader/writers for the inputs
defaultInputReader = new(bytes.Buffer)
defaultInputWriter = new(bytes.Buffer)

// Create a remote state
state := testState()
conf, srv := testRemoteState(t, state, 200)
defer srv.Close()
state.Remote = conf

planPath := testPlanFile(t, &terraform.Plan{
Module: testModule(t, "apply"),
State: state,
})

p := testProvider()
ui := new(cli.MockUi)
c := &ApplyCommand{
Meta: Meta{
ContextOpts: testCtxConfig(p),
Ui: ui,
},
}

args := []string{
planPath,
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}

if p.InputCalled {
t.Fatalf("input should not be called for plans")
}

// State file should be not be installed
exists, err := remote.ExistsFile(DefaultStateFilename)
if err != nil {
t.Fatalf("err: %v", err)
}
if exists {
t.Fatalf("State path should not exist")
}

// Check for remote state
output, _, err := remote.ReadLocalState()
if err != nil {
t.Fatalf("err: %v", err)
}
if output == nil {
t.Fatalf("missing remote state")
}
}

func TestApply_planWithVarFile(t *testing.T) {
varFileDir := testTempDir(t)
varFilePath := filepath.Join(varFileDir, "terraform.tfvars")
Expand Down
8 changes: 8 additions & 0 deletions command/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ func (m *Meta) Context(copts contextOpts) (*terraform.Context, bool, error) {
plan, err := terraform.ReadPlan(f)
f.Close()
if err == nil {
// Check if remote state is enabled, but do not refresh.
// Since a plan is supposed to lock-in the changes, we do not
// attempt a state refresh.
if plan.State.Remote != nil && plan.State.Remote.Type != "" {
log.Printf("[INFO] Enabling remote state from plan")
m.useRemoteState = true
}

if len(m.variables) > 0 {
return nil, false, fmt.Errorf(
"You can't set variables with the '-var' or '-var-file' flag\n" +
Expand Down

0 comments on commit 408ae62

Please sign in to comment.