Skip to content

Commit

Permalink
terraform: GraphWalker interface supports partial-expanded module addrs
Browse files Browse the repository at this point in the history
Nothing calls this yet, because we don't yet have an interface for a
graph node to declare that it belongs to a partially-evaluated module
prefix address. That will follow in later commits.
  • Loading branch information
apparentlymart committed Jan 25, 2024
1 parent 027b67d commit 8bdb8d3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
12 changes: 9 additions & 3 deletions internal/terraform/graph_walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type GraphWalker interface {
EvalContext() EvalContext
EnterPath(addrs.ModuleInstance) EvalContext
ExitPath(addrs.ModuleInstance)
EnterPartialExpandedPath(addrs.PartialExpandedModule) EvalContext
ExitPartialExpandedPath(addrs.PartialExpandedModule)
Execute(EvalContext, GraphNodeExecutable) tfdiags.Diagnostics
}

Expand All @@ -22,7 +24,11 @@ type GraphWalker interface {
// implementing all the required functions.
type NullGraphWalker struct{}

func (NullGraphWalker) EvalContext() EvalContext { return new(MockEvalContext) }
func (NullGraphWalker) EnterPath(addrs.ModuleInstance) EvalContext { return new(MockEvalContext) }
func (NullGraphWalker) ExitPath(addrs.ModuleInstance) {}
func (NullGraphWalker) EvalContext() EvalContext { return new(MockEvalContext) }
func (NullGraphWalker) EnterPath(addrs.ModuleInstance) EvalContext { return new(MockEvalContext) }
func (NullGraphWalker) ExitPath(addrs.ModuleInstance) {}
func (NullGraphWalker) EnterPartialExpandedPath(addrs.PartialExpandedModule) EvalContext {
return new(MockEvalContext)
}
func (NullGraphWalker) ExitPartialExpandedPath(addrs.PartialExpandedModule) {}
func (NullGraphWalker) Execute(EvalContext, GraphNodeExecutable) tfdiags.Diagnostics { return nil }
17 changes: 17 additions & 0 deletions internal/terraform/graph_walk_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type ContextGraphWalker struct {
provisionerLock sync.Mutex
}

var _ GraphWalker = (*ContextGraphWalker)(nil)

func (w *ContextGraphWalker) EnterPath(path addrs.ModuleInstance) EvalContext {
w.contextLock.Lock()
defer w.contextLock.Unlock()
Expand All @@ -78,6 +80,21 @@ func (w *ContextGraphWalker) EnterPath(path addrs.ModuleInstance) EvalContext {
return ctx
}

func (w *ContextGraphWalker) EnterPartialExpandedPath(path addrs.PartialExpandedModule) EvalContext {
w.contextLock.Lock()
defer w.contextLock.Unlock()

// If we already have a context for this path cached, use that
key := path.String()
if ctx, ok := w.contexts[key]; ok {
return ctx
}

ctx := w.EvalContext().WithPartialExpandedPath(path)
w.contexts[key] = ctx.(*BuiltinEvalContext)
return ctx
}

func (w *ContextGraphWalker) EvalContext() EvalContext {
w.once.Do(w.init)

Expand Down

0 comments on commit 8bdb8d3

Please sign in to comment.