diff --git a/terraform/transform_orphan.go b/terraform/transform_orphan.go index 0dc0c7303..418baa1be 100644 --- a/terraform/transform_orphan.go +++ b/terraform/transform_orphan.go @@ -64,8 +64,16 @@ type graphNodeOrphanModule struct { Path []string } +func (n *graphNodeOrphanModule) DependableName() []string { + return []string{n.dependableName()} +} + func (n *graphNodeOrphanModule) Name() string { - return fmt.Sprintf("module.%s (orphan)", n.Path[len(n.Path)-1]) + return fmt.Sprintf("%s (orphan)", n.dependableName()) +} + +func (n *graphNodeOrphanModule) dependableName() string { + return fmt.Sprintf("module.%s", n.Path[len(n.Path)-1]) } // graphNodeOrphanResource is the graph vertex representing an orphan resource.. @@ -73,6 +81,14 @@ type graphNodeOrphanResource struct { ResourceName string } +func (n *graphNodeOrphanResource) DependableName() []string { + return []string{n.dependableName()} +} + func (n *graphNodeOrphanResource) Name() string { return fmt.Sprintf("%s (orphan)", n.ResourceName) } + +func (n *graphNodeOrphanResource) dependableName() string { + return n.ResourceName +} diff --git a/terraform/transform_orphan_test.go b/terraform/transform_orphan_test.go index 98c063c1a..fa4409e72 100644 --- a/terraform/transform_orphan_test.go +++ b/terraform/transform_orphan_test.go @@ -156,6 +156,60 @@ func TestOrphanTransformer_modulesDeps(t *testing.T) { } } +func TestOrphanTransformer_modulesDepsOrphan(t *testing.T) { + mod := testModule(t, "transform-orphan-modules") + state := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: RootModulePath, + Resources: map[string]*ResourceState{ + "aws_instance.web": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + }, + }, + + // Orphan module + &ModuleState{ + Path: []string{RootModuleName, "foo"}, + Resources: map[string]*ResourceState{ + "aws_instance.web": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + }, + Dependencies: []string{ + "aws_instance.web", + }, + }, + }, + } + + g := Graph{Path: RootModulePath} + { + tf := &ConfigTransformer{Module: mod} + if err := tf.Transform(&g); err != nil { + t.Fatalf("err: %s", err) + } + } + + transform := &OrphanTransformer{State: state, Config: mod.Config()} + if err := transform.Transform(&g); err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(g.String()) + expected := strings.TrimSpace(testTransformOrphanModulesDepsOrphanStr) + if actual != expected { + t.Fatalf("bad:\n\n%s", actual) + } +} + func TestOrphanTransformer_resourceDepends(t *testing.T) { mod := testModule(t, "transform-orphan-basic") state := &State{ @@ -221,6 +275,13 @@ module.foo (orphan) aws_instance.foo ` +const testTransformOrphanModulesDepsOrphanStr = ` +aws_instance.foo +aws_instance.web (orphan) +module.foo (orphan) + aws_instance.web (orphan) +` + const testTransformOrphanResourceDependsStr = ` aws_instance.db (orphan) aws_instance.web