opentofu/terraform/eval_lang.go
Martin Atkins d4cfe85361 core: pass InstanceKey to EvaluateBlock
This gives us the value we need to evaluate a "count.index" reference, and
later also the equivalent for the "for_each" argument once implemented.
2018-10-16 18:46:46 -07:00

62 lines
1.6 KiB
Go

package terraform
import (
"log"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/terraform/config/configschema"
"github.com/zclconf/go-cty/cty"
)
// EvalConfigBlock is an EvalNode implementation that takes a raw
// configuration block and evaluates any expressions within it.
//
// ExpandedConfig is populated with the result of expanding any "dynamic"
// blocks in the given body, which can be useful for extracting correct source
// location information for specific attributes in the result.
type EvalConfigBlock struct {
Config *hcl.Body
Schema *configschema.Block
SelfAddr addrs.Referenceable
Output *cty.Value
ExpandedConfig *hcl.Body
ContinueOnErr bool
}
func (n *EvalConfigBlock) Eval(ctx EvalContext) (interface{}, error) {
val, body, diags := ctx.EvaluateBlock(*n.Config, n.Schema, n.SelfAddr, addrs.NoKey)
if diags.HasErrors() && n.ContinueOnErr {
log.Printf("[WARN] Block evaluation failed: %s", diags.Err())
return nil, EvalEarlyExitError{}
}
if n.Output != nil {
*n.Output = val
}
if n.ExpandedConfig != nil {
*n.ExpandedConfig = body
}
return nil, diags.ErrWithWarnings()
}
// EvalConfigExpr is an EvalNode implementation that takes a raw configuration
// expression and evaluates it.
type EvalConfigExpr struct {
Expr hcl.Expression
SelfAddr addrs.Referenceable
Output *cty.Value
}
func (n *EvalConfigExpr) Eval(ctx EvalContext) (interface{}, error) {
val, diags := ctx.EvaluateExpr(n.Expr, cty.DynamicPseudoType, n.SelfAddr)
if n.Output != nil {
*n.Output = val
}
return nil, diags.ErrWithWarnings()
}