From f79a768a4eb09b33355652fcb8f006e7575d16ee Mon Sep 17 00:00:00 2001 From: James Bardin Date: Sat, 27 Apr 2019 11:23:37 -0400 Subject: [PATCH] command/format: take noop changes from lcs When rendering the diff, the NoOp changes should come from the LCS sequence, rather than the new sequence. The two indexes will not align in many cases, adding the wrong new object or indexing out of bounds. --- command/format/diff.go | 4 ++-- command/format/diff_test.go | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/command/format/diff.go b/command/format/diff.go index 9d6a9f1de..c726f0ede 100644 --- a/command/format/diff.go +++ b/command/format/diff.go @@ -1101,8 +1101,8 @@ func ctySequenceDiff(old, new []cty.Value) []*plans.Change { if lcsI < len(lcs) { ret = append(ret, &plans.Change{ Action: plans.NoOp, - Before: new[newI], - After: new[newI], + Before: lcs[lcsI], + After: lcs[lcsI], }) // All of our indexes advance together now, since the line diff --git a/command/format/diff_test.go b/command/format/diff_test.go index b50b70c71..d93e26c2c 100644 --- a/command/format/diff_test.go +++ b/command/format/diff_test.go @@ -3006,6 +3006,49 @@ func TestResourceChange_nestedMap(t *testing.T) { - volume_type = "gp2" -> null } } +`, + }, + "in-place sequence update - deletion": { + Action: plans.Update, + Mode: addrs.ManagedResourceMode, + Before: cty.ObjectVal(map[string]cty.Value{ + "list": cty.ListVal([]cty.Value{ + cty.ObjectVal(map[string]cty.Value{"attr": cty.StringVal("x")}), + cty.ObjectVal(map[string]cty.Value{"attr": cty.StringVal("y")}), + }), + }), + After: cty.ObjectVal(map[string]cty.Value{ + "list": cty.ListVal([]cty.Value{ + cty.ObjectVal(map[string]cty.Value{"attr": cty.StringVal("y")}), + cty.ObjectVal(map[string]cty.Value{"attr": cty.StringVal("z")}), + }), + }), + RequiredReplace: cty.NewPathSet(), + Tainted: false, + Schema: &configschema.Block{ + BlockTypes: map[string]*configschema.NestedBlock{ + "list": { + Block: configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "attr": { + Type: cty.String, + Required: true, + }, + }, + }, + Nesting: configschema.NestingList, + }, + }, + }, + ExpectedOutput: ` # test_instance.example will be updated in-place + ~ resource "test_instance" "example" { + ~ list { + ~ attr = "x" -> "y" + } + ~ list { + ~ attr = "y" -> "z" + } + } `, }, }