From 87776913c6e894307ba33c6784cbcaefe18fea80 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 6 Mar 2020 17:16:07 -0500 Subject: [PATCH] nodeExpandModule doesn't need a Path() method Unexpanded nodes can't implement GraphNodeModuleInstance (nee GraphNodeSubPath), because they aren't aware how they have been expanded, and may be in multiple distinct paths. Since that means the EvalContext won't be in the correct path during the walk, we just have to ensure that we don't use `ctx.Path()` inside Eval. --- terraform/node_module_expand.go | 36 +++++++++++++------------------- terraform/transform_reference.go | 6 ------ 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/terraform/node_module_expand.go b/terraform/node_module_expand.go index 49f504052..1e7d4042a 100644 --- a/terraform/node_module_expand.go +++ b/terraform/node_module_expand.go @@ -17,25 +17,15 @@ type nodeExpandModule struct { } var ( - _ GraphNodeModuleInstance = (*nodeExpandModule)(nil) - _ RemovableIfNotTargeted = (*nodeExpandModule)(nil) - _ GraphNodeEvalable = (*nodeExpandModule)(nil) - _ GraphNodeReferencer = (*nodeExpandModule)(nil) + _ RemovableIfNotTargeted = (*nodeExpandModule)(nil) + _ GraphNodeEvalable = (*nodeExpandModule)(nil) + _ GraphNodeReferencer = (*nodeExpandModule)(nil) ) func (n *nodeExpandModule) Name() string { return n.Addr.String() } -// GraphNodeModuleInstance implementation -func (n *nodeExpandModule) Path() addrs.ModuleInstance { - // 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 - // FIXME: - return n.Addr.Parent().UnkeyedInstanceShim() -} - // GraphNodeModulePath implementation func (n *nodeExpandModule) ModulePath() addrs.Module { // This node represents the module call within a module, @@ -97,7 +87,6 @@ type evalPrepareModuleExpansion struct { } func (n *evalPrepareModuleExpansion) Eval(ctx EvalContext) (interface{}, error) { - path := ctx.Path() eachMode := states.NoEach expander := ctx.InstanceExpander() @@ -121,13 +110,18 @@ func (n *evalPrepareModuleExpansion) Eval(ctx EvalContext) (interface{}, error) eachMode = states.EachMap } - switch eachMode { - case states.EachList: - expander.SetModuleCount(path, call, count) - case states.EachMap: - expander.SetModuleForEach(path, call, forEach) - default: - expander.SetModuleSingle(path, call) + // nodeExpandModule itself does not have visibility into how it's ancestors + // were expended, so we use the expander here to provide all possible paths + // to our module, and register module instances with each of them. + for _, path := range expander.ExpandModule(n.Addr.Parent()) { + switch eachMode { + case states.EachList: + expander.SetModuleCount(path, call, count) + case states.EachMap: + expander.SetModuleForEach(path, call, forEach) + default: + expander.SetModuleSingle(path, call) + } } return nil, nil diff --git a/terraform/transform_reference.go b/terraform/transform_reference.go index 13f1046af..1727132ef 100644 --- a/terraform/transform_reference.go +++ b/terraform/transform_reference.go @@ -359,12 +359,6 @@ func NewReferenceMap(vs []dag.Vertex) *ReferenceMap { // Build the lookup table vertices := make(map[string][]dag.Vertex) for _, v := range vs { - _, ok := v.(GraphNodeModuleInstance) - if !ok { - // Only nodes with paths can participate in a reference map. - continue - } - // We're only looking for referenceable nodes rn, ok := v.(GraphNodeReferenceable) if !ok {