Skip to content

Commit 922cfa3

Browse files
authored
internal: simplify ActionType: make it a string; shorter constants (#4214)
## Why Simpler. Avoid conversion back and forth.
1 parent ccbd725 commit 922cfa3

29 files changed

+138
-210
lines changed

bundle/deploy/terraform/showplanfile.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ func populatePlan(ctx context.Context, plan *deployplan.Plan, changes []*tfjson.
7272
var actionType deployplan.ActionType
7373
switch {
7474
case rc.Change.Actions.Delete():
75-
actionType = deployplan.ActionTypeDelete
75+
actionType = deployplan.Delete
7676
case rc.Change.Actions.Replace():
77-
actionType = deployplan.ActionTypeRecreate
77+
actionType = deployplan.Recreate
7878
case rc.Change.Actions.Create():
79-
actionType = deployplan.ActionTypeCreate
79+
actionType = deployplan.Create
8080
case rc.Change.Actions.Update():
81-
actionType = deployplan.ActionTypeUpdate
81+
actionType = deployplan.Update
8282
case rc.Change.Actions.NoOp():
83-
actionType = deployplan.ActionTypeSkip
83+
actionType = deployplan.Skip
8484
default:
8585
continue
8686
}
@@ -105,7 +105,7 @@ func populatePlan(ctx context.Context, plan *deployplan.Plan, changes []*tfjson.
105105
key = "resources." + group + "." + rc.Name
106106
}
107107

108-
plan.Plan[key] = &deployplan.PlanEntry{Action: actionType.String()}
108+
plan.Plan[key] = &deployplan.PlanEntry{Action: actionType}
109109
}
110110
}
111111

