github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/internal/terraform/eval_context.go (about)

     1  package terraform
     2  
     3  import (
     4  	"github.com/hashicorp/hcl/v2"
     5  	"github.com/hashicorp/terraform/internal/addrs"
     6  	"github.com/hashicorp/terraform/internal/checks"
     7  	"github.com/hashicorp/terraform/internal/configs/configschema"
     8  	"github.com/hashicorp/terraform/internal/instances"
     9  	"github.com/hashicorp/terraform/internal/lang"
    10  	"github.com/hashicorp/terraform/internal/plans"
    11  	"github.com/hashicorp/terraform/internal/providers"
    12  	"github.com/hashicorp/terraform/internal/provisioners"
    13  	"github.com/hashicorp/terraform/internal/refactoring"
    14  	"github.com/hashicorp/terraform/internal/states"
    15  	"github.com/hashicorp/terraform/internal/tfdiags"
    16  	"github.com/zclconf/go-cty/cty"
    17  )
    18  
    19  // EvalContext is the interface that is given to eval nodes to execute.
    20  type EvalContext interface {
    21  	// Stopped returns a channel that is closed when evaluation is stopped
    22  	// via Terraform.Context.Stop()
    23  	Stopped() <-chan struct{}
    24  
    25  	// Path is the current module path.
    26  	Path() addrs.ModuleInstance
    27  
    28  	// Hook is used to call hook methods. The callback is called for each
    29  	// hook and should return the hook action to take and the error.
    30  	Hook(func(Hook) (HookAction, error)) error
    31  
    32  	// Input is the UIInput object for interacting with the UI.
    33  	Input() UIInput
    34  
    35  	// InitProvider initializes the provider with the given address, and returns
    36  	// the implementation of the resource provider or an error.
    37  	//
    38  	// It is an error to initialize the same provider more than once. This
    39  	// method will panic if the module instance address of the given provider
    40  	// configuration does not match the Path() of the EvalContext.
    41  	InitProvider(addr addrs.AbsProviderConfig) (providers.Interface, error)
    42  
    43  	// Provider gets the provider instance with the given address (already
    44  	// initialized) or returns nil if the provider isn't initialized.
    45  	//
    46  	// This method expects an _absolute_ provider configuration address, since
    47  	// resources in one module are able to use providers from other modules.
    48  	// InitProvider must've been called on the EvalContext of the module
    49  	// that owns the given provider before calling this method.
    50  	Provider(addrs.AbsProviderConfig) providers.Interface
    51  
    52  	// ProviderSchema retrieves the schema for a particular provider, which
    53  	// must have already been initialized with InitProvider.
    54  	//
    55  	// This method expects an _absolute_ provider configuration address, since
    56  	// resources in one module are able to use providers from other modules.
    57  	ProviderSchema(addrs.AbsProviderConfig) (*ProviderSchema, error)
    58  
    59  	// CloseProvider closes provider connections that aren't needed anymore.
    60  	//
    61  	// This method will panic if the module instance address of the given
    62  	// provider configuration does not match the Path() of the EvalContext.
    63  	CloseProvider(addrs.AbsProviderConfig) error
    64  
    65  	// ConfigureProvider configures the provider with the given
    66  	// configuration. This is a separate context call because this call
    67  	// is used to store the provider configuration for inheritance lookups
    68  	// with ParentProviderConfig().
    69  	//
    70  	// This method will panic if the module instance address of the given
    71  	// provider configuration does not match the Path() of the EvalContext.
    72  	ConfigureProvider(addrs.AbsProviderConfig, cty.Value) tfdiags.Diagnostics
    73  
    74  	// ProviderInput and SetProviderInput are used to configure providers
    75  	// from user input.
    76  	//
    77  	// These methods will panic if the module instance address of the given
    78  	// provider configuration does not match the Path() of the EvalContext.
    79  	ProviderInput(addrs.AbsProviderConfig) map[string]cty.Value
    80  	SetProviderInput(addrs.AbsProviderConfig, map[string]cty.Value)
    81  
    82  	// Provisioner gets the provisioner instance with the given name.
    83  	Provisioner(string) (provisioners.Interface, error)
    84  
    85  	// ProvisionerSchema retrieves the main configuration schema for a
    86  	// particular provisioner, which must have already been initialized with
    87  	// InitProvisioner.
    88  	ProvisionerSchema(string) (*configschema.Block, error)
    89  
    90  	// CloseProvisioner closes all provisioner plugins.
    91  	CloseProvisioners() error
    92  
    93  	// EvaluateBlock takes the given raw configuration block and associated
    94  	// schema and evaluates it to produce a value of an object type that
    95  	// conforms to the implied type of the schema.
    96  	//
    97  	// The "self" argument is optional. If given, it is the referenceable
    98  	// address that the name "self" should behave as an alias for when
    99  	// evaluating. Set this to nil if the "self" object should not be available.
   100  	//
   101  	// The "key" argument is also optional. If given, it is the instance key
   102  	// of the current object within the multi-instance container it belongs
   103  	// to. For example, on a resource block with "count" set this should be
   104  	// set to a different addrs.IntKey for each instance created from that
   105  	// block. Set this to addrs.NoKey if not appropriate.
   106  	//
   107  	// The returned body is an expanded version of the given body, with any
   108  	// "dynamic" blocks replaced with zero or more static blocks. This can be
   109  	// used to extract correct source location information about attributes of
   110  	// the returned object value.
   111  	EvaluateBlock(body hcl.Body, schema *configschema.Block, self addrs.Referenceable, keyData InstanceKeyEvalData) (cty.Value, hcl.Body, tfdiags.Diagnostics)
   112  
   113  	// EvaluateExpr takes the given HCL expression and evaluates it to produce
   114  	// a value.
   115  	//
   116  	// The "self" argument is optional. If given, it is the referenceable
   117  	// address that the name "self" should behave as an alias for when
   118  	// evaluating. Set this to nil if the "self" object should not be available.
   119  	EvaluateExpr(expr hcl.Expression, wantType cty.Type, self addrs.Referenceable) (cty.Value, tfdiags.Diagnostics)
   120  
   121  	// EvaluateReplaceTriggeredBy takes the raw reference expression from the
   122  	// config, and returns the evaluated *addrs.Reference along with a boolean
   123  	// indicating if that reference forces replacement.
   124  	EvaluateReplaceTriggeredBy(expr hcl.Expression, repData instances.RepetitionData) (*addrs.Reference, bool, tfdiags.Diagnostics)
   125  
   126  	// EvaluationScope returns a scope that can be used to evaluate reference
   127  	// addresses in this context.
   128  	EvaluationScope(self addrs.Referenceable, keyData InstanceKeyEvalData) *lang.Scope
   129  
   130  	// SetRootModuleArgument defines the value for one variable of the root
   131  	// module. The caller must ensure that given value is a suitable
   132  	// "final value" for the variable, which means that it's already converted
   133  	// and validated to match any configured constraints and validation rules.
   134  	//
   135  	// Calling this function multiple times with the same variable address
   136  	// will silently overwrite the value provided by a previous call.
   137  	SetRootModuleArgument(addrs.InputVariable, cty.Value)
   138  
   139  	// SetModuleCallArgument defines the value for one input variable of a
   140  	// particular child module call. The caller must ensure that the given
   141  	// value is a suitable "final value" for the variable, which means that
   142  	// it's already converted and validated to match any configured
   143  	// constraints and validation rules.
   144  	//
   145  	// Calling this function multiple times with the same variable address
   146  	// will silently overwrite the value provided by a previous call.
   147  	SetModuleCallArgument(addrs.ModuleCallInstance, addrs.InputVariable, cty.Value)
   148  
   149  	// GetVariableValue returns the value provided for the input variable with
   150  	// the given address, or cty.DynamicVal if the variable hasn't been assigned
   151  	// a value yet.
   152  	//
   153  	// Most callers should deal with variable values only indirectly via
   154  	// EvaluationScope and the other expression evaluation functions, but
   155  	// this is provided because variables tend to be evaluated outside of
   156  	// the context of the module they belong to and so we sometimes need to
   157  	// override the normal expression evaluation behavior.
   158  	GetVariableValue(addr addrs.AbsInputVariableInstance) cty.Value
   159  
   160  	// Changes returns the writer object that can be used to write new proposed
   161  	// changes into the global changes set.
   162  	Changes() *plans.ChangesSync
   163  
   164  	// State returns a wrapper object that provides safe concurrent access to
   165  	// the global state.
   166  	State() *states.SyncState
   167  
   168  	// Checks returns the object that tracks the state of any custom checks
   169  	// declared in the configuration.
   170  	Checks() *checks.State
   171  
   172  	// RefreshState returns a wrapper object that provides safe concurrent
   173  	// access to the state used to store the most recently refreshed resource
   174  	// values.
   175  	RefreshState() *states.SyncState
   176  
   177  	// PrevRunState returns a wrapper object that provides safe concurrent
   178  	// access to the state which represents the result of the previous run,
   179  	// updated only so that object data conforms to current schemas for
   180  	// meaningful comparison with RefreshState.
   181  	PrevRunState() *states.SyncState
   182  
   183  	// InstanceExpander returns a helper object for tracking the expansion of
   184  	// graph nodes during the plan phase in response to "count" and "for_each"
   185  	// arguments.
   186  	//
   187  	// The InstanceExpander is a global object that is shared across all of the
   188  	// EvalContext objects for a given configuration.
   189  	InstanceExpander() *instances.Expander
   190  
   191  	// MoveResults returns a map describing the results of handling any
   192  	// resource instance move statements prior to the graph walk, so that
   193  	// the graph walk can then record that information appropriately in other
   194  	// artifacts produced by the graph walk.
   195  	//
   196  	// This data structure is created prior to the graph walk and read-only
   197  	// thereafter, so callers must not modify the returned map or any other
   198  	// objects accessible through it.
   199  	MoveResults() refactoring.MoveResults
   200  
   201  	// WithPath returns a copy of the context with the internal path set to the
   202  	// path argument.
   203  	WithPath(path addrs.ModuleInstance) EvalContext
   204  }