Skip to content

Commit a6fbfc6

Browse files
authored
direct: refactor: query graph for IsReferenced info (#3458)
Follow up to #3455
1 parent 0297797 commit a6fbfc6

File tree

5 files changed

+11
-14
lines changed

5 files changed

+11
-14
lines changed

bundle/bundle.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,6 @@ type Bundle struct {
140140
// (direct only) planned action for each resource
141141
PlannedActions map[deployplan.ResourceNode]deployplan.ActionType
142142

143-
// (direct only) flag that tells if a given node has references to it
144-
// TODO: move this inside Graph
145-
IsReferenced map[deployplan.ResourceNode]bool
146-
147143
// if true, we skip approval checks for deploy, destroy resources and delete
148144
// files
149145
AutoApprove bool

bundle/terranova/apply.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (m *terranovaApplyMutator) Apply(ctx context.Context, b *bundle.Bundle) dia
127127
}
128128

129129
// Update resources.id after successful deploy so that future ${resources...id} refs are replaced
130-
if b.IsReferenced[node] {
130+
if b.Graph.HasOutgoingEdges(node) {
131131
err = resolveIDReference(ctx, b, node.Group, node.Key)
132132
if err != nil {
133133
// not using errorPrefix because resource was deployed

bundle/terranova/graph.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ type fieldRef struct {
2424
}
2525

2626
// makeResourceGraph creates node graph based on ${resources.group.name.id} references.
27-
// Returns a graph and a map of all references that have references to them
28-
func makeResourceGraph(ctx context.Context, b *bundle.Bundle) (*dagrun.Graph[deployplan.ResourceNode], map[deployplan.ResourceNode]bool, error) {
29-
isReferenced := make(map[deployplan.ResourceNode]bool)
27+
func makeResourceGraph(ctx context.Context, b *bundle.Bundle) (*dagrun.Graph[deployplan.ResourceNode], error) {
3028
g := dagrun.NewGraph[deployplan.ResourceNode]()
3129

3230
// Collect and sort nodes first, because MapByPattern gives them in randomized order
@@ -49,7 +47,7 @@ func makeResourceGraph(ctx context.Context, b *bundle.Bundle) (*dagrun.Graph[dep
4947
},
5048
)
5149
if err != nil {
52-
return nil, nil, fmt.Errorf("reading config: %w", err)
50+
return nil, fmt.Errorf("reading config: %w", err)
5351
}
5452

5553
slices.SortFunc(nodes, func(a, b deployplan.ResourceNode) int {
@@ -64,7 +62,7 @@ func makeResourceGraph(ctx context.Context, b *bundle.Bundle) (*dagrun.Graph[dep
6462

6563
fieldRefs, err := extractReferences(b.Config.Value(), node)
6664
if err != nil {
67-
return nil, nil, fmt.Errorf("failed to read references from config for %s: %w", node.String(), err)
65+
return nil, fmt.Errorf("failed to read references from config for %s: %w", node.String(), err)
6866
}
6967

7068
for _, fieldRef := range fieldRefs {
@@ -78,12 +76,11 @@ func makeResourceGraph(ctx context.Context, b *bundle.Bundle) (*dagrun.Graph[dep
7876
node,
7977
label,
8078
)
81-
isReferenced[referencedNode] = true
8279
}
8380
}
8481
}
8582

86-
return g, isReferenced, nil
83+
return g, nil
8784
}
8885

8986
func extractReferences(root dyn.Value, node deployplan.ResourceNode) ([]fieldRef, error) {

bundle/terranova/plan.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func CalculatePlanForDeploy(ctx context.Context, b *bundle.Bundle) error {
9999

100100
client := b.WorkspaceClient()
101101

102-
b.Graph, b.IsReferenced, err = makeResourceGraph(ctx, b)
102+
b.Graph, err = makeResourceGraph(ctx, b)
103103
if err != nil {
104104
return fmt.Errorf("reading config: %w", err)
105105
}
@@ -150,7 +150,7 @@ func CalculatePlanForDeploy(ctx context.Context, b *bundle.Bundle) error {
150150
return false
151151
}
152152

153-
if b.IsReferenced[node] && actionType.KeepsID() {
153+
if b.Graph.HasOutgoingEdges(node) && actionType.KeepsID() {
154154
err = resolveIDReference(ctx, b, pl.group, pl.resourceName)
155155
if err != nil {
156156
logdiag.LogError(ctx, fmt.Errorf("failed to replace ref to resources.%s.%s.id: %w", pl.group, pl.resourceName, err))

libs/dagrun/dagrun.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func (g *Graph[N]) HasNode(n N) bool {
4141
return ok
4242
}
4343

44+
// HasOutgoingEdges reports whether this node has at least one outgoing edge.
45+
// In this graph, an outgoing edge from X->Y means Y references X.
46+
func (g *Graph[N]) HasOutgoingEdges(n N) bool { return len(g.adj[n]) > 0 }
47+
4448
func (g *Graph[N]) AddDirectedEdge(from, to N, label string) {
4549
g.AddNode(from)
4650
g.AddNode(to)

0 commit comments

Comments
 (0)