Merge pull request #755 from hashicorp/f-plan-remote-state

command/apply: Handle remote state in a plan file
This commit is contained in:
Armon Dadgar 2015-01-07 13:13:21 -08:00
commit 58a4a0b253
2 changed files with 73 additions and 0 deletions

View File

@ -15,6 +15,7 @@ import (
"testing"
"time"
"github.com/hashicorp/terraform/remote"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
)
@ -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")

View File

@ -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" +