2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-09-20 07:16:53 -05:00
|
|
|
package tofu
|
2016-09-16 15:56:10 -05:00
|
|
|
|
|
|
|
import (
|
2020-02-24 16:42:32 -06:00
|
|
|
"fmt"
|
2020-09-16 10:32:48 -05:00
|
|
|
"log"
|
2020-02-24 16:42:32 -06:00
|
|
|
|
2019-09-09 17:58:44 -05:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2023-06-28 02:47:24 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/addrs"
|
|
|
|
"github.com/opentofu/opentofu/internal/configs"
|
|
|
|
"github.com/opentofu/opentofu/internal/dag"
|
|
|
|
"github.com/opentofu/opentofu/internal/instances"
|
|
|
|
"github.com/opentofu/opentofu/internal/lang"
|
|
|
|
"github.com/opentofu/opentofu/internal/tfdiags"
|
2016-09-16 15:56:10 -05:00
|
|
|
)
|
|
|
|
|
2020-04-07 16:11:32 -05:00
|
|
|
// nodeExpandModuleVariable is the placeholder for an variable that has not yet had
|
2020-02-24 16:42:32 -06:00
|
|
|
// its module path expanded.
|
2020-04-07 16:11:32 -05:00
|
|
|
type nodeExpandModuleVariable struct {
|
2020-02-24 16:42:32 -06:00
|
|
|
Addr addrs.InputVariable
|
|
|
|
Module addrs.Module
|
|
|
|
Config *configs.Variable
|
|
|
|
Expr hcl.Expression
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-04-07 16:11:32 -05:00
|
|
|
_ GraphNodeDynamicExpandable = (*nodeExpandModuleVariable)(nil)
|
|
|
|
_ GraphNodeReferenceOutside = (*nodeExpandModuleVariable)(nil)
|
|
|
|
_ GraphNodeReferenceable = (*nodeExpandModuleVariable)(nil)
|
|
|
|
_ GraphNodeReferencer = (*nodeExpandModuleVariable)(nil)
|
|
|
|
_ graphNodeTemporaryValue = (*nodeExpandModuleVariable)(nil)
|
2020-05-29 09:31:10 -05:00
|
|
|
_ graphNodeExpandsInstances = (*nodeExpandModuleVariable)(nil)
|
2020-05-21 21:11:58 -05:00
|
|
|
)
|
2020-05-18 19:59:17 -05:00
|
|
|
|
2020-05-21 21:11:58 -05:00
|
|
|
func (n *nodeExpandModuleVariable) expandsInstances() {}
|
2020-05-18 19:59:17 -05:00
|
|
|
|
2020-04-07 16:11:32 -05:00
|
|
|
func (n *nodeExpandModuleVariable) temporaryValue() bool {
|
2020-04-02 12:26:53 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:11:32 -05:00
|
|
|
func (n *nodeExpandModuleVariable) DynamicExpand(ctx EvalContext) (*Graph, error) {
|
2020-02-24 16:42:32 -06:00
|
|
|
var g Graph
|
2023-07-10 05:33:45 -05:00
|
|
|
|
|
|
|
// If this variable has preconditions, we need to report these checks now.
|
|
|
|
//
|
|
|
|
// We should only do this during planning as the apply phase starts with
|
|
|
|
// all the same checkable objects that were registered during the plan.
|
|
|
|
var checkableAddrs addrs.Set[addrs.Checkable]
|
2023-11-08 17:07:09 -06:00
|
|
|
if checkState := ctx.Checks(); checkState.ConfigHasChecks(n.Addr.InModule(n.Module)) {
|
|
|
|
checkableAddrs = addrs.MakeSet[addrs.Checkable]()
|
2023-07-10 05:33:45 -05:00
|
|
|
}
|
|
|
|
|
2020-02-24 16:42:32 -06:00
|
|
|
expander := ctx.InstanceExpander()
|
2020-03-06 17:02:20 -06:00
|
|
|
for _, module := range expander.ExpandModule(n.Module) {
|
2023-07-10 05:33:45 -05:00
|
|
|
addr := n.Addr.Absolute(module)
|
|
|
|
if checkableAddrs != nil {
|
|
|
|
checkableAddrs.Add(addr)
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:11:32 -05:00
|
|
|
o := &nodeModuleVariable{
|
2023-07-10 05:33:45 -05:00
|
|
|
Addr: addr,
|
2020-04-03 14:28:04 -05:00
|
|
|
Config: n.Config,
|
|
|
|
Expr: n.Expr,
|
|
|
|
ModuleInstance: module,
|
2020-02-24 16:42:32 -06:00
|
|
|
}
|
|
|
|
g.Add(o)
|
|
|
|
}
|
2022-09-27 18:25:04 -05:00
|
|
|
addRootNodeToGraph(&g)
|
2023-07-10 05:33:45 -05:00
|
|
|
|
|
|
|
if checkableAddrs != nil {
|
|
|
|
ctx.Checks().ReportCheckableObjects(n.Addr.InModule(n.Module), checkableAddrs)
|
|
|
|
}
|
|
|
|
|
2020-02-24 16:42:32 -06:00
|
|
|
return &g, nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:11:32 -05:00
|
|
|
func (n *nodeExpandModuleVariable) Name() string {
|
2020-05-12 09:28:33 -05:00
|
|
|
return fmt.Sprintf("%s.%s (expand)", n.Module, n.Addr.String())
|
2020-02-24 16:42:32 -06:00
|
|
|
}
|
|
|
|
|
2020-03-04 20:00:16 -06:00
|
|
|
// GraphNodeModulePath
|
2020-04-07 16:11:32 -05:00
|
|
|
func (n *nodeExpandModuleVariable) ModulePath() addrs.Module {
|
2020-03-04 20:00:16 -06:00
|
|
|
return n.Module
|
|
|
|
}
|
|
|
|
|
2020-02-24 16:42:32 -06:00
|
|
|
// GraphNodeReferencer
|
2020-04-07 16:11:32 -05:00
|
|
|
func (n *nodeExpandModuleVariable) References() []*addrs.Reference {
|
2020-02-24 16:42:32 -06:00
|
|
|
|
|
|
|
// If we have no value expression, we cannot depend on anything.
|
|
|
|
if n.Expr == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variables in the root don't depend on anything, because their values
|
|
|
|
// are gathered prior to the graph walk and recorded in the context.
|
|
|
|
if len(n.Module) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we depend on anything referenced by our value expression.
|
|
|
|
// We ignore diagnostics here under the assumption that we'll re-eval
|
|
|
|
// all these things later and catch them then; for our purposes here,
|
|
|
|
// we only care about valid references.
|
|
|
|
//
|
|
|
|
// Due to our GraphNodeReferenceOutside implementation, the addresses
|
|
|
|
// returned by this function are interpreted in the _parent_ module from
|
|
|
|
// where our associated variable was declared, which is correct because
|
|
|
|
// our value expression is assigned within a "module" block in the parent
|
|
|
|
// module.
|
2023-06-28 02:47:24 -05:00
|
|
|
refs, _ := lang.ReferencesInExpr(addrs.ParseRef, n.Expr)
|
2020-02-24 16:42:32 -06:00
|
|
|
return refs
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeReferenceOutside implementation
|
2020-04-07 16:11:32 -05:00
|
|
|
func (n *nodeExpandModuleVariable) ReferenceOutside() (selfPath, referencePath addrs.Module) {
|
2020-02-24 16:42:32 -06:00
|
|
|
return n.Module, n.Module.Parent()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeReferenceable
|
2020-04-07 16:11:32 -05:00
|
|
|
func (n *nodeExpandModuleVariable) ReferenceableAddrs() []addrs.Referenceable {
|
2020-06-15 19:46:03 -05:00
|
|
|
return []addrs.Referenceable{n.Addr}
|
2020-02-24 16:42:32 -06:00
|
|
|
}
|
|
|
|
|
2020-04-07 16:11:32 -05:00
|
|
|
// nodeModuleVariable represents a module variable input during
|
2016-09-16 16:00:21 -05:00
|
|
|
// the apply step.
|
2020-04-07 16:11:32 -05:00
|
|
|
type nodeModuleVariable struct {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
Addr addrs.AbsInputVariableInstance
|
|
|
|
Config *configs.Variable // Config is the var in the config
|
|
|
|
Expr hcl.Expression // Expr is the value expression given in the call
|
2020-04-03 14:28:04 -05:00
|
|
|
// ModuleInstance in order to create the appropriate context for evaluating
|
|
|
|
// ModuleCallArguments, ex. so count.index and each.key can resolve
|
|
|
|
ModuleInstance addrs.ModuleInstance
|
2016-09-16 15:56:10 -05:00
|
|
|
}
|
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
// Ensure that we are implementing all of the interfaces we think we are
|
|
|
|
// implementing.
|
|
|
|
var (
|
2020-04-07 16:11:32 -05:00
|
|
|
_ GraphNodeModuleInstance = (*nodeModuleVariable)(nil)
|
2020-09-16 10:32:48 -05:00
|
|
|
_ GraphNodeExecutable = (*nodeModuleVariable)(nil)
|
2020-04-07 16:11:32 -05:00
|
|
|
_ graphNodeTemporaryValue = (*nodeModuleVariable)(nil)
|
|
|
|
_ dag.GraphNodeDotter = (*nodeModuleVariable)(nil)
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
)
|
2016-09-16 15:56:10 -05:00
|
|
|
|
2020-04-07 16:11:32 -05:00
|
|
|
func (n *nodeModuleVariable) temporaryValue() bool {
|
2020-04-02 12:26:53 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:11:32 -05:00
|
|
|
func (n *nodeModuleVariable) Name() string {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
return n.Addr.String()
|
2016-09-16 15:56:10 -05:00
|
|
|
}
|
|
|
|
|
2020-03-05 15:13:54 -06:00
|
|
|
// GraphNodeModuleInstance
|
2020-04-07 16:11:32 -05:00
|
|
|
func (n *nodeModuleVariable) Path() addrs.ModuleInstance {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
// We execute in the parent scope (above our own module) because
|
|
|
|
// expressions in our value are resolved in that context.
|
|
|
|
return n.Addr.Module.Parent()
|
2016-09-16 15:56:10 -05:00
|
|
|
}
|
|
|
|
|
2020-03-04 20:00:16 -06:00
|
|
|
// GraphNodeModulePath
|
2020-04-07 16:11:32 -05:00
|
|
|
func (n *nodeModuleVariable) ModulePath() addrs.Module {
|
2020-06-06 20:38:08 -05:00
|
|
|
return n.Addr.Module.Module()
|
2020-03-04 20:00:16 -06:00
|
|
|
}
|
|
|
|
|
2020-09-16 10:32:48 -05:00
|
|
|
// GraphNodeExecutable
|
2020-10-28 12:47:04 -05:00
|
|
|
func (n *nodeModuleVariable) Execute(ctx EvalContext, op walkOperation) (diags tfdiags.Diagnostics) {
|
core: Handle root and child module input variables consistently
Previously we had a significant discrepancy between these two situations:
we wrote the raw root module variables directly into the EvalContext and
then applied type conversions only at expression evaluation time, while
for child modules we converted and validated the values while visiting
the variable graph node and wrote only the _final_ value into the
EvalContext.
This confusion seems to have been the root cause for #29899, where
validation rules for root module variables were being applied at the wrong
point in the process, prior to type conversion.
To fix that bug and also make similar mistakes less likely in the future,
I've made the root module variable handling more like the child module
variable handling in the following ways:
- The "raw value" (exactly as given by the user) lives only in the graph
node representing the variable, which mirrors how the _expression_
for a child module variable lives in its graph node. This means that
the flow for the two is the same except that there's no expression
evaluation step for root module variables, because they arrive as
constant values from the caller.
- The set of variable values in the EvalContext is always only "final"
values, after type conversion is complete. That in turn means we no
longer need to do "just in time" conversion in
evaluationStateData.GetInputVariable, and can just return the value
exactly as stored, which is consistent with how we handle all other
references between objects.
This diff is noisier than I'd like because of how much it takes to wire
a new argument (the raw variable values) through to the plan graph builder,
but those changes are pretty mechanical and the interesting logic lives
inside the plan graph builder itself, in NodeRootVariable, and
the shared helper functions in eval_variable.go.
While here I also took the opportunity to fix a historical API wart in
EvalContext, where SetModuleCallArguments was built to take a set of
variable values all at once but our current caller always calls with only
one at a time. That is now just SetModuleCallArgument singular, to match
with the new SetRootModuleArgument to deal with root module variables.
2021-11-10 19:29:45 -06:00
|
|
|
log.Printf("[TRACE] nodeModuleVariable: evaluating %s", n.Addr)
|
2016-09-16 15:56:10 -05:00
|
|
|
|
core: Handle root and child module input variables consistently
Previously we had a significant discrepancy between these two situations:
we wrote the raw root module variables directly into the EvalContext and
then applied type conversions only at expression evaluation time, while
for child modules we converted and validated the values while visiting
the variable graph node and wrote only the _final_ value into the
EvalContext.
This confusion seems to have been the root cause for #29899, where
validation rules for root module variables were being applied at the wrong
point in the process, prior to type conversion.
To fix that bug and also make similar mistakes less likely in the future,
I've made the root module variable handling more like the child module
variable handling in the following ways:
- The "raw value" (exactly as given by the user) lives only in the graph
node representing the variable, which mirrors how the _expression_
for a child module variable lives in its graph node. This means that
the flow for the two is the same except that there's no expression
evaluation step for root module variables, because they arrive as
constant values from the caller.
- The set of variable values in the EvalContext is always only "final"
values, after type conversion is complete. That in turn means we no
longer need to do "just in time" conversion in
evaluationStateData.GetInputVariable, and can just return the value
exactly as stored, which is consistent with how we handle all other
references between objects.
This diff is noisier than I'd like because of how much it takes to wire
a new argument (the raw variable values) through to the plan graph builder,
but those changes are pretty mechanical and the interesting logic lives
inside the plan graph builder itself, in NodeRootVariable, and
the shared helper functions in eval_variable.go.
While here I also took the opportunity to fix a historical API wart in
EvalContext, where SetModuleCallArguments was built to take a set of
variable values all at once but our current caller always calls with only
one at a time. That is now just SetModuleCallArgument singular, to match
with the new SetRootModuleArgument to deal with root module variables.
2021-11-10 19:29:45 -06:00
|
|
|
var val cty.Value
|
2020-09-16 10:32:48 -05:00
|
|
|
var err error
|
|
|
|
|
|
|
|
switch op {
|
2020-10-06 16:14:53 -05:00
|
|
|
case walkValidate:
|
2022-01-10 15:14:30 -06:00
|
|
|
val, err = n.evalModuleVariable(ctx, true)
|
2020-10-28 12:47:04 -05:00
|
|
|
diags = diags.Append(err)
|
2020-10-06 16:14:53 -05:00
|
|
|
default:
|
2022-01-10 15:14:30 -06:00
|
|
|
val, err = n.evalModuleVariable(ctx, false)
|
2020-10-28 12:47:04 -05:00
|
|
|
diags = diags.Append(err)
|
core: Handle root and child module input variables consistently
Previously we had a significant discrepancy between these two situations:
we wrote the raw root module variables directly into the EvalContext and
then applied type conversions only at expression evaluation time, while
for child modules we converted and validated the values while visiting
the variable graph node and wrote only the _final_ value into the
EvalContext.
This confusion seems to have been the root cause for #29899, where
validation rules for root module variables were being applied at the wrong
point in the process, prior to type conversion.
To fix that bug and also make similar mistakes less likely in the future,
I've made the root module variable handling more like the child module
variable handling in the following ways:
- The "raw value" (exactly as given by the user) lives only in the graph
node representing the variable, which mirrors how the _expression_
for a child module variable lives in its graph node. This means that
the flow for the two is the same except that there's no expression
evaluation step for root module variables, because they arrive as
constant values from the caller.
- The set of variable values in the EvalContext is always only "final"
values, after type conversion is complete. That in turn means we no
longer need to do "just in time" conversion in
evaluationStateData.GetInputVariable, and can just return the value
exactly as stored, which is consistent with how we handle all other
references between objects.
This diff is noisier than I'd like because of how much it takes to wire
a new argument (the raw variable values) through to the plan graph builder,
but those changes are pretty mechanical and the interesting logic lives
inside the plan graph builder itself, in NodeRootVariable, and
the shared helper functions in eval_variable.go.
While here I also took the opportunity to fix a historical API wart in
EvalContext, where SetModuleCallArguments was built to take a set of
variable values all at once but our current caller always calls with only
one at a time. That is now just SetModuleCallArgument singular, to match
with the new SetRootModuleArgument to deal with root module variables.
2021-11-10 19:29:45 -06:00
|
|
|
}
|
|
|
|
if diags.HasErrors() {
|
|
|
|
return diags
|
2020-09-16 10:32:48 -05:00
|
|
|
}
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
|
2020-09-16 10:32:48 -05:00
|
|
|
// Set values for arguments of a child module call, for later retrieval
|
|
|
|
// during expression evaluation.
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
_, call := n.Addr.Module.CallInstance()
|
core: Handle root and child module input variables consistently
Previously we had a significant discrepancy between these two situations:
we wrote the raw root module variables directly into the EvalContext and
then applied type conversions only at expression evaluation time, while
for child modules we converted and validated the values while visiting
the variable graph node and wrote only the _final_ value into the
EvalContext.
This confusion seems to have been the root cause for #29899, where
validation rules for root module variables were being applied at the wrong
point in the process, prior to type conversion.
To fix that bug and also make similar mistakes less likely in the future,
I've made the root module variable handling more like the child module
variable handling in the following ways:
- The "raw value" (exactly as given by the user) lives only in the graph
node representing the variable, which mirrors how the _expression_
for a child module variable lives in its graph node. This means that
the flow for the two is the same except that there's no expression
evaluation step for root module variables, because they arrive as
constant values from the caller.
- The set of variable values in the EvalContext is always only "final"
values, after type conversion is complete. That in turn means we no
longer need to do "just in time" conversion in
evaluationStateData.GetInputVariable, and can just return the value
exactly as stored, which is consistent with how we handle all other
references between objects.
This diff is noisier than I'd like because of how much it takes to wire
a new argument (the raw variable values) through to the plan graph builder,
but those changes are pretty mechanical and the interesting logic lives
inside the plan graph builder itself, in NodeRootVariable, and
the shared helper functions in eval_variable.go.
While here I also took the opportunity to fix a historical API wart in
EvalContext, where SetModuleCallArguments was built to take a set of
variable values all at once but our current caller always calls with only
one at a time. That is now just SetModuleCallArgument singular, to match
with the new SetRootModuleArgument to deal with root module variables.
2021-11-10 19:29:45 -06:00
|
|
|
ctx.SetModuleCallArgument(call, n.Addr.Variable, val)
|
2017-07-21 11:41:29 -05:00
|
|
|
|
2020-09-16 10:32:48 -05:00
|
|
|
return evalVariableValidations(n.Addr, n.Config, n.Expr, ctx)
|
2016-09-16 15:56:10 -05:00
|
|
|
}
|
2018-05-02 21:55:53 -05:00
|
|
|
|
|
|
|
// dag.GraphNodeDotter impl.
|
2020-04-07 16:11:32 -05:00
|
|
|
func (n *nodeModuleVariable) DotNode(name string, opts *dag.DotOpts) *dag.DotNode {
|
2018-05-02 21:55:53 -05:00
|
|
|
return &dag.DotNode{
|
|
|
|
Name: name,
|
|
|
|
Attrs: map[string]string{
|
|
|
|
"label": n.Name(),
|
|
|
|
"shape": "note",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-09-16 10:32:48 -05:00
|
|
|
|
2022-01-10 15:14:30 -06:00
|
|
|
// evalModuleVariable produces the value for a particular variable as will
|
2020-09-16 10:32:48 -05:00
|
|
|
// be used by a child module instance.
|
|
|
|
//
|
|
|
|
// The result is written into a map, with its key set to the local name of the
|
|
|
|
// variable, disregarding the module instance address. A map is returned instead
|
|
|
|
// of a single value as a result of trying to be convenient for use with
|
|
|
|
// EvalContext.SetModuleCallArguments, which expects a map to merge in with any
|
|
|
|
// existing arguments.
|
|
|
|
//
|
|
|
|
// validateOnly indicates that this evaluation is only for config
|
|
|
|
// validation, and we will not have any expansion module instance
|
|
|
|
// repetition data.
|
2022-01-10 15:14:30 -06:00
|
|
|
func (n *nodeModuleVariable) evalModuleVariable(ctx EvalContext, validateOnly bool) (cty.Value, error) {
|
core: Handle root and child module input variables consistently
Previously we had a significant discrepancy between these two situations:
we wrote the raw root module variables directly into the EvalContext and
then applied type conversions only at expression evaluation time, while
for child modules we converted and validated the values while visiting
the variable graph node and wrote only the _final_ value into the
EvalContext.
This confusion seems to have been the root cause for #29899, where
validation rules for root module variables were being applied at the wrong
point in the process, prior to type conversion.
To fix that bug and also make similar mistakes less likely in the future,
I've made the root module variable handling more like the child module
variable handling in the following ways:
- The "raw value" (exactly as given by the user) lives only in the graph
node representing the variable, which mirrors how the _expression_
for a child module variable lives in its graph node. This means that
the flow for the two is the same except that there's no expression
evaluation step for root module variables, because they arrive as
constant values from the caller.
- The set of variable values in the EvalContext is always only "final"
values, after type conversion is complete. That in turn means we no
longer need to do "just in time" conversion in
evaluationStateData.GetInputVariable, and can just return the value
exactly as stored, which is consistent with how we handle all other
references between objects.
This diff is noisier than I'd like because of how much it takes to wire
a new argument (the raw variable values) through to the plan graph builder,
but those changes are pretty mechanical and the interesting logic lives
inside the plan graph builder itself, in NodeRootVariable, and
the shared helper functions in eval_variable.go.
While here I also took the opportunity to fix a historical API wart in
EvalContext, where SetModuleCallArguments was built to take a set of
variable values all at once but our current caller always calls with only
one at a time. That is now just SetModuleCallArgument singular, to match
with the new SetRootModuleArgument to deal with root module variables.
2021-11-10 19:29:45 -06:00
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
var givenVal cty.Value
|
|
|
|
var errSourceRange tfdiags.SourceRange
|
|
|
|
if expr := n.Expr; expr != nil {
|
|
|
|
var moduleInstanceRepetitionData instances.RepetitionData
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case validateOnly:
|
|
|
|
// the instance expander does not track unknown expansion values, so we
|
|
|
|
// have to assume all RepetitionData is unknown.
|
|
|
|
moduleInstanceRepetitionData = instances.RepetitionData{
|
|
|
|
CountIndex: cty.UnknownVal(cty.Number),
|
|
|
|
EachKey: cty.UnknownVal(cty.String),
|
|
|
|
EachValue: cty.DynamicVal,
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Get the repetition data for this module instance,
|
|
|
|
// so we can create the appropriate scope for evaluating our expression
|
|
|
|
moduleInstanceRepetitionData = ctx.InstanceExpander().GetModuleInstanceRepetitionData(n.ModuleInstance)
|
2020-09-16 10:32:48 -05:00
|
|
|
}
|
|
|
|
|
2023-03-10 04:11:10 -06:00
|
|
|
scope := ctx.EvaluationScope(nil, nil, moduleInstanceRepetitionData)
|
core: Handle root and child module input variables consistently
Previously we had a significant discrepancy between these two situations:
we wrote the raw root module variables directly into the EvalContext and
then applied type conversions only at expression evaluation time, while
for child modules we converted and validated the values while visiting
the variable graph node and wrote only the _final_ value into the
EvalContext.
This confusion seems to have been the root cause for #29899, where
validation rules for root module variables were being applied at the wrong
point in the process, prior to type conversion.
To fix that bug and also make similar mistakes less likely in the future,
I've made the root module variable handling more like the child module
variable handling in the following ways:
- The "raw value" (exactly as given by the user) lives only in the graph
node representing the variable, which mirrors how the _expression_
for a child module variable lives in its graph node. This means that
the flow for the two is the same except that there's no expression
evaluation step for root module variables, because they arrive as
constant values from the caller.
- The set of variable values in the EvalContext is always only "final"
values, after type conversion is complete. That in turn means we no
longer need to do "just in time" conversion in
evaluationStateData.GetInputVariable, and can just return the value
exactly as stored, which is consistent with how we handle all other
references between objects.
This diff is noisier than I'd like because of how much it takes to wire
a new argument (the raw variable values) through to the plan graph builder,
but those changes are pretty mechanical and the interesting logic lives
inside the plan graph builder itself, in NodeRootVariable, and
the shared helper functions in eval_variable.go.
While here I also took the opportunity to fix a historical API wart in
EvalContext, where SetModuleCallArguments was built to take a set of
variable values all at once but our current caller always calls with only
one at a time. That is now just SetModuleCallArgument singular, to match
with the new SetRootModuleArgument to deal with root module variables.
2021-11-10 19:29:45 -06:00
|
|
|
val, moreDiags := scope.EvalExpr(expr, cty.DynamicPseudoType)
|
|
|
|
diags = diags.Append(moreDiags)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
return cty.DynamicVal, diags.ErrWithWarnings()
|
|
|
|
}
|
|
|
|
givenVal = val
|
|
|
|
errSourceRange = tfdiags.SourceRangeFromHCL(expr.Range())
|
|
|
|
} else {
|
|
|
|
// We'll use cty.NilVal to represent the variable not being set at all.
|
|
|
|
givenVal = cty.NilVal
|
|
|
|
errSourceRange = tfdiags.SourceRangeFromHCL(n.Config.DeclRange) // we use the declaration range as a fallback for an undefined variable
|
2021-11-01 08:02:32 -05:00
|
|
|
}
|
|
|
|
|
2021-12-21 20:04:24 -06:00
|
|
|
// We construct a synthetic InputValue here to pretend as if this were
|
|
|
|
// a root module variable set from outside, just as a convenience so we
|
|
|
|
// can reuse the InputValue type for this.
|
|
|
|
rawVal := &InputValue{
|
|
|
|
Value: givenVal,
|
|
|
|
SourceType: ValueFromConfig,
|
|
|
|
SourceRange: errSourceRange,
|
|
|
|
}
|
|
|
|
|
|
|
|
finalVal, moreDiags := prepareFinalInputVariableValue(n.Addr, rawVal, n.Config)
|
core: Handle root and child module input variables consistently
Previously we had a significant discrepancy between these two situations:
we wrote the raw root module variables directly into the EvalContext and
then applied type conversions only at expression evaluation time, while
for child modules we converted and validated the values while visiting
the variable graph node and wrote only the _final_ value into the
EvalContext.
This confusion seems to have been the root cause for #29899, where
validation rules for root module variables were being applied at the wrong
point in the process, prior to type conversion.
To fix that bug and also make similar mistakes less likely in the future,
I've made the root module variable handling more like the child module
variable handling in the following ways:
- The "raw value" (exactly as given by the user) lives only in the graph
node representing the variable, which mirrors how the _expression_
for a child module variable lives in its graph node. This means that
the flow for the two is the same except that there's no expression
evaluation step for root module variables, because they arrive as
constant values from the caller.
- The set of variable values in the EvalContext is always only "final"
values, after type conversion is complete. That in turn means we no
longer need to do "just in time" conversion in
evaluationStateData.GetInputVariable, and can just return the value
exactly as stored, which is consistent with how we handle all other
references between objects.
This diff is noisier than I'd like because of how much it takes to wire
a new argument (the raw variable values) through to the plan graph builder,
but those changes are pretty mechanical and the interesting logic lives
inside the plan graph builder itself, in NodeRootVariable, and
the shared helper functions in eval_variable.go.
While here I also took the opportunity to fix a historical API wart in
EvalContext, where SetModuleCallArguments was built to take a set of
variable values all at once but our current caller always calls with only
one at a time. That is now just SetModuleCallArgument singular, to match
with the new SetRootModuleArgument to deal with root module variables.
2021-11-10 19:29:45 -06:00
|
|
|
diags = diags.Append(moreDiags)
|
2021-11-01 08:02:32 -05:00
|
|
|
|
core: Handle root and child module input variables consistently
Previously we had a significant discrepancy between these two situations:
we wrote the raw root module variables directly into the EvalContext and
then applied type conversions only at expression evaluation time, while
for child modules we converted and validated the values while visiting
the variable graph node and wrote only the _final_ value into the
EvalContext.
This confusion seems to have been the root cause for #29899, where
validation rules for root module variables were being applied at the wrong
point in the process, prior to type conversion.
To fix that bug and also make similar mistakes less likely in the future,
I've made the root module variable handling more like the child module
variable handling in the following ways:
- The "raw value" (exactly as given by the user) lives only in the graph
node representing the variable, which mirrors how the _expression_
for a child module variable lives in its graph node. This means that
the flow for the two is the same except that there's no expression
evaluation step for root module variables, because they arrive as
constant values from the caller.
- The set of variable values in the EvalContext is always only "final"
values, after type conversion is complete. That in turn means we no
longer need to do "just in time" conversion in
evaluationStateData.GetInputVariable, and can just return the value
exactly as stored, which is consistent with how we handle all other
references between objects.
This diff is noisier than I'd like because of how much it takes to wire
a new argument (the raw variable values) through to the plan graph builder,
but those changes are pretty mechanical and the interesting logic lives
inside the plan graph builder itself, in NodeRootVariable, and
the shared helper functions in eval_variable.go.
While here I also took the opportunity to fix a historical API wart in
EvalContext, where SetModuleCallArguments was built to take a set of
variable values all at once but our current caller always calls with only
one at a time. That is now just SetModuleCallArgument singular, to match
with the new SetRootModuleArgument to deal with root module variables.
2021-11-10 19:29:45 -06:00
|
|
|
return finalVal, diags.ErrWithWarnings()
|
2020-09-16 10:32:48 -05:00
|
|
|
}
|