diff --git a/terraform/node_module_expand.go b/terraform/node_module_expand.go index 5168508ee0..676a230498 100644 --- a/terraform/node_module_expand.go +++ b/terraform/node_module_expand.go @@ -21,9 +21,10 @@ type nodeExpandModule struct { } var ( - _ RemovableIfNotTargeted = (*nodeExpandModule)(nil) - _ GraphNodeEvalable = (*nodeExpandModule)(nil) - _ GraphNodeReferencer = (*nodeExpandModule)(nil) + _ RemovableIfNotTargeted = (*nodeExpandModule)(nil) + _ GraphNodeEvalable = (*nodeExpandModule)(nil) + _ GraphNodeReferencer = (*nodeExpandModule)(nil) + _ GraphNodeReferenceOutside = (*nodeExpandModule)(nil) // modules both record their expansion, and require expansion from their // parent modules. @@ -48,7 +49,7 @@ func (n *nodeExpandModule) ModulePath() addrs.Module { // This node represents the module call within a module, // so return the CallerAddr as the path as the module // call may expand into multiple child instances - return n.Addr.Parent() + return n.Addr } // GraphNodeReferencer implementation @@ -94,6 +95,11 @@ func (n *nodeExpandModule) References() []*addrs.Reference { return appendResourceDestroyReferences(refs) } +// GraphNodeReferenceOutside +func (n *nodeExpandModule) ReferenceOutside() (selfPath, referencePath addrs.Module) { + return n.Addr, n.Addr.Parent() +} + // RemovableIfNotTargeted implementation func (n *nodeExpandModule) RemoveIfNotTargeted() bool { // We need to add this so that this node will be removed if diff --git a/terraform/transform_destroy_edge.go b/terraform/transform_destroy_edge.go index 0d9a06ddf4..226a0162e5 100644 --- a/terraform/transform_destroy_edge.go +++ b/terraform/transform_destroy_edge.go @@ -187,9 +187,6 @@ func (t *pruneUnusedNodesTransformer) Transform(g *Graph) error { for _, v := range g.Vertices() { var path addrs.Module switch v := v.(type) { - case instanceExpander: - path = v.expandsInstances() - case GraphNodeModulePath: path = v.ModulePath() default: diff --git a/terraform/transform_module_expansion.go b/terraform/transform_module_expansion.go index e6c961c324..d1d4185e55 100644 --- a/terraform/transform_module_expansion.go +++ b/terraform/transform_module_expansion.go @@ -3,6 +3,7 @@ package terraform import ( "log" + "github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/configs" "github.com/hashicorp/terraform/dag" ) @@ -105,11 +106,22 @@ func (t *ModuleExpansionTransformer) transform(g *Graph, c *configs.Config, pare t.closers[c.Path.String()] = closer for _, childV := range g.Vertices() { - pather, ok := childV.(GraphNodeModulePath) - if !ok { + // don't connect a node to itself + if childV == v { continue } - if pather.ModulePath().Equal(c.Path) { + + var path addrs.Module + switch t := childV.(type) { + case GraphNodeModulePath: + path = t.ModulePath() + case GraphNodeReferenceOutside: + path, _ = t.ReferenceOutside() + default: + continue + } + + if path.Equal(c.Path) { log.Printf("[TRACE] ModuleExpansionTransformer: %s must wait for expansion of %s", dag.VertexName(childV), c.Path) g.Connect(dag.BasicEdge(childV, v)) }