github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/command/views/json/change.go (about) 1 package json 2 3 import ( 4 "fmt" 5 6 "github.com/iaas-resource-provision/iaas-rpc/internal/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 16 return c 17 } 18 19 type ResourceInstanceChange struct { 20 Resource ResourceAddr `json:"resource"` 21 Action ChangeAction `json:"action"` 22 Reason ChangeReason `json:"reason,omitempty"` 23 } 24 25 func (c *ResourceInstanceChange) String() string { 26 return fmt.Sprintf("%s: Plan to %s", c.Resource.Addr, c.Action) 27 } 28 29 type ChangeAction string 30 31 const ( 32 ActionNoOp ChangeAction = "noop" 33 ActionCreate ChangeAction = "create" 34 ActionRead ChangeAction = "read" 35 ActionUpdate ChangeAction = "update" 36 ActionReplace ChangeAction = "replace" 37 ActionDelete ChangeAction = "delete" 38 ) 39 40 func changeAction(action plans.Action) ChangeAction { 41 switch action { 42 case plans.NoOp: 43 return ActionNoOp 44 case plans.Create: 45 return ActionCreate 46 case plans.Read: 47 return ActionRead 48 case plans.Update: 49 return ActionUpdate 50 case plans.DeleteThenCreate, plans.CreateThenDelete: 51 return ActionReplace 52 case plans.Delete: 53 return ActionDelete 54 default: 55 return ActionNoOp 56 } 57 } 58 59 type ChangeReason string 60 61 const ( 62 ReasonNone ChangeReason = "" 63 ReasonTainted ChangeReason = "tainted" 64 ReasonRequested ChangeReason = "requested" 65 ReasonCannotUpdate ChangeReason = "cannot_update" 66 ReasonUnknown ChangeReason = "unknown" 67 ) 68 69 func changeReason(reason plans.ResourceInstanceChangeActionReason) ChangeReason { 70 switch reason { 71 case plans.ResourceInstanceChangeNoReason: 72 return ReasonNone 73 case plans.ResourceInstanceReplaceBecauseTainted: 74 return ReasonTainted 75 case plans.ResourceInstanceReplaceByRequest: 76 return ReasonRequested 77 case plans.ResourceInstanceReplaceBecauseCannotUpdate: 78 return ReasonCannotUpdate 79 default: 80 // This should never happen, but there's no good way to guarantee 81 // exhaustive handling of the enum, so a generic fall back is better 82 // than a misleading result or a panic 83 return ReasonUnknown 84 } 85 }