Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command/apply: Handle remote state in a plan file #755

Merged
merged 1 commit into from
Jan 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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