From 0f1f504c22bd9ac1deb92bf20d35bbadf9aba2ae Mon Sep 17 00:00:00 2001 From: Frederic Date: Fri, 29 Mar 2019 22:52:13 +0100 Subject: [PATCH] command/format: indicate in diff when adding an attribute forces replacement --- command/format/diff.go | 3 ++ command/format/diff_test.go | 56 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/command/format/diff.go b/command/format/diff.go index b9d652722a..cf8aea032b 100644 --- a/command/format/diff.go +++ b/command/format/diff.go @@ -253,6 +253,9 @@ func (p *blockBodyDiffPrinter) writeAttrDiff(name string, attrS *configschema.At switch { case showJustNew: p.writeValue(new, action, indent+2) + if p.pathForcesNewResource(path) { + p.buf.WriteString(p.color.Color(forcesNewResourceCaption)) + } default: // We show new even if it is null to emphasize the fact // that it is being unset, since otherwise it is easy to diff --git a/command/format/diff_test.go b/command/format/diff_test.go index b266e6b0a8..b50b70c711 100644 --- a/command/format/diff_test.go +++ b/command/format/diff_test.go @@ -327,6 +327,62 @@ new line ~ ami = "ami-BEFORE" -> "ami-AFTER" # forces replacement ~ id = "i-02ae66f368e8518a9" -> (known after apply) } +`, + }, + "force replacement with empty before value": { + Action: plans.DeleteThenCreate, + Mode: addrs.ManagedResourceMode, + Before: cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("name"), + "forced": cty.NullVal(cty.String), + }), + After: cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("name"), + "forced": cty.StringVal("example"), + }), + Schema: &configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "name": {Type: cty.String, Optional: true}, + "forced": {Type: cty.String, Optional: true}, + }, + }, + RequiredReplace: cty.NewPathSet(cty.Path{ + cty.GetAttrStep{Name: "forced"}, + }), + Tainted: false, + ExpectedOutput: ` # test_instance.example must be replaced +-/+ resource "test_instance" "example" { + + forced = "example" # forces replacement + name = "name" + } +`, + }, + "force replacement with empty before value legacy": { + Action: plans.DeleteThenCreate, + Mode: addrs.ManagedResourceMode, + Before: cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("name"), + "forced": cty.StringVal(""), + }), + After: cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("name"), + "forced": cty.StringVal("example"), + }), + Schema: &configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "name": {Type: cty.String, Optional: true}, + "forced": {Type: cty.String, Optional: true}, + }, + }, + RequiredReplace: cty.NewPathSet(cty.Path{ + cty.GetAttrStep{Name: "forced"}, + }), + Tainted: false, + ExpectedOutput: ` # test_instance.example must be replaced +-/+ resource "test_instance" "example" { + + forced = "example" # forces replacement + name = "name" + } `, }, }