diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 2f4d43296152..a5e12e2d3eca 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -2097,6 +2097,105 @@ func TestContext2Apply_destroy(t *testing.T) { } } +func TestContext2Apply_destroyNestedModule(t *testing.T) { + m := testModule(t, "apply-destroy-nested-module") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + + s := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: []string{"root", "child", "subchild"}, + Resources: map[string]*ResourceState{ + "aws_instance.bar": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + }, + }, + }, + }, + }, + } + + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: s, + }) + + // First plan and apply a create operation + if _, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + // Test that things were destroyed + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(testTerraformApplyDestroyNestedModuleStr) + if actual != expected { + t.Fatalf("bad: \n%s", actual) + } +} + +func TestContext2Apply_destroyDeeplyNestedModule(t *testing.T) { + m := testModule(t, "apply-destroy-deeply-nested-module") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + + s := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: []string{"root", "child", "subchild", "subsubchild"}, + Resources: map[string]*ResourceState{ + "aws_instance.bar": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + }, + }, + }, + }, + }, + } + + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: s, + }) + + // First plan and apply a create operation + if _, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + // Test that things were destroyed + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(` +module.child.subchild.subsubchild: + + `) + if actual != expected { + t.Fatalf("bad: \n%s", actual) + } +} + func TestContext2Apply_destroyOutputs(t *testing.T) { m := testModule(t, "apply-destroy-outputs") h := new(HookRecordApplyOrder) diff --git a/terraform/state.go b/terraform/state.go index d3b19c9de626..2ba005e397d4 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -104,24 +104,56 @@ func (s *State) ModuleByPath(path []string) *ModuleState { // returning their full paths. These paths can be used with ModuleByPath // to return the actual state. func (s *State) ModuleOrphans(path []string, c *config.Config) [][]string { + // direct keeps track of what direct children we have both in our config + // and in our state. childrenKeys keeps track of what isn't an orphan. + direct := make(map[string]struct{}) childrenKeys := make(map[string]struct{}) if c != nil { for _, m := range c.Modules { childrenKeys[m.Name] = struct{}{} + direct[m.Name] = struct{}{} } } - // Go over the direct children and find any that aren't in our - // keys. + // Go over the direct children and find any that aren't in our keys. var orphans [][]string for _, m := range s.Children(path) { - if _, ok := childrenKeys[m.Path[len(m.Path)-1]]; ok { + key := m.Path[len(m.Path)-1] + + // Record that we found this key as a direct child. We use this + // later to find orphan nested modules. + direct[key] = struct{}{} + + // If we have a direct child still in our config, it is not an orphan + if _, ok := childrenKeys[key]; ok { continue } orphans = append(orphans, m.Path) } + // Find the orphans that are nested... + for _, m := range s.Modules { + // We only want modules that are at least grandchildren + if len(m.Path) < len(path)+2 { + continue + } + + // If it isn't part of our tree, continue + if !reflect.DeepEqual(path, m.Path[:len(path)]) { + continue + } + + // If we have the direct child, then just skip it. + key := m.Path[len(path)] + if _, ok := direct[key]; ok { + continue + } + + // Add this orphan + orphans = append(orphans, m.Path[:len(path)+1]) + } + return orphans } diff --git a/terraform/state_test.go b/terraform/state_test.go index 7f3dbb5674f3..eeb974d0b52c 100644 --- a/terraform/state_test.go +++ b/terraform/state_test.go @@ -85,6 +85,28 @@ func TestStateModuleOrphans(t *testing.T) { } } +func TestStateModuleOrphans_nested(t *testing.T) { + state := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: RootModulePath, + }, + &ModuleState{ + Path: []string{RootModuleName, "foo", "bar"}, + }, + }, + } + + actual := state.ModuleOrphans(RootModulePath, nil) + expected := [][]string{ + []string{RootModuleName, "foo"}, + } + + if !reflect.DeepEqual(actual, expected) { + t.Fatalf("bad: %#v", actual) + } +} + func TestStateModuleOrphans_nilConfig(t *testing.T) { state := &State{ Modules: []*ModuleState{ diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 59dad38d4909..c84e9803cfc7 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -483,6 +483,11 @@ const testTerraformApplyDestroyStr = ` ` +const testTerraformApplyDestroyNestedModuleStr = ` +module.child.subchild: + +` + const testTerraformApplyErrorStr = ` aws_instance.bar: ID = bar diff --git a/terraform/test-fixtures/apply-destroy-deeply-nested-module/child/main.tf b/terraform/test-fixtures/apply-destroy-deeply-nested-module/child/main.tf new file mode 100644 index 000000000000..3694951f572f --- /dev/null +++ b/terraform/test-fixtures/apply-destroy-deeply-nested-module/child/main.tf @@ -0,0 +1,3 @@ +module "subchild" { + source = "./subchild" +} diff --git a/terraform/test-fixtures/apply-destroy-deeply-nested-module/child/subchild/main.tf b/terraform/test-fixtures/apply-destroy-deeply-nested-module/child/subchild/main.tf new file mode 100644 index 000000000000..d31b87e0c640 --- /dev/null +++ b/terraform/test-fixtures/apply-destroy-deeply-nested-module/child/subchild/main.tf @@ -0,0 +1,5 @@ +/* +module "subsubchild" { + source = "./subsubchild" +} +*/ diff --git a/terraform/test-fixtures/apply-destroy-deeply-nested-module/child/subchild/subsubchild/main.tf b/terraform/test-fixtures/apply-destroy-deeply-nested-module/child/subchild/subsubchild/main.tf new file mode 100644 index 000000000000..6ff716a4d4c1 --- /dev/null +++ b/terraform/test-fixtures/apply-destroy-deeply-nested-module/child/subchild/subsubchild/main.tf @@ -0,0 +1 @@ +resource "aws_instance" "bar" {} diff --git a/terraform/test-fixtures/apply-destroy-deeply-nested-module/main.tf b/terraform/test-fixtures/apply-destroy-deeply-nested-module/main.tf new file mode 100644 index 000000000000..1f95749fa7ea --- /dev/null +++ b/terraform/test-fixtures/apply-destroy-deeply-nested-module/main.tf @@ -0,0 +1,3 @@ +module "child" { + source = "./child" +} diff --git a/terraform/test-fixtures/apply-destroy-nested-module/child/main.tf b/terraform/test-fixtures/apply-destroy-nested-module/child/main.tf new file mode 100644 index 000000000000..852bce8b9f39 --- /dev/null +++ b/terraform/test-fixtures/apply-destroy-nested-module/child/main.tf @@ -0,0 +1,3 @@ +module "subchild" { + source = "./subchild" +} diff --git a/terraform/test-fixtures/apply-destroy-nested-module/child/subchild/main.tf b/terraform/test-fixtures/apply-destroy-nested-module/child/subchild/main.tf new file mode 100644 index 000000000000..6ff716a4d4c1 --- /dev/null +++ b/terraform/test-fixtures/apply-destroy-nested-module/child/subchild/main.tf @@ -0,0 +1 @@ +resource "aws_instance" "bar" {} diff --git a/terraform/test-fixtures/apply-destroy-nested-module/main.tf b/terraform/test-fixtures/apply-destroy-nested-module/main.tf new file mode 100644 index 000000000000..8a5a1b2e5be7 --- /dev/null +++ b/terraform/test-fixtures/apply-destroy-nested-module/main.tf @@ -0,0 +1,5 @@ +/* +module "child" { + source = "./child" +} +*/ diff --git a/terraform/transform_orphan.go b/terraform/transform_orphan.go index 8e609f6095bd..bb381c823d48 100644 --- a/terraform/transform_orphan.go +++ b/terraform/transform_orphan.go @@ -100,9 +100,14 @@ func (t *OrphanTransformer) Transform(g *Graph) error { moduleOrphans := t.State.ModuleOrphans(g.Path, config) moduleVertexes := make([]dag.Vertex, len(moduleOrphans)) for i, path := range moduleOrphans { + var deps []string + if s := t.State.ModuleByPath(path); s != nil { + deps = s.Dependencies + } + moduleVertexes[i] = g.Add(&graphNodeOrphanModule{ Path: path, - dependentOn: t.State.ModuleByPath(path).Dependencies, + dependentOn: deps, }) } @@ -356,3 +361,9 @@ func (n *graphNodeOrphanResourceFlat) CreateBeforeDestroy() bool { func (n *graphNodeOrphanResourceFlat) CreateNode() dag.Vertex { return n } + +func (n *graphNodeOrphanResourceFlat) ProvidedBy() []string { + return modulePrefixList( + n.graphNodeOrphanResource.ProvidedBy(), + modulePrefixStr(n.PathValue)) +}