From 1384cf6861446ac0b31448fc21e1ea251d2ca288 Mon Sep 17 00:00:00 2001 From: Mark Freebairn Date: Tue, 9 Jan 2018 21:00:55 +0000 Subject: [PATCH] Issue 16360 - Empty -target= points to all resources in state --- terraform/context.go | 14 ++++++++++++++ terraform/context_apply_test.go | 21 +++++++++++++++++++++ terraform/context_plan_test.go | 20 ++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/terraform/context.go b/terraform/context.go index 53a12314a0..f133cc20ad 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -487,6 +487,13 @@ func (c *Context) Input(mode InputMode) error { func (c *Context) Apply() (*State, error) { defer c.acquireRun("apply")() + // Check there are no empty target parameter values + for _, target := range c.targets { + if target == "" { + return nil, fmt.Errorf("Target parameter must not have empty value") + } + } + // Copy our own state c.state = c.state.DeepCopy() @@ -524,6 +531,13 @@ func (c *Context) Apply() (*State, error) { func (c *Context) Plan() (*Plan, error) { defer c.acquireRun("plan")() + // Check there are no empty target parameter values + for _, target := range c.targets { + if target == "" { + return nil, fmt.Errorf("Target parameter must not have empty value") + } + } + p := &Plan{ Module: c.module, Vars: c.variables, diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index ddaaae0b4f..8a09c86597 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -7412,6 +7412,27 @@ aws_instance.foo: `) } +func TestContext2Apply_targetEmpty(t *testing.T) { + m := testModule(t, "apply-targeted") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Module: m, + ProviderResolver: ResourceProviderResolverFixed( + map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + ), + Targets: []string{""}, + }) + + _, err := ctx.Apply() + if err == nil { + t.Fatalf("should error") + } +} + func TestContext2Apply_targetedCount(t *testing.T) { m := testModule(t, "apply-targeted-count") p := testProvider("aws") diff --git a/terraform/context_plan_test.go b/terraform/context_plan_test.go index 8b680879dc..345b1f31a7 100644 --- a/terraform/context_plan_test.go +++ b/terraform/context_plan_test.go @@ -2733,6 +2733,26 @@ STATE: } } +func TestContext2Plan_targetEmpty(t *testing.T) { + m := testModule(t, "plan-targeted") + p := testProvider("aws") + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Module: m, + ProviderResolver: ResourceProviderResolverFixed( + map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + ), + Targets: []string{""}, + }) + + _, err := ctx.Plan() + if err == nil { + t.Fatal("should error") + } +} + // Test that targeting a module properly plans any inputs that depend // on another module. func TestContext2Plan_targetedCrossModule(t *testing.T) {