bundle/deploy/terraform/showplanfile_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,29 @@ func TestPopulatePlan(t *testing.T) {
5050
// Assert that the actions list contains all expected actions
5151
expectedActions := []deployplan.Action{
5252
{
53-
ActionType: deployplan.ActionTypeCreate,
53+
ActionType: deployplan.Create,
5454
ResourceKey: "resources.pipelines.create pipeline",
5555
},
5656
{
57-
ActionType: deployplan.ActionTypeDelete,
57+
ActionType: deployplan.Delete,
5858
ResourceKey: "resources.pipelines.delete pipeline",
5959
},
6060
{
61-
ActionType: deployplan.ActionTypeRecreate,
61+
ActionType: deployplan.Recreate,
6262
ResourceKey: "resources.pipelines.recreate pipeline",
6363
},
6464
}
6565
assert.Equal(t, expectedActions, actions)
6666

6767
// Also test that the plan was populated correctly with expected entries
6868
assert.Contains(t, plan.Plan, "resources.pipelines.create pipeline")
69-
assert.Equal(t, "create", plan.Plan["resources.pipelines.create pipeline"].Action)
69+
assert.Equal(t, deployplan.Create, plan.Plan["resources.pipelines.create pipeline"].Action)
7070

7171
assert.Contains(t, plan.Plan, "resources.pipelines.delete pipeline")
72-
assert.Equal(t, "delete", plan.Plan["resources.pipelines.delete pipeline"].Action)
72+
assert.Equal(t, deployplan.Delete, plan.Plan["resources.pipelines.delete pipeline"].Action)
7373

7474
assert.Contains(t, plan.Plan, "resources.pipelines.recreate pipeline")
75-
assert.Equal(t, "recreate", plan.Plan["resources.pipelines.recreate pipeline"].Action)
75+
assert.Equal(t, deployplan.Recreate, plan.Plan["resources.pipelines.recreate pipeline"].Action)
7676

7777
// Unknown resource type should not be in the plan
7878
assert.NotContains(t, plan.Plan, "resources.recreate whatever")

bundle/deployplan/action.go

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -22,57 +22,36 @@ func (a Action) IsChildResource() bool {
2222
return len(items) == 4
2323
}
2424

25-
type ActionType int
25+
type ActionType string
2626

2727
// Actions are ordered in increasing severity.
2828
// If case of several options, action with highest severity wins.
2929
// Note, Create/Delete are handled explicitly and never compared.
3030
const (
31-
ActionTypeUndefined ActionType = iota
32-
ActionTypeSkip
33-
ActionTypeResize
34-
ActionTypeUpdate
35-
ActionTypeUpdateWithID
36-
ActionTypeCreate
37-
ActionTypeRecreate
38-
ActionTypeDelete
31+
Undefined ActionType = ""
32+
Skip ActionType = "skip"
33+
Resize ActionType = "resize"
34+
Update ActionType = "update"
35+
UpdateWithID ActionType = "update_id"
36+
Create ActionType = "create"
37+
Recreate ActionType = "recreate"
38+
Delete ActionType = "delete"
3939
)
4040

41-
const (
42-
ActionTypeUndefinedString = ""
43-
ActionTypeSkipString = "skip"
44-
ActionTypeResizeString = "resize"
45-
ActionTypeUpdateString = "update"
46-
ActionTypeUpdateWithIDString = "update_id"
47-
ActionTypeCreateString = "create"
48-
ActionTypeRecreateString = "recreate"
49-
ActionTypeDeleteString = "delete"
50-
)
51-
52-
var actionName = map[ActionType]string{
53-
ActionTypeSkip: ActionTypeSkipString,
54-
ActionTypeResize: ActionTypeResizeString,
55-
ActionTypeUpdate: ActionTypeUpdateString,
56-
ActionTypeUpdateWithID: ActionTypeUpdateWithIDString,
57-
ActionTypeCreate: ActionTypeCreateString,
58-
ActionTypeRecreate: ActionTypeRecreateString,
59-
ActionTypeDelete: ActionTypeDeleteString,
60-
}
61-
62-
var nameToAction = map[string]ActionType{}
63-
64-
func init() {
65-
for k, v := range actionName {
66-
if _, ok := nameToAction[v]; ok {
67-
panic("duplicate action string: " + v)
68-
}
69-
nameToAction[v] = k
70-
}
41+
var actionOrder = map[ActionType]int{
42+
Undefined: 0,
43+
Skip: 1,
44+
Resize: 2,
45+
Update: 3,
46+
UpdateWithID: 4,
47+
Create: 5,
48+
Recreate: 6,
49+
Delete: 7,
7150
}
7251

7352
func (a ActionType) KeepsID() bool {
7453
switch a {
75-
case ActionTypeCreate, ActionTypeUpdateWithID, ActionTypeRecreate, ActionTypeDelete:
54+
case Create, UpdateWithID, Recreate, Delete:
7655
return false
7756
default:
7857
return true
@@ -81,30 +60,15 @@ func (a ActionType) KeepsID() bool {
8160

8261
// StringShort short version of action string, without suffix
8362
func (a ActionType) StringShort() string {
84-
items := strings.SplitN(actionName[a], "_", 2)
63+
items := strings.SplitN(string(a), "_", 2)
8564
return items[0]
8665
}
8766

88-
// String returns the string representation of the action type.
89-
func (a ActionType) String() string {
90-
return actionName[a]
91-
}
92-
93-
func ActionTypeFromString(s string) ActionType {
94-
actionType, ok := nameToAction[s]
95-
if !ok {
96-
return ActionTypeUndefined
97-
}
98-
return actionType
99-
}
100-
101-
// Filter returns actions that match the specified action type
102-
func Filter(changes []Action, actionType ActionType) []Action {
103-
var result []Action
104-
for _, action := range changes {
105-
if action.ActionType == actionType {
106-
result = append(result, action)
107-
}
67+
// GetHigherAction returns the action with higher severity between a and b.
68+
// Actions are ordered by severity in actionOrder map.
69+
func GetHigherAction(a, b ActionType) ActionType {
70+
if actionOrder[a] > actionOrder[b] {
71+
return a
10872
}
109-
return result
73+
return b
11074
}

bundle/deployplan/action_test.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

bundle/deployplan/plan.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func LoadPlanFromFile(path string) (*Plan, error) {
6565
type PlanEntry struct {
6666
ID string `json:"id,omitempty"`
6767
DependsOn []DependsOnEntry `json:"depends_on,omitempty"`
68-
Action string `json:"action,omitempty"`
68+
Action ActionType `json:"action,omitempty"`
6969
NewState *structvar.StructVarJSON `json:"new_state,omitempty"`
7070
RemoteState any `json:"remote_state,omitempty"`
7171
Changes Changes `json:"changes,omitempty"`
@@ -79,11 +79,11 @@ type DependsOnEntry struct {
7979
type Changes map[string]*ChangeDesc
8080

8181
type ChangeDesc struct {
82-
Action string `json:"action"`
83-
Reason string `json:"reason,omitempty"`
84-
Old any `json:"old,omitempty"`
85-
New any `json:"new,omitempty"`
86-
Remote any `json:"remote,omitempty"`
82+
Action ActionType `json:"action"`
83+
Reason string `json:"reason,omitempty"`
84+
Old any `json:"old,omitempty"`
85+
New any `json:"new,omitempty"`
86+
Remote any `json:"remote,omitempty"`
8787
}
8888

8989
// Possible values for Reason field
@@ -118,10 +118,9 @@ func (c *Changes) HasChange(fieldPath string) bool {
118118
func (p *Plan) GetActions() []Action {
119119
actions := make([]Action, 0, len(p.Plan))
120120
for key, entry := range p.Plan {
121-
at := ActionTypeFromString(entry.Action)
122121
actions = append(actions, Action{
123122
ResourceKey: key,
124-
ActionType: at,
123+
ActionType: entry.Action,
125124
})
126125
}
127126

bundle/direct/apply.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (d *DeploymentUnit) Destroy(ctx context.Context, db *dstate.DeploymentState
2828
}
2929

3030
func (d *DeploymentUnit) Deploy(ctx context.Context, db *dstate.DeploymentState, newState any, actionType deployplan.ActionType, changes deployplan.Changes) error {
31-
if actionType == deployplan.ActionTypeCreate {
31+
if actionType == deployplan.Create {
3232
return d.Create(ctx, db, newState)
3333
}
3434

@@ -43,13 +43,13 @@ func (d *DeploymentUnit) Deploy(ctx context.Context, db *dstate.DeploymentState,
4343
}
4444

4545
switch actionType {
46-
case deployplan.ActionTypeRecreate:
46+
case deployplan.Recreate:
4747
return d.Recreate(ctx, db, oldID, newState)
48-
case deployplan.ActionTypeUpdate:
48+
case deployplan.Update:
4949
return d.Update(ctx, db, oldID, newState, changes)
50-
case deployplan.ActionTypeUpdateWithID:
50+
case deployplan.UpdateWithID:
5151
return d.UpdateWithID(ctx, db, oldID, newState)
52-
case deployplan.ActionTypeResize:
52+
case deployplan.Resize:
5353
return d.Resize(ctx, db, oldID, newState)
5454
default:
5555
return fmt.Errorf("internal error: unexpected actionType: %#v", actionType)

bundle/direct/bundle_apply.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,14 @@ func (b *DeploymentBundle) Apply(ctx context.Context, client *databricks.Workspa
5555
errorPrefix = "cannot migrate " + resourceKey
5656
}
5757

58-
at := deployplan.ActionTypeFromString(action)
59-
if at == deployplan.ActionTypeUndefined {
58+
if action == deployplan.Undefined {
6059
logdiag.LogError(ctx, fmt.Errorf("cannot deploy %s: unknown action %q", resourceKey, action))
6160
return false
6261
}
6362

6463
// If a dependency failed, report and skip execution for this node by returning false
6564
if failedDependency != nil {
66-
if at != deployplan.ActionTypeSkip {
65+
if action != deployplan.Skip {
6766
logdiag.LogError(ctx, fmt.Errorf("%s: dependency failed: %s", errorPrefix, *failedDependency))
6867
}
6968
return false
@@ -81,7 +80,7 @@ func (b *DeploymentBundle) Apply(ctx context.Context, client *databricks.Workspa
8180
DependsOn: entry.DependsOn,
8281
}
8382

84-
if at == deployplan.ActionTypeDelete {
83+
if action == deployplan.Delete {
8584
if migrateMode {
8685
logdiag.LogError(ctx, fmt.Errorf("%s: Unexpected delete action during migration", errorPrefix))
8786
return false
@@ -96,7 +95,7 @@ func (b *DeploymentBundle) Apply(ctx context.Context, client *databricks.Workspa
9695

9796
// We don't keep NewState around for 'skip' nodes
9897

99-
if at != deployplan.ActionTypeSkip {
98+
if action != deployplan.Skip {
10099
if !b.resolveReferences(ctx, resourceKey, entry, errorPrefix, false) {
101100
return false
102101
}
@@ -123,7 +122,7 @@ func (b *DeploymentBundle) Apply(ctx context.Context, client *databricks.Workspa
123122
err = b.StateDB.SaveState(resourceKey, dbentry.ID, sv.Value, entry.DependsOn)
124123
} else {
125124
// TODO: redo calcDiff to downgrade planned action if possible (?)
126-
err = d.Deploy(ctx, &b.StateDB, sv.Value, at, entry.Changes)
125+
err = d.Deploy(ctx, &b.StateDB, sv.Value, action, entry.Changes)
127126
}
128127

129128
if err != nil {
@@ -178,8 +177,8 @@ func (b *DeploymentBundle) LookupReferenceRemote(ctx context.Context, path *stru
178177

179178
defer b.Plan.ReadUnlockEntry(targetResourceKey)
180179

181-
targetAction := deployplan.ActionTypeFromString(targetEntry.Action)
182-
if targetAction == deployplan.ActionTypeUndefined {
180+
targetAction := targetEntry.Action
181+
if targetAction == deployplan.Undefined {
183182
return nil, fmt.Errorf("internal error: %s: missing action in the plan", targetResourceKey)
184183
}
185184

0 commit comments

Comments
 (0)