From 9062a1893e8be27c87cd5fe2abf629625afa67b1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 17 Feb 2017 09:21:29 -0800 Subject: [PATCH] terraform: don't include providers if not targeted Fixes #12009 This is a simple change similar to #10911 where we need to exclude providers that aren't targeted. --- terraform/context_plan_test.go | 33 +++++++++++++++++++ terraform/graph_builder_plan_test.go | 18 ++++++++++ terraform/node_provider_abstract.go | 7 ++++ .../child1/main.tf | 4 +++ .../child2/main.tf | 4 +++ .../main.tf | 2 ++ .../child1/main.tf | 4 +++ .../child2/main.tf | 4 +++ .../main.tf | 9 +++++ 9 files changed, 85 insertions(+) create mode 100644 terraform/test-fixtures/graph-builder-plan-target-module-provider/child1/main.tf create mode 100644 terraform/test-fixtures/graph-builder-plan-target-module-provider/child2/main.tf create mode 100644 terraform/test-fixtures/graph-builder-plan-target-module-provider/main.tf create mode 100644 terraform/test-fixtures/plan-targeted-module-with-provider/child1/main.tf create mode 100644 terraform/test-fixtures/plan-targeted-module-with-provider/child2/main.tf create mode 100644 terraform/test-fixtures/plan-targeted-module-with-provider/main.tf diff --git a/terraform/context_plan_test.go b/terraform/context_plan_test.go index aaa2c69a5..8f576a1b3 100644 --- a/terraform/context_plan_test.go +++ b/terraform/context_plan_test.go @@ -2519,6 +2519,39 @@ STATE: } } +func TestContext2Plan_targetedModuleWithProvider(t *testing.T) { + m := testModule(t, "plan-targeted-module-with-provider") + p := testProvider("null") + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "null": testProviderFuncFixed(p), + }, + Targets: []string{"module.child2"}, + }) + + plan, err := ctx.Plan() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(plan.String()) + expected := strings.TrimSpace(` +DIFF: + +module.child2: + CREATE: null_resource.foo + +STATE: + + + `) + if actual != expected { + t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual) + } +} + func TestContext2Plan_targetedOrphan(t *testing.T) { m := testModule(t, "plan-targeted-orphan") p := testProvider("aws") diff --git a/terraform/graph_builder_plan_test.go b/terraform/graph_builder_plan_test.go index 235fea745..02366fe4f 100644 --- a/terraform/graph_builder_plan_test.go +++ b/terraform/graph_builder_plan_test.go @@ -33,6 +33,24 @@ func TestPlanGraphBuilder(t *testing.T) { } } +func TestPlanGraphBuilder_targetModule(t *testing.T) { + b := &PlanGraphBuilder{ + Module: testModule(t, "graph-builder-plan-target-module-provider"), + Providers: []string{"null"}, + Targets: []string{"module.child2"}, + } + + g, err := b.Build(RootModulePath) + if err != nil { + t.Fatalf("err: %s", err) + } + + t.Logf("Graph: %s", g.String()) + + testGraphNotContains(t, g, "module.child1.provider.null") + testGraphNotContains(t, g, "module.child1.null_resource.foo") +} + const testPlanGraphBuilderStr = ` aws_instance.web aws_security_group.firewall diff --git a/terraform/node_provider_abstract.go b/terraform/node_provider_abstract.go index f82c3f398..6cc836560 100644 --- a/terraform/node_provider_abstract.go +++ b/terraform/node_provider_abstract.go @@ -38,6 +38,13 @@ func (n *NodeAbstractProvider) Path() []string { return n.PathValue } +// RemovableIfNotTargeted +func (n *NodeAbstractProvider) RemoveIfNotTargeted() bool { + // We need to add this so that this node will be removed if + // it isn't targeted or a dependency of a target. + return true +} + // GraphNodeReferencer func (n *NodeAbstractProvider) References() []string { if n.Config == nil { diff --git a/terraform/test-fixtures/graph-builder-plan-target-module-provider/child1/main.tf b/terraform/test-fixtures/graph-builder-plan-target-module-provider/child1/main.tf new file mode 100644 index 000000000..48bfc9c84 --- /dev/null +++ b/terraform/test-fixtures/graph-builder-plan-target-module-provider/child1/main.tf @@ -0,0 +1,4 @@ +variable "key" {} +provider "null" { key = "${var.key}" } + +resource "null_resource" "foo" {} diff --git a/terraform/test-fixtures/graph-builder-plan-target-module-provider/child2/main.tf b/terraform/test-fixtures/graph-builder-plan-target-module-provider/child2/main.tf new file mode 100644 index 000000000..48bfc9c84 --- /dev/null +++ b/terraform/test-fixtures/graph-builder-plan-target-module-provider/child2/main.tf @@ -0,0 +1,4 @@ +variable "key" {} +provider "null" { key = "${var.key}" } + +resource "null_resource" "foo" {} diff --git a/terraform/test-fixtures/graph-builder-plan-target-module-provider/main.tf b/terraform/test-fixtures/graph-builder-plan-target-module-provider/main.tf new file mode 100644 index 000000000..78e799d5a --- /dev/null +++ b/terraform/test-fixtures/graph-builder-plan-target-module-provider/main.tf @@ -0,0 +1,2 @@ +module "child1" { source = "./child1" } +module "child2" { source = "./child2" } diff --git a/terraform/test-fixtures/plan-targeted-module-with-provider/child1/main.tf b/terraform/test-fixtures/plan-targeted-module-with-provider/child1/main.tf new file mode 100644 index 000000000..48bfc9c84 --- /dev/null +++ b/terraform/test-fixtures/plan-targeted-module-with-provider/child1/main.tf @@ -0,0 +1,4 @@ +variable "key" {} +provider "null" { key = "${var.key}" } + +resource "null_resource" "foo" {} diff --git a/terraform/test-fixtures/plan-targeted-module-with-provider/child2/main.tf b/terraform/test-fixtures/plan-targeted-module-with-provider/child2/main.tf new file mode 100644 index 000000000..48bfc9c84 --- /dev/null +++ b/terraform/test-fixtures/plan-targeted-module-with-provider/child2/main.tf @@ -0,0 +1,4 @@ +variable "key" {} +provider "null" { key = "${var.key}" } + +resource "null_resource" "foo" {} diff --git a/terraform/test-fixtures/plan-targeted-module-with-provider/main.tf b/terraform/test-fixtures/plan-targeted-module-with-provider/main.tf new file mode 100644 index 000000000..0fa7bcffd --- /dev/null +++ b/terraform/test-fixtures/plan-targeted-module-with-provider/main.tf @@ -0,0 +1,9 @@ +module "child1" { + source = "./child1" + key = "value" +} + +module "child2" { + source = "./child2" + key = "value" +}