diff --git a/terraform/context.go b/terraform/context.go index d45420c58..fa234645c 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -939,6 +939,22 @@ func (c *walkContext) planDestroyWalkFn() depgraph.WalkFunc { // Build another walkContext for this module and walk it. wc := c.Context.walkContext(c.Operation, m.Path) + // compute incoming vars + if m.Config != nil { + wc.Variables = make(map[string]string) + + rc := NewResourceConfig(m.Config.RawConfig) + rc.interpolate(c, nil) + for k, v := range rc.Config { + wc.Variables[k] = v.(string) + } + for k, _ := range rc.Raw { + if _, ok := wc.Variables[k]; !ok { + wc.Variables[k] = config.UnknownVariableValue + } + } + } + // Set the graph to specifically walk this subgraph wc.graph = m.Graph @@ -1716,7 +1732,7 @@ func (c *walkContext) computeResourceMultiVariable( } // If we have no module in the state yet or count, return empty - if module == nil || count == 0 { + if module == nil || len(module.Resources) == 0 || count == 0 { return "", nil } diff --git a/terraform/context_test.go b/terraform/context_test.go index db839195f..dcaccc355 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -3716,6 +3716,55 @@ func TestContextPlan_moduleDestroy(t *testing.T) { } } +func TestContextPlan_moduleDestroyMultivar(t *testing.T) { + m := testModule(t, "plan-module-destroy-multivar") + p := testProvider("aws") + p.DiffFn = testDiffFn + s := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: rootModulePath, + Resources: map[string]*ResourceState{}, + }, + &ModuleState{ + Path: []string{"root", "child"}, + Resources: map[string]*ResourceState{ + "aws_instance.foo.0": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar0", + }, + }, + "aws_instance.foo.1": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar1", + }, + }, + }, + }, + }, + } + ctx := testContext(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: s, + }) + + plan, err := ctx.Plan(&PlanOpts{Destroy: true}) + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(plan.String()) + expected := strings.TrimSpace(testTerraformPlanModuleDestroyMultivarStr) + if actual != expected { + t.Fatalf("bad:\n%s", actual) + } +} + func TestContextPlan_pathVar(t *testing.T) { cwd, err := os.Getwd() if err != nil { diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 7956a94d5..5373894b9 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -790,6 +790,24 @@ module.child: ID = bar ` +const testTerraformPlanModuleDestroyMultivarStr = ` +DIFF: + +module.child: + DESTROY MODULE + DESTROY: aws_instance.foo.0 + DESTROY: aws_instance.foo.1 + +STATE: + + +module.child: + aws_instance.foo.0: + ID = bar0 + aws_instance.foo.1: + ID = bar1 +` + const testTerraformPlanModuleInputStr = ` DIFF: diff --git a/terraform/test-fixtures/plan-module-destroy-multivar/child/main.tf b/terraform/test-fixtures/plan-module-destroy-multivar/child/main.tf new file mode 100644 index 000000000..6a496f06f --- /dev/null +++ b/terraform/test-fixtures/plan-module-destroy-multivar/child/main.tf @@ -0,0 +1,8 @@ +variable "instance_count" { + default = "1" +} + +resource "aws_instance" "foo" { + count = "${var.instance_count}" + bar = "bar" +} diff --git a/terraform/test-fixtures/plan-module-destroy-multivar/main.tf b/terraform/test-fixtures/plan-module-destroy-multivar/main.tf new file mode 100644 index 000000000..ae334b5a5 --- /dev/null +++ b/terraform/test-fixtures/plan-module-destroy-multivar/main.tf @@ -0,0 +1,5 @@ +module "child" { + source = "./child" + instance_count = "2" +} +