opentofu/terraform/node_resource_abstract_count.go
Mitchell Hashimoto cf46e1c3e0
terraform: don't validate computed values in validate
This disables the computed value check for `count` during the validation
pass. This enables partial support for #3888 or #1497: as long as the
value is non-computed during the plan, complex values will work in
counts.

**Notably, this allows data source values to be present in counts!**

The "count" value can be disabled during validation safely because we
can treat it as if any field that uses `count.index` is computed for
validation. We then validate a single instance (as if `count = 1`) just
to make sure all required fields are set.
2017-01-27 21:15:43 -08:00

51 lines
1.5 KiB
Go

package terraform
// NodeAbstractCountResource should be embedded instead of NodeAbstractResource
// if the resource has a `count` value that needs to be expanded.
//
// The embedder should implement `DynamicExpand` to process the count.
type NodeAbstractCountResource struct {
*NodeAbstractResource
// Validate, if true, will perform the validation for the count.
// This should only be turned on for the "validate" operation.
Validate bool
}
// GraphNodeEvalable
func (n *NodeAbstractCountResource) EvalTree() EvalNode {
// We only check if the count is computed if we're not validating.
// If we're validating we allow computed counts since they just turn
// into more computed values.
var evalCountCheckComputed EvalNode
if !n.Validate {
evalCountCheckComputed = &EvalCountCheckComputed{Resource: n.Config}
}
return &EvalSequence{
Nodes: []EvalNode{
// The EvalTree for a plannable resource primarily involves
// interpolating the count since it can contain variables
// we only just received access to.
//
// With the interpolated count, we can then DynamicExpand
// into the proper number of instances.
&EvalInterpolate{Config: n.Config.RawCount},
// Check if the count is computed
evalCountCheckComputed,
// If validation is enabled, perform the validation
&EvalIf{
If: func(ctx EvalContext) (bool, error) {
return n.Validate, nil
},
Then: &EvalValidateCount{Resource: n.Config},
},
&EvalCountFixZeroOneBoundary{Resource: n.Config},
},
}
}