mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-29 10:21:01 -06:00
37b1413ab3
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.
44 lines
1015 B
Go
44 lines
1015 B
Go
package terraform
|
|
|
|
import (
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
|
"github.com/hashicorp/terraform/internal/configs"
|
|
)
|
|
|
|
// RootVariableTransformer is a GraphTransformer that adds all the root
|
|
// variables to the graph.
|
|
//
|
|
// Root variables are currently no-ops but they must be added to the
|
|
// graph since downstream things that depend on them must be able to
|
|
// reach them.
|
|
type RootVariableTransformer struct {
|
|
Config *configs.Config
|
|
|
|
RawValues InputValues
|
|
}
|
|
|
|
func (t *RootVariableTransformer) Transform(g *Graph) error {
|
|
// We can have no variables if we have no config.
|
|
if t.Config == nil {
|
|
return nil
|
|
}
|
|
|
|
// We're only considering root module variables here, since child
|
|
// module variables are handled by ModuleVariableTransformer.
|
|
vars := t.Config.Module.Variables
|
|
|
|
// Add all variables here
|
|
for _, v := range vars {
|
|
node := &NodeRootVariable{
|
|
Addr: addrs.InputVariable{
|
|
Name: v.Name,
|
|
},
|
|
Config: v,
|
|
RawValue: t.RawValues[v.Name],
|
|
}
|
|
g.Add(node)
|
|
}
|
|
|
|
return nil
|
|
}
|