github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/views/json/change.go (about)

     1  package json
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/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  	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  	ReasonReplaceTriggeredBy ChangeReason = "replace_triggered_by"
    75  	ReasonCannotUpdate       ChangeReason = "cannot_update"
    76  	ReasonUnknown            ChangeReason = "unknown"
    77  
    78  	ReasonDeleteBecauseNoResourceConfig ChangeReason = "delete_because_no_resource_config"
    79  	ReasonDeleteBecauseWrongRepetition  ChangeReason = "delete_because_wrong_repetition"
    80  	ReasonDeleteBecauseCountIndex       ChangeReason = "delete_because_count_index"
    81  	ReasonDeleteBecauseEachKey          ChangeReason = "delete_because_each_key"
    82  	ReasonDeleteBecauseNoModule         ChangeReason = "delete_because_no_module"
    83  	ReasonDeleteBecauseNoMoveTarget     ChangeReason = "delete_because_no_move_target"
    84  	ReasonReadBecauseConfigUnknown      ChangeReason = "read_because_config_unknown"
    85  	ReasonReadBecauseDependencyPending  ChangeReason = "read_because_dependency_pending"
    86  )
    87  
    88  func changeReason(reason plans.ResourceInstanceChangeActionReason) ChangeReason {
    89  	switch reason {
    90  	case plans.ResourceInstanceChangeNoReason:
    91  		return ReasonNone
    92  	case plans.ResourceInstanceReplaceBecauseTainted:
    93  		return ReasonTainted
    94  	case plans.ResourceInstanceReplaceByRequest:
    95  		return ReasonRequested
    96  	case plans.ResourceInstanceReplaceBecauseCannotUpdate:
    97  		return ReasonCannotUpdate
    98  	case plans.ResourceInstanceReplaceByTriggers:
    99  		return ReasonReplaceTriggeredBy
   100  	case plans.ResourceInstanceDeleteBecauseNoResourceConfig:
   101  		return ReasonDeleteBecauseNoResourceConfig
   102  	case plans.ResourceInstanceDeleteBecauseWrongRepetition:
   103  		return ReasonDeleteBecauseWrongRepetition
   104  	case plans.ResourceInstanceDeleteBecauseCountIndex:
   105  		return ReasonDeleteBecauseCountIndex
   106  	case plans.ResourceInstanceDeleteBecauseEachKey:
   107  		return ReasonDeleteBecauseEachKey
   108  	case plans.ResourceInstanceDeleteBecauseNoModule:
   109  		return ReasonDeleteBecauseNoModule
   110  	case plans.ResourceInstanceReadBecauseConfigUnknown:
   111  		return ReasonReadBecauseConfigUnknown
   112  	case plans.ResourceInstanceDeleteBecauseNoMoveTarget:
   113  		return ReasonDeleteBecauseNoMoveTarget
   114  	case plans.ResourceInstanceReadBecauseDependencyPending:
   115  		return ReasonReadBecauseDependencyPending
   116  	default:
   117  		// This should never happen, but there's no good way to guarantee
   118  		// exhaustive handling of the enum, so a generic fall back is better
   119  		// than a misleading result or a panic
   120  		return ReasonUnknown
   121  	}
   122  }