terraform/internal/stacks/stackruntime/eval_expr.go
Martin Atkins 67d8a5137b rpcapi+stacks: Stacks runtime can see whether experiments are allowed
We allow experiments only in alpha builds, and so we propagate the flag
for whether that's allowed in from "package main". We previously had that
plumbed in only as far as the rpcapi startup.

This plumbs the flag all the way into package stackeval so that we can
in turn propagate it to Terraform's module config loader, which is
ultimately the one responsible for ensuring that language experiments can
be enabled only when the flag is set.

Therefore it will now be possible to opt in to language experiments in
modules that are used in stack components.
2024-02-12 12:13:08 -08:00

56 lines
2.2 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package stackruntime
import (
"context"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/stacks/stackaddrs"
"github.com/hashicorp/terraform/internal/stacks/stackconfig"
"github.com/hashicorp/terraform/internal/stacks/stackruntime/internal/stackeval"
"github.com/hashicorp/terraform/internal/stacks/stackstate"
"github.com/hashicorp/terraform/internal/tfdiags"
"github.com/zclconf/go-cty/cty"
)
// EvalExpr evaluates the given expression in a specified evaluation
// environment and scope.
//
// This is intended for situations like the "terraform console" command which
// need to evaluate arbitrary expressions against a configuration and
// previously-established state snapshot.
func EvalExpr(ctx context.Context, expr hcl.Expression, req *EvalExprRequest) (cty.Value, tfdiags.Diagnostics) {
main := stackeval.NewForInspecting(req.Config, req.State, stackeval.InspectOpts{
InputVariableValues: req.InputValues,
ProviderFactories: req.ProviderFactories,
})
main.AllowLanguageExperiments(req.ExperimentsAllowed)
return main.EvalExpr(ctx, expr, req.EvalStackInstance, stackeval.InspectPhase)
}
// EvalExprRequest represents the inputs to an [EvalExpr] call.
type EvalExprRequest struct {
// Config and State together provide the global environment in which
// the expression will be evaluated.
Config *stackconfig.Config
State *stackstate.State
// EvalStackInstance is the address of the stack instance where the
// expression is to be evaluated. If unspecified, the default is
// to evaluate in the root stack instance.
EvalStackInstance stackaddrs.StackInstance
// InputValues and ProviderFactories are both optional extras to
// provide a more complete evaluation environment, although neither
// needs to be provided if the expression to be evaluated doesn't
// (directly or indirectly) make use of input variables or provider
// configurations corresponding to these.
InputValues map[stackaddrs.InputVariable]ExternalInputValue
ProviderFactories map[addrs.Provider]providers.Factory
ExperimentsAllowed bool
}