From 79033f240fdcc846fc28081efe648015a0154849 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 26 Jul 2014 14:32:09 -0700 Subject: [PATCH] command: error if variables present when creating context from plan --- command/apply_test.go | 25 +++++++++++++++++++++++++ command/meta.go | 8 ++++++++ 2 files changed, 33 insertions(+) diff --git a/command/apply_test.go b/command/apply_test.go index aeea86414..452e2a8a8 100644 --- a/command/apply_test.go +++ b/command/apply_test.go @@ -288,6 +288,31 @@ func TestApply_plan(t *testing.T) { } } +func TestApply_planVars(t *testing.T) { + planPath := testPlanFile(t, &terraform.Plan{ + Config: new(config.Config), + }) + statePath := testTempFile(t) + + p := testProvider() + ui := new(cli.MockUi) + c := &ApplyCommand{ + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, + }, + } + + args := []string{ + "-state", statePath, + "-var", "foo=bar", + planPath, + } + if code := c.Run(args); code == 0 { + t.Fatal("should've failed") + } +} + func TestApply_shutdown(t *testing.T) { stopped := false stopCh := make(chan struct{}) diff --git a/command/meta.go b/command/meta.go index d33831e44..a21c47d5e 100644 --- a/command/meta.go +++ b/command/meta.go @@ -47,6 +47,14 @@ func (m *Meta) Context(path, statePath string, doPlan bool) (*terraform.Context, plan, err := terraform.ReadPlan(f) f.Close() if err == nil { + if len(m.variables) > 0 { + return nil, fmt.Errorf( + "You can't set variables with the '-var' or '-var-file' flag\n" + + "when you're applying a plan file. The variables used when\n" + + "the plan was created will be used. If you wish to use different\n" + + "variable values, create a new plan file.") + } + return plan.Context(opts), nil } }