2021-02-23 16:16:09 +01:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-05-17 21:33:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
2021-02-23 16:16:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewResourceInstanceChange(change *plans.ResourceInstanceChangeSrc) *ResourceInstanceChange {
|
|
|
|
c := &ResourceInstanceChange{
|
|
|
|
Resource: newResourceAddr(change.Addr),
|
|
|
|
Action: changeAction(change.Action),
|
2021-05-03 12:49:42 +02:00
|
|
|
Reason: changeReason(change.ActionReason),
|
2021-02-23 16:16:09 +01:00
|
|
|
}
|
2021-09-17 20:09:20 +02:00
|
|
|
if !change.Addr.Equal(change.PrevRunAddr) {
|
|
|
|
if c.Action == ActionNoOp {
|
|
|
|
c.Action = ActionMove
|
|
|
|
}
|
|
|
|
pr := newResourceAddr(change.PrevRunAddr)
|
|
|
|
c.PreviousResource = &pr
|
|
|
|
}
|
2021-02-23 16:16:09 +01:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceInstanceChange struct {
|
2021-09-17 20:09:20 +02:00
|
|
|
Resource ResourceAddr `json:"resource"`
|
|
|
|
PreviousResource *ResourceAddr `json:"previous_resource,omitempty"`
|
|
|
|
Action ChangeAction `json:"action"`
|
|
|
|
Reason ChangeReason `json:"reason,omitempty"`
|
2021-02-23 16:16:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ResourceInstanceChange) String() string {
|
|
|
|
return fmt.Sprintf("%s: Plan to %s", c.Resource.Addr, c.Action)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChangeAction string
|
|
|
|
|
|
|
|
const (
|
|
|
|
ActionNoOp ChangeAction = "noop"
|
2021-09-17 20:09:20 +02:00
|
|
|
ActionMove ChangeAction = "move"
|
2021-02-23 16:16:09 +01:00
|
|
|
ActionCreate ChangeAction = "create"
|
|
|
|
ActionRead ChangeAction = "read"
|
|
|
|
ActionUpdate ChangeAction = "update"
|
|
|
|
ActionReplace ChangeAction = "replace"
|
|
|
|
ActionDelete ChangeAction = "delete"
|
|
|
|
)
|
|
|
|
|
|
|
|
func changeAction(action plans.Action) ChangeAction {
|
|
|
|
switch action {
|
|
|
|
case plans.NoOp:
|
|
|
|
return ActionNoOp
|
|
|
|
case plans.Create:
|
|
|
|
return ActionCreate
|
|
|
|
case plans.Read:
|
|
|
|
return ActionRead
|
|
|
|
case plans.Update:
|
|
|
|
return ActionUpdate
|
|
|
|
case plans.DeleteThenCreate, plans.CreateThenDelete:
|
|
|
|
return ActionReplace
|
|
|
|
case plans.Delete:
|
|
|
|
return ActionDelete
|
|
|
|
default:
|
|
|
|
return ActionNoOp
|
|
|
|
}
|
|
|
|
}
|
2021-05-03 12:49:42 +02:00
|
|
|
|
|
|
|
type ChangeReason string
|
|
|
|
|
|
|
|
const (
|
|
|
|
ReasonNone ChangeReason = ""
|
|
|
|
ReasonTainted ChangeReason = "tainted"
|
|
|
|
ReasonRequested ChangeReason = "requested"
|
|
|
|
ReasonCannotUpdate ChangeReason = "cannot_update"
|
|
|
|
ReasonUnknown ChangeReason = "unknown"
|
2021-09-24 17:18:39 +02:00
|
|
|
|
|
|
|
ReasonDeleteBecauseNoResourceConfig ChangeReason = "delete_because_no_resource_config"
|
|
|
|
ReasonDeleteBecauseWrongRepetition ChangeReason = "delete_because_wrong_repetition"
|
|
|
|
ReasonDeleteBecauseCountIndex ChangeReason = "delete_because_count_index"
|
|
|
|
ReasonDeleteBecauseEachKey ChangeReason = "delete_because_each_key"
|
|
|
|
ReasonDeleteBecauseNoModule ChangeReason = "delete_because_no_module"
|
2021-05-03 12:49:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func changeReason(reason plans.ResourceInstanceChangeActionReason) ChangeReason {
|
|
|
|
switch reason {
|
|
|
|
case plans.ResourceInstanceChangeNoReason:
|
|
|
|
return ReasonNone
|
|
|
|
case plans.ResourceInstanceReplaceBecauseTainted:
|
|
|
|
return ReasonTainted
|
|
|
|
case plans.ResourceInstanceReplaceByRequest:
|
|
|
|
return ReasonRequested
|
|
|
|
case plans.ResourceInstanceReplaceBecauseCannotUpdate:
|
|
|
|
return ReasonCannotUpdate
|
2021-09-24 17:18:39 +02:00
|
|
|
case plans.ResourceInstanceDeleteBecauseNoResourceConfig:
|
|
|
|
return ReasonDeleteBecauseNoResourceConfig
|
|
|
|
case plans.ResourceInstanceDeleteBecauseWrongRepetition:
|
|
|
|
return ReasonDeleteBecauseWrongRepetition
|
|
|
|
case plans.ResourceInstanceDeleteBecauseCountIndex:
|
|
|
|
return ReasonDeleteBecauseCountIndex
|
|
|
|
case plans.ResourceInstanceDeleteBecauseEachKey:
|
|
|
|
return ReasonDeleteBecauseEachKey
|
|
|
|
case plans.ResourceInstanceDeleteBecauseNoModule:
|
|
|
|
return ReasonDeleteBecauseNoModule
|
2021-05-03 12:49:42 +02:00
|
|
|
default:
|
|
|
|
// This should never happen, but there's no good way to guarantee
|
|
|
|
// exhaustive handling of the enum, so a generic fall back is better
|
|
|
|
// than a misleading result or a panic
|
|
|
|
return ReasonUnknown
|
|
|
|
}
|
|
|
|
}
|