From 5fda76e31d0e75570564f5407035859a24684359 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Sun, 5 Apr 2020 10:46:36 -0400 Subject: [PATCH] simplify module expansion eval Make the expansion logic easier to follow, keeping the evaluation and registration local to switch cases. We don't validate anything between count or for_each (config loading should handle that), and we don't need to keep relying on the count == -1 sentinel value. --- terraform/node_module_expand.go | 36 ++++++++++++--------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/terraform/node_module_expand.go b/terraform/node_module_expand.go index 8e0729fb8..dd06d1bbd 100644 --- a/terraform/node_module_expand.go +++ b/terraform/node_module_expand.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/configs" "github.com/hashicorp/terraform/lang" - "github.com/hashicorp/terraform/states" ) // graphNodeModuleCloser is an interface implemented by nodes that finalize the @@ -184,9 +183,7 @@ type evalPrepareModuleExpansion struct { } func (n *evalPrepareModuleExpansion) Eval(ctx EvalContext) (interface{}, error) { - eachMode := states.NoEach expander := ctx.InstanceExpander() - _, call := n.Addr.Call() // nodeExpandModule itself does not have visibility into how its ancestors @@ -194,29 +191,22 @@ func (n *evalPrepareModuleExpansion) Eval(ctx EvalContext) (interface{}, error) // to our module, and register module instances with each of them. for _, module := range expander.ExpandModule(n.Addr.Parent()) { ctx = ctx.WithPath(module) - count, countDiags := evaluateResourceCountExpression(n.ModuleCall.Count, ctx) - if countDiags.HasErrors() { - return nil, countDiags.Err() - } - if count >= 0 { // -1 signals "count not set" - eachMode = states.EachList - } - - forEach, forEachDiags := evaluateResourceForEachExpression(n.ModuleCall.ForEach, ctx) - if forEachDiags.HasErrors() { - return nil, forEachDiags.Err() - } - - if forEach != nil { - eachMode = states.EachMap - } - - switch eachMode { - case states.EachList: + switch { + case n.ModuleCall.Count != nil: + count, diags := evaluateResourceCountExpression(n.ModuleCall.Count, ctx) + if diags.HasErrors() { + return nil, diags.Err() + } expander.SetModuleCount(module, call, count) - case states.EachMap: + + case n.ModuleCall.ForEach != nil: + forEach, diags := evaluateResourceForEachExpression(n.ModuleCall.ForEach, ctx) + if diags.HasErrors() { + return nil, diags.Err() + } expander.SetModuleForEach(module, call, forEach) + default: expander.SetModuleSingle(module, call) }