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.
This commit is contained in:
parent
60e407a59b
commit
5fda76e31d
|
@ -6,7 +6,6 @@ import (
|
||||||
"github.com/hashicorp/terraform/addrs"
|
"github.com/hashicorp/terraform/addrs"
|
||||||
"github.com/hashicorp/terraform/configs"
|
"github.com/hashicorp/terraform/configs"
|
||||||
"github.com/hashicorp/terraform/lang"
|
"github.com/hashicorp/terraform/lang"
|
||||||
"github.com/hashicorp/terraform/states"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// graphNodeModuleCloser is an interface implemented by nodes that finalize the
|
// 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) {
|
func (n *evalPrepareModuleExpansion) Eval(ctx EvalContext) (interface{}, error) {
|
||||||
eachMode := states.NoEach
|
|
||||||
expander := ctx.InstanceExpander()
|
expander := ctx.InstanceExpander()
|
||||||
|
|
||||||
_, call := n.Addr.Call()
|
_, call := n.Addr.Call()
|
||||||
|
|
||||||
// nodeExpandModule itself does not have visibility into how its ancestors
|
// 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.
|
// to our module, and register module instances with each of them.
|
||||||
for _, module := range expander.ExpandModule(n.Addr.Parent()) {
|
for _, module := range expander.ExpandModule(n.Addr.Parent()) {
|
||||||
ctx = ctx.WithPath(module)
|
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"
|
switch {
|
||||||
eachMode = states.EachList
|
case n.ModuleCall.Count != nil:
|
||||||
}
|
count, diags := evaluateResourceCountExpression(n.ModuleCall.Count, ctx)
|
||||||
|
if diags.HasErrors() {
|
||||||
forEach, forEachDiags := evaluateResourceForEachExpression(n.ModuleCall.ForEach, ctx)
|
return nil, diags.Err()
|
||||||
if forEachDiags.HasErrors() {
|
}
|
||||||
return nil, forEachDiags.Err()
|
|
||||||
}
|
|
||||||
|
|
||||||
if forEach != nil {
|
|
||||||
eachMode = states.EachMap
|
|
||||||
}
|
|
||||||
|
|
||||||
switch eachMode {
|
|
||||||
case states.EachList:
|
|
||||||
expander.SetModuleCount(module, call, count)
|
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)
|
expander.SetModuleForEach(module, call, forEach)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
expander.SetModuleSingle(module, call)
|
expander.SetModuleSingle(module, call)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue