Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

json-output: Add change reasons to explain deletes #29649

Merged
merged 1 commit into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions internal/command/views/json/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ const (
ReasonRequested ChangeReason = "requested"
ReasonCannotUpdate ChangeReason = "cannot_update"
ReasonUnknown ChangeReason = "unknown"

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"
)

func changeReason(reason plans.ResourceInstanceChangeActionReason) ChangeReason {
Expand All @@ -85,6 +91,16 @@ func changeReason(reason plans.ResourceInstanceChangeActionReason) ChangeReason
return ReasonRequested
case plans.ResourceInstanceReplaceBecauseCannotUpdate:
return ReasonCannotUpdate
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
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
Expand Down
12 changes: 12 additions & 0 deletions internal/command/views/json/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ func startActionVerb(action plans.Action) string {
// This is not currently possible to reach, as we receive separate
// passes for create and delete
return "Replacing"
case plans.NoOp:
// This should never be possible: a no-op planned change should not
// be applied. We'll fall back to "Applying".
fallthrough
default:
return "Applying"
}
Expand All @@ -336,6 +340,10 @@ func progressActionVerb(action plans.Action) string {
// This is not currently possible to reach, as we receive separate
// passes for create and delete
return "replacing"
case plans.NoOp:
// This should never be possible: a no-op planned change should not
// be applied. We'll fall back to "applying".
fallthrough
default:
return "applying"
}
Expand All @@ -358,6 +366,10 @@ func actionNoun(action plans.Action) string {
// This is not currently possible to reach, as we receive separate
// passes for create and delete
return "Replacement"
case plans.NoOp:
// This should never be possible: a no-op planned change should not
// be applied. We'll fall back to "Apply".
fallthrough
default:
return "Apply"
}
Expand Down
7 changes: 6 additions & 1 deletion website/docs/internals/machine-readable-ui.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,15 @@ At the end of a plan or before an apply, Terraform will emit a `planned_change`
- `resource`: object describing the address of the resource to be changed; see [resource object](#resource-object) below for details
- `previous_resource`: object describing the previous address of the resource, if this change includes a configuration-driven move
- `action`: the action planned to be taken for the resource. Values: `noop`, `create`, `read`, `update`, `replace`, `delete`, `move`.
- `reason`: an optional reason for the change, currently only used when the action is `replace`. Values:
- `reason`: an optional reason for the change, currently only used when the action is `replace` or `delete`. Values:
- `tainted`: resource was marked as tainted
- `requested`: user requested that the resource be replaced, for example via the `-replace` plan flag
- `cannot_update`: changes to configuration force the resource to be deleted and created rather than updated
- `delete_because_no_resource_config`: no matching resource in configuration
- `delete_because_wrong_repetition`: resource instance key has no corresponding `count` or `for_each` in configuration
- `delete_because_count_index`: resource instance key is outside the range of the `count` argument
- `delete_because_each_key`: resource instance key is not included in the `for_each` argument
- `delete_because_no_module`: enclosing module instance is not in configuration

This message does not include details about the exact changes which caused the change to be planned. That information is available in [the JSON plan output](./json-format.html).

Expand Down