Skip to content

Commit 009dc5c

Browse files
authored
direct: internal: simplify makeResourceGraph function (#3450)
More clear responsibility of makeResourceGraph. Also doing more validation on planned actions there.
1 parent e712e7d commit 009dc5c

File tree

4 files changed

+32
-29
lines changed

4 files changed

+32
-29
lines changed

bundle/terranova/apply.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/databricks/cli/libs/diag"
1515
"github.com/databricks/cli/libs/log"
1616
"github.com/databricks/cli/libs/logdiag"
17-
"github.com/databricks/cli/libs/utils"
1817
"github.com/databricks/databricks-sdk-go"
1918
)
2019

@@ -37,32 +36,32 @@ func (m *terranovaApplyMutator) Apply(ctx context.Context, b *bundle.Bundle) dia
3736
return nil
3837
}
3938

39+
g, isReferenced, err := makeResourceGraph(ctx, b)
40+
if err != nil {
41+
logdiag.LogError(ctx, fmt.Errorf("error while reading config: %w", err))
42+
}
43+
4044
// Maps node key to originally planned action
4145
plannedActionsMap := map[nodeKey]deployplan.ActionType{}
4246

4347
for _, action := range b.Plan.Actions {
48+
node := nodeKey{action.Group, action.Name}
4449
plannedActionsMap[nodeKey{action.Group, action.Name}] = action.ActionType
45-
}
46-
47-
state := b.ResourceDatabase.ExportState(ctx)
48-
g, isReferenced, err := makeResourceGraph(ctx, b, state)
49-
if err != nil {
50-
logdiag.LogError(ctx, fmt.Errorf("error while reading config: %w", err))
51-
}
52-
53-
// Remained in state are resources that no longer present in the config
54-
for _, group := range utils.SortedKeys(state) {
55-
groupData := state[group]
56-
for _, name := range utils.SortedKeys(groupData) {
57-
n := nodeKey{group, name}
58-
g.AddNode(n)
59-
if plannedActionsMap[n] != deployplan.ActionTypeDelete {
60-
logdiag.LogError(ctx, fmt.Errorf("internal error, resources %s.%s is missing from state but action is not delete but %v", group, name, plannedActionsMap[n]))
61-
return nil
50+
if !g.HasNode(node) {
51+
if action.ActionType == deployplan.ActionTypeDelete {
52+
// it is expected that this node is not seen by makeResourceGraph
53+
g.AddNode(node)
54+
} else {
55+
// it's internal error today because plan cannot be outdated. In the future when we load serialized plan, this will become user error
56+
logdiag.LogError(ctx, fmt.Errorf("cannot %s %s.%s: internal error, plan is outdated", action.ActionType, action.Group, action.Name))
6257
}
6358
}
6459
}
6560

61+
if logdiag.HasError(ctx) {
62+
return nil
63+
}
64+
6665
err = g.DetectCycle()
6766
if err != nil {
6867
return diag.FromErr(err)

bundle/terranova/graph.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strings"
99

1010
"github.com/databricks/cli/bundle"
11-
"github.com/databricks/cli/bundle/statemgmt/resourcestate"
1211
"github.com/databricks/cli/libs/dagrun"
1312
"github.com/databricks/cli/libs/dyn"
1413
"github.com/databricks/cli/libs/dyn/convert"
@@ -38,8 +37,7 @@ type fieldRef struct {
3837

3938
// makeResourceGraph creates node graph based on ${resources.group.name.id} references.
4039
// Returns a graph and a map of all references that have references to them
41-
// Modifies 'state' in place: all the resources found in the config are removed from state
42-
func makeResourceGraph(ctx context.Context, b *bundle.Bundle, state resourcestate.ExportedResourcesMap) (*dagrun.Graph[nodeKey], map[nodeKey]bool, error) {
40+
func makeResourceGraph(ctx context.Context, b *bundle.Bundle) (*dagrun.Graph[nodeKey], map[nodeKey]bool, error) {
4341
isReferenced := make(map[nodeKey]bool)
4442
g := dagrun.NewGraph[nodeKey]()
4543

@@ -74,9 +72,6 @@ func makeResourceGraph(ctx context.Context, b *bundle.Bundle, state resourcestat
7472
})
7573

7674
for _, node := range nodes {
77-
groupState := state[node.Group]
78-
delete(groupState, node.Name)
79-
8075
g.AddNode(node)
8176

8277
fieldRefs, err := extractReferences(b.Config.Value(), node)

bundle/terranova/plan.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ func CalculateDeployActions(ctx context.Context, b *bundle.Bundle) ([]deployplan
7878

7979
client := b.WorkspaceClient()
8080

81-
state := b.ResourceDatabase.ExportState(ctx)
82-
83-
g, isReferenced, err := makeResourceGraph(ctx, b, state)
81+
g, isReferenced, err := makeResourceGraph(ctx, b)
8482
if err != nil {
8583
return nil, fmt.Errorf("reading config: %w", err)
8684
}
@@ -90,13 +88,19 @@ func CalculateDeployActions(ctx context.Context, b *bundle.Bundle) ([]deployplan
9088
return nil, err
9189
}
9290

91+
state := b.ResourceDatabase.ExportState(ctx)
92+
9393
// Remained in state are resources that no longer present in the config
9494
for _, group := range utils.SortedKeys(state) {
9595
groupData := state[group]
96-
for _, name := range utils.SortedKeys(groupData) {
96+
for _, key := range utils.SortedKeys(groupData) {
97+
if g.HasNode(nodeKey{group, key}) {
98+
// alive resources are processed by makeResourceGraph
99+
continue
100+
}
97101
actions = append(actions, deployplan.Action{
98102
Group: group,
99-
Name: name,
103+
Name: key,
100104
ActionType: deployplan.ActionTypeDelete,
101105
})
102106
}

libs/dagrun/dagrun.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ func (g *Graph[N]) AddNode(n N) {
3636
}
3737
}
3838

39+
func (g *Graph[N]) HasNode(n N) bool {
40+
_, ok := g.adj[n]
41+
return ok
42+
}
43+
3944
func (g *Graph[N]) AddDirectedEdge(from, to N, label string) {
4045
g.AddNode(from)
4146
g.AddNode(to)

0 commit comments

Comments
 (0)