Skip to content

Commit fd14d40

Browse files
authored
internal: Convert deployplan.ActionType to int-based enum (#3585)
## Changes - The order defines severity of action which will be used to decide final action for resource. - Add a new 'resize' action type, will be useful when we add clusters. ## Why We will be able to decide on final action for the given resource as max(actions...), where actions come from local and remote comparison of individual fields. ## Tests Existing tests.
1 parent 03c4fc5 commit fd14d40

File tree

4 files changed

+32
-34
lines changed

4 files changed

+32
-34
lines changed

bundle/deployplan/action.go

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,38 @@ type Action struct {
1313

1414
func (a Action) String() string {
1515
typ, _ := strings.CutSuffix(a.Group, "s")
16-
return fmt.Sprintf(" %s %s %s", a.ActionType, typ, a.Key)
16+
return fmt.Sprintf(" %s %s %s", a.ActionType.String(), typ, a.Key)
1717
}
1818

1919
// Implements cmdio.Event for cmdio.Log
2020
func (a Action) IsInplaceSupported() bool {
2121
return false
2222
}
2323

24-
// These enum values are superset to action types defined in the tfjson library.
25-
// "recreate" maps to the tfjson.Actions.Replace() function.
26-
// "update" and "update_with_id" maps to tfjson.Actions.Update() and so on. source:
27-
// https://github.com/hashicorp/terraform-json/blob/0104004301ca8e7046d089cdc2e2db2179d225be/action.go#L14
28-
type ActionType string
24+
type ActionType int
2925

26+
// Actions are ordered in increasing severity.
27+
// If case of several options, action with highest severity wins.
28+
// Note, Create/Delete are handled explicitly and never compared.
3029
const (
31-
ActionTypeUnset ActionType = ""
32-
ActionTypeNoop ActionType = "noop"
33-
ActionTypeCreate ActionType = "create"
34-
ActionTypeDelete ActionType = "delete"
35-
ActionTypeUpdate ActionType = "update"
36-
ActionTypeUpdateWithID ActionType = "update_with_id"
37-
ActionTypeRecreate ActionType = "recreate"
30+
ActionTypeUnset ActionType = iota
31+
ActionTypeNoop
32+
ActionTypeResize
33+
ActionTypeUpdate
34+
ActionTypeUpdateWithID
35+
ActionTypeCreate
36+
ActionTypeRecreate
37+
ActionTypeDelete
3838
)
3939

40-
var ShortName = map[ActionType]ActionType{
41-
ActionTypeUpdateWithID: ActionTypeUpdate,
40+
var ShortName = map[ActionType]string{
41+
ActionTypeNoop: "noop",
42+
ActionTypeResize: "resize",
43+
ActionTypeUpdate: "update",
44+
ActionTypeUpdateWithID: "update",
45+
ActionTypeCreate: "create",
46+
ActionTypeRecreate: "recreate",
47+
ActionTypeDelete: "delete",
4248
}
4349

4450
func (a ActionType) IsNoop() bool {
@@ -47,23 +53,15 @@ func (a ActionType) IsNoop() bool {
4753

4854
func (a ActionType) KeepsID() bool {
4955
switch a {
50-
case ActionTypeCreate:
51-
return false
52-
case ActionTypeUpdateWithID:
53-
return false
54-
case ActionTypeRecreate:
56+
case ActionTypeCreate, ActionTypeUpdateWithID, ActionTypeRecreate:
5557
return false
5658
default:
5759
return true
5860
}
5961
}
6062

6163
func (a ActionType) String() string {
62-
shortAction := ShortName[a]
63-
if shortAction != "" {
64-
return string(shortAction)
65-
}
66-
return string(a)
64+
return ShortName[a]
6765
}
6866

6967
// Filter returns actions that match the specified action type

bundle/terranova/plan.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ func (d *DeploymentUnit) plan(ctx context.Context, client *databricks.WorkspaceC
3232
return deployplan.ActionTypeCreate, nil
3333
}
3434
if entry.ID == "" {
35-
return "", errors.New("invalid state: empty id")
35+
return deployplan.ActionTypeUnset, errors.New("invalid state: empty id")
3636
}
3737

3838
newState, err := d.Adapter.PrepareState(inputConfig)
3939
if err != nil {
40-
return "", fmt.Errorf("reading config: %w", err)
40+
return deployplan.ActionTypeUnset, fmt.Errorf("reading config: %w", err)
4141
}
4242

4343
savedState, err := typeConvert(d.Adapter.StateType(), entry.State)
4444
if err != nil {
45-
return "", fmt.Errorf("interpreting state: %w", err)
45+
return deployplan.ActionTypeUnset, fmt.Errorf("interpreting state: %w", err)
4646
}
4747

4848
// Note, currently we're diffing static structs, not dynamic value.
@@ -200,7 +200,7 @@ func (d *DeploymentUnit) ReadRemoteStateField(ctx context.Context, db *tnstate.T
200200
func calcDiff(adapter *tnresources.Adapter, savedState, config any) (deployplan.ActionType, error) {
201201
localDiff, err := structdiff.GetStructDiff(savedState, config)
202202
if err != nil {
203-
return "", err
203+
return deployplan.ActionTypeUnset, err
204204
}
205205

206206
if len(localDiff) == 0 {
@@ -214,11 +214,11 @@ func calcDiff(adapter *tnresources.Adapter, savedState, config any) (deployplan.
214214
if adapter.HasClassifyChanges() {
215215
result, err := adapter.ClassifyChanges(localDiff)
216216
if err != nil {
217-
return "", err
217+
return deployplan.ActionTypeUnset, err
218218
}
219219

220220
if result == deployplan.ActionTypeUpdateWithID && !adapter.HasDoUpdateWithID() {
221-
return "", errors.New("internal error: unexpected plan='update_with_id'")
221+
return deployplan.ActionTypeUnset, errors.New("internal error: unexpected plan='update_with_id'")
222222
}
223223

224224
return result, nil

bundle/terranova/tnresources/adapter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,11 @@ func (a *Adapter) MustRecreate(changes []structdiff.Change) bool {
428428
// ClassifyChanges calls the resource's ClassifyChanges method if implemented.
429429
func (a *Adapter) ClassifyChanges(changes []structdiff.Change) (deployplan.ActionType, error) {
430430
if a.classifyChanges == nil {
431-
return "", errors.New("internal error: ClassifyChanges not implemented")
431+
return deployplan.ActionTypeUnset, errors.New("internal error: ClassifyChanges not implemented")
432432
}
433433
outs, err := a.classifyChanges.Call(changes)
434434
if err != nil {
435-
return "", err
435+
return deployplan.ActionTypeUnset, err
436436
}
437437
result := outs[0].(deployplan.ActionType)
438438
return result, nil

cmd/bundle/plan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func newPlanCommand() *cobra.Command {
7575
changes := phases.Diff(ctx, b)
7676

7777
for _, change := range changes {
78-
cmdio.LogString(ctx, fmt.Sprintf("%s %s.%s", change.ActionType, change.Group, change.Key))
78+
cmdio.LogString(ctx, fmt.Sprintf("%s %s.%s", change.ActionType.String(), change.Group, change.Key))
7979
}
8080

8181
if logdiag.HasError(ctx) {

0 commit comments

Comments
 (0)