kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/command/views/json/change.go (about) 1 package json 2 3 import ( 4 "fmt" 5 6 "kubeform.dev/terraform-backend-sdk/plans" 7 ) 8 9 func NewResourceInstanceChange(change *plans.ResourceInstanceChangeSrc) *ResourceInstanceChange { 10 c := &ResourceInstanceChange{ 11 Resource: newResourceAddr(change.Addr), 12 Action: changeAction(change.Action), 13 Reason: changeReason(change.ActionReason), 14 } 15 if !change.Addr.Equal(change.PrevRunAddr) { 16 if c.Action == ActionNoOp { 17 c.Action = ActionMove 18 } 19 pr := newResourceAddr(change.PrevRunAddr) 20 c.PreviousResource = &pr 21 } 22 23 return c 24 } 25 26 type ResourceInstanceChange struct { 27 Resource ResourceAddr `json:"resource"` 28 PreviousResource *ResourceAddr `json:"previous_resource,omitempty"` 29 Action ChangeAction `json:"action"` 30 Reason ChangeReason `json:"reason,omitempty"` 31 } 32 33 func (c *ResourceInstanceChange) String() string { 34 return fmt.Sprintf("%s: Plan to %s", c.Resource.Addr, c.Action) 35 } 36 37 type ChangeAction string 38 39 const ( 40 ActionNoOp ChangeAction = "noop" 41 ActionMove ChangeAction = "move" 42 ActionCreate ChangeAction = "create" 43 ActionRead ChangeAction = "read" 44 ActionUpdate ChangeAction = "update" 45 ActionReplace ChangeAction = "replace" 46 ActionDelete ChangeAction = "delete" 47 ) 48 49 func changeAction(action plans.Action) ChangeAction { 50 switch action { 51 case plans.NoOp: 52 return ActionNoOp 53 case plans.Create: 54 return ActionCreate 55 case plans.Read: 56 return ActionRead 57 case plans.Update: 58 return ActionUpdate 59 case plans.DeleteThenCreate, plans.CreateThenDelete: 60 return ActionReplace 61 case plans.Delete: 62 return ActionDelete 63 default: 64 return ActionNoOp 65 } 66 } 67 68 type ChangeReason string 69 70 const ( 71 ReasonNone ChangeReason = "" 72 ReasonTainted ChangeReason = "tainted" 73 ReasonRequested ChangeReason = "requested" 74 ReasonCannotUpdate ChangeReason = "cannot_update" 75 ReasonUnknown ChangeReason = "unknown" 76 ) 77 78 func changeReason(reason plans.ResourceInstanceChangeActionReason) ChangeReason { 79 switch reason { 80 case plans.ResourceInstanceChangeNoReason: 81 return ReasonNone 82 case plans.ResourceInstanceReplaceBecauseTainted: 83 return ReasonTainted 84 case plans.ResourceInstanceReplaceByRequest: 85 return ReasonRequested 86 case plans.ResourceInstanceReplaceBecauseCannotUpdate: 87 return ReasonCannotUpdate 88 default: 89 // This should never happen, but there's no good way to guarantee 90 // exhaustive handling of the enum, so a generic fall back is better 91 // than a misleading result or a panic 92 return ReasonUnknown 93 } 94 }