mirror of
https://github.com/opentffoundation/opentf.git
synced 2026-01-08 05:00:13 -05:00
backend/local: Render CBD replacement (+/-) correctly (#19642)
* backend/local: Render CBD replacement (+/-) correctly * command/format: Use IsReplace helper function
This commit is contained in:
@@ -29,7 +29,7 @@ type Plan struct {
|
||||
// for display, in conjunction with DisplayPlan.
|
||||
type InstanceDiff struct {
|
||||
Addr *terraform.ResourceAddress
|
||||
Action terraform.DiffChangeType
|
||||
Action plans.Action
|
||||
|
||||
// Attributes describes changes to the attributes of the instance.
|
||||
//
|
||||
@@ -47,7 +47,7 @@ type AttributeDiff struct {
|
||||
// intended for display purposes only.
|
||||
Path string
|
||||
|
||||
Action terraform.DiffChangeType
|
||||
Action plans.Action
|
||||
|
||||
OldValue string
|
||||
NewValue string
|
||||
@@ -73,7 +73,7 @@ func NewPlan(changes *plans.Changes) *Plan {
|
||||
|
||||
for _, rc := range changes.Resources {
|
||||
addr := rc.Addr
|
||||
log.Printf("[TRACE] NewPlan found %s", addr)
|
||||
log.Printf("[TRACE] NewPlan found %s (%s)", addr, rc.Action)
|
||||
dataSource := addr.Resource.Resource.Mode == addrs.DataResourceMode
|
||||
|
||||
// We create "delete" actions for data resources so we can clean
|
||||
@@ -87,32 +87,8 @@ func NewPlan(changes *plans.Changes) *Plan {
|
||||
// TODO: Update for the new plan types, ideally also switching over to
|
||||
// a structural diff renderer instead of a flat renderer.
|
||||
did := &InstanceDiff{
|
||||
Addr: terraform.NewLegacyResourceInstanceAddress(addr),
|
||||
}
|
||||
|
||||
switch rc.Action {
|
||||
case plans.NoOp:
|
||||
continue
|
||||
case plans.Create:
|
||||
if dataSource {
|
||||
// Use "refresh" as the action for display, but core
|
||||
// currently uses Create for this internally.
|
||||
// FIXME: Update core to generate plans.Read for this case
|
||||
// instead.
|
||||
did.Action = terraform.DiffRefresh
|
||||
} else {
|
||||
did.Action = terraform.DiffCreate
|
||||
}
|
||||
case plans.Read:
|
||||
did.Action = terraform.DiffRefresh
|
||||
case plans.Delete:
|
||||
did.Action = terraform.DiffDestroy
|
||||
case plans.DeleteThenCreate, plans.CreateThenDelete:
|
||||
did.Action = terraform.DiffDestroyCreate
|
||||
case plans.Update:
|
||||
did.Action = terraform.DiffUpdate
|
||||
default:
|
||||
panic(fmt.Sprintf("unexpected change action %s", rc.Action))
|
||||
Addr: terraform.NewLegacyResourceInstanceAddress(addr),
|
||||
Action: rc.Action,
|
||||
}
|
||||
|
||||
if rc.DeposedKey != states.NotDeposed {
|
||||
@@ -180,14 +156,14 @@ func (p *Plan) Stats() PlanStats {
|
||||
var ret PlanStats
|
||||
for _, r := range p.Resources {
|
||||
switch r.Action {
|
||||
case terraform.DiffCreate:
|
||||
case plans.Create:
|
||||
ret.ToAdd++
|
||||
case terraform.DiffUpdate:
|
||||
case plans.Update:
|
||||
ret.ToChange++
|
||||
case terraform.DiffDestroyCreate:
|
||||
case plans.DeleteThenCreate, plans.CreateThenDelete:
|
||||
ret.ToAdd++
|
||||
ret.ToDestroy++
|
||||
case terraform.DiffDestroy:
|
||||
case plans.Delete:
|
||||
ret.ToDestroy++
|
||||
}
|
||||
}
|
||||
@@ -195,8 +171,8 @@ func (p *Plan) Stats() PlanStats {
|
||||
}
|
||||
|
||||
// ActionCounts returns the number of diffs for each action type
|
||||
func (p *Plan) ActionCounts() map[terraform.DiffChangeType]int {
|
||||
ret := map[terraform.DiffChangeType]int{}
|
||||
func (p *Plan) ActionCounts() map[plans.Action]int {
|
||||
ret := map[plans.Action]int{}
|
||||
for _, r := range p.Resources {
|
||||
ret[r.Action]++
|
||||
}
|
||||
@@ -212,18 +188,22 @@ func (p *Plan) Empty() bool {
|
||||
// colorstring.Colorize, will produce a result that can be written
|
||||
// to a terminal to produce a symbol made of three printable
|
||||
// characters, possibly interspersed with VT100 color codes.
|
||||
func DiffActionSymbol(action terraform.DiffChangeType) string {
|
||||
func DiffActionSymbol(action plans.Action) string {
|
||||
switch action {
|
||||
case terraform.DiffDestroyCreate:
|
||||
case plans.DeleteThenCreate:
|
||||
return "[red]-[reset]/[green]+[reset]"
|
||||
case terraform.DiffCreate:
|
||||
case plans.CreateThenDelete:
|
||||
return "[green]+[reset]/[red]-[reset]"
|
||||
case plans.Create:
|
||||
return " [green]+[reset]"
|
||||
case terraform.DiffDestroy:
|
||||
case plans.Delete:
|
||||
return " [red]-[reset]"
|
||||
case terraform.DiffRefresh:
|
||||
case plans.Read:
|
||||
return " [cyan]<=[reset]"
|
||||
default:
|
||||
case plans.Update:
|
||||
return " [yellow]~[reset]"
|
||||
default:
|
||||
return " ?"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,14 +219,14 @@ func formatPlanInstanceDiff(buf *bytes.Buffer, r *InstanceDiff, keyLen int, colo
|
||||
symbol := DiffActionSymbol(r.Action)
|
||||
oldValues := true
|
||||
switch r.Action {
|
||||
case terraform.DiffDestroyCreate:
|
||||
case plans.DeleteThenCreate, plans.CreateThenDelete:
|
||||
color = "yellow"
|
||||
case terraform.DiffCreate:
|
||||
case plans.Create:
|
||||
color = "green"
|
||||
oldValues = false
|
||||
case terraform.DiffDestroy:
|
||||
case plans.Delete:
|
||||
color = "red"
|
||||
case terraform.DiffRefresh:
|
||||
case plans.Read:
|
||||
color = "cyan"
|
||||
oldValues = false
|
||||
}
|
||||
@@ -258,7 +238,7 @@ func formatPlanInstanceDiff(buf *bytes.Buffer, r *InstanceDiff, keyLen int, colo
|
||||
if r.Deposed {
|
||||
extraStr = extraStr + " (deposed)"
|
||||
}
|
||||
if r.Action == terraform.DiffDestroyCreate {
|
||||
if r.Action.IsReplace() {
|
||||
extraStr = extraStr + colorizer.Color(" [red][bold](new resource required)")
|
||||
}
|
||||
|
||||
@@ -284,7 +264,7 @@ func formatPlanInstanceDiff(buf *bytes.Buffer, r *InstanceDiff, keyLen int, colo
|
||||
|
||||
updateMsg := ""
|
||||
switch {
|
||||
case attr.ForcesNew && r.Action == terraform.DiffDestroyCreate:
|
||||
case attr.ForcesNew && r.Action.IsReplace():
|
||||
updateMsg = colorizer.Color(" [red](forces new resource)")
|
||||
case attr.Sensitive && oldValues:
|
||||
updateMsg = colorizer.Color(" [yellow](attribute changed)")
|
||||
|
||||
Reference in New Issue
Block a user