github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/eval_lang.go (about)

     1  package terraform
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/hashicorp/terraform-plugin-sdk/internal/addrs"
     7  
     8  	"github.com/hashicorp/hcl/v2"
     9  	"github.com/hashicorp/terraform-plugin-sdk/internal/configs/configschema"
    10  	"github.com/zclconf/go-cty/cty"
    11  )
    12  
    13  // EvalConfigBlock is an EvalNode implementation that takes a raw
    14  // configuration block and evaluates any expressions within it.
    15  //
    16  // ExpandedConfig is populated with the result of expanding any "dynamic"
    17  // blocks in the given body, which can be useful for extracting correct source
    18  // location information for specific attributes in the result.
    19  type EvalConfigBlock struct {
    20  	Config         *hcl.Body
    21  	Schema         *configschema.Block
    22  	SelfAddr       addrs.Referenceable
    23  	Output         *cty.Value
    24  	ExpandedConfig *hcl.Body
    25  	ContinueOnErr  bool
    26  }
    27  
    28  func (n *EvalConfigBlock) Eval(ctx EvalContext) (interface{}, error) {
    29  	val, body, diags := ctx.EvaluateBlock(*n.Config, n.Schema, n.SelfAddr, EvalDataForNoInstanceKey)
    30  	if diags.HasErrors() && n.ContinueOnErr {
    31  		log.Printf("[WARN] Block evaluation failed: %s", diags.Err())
    32  		return nil, EvalEarlyExitError{}
    33  	}
    34  
    35  	if n.Output != nil {
    36  		*n.Output = val
    37  	}
    38  	if n.ExpandedConfig != nil {
    39  		*n.ExpandedConfig = body
    40  	}
    41  
    42  	return nil, diags.ErrWithWarnings()
    43  }
    44  
    45  // EvalConfigExpr is an EvalNode implementation that takes a raw configuration
    46  // expression and evaluates it.
    47  type EvalConfigExpr struct {
    48  	Expr     hcl.Expression
    49  	SelfAddr addrs.Referenceable
    50  	Output   *cty.Value
    51  }
    52  
    53  func (n *EvalConfigExpr) Eval(ctx EvalContext) (interface{}, error) {
    54  	val, diags := ctx.EvaluateExpr(n.Expr, cty.DynamicPseudoType, n.SelfAddr)
    55  
    56  	if n.Output != nil {
    57  		*n.Output = val
    58  	}
    59  
    60  	return nil, diags.ErrWithWarnings()
    61  }