-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
terraform: EvalContextBuiltin supports partial-expanded module instances
Previously an EvalContextBuiltin could be associated either with no module at all or with a fully-qualified module instance, depending on whether each graph node implements the interface required to announce association with a particular module instance. For supporting partial-expanded module prefixes we now need a third mode where the context is associated with an addrs.PartialExpandedModule address, which conceptually represents an unbounded set of possible module instances that share a common known address prefix. The new type evalContextScope serves as a sum type which represents these three different situations. A nil value of that interface type represents no scope at all, whereas the two implementations of that type represent the two different possible scope types. Both scope types can produce an addrs.Module that they represent, allowing some degree of shared code between the two cases as long as no full instance addresses are required. The most important part of this change is that method EvaluationScope now uses a different implementation of lang.Data when the context scope is a partial-expanded module address. That implementation only returns placeholders that approximate what we expect all instances of the affected modules to have in common, in the hope of still catching some downstream errors even though full evaluation is being deferred to a future run. We currently have no means for a graph node to announce itself as belonging to a partially-expanded module address, and so in practice this new mode is not yet reachable. Future commits will teach the graph walker to be able to recognize and handle graph nodes that need this new treatment, making their DynamicExpand/Execute methods use this new scope type.
- Loading branch information
1 parent
39a6f3a
commit 641837d
Showing
4 changed files
with
138 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package terraform | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/internal/addrs" | ||
) | ||
|
||
// evalContextScope represents the scope that an [EvalContext] (or rather, | ||
// an [EvalContextBuiltin] is associated with. | ||
// | ||
// This is a closed interface representing a sum type, with three possible | ||
// variants: | ||
// | ||
// - a nil value of this type represents a "global" evaluation context used | ||
// for graph nodes that aren't considered to belong to any specific module | ||
// instance. Some [EvalContext] methods are not appropriate for such a | ||
// context, and so will panic on a global evaluation context. | ||
// - [evalContextModuleInstance] is for an evaluation context used for | ||
// graph nodes that implement [GraphNodeModuleInstance], meaning that | ||
// they belong to a fully-expanded single module instance. | ||
// - [evalContextPartialExpandedModule] is for an evaluation context used for | ||
// graph nodes that implement [GraphNodeUnexpandedModule], meaning that | ||
// they belong to an unbounded set of possible module instances sharing | ||
// a common known prefix, in situations where a module call has an unknown | ||
// value for its count or for_each argument. | ||
type evalContextScope interface { | ||
// evalContextScopeModule returns the static module address of whatever | ||
// fully- or partially-expanded module instance address this scope is | ||
// associated with. | ||
// | ||
// A "global" evaluation context is a nil [evalContextScope], and so | ||
// this method will panic for that scope. | ||
evalContextScopeModule() addrs.Module | ||
} | ||
|
||
// evalContextGlobal is the nil [evalContextScope] used to represent an | ||
// [EvalContext] that isn't associated with any module at all. | ||
var evalContextGlobal evalContextScope | ||
|
||
// evalContextModuleInstance is an [evalContextScope] associated with a | ||
// fully-expanded single module instance. | ||
type evalContextModuleInstance struct { | ||
Addr addrs.ModuleInstance | ||
} | ||
|
||
func (s evalContextModuleInstance) evalContextScopeModule() addrs.Module { | ||
return s.Addr.Module() | ||
} | ||
|
||
// evalContextPartialExpandedModule is an [evalContextScope] associated with | ||
// an unbounded set of possible module instances that share a common known | ||
// address prefix. | ||
type evalContextPartialExpandedModule struct { | ||
Addr addrs.PartialExpandedModule | ||
} | ||
|
||
func (s evalContextPartialExpandedModule) evalContextScopeModule() addrs.Module { | ||
return s.Addr.Module() | ||
} |