From fe117e9f02d25ba37e170829ed80e9bc44acd58b Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Tue, 11 Dec 2018 12:56:11 +0000 Subject: [PATCH] command/format: Fix rendering of nested blocks during update --- command/format/diff.go | 7 ++++- command/format/diff_test.go | 53 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/command/format/diff.go b/command/format/diff.go index c58661663b..37127e2d02 100644 --- a/command/format/diff.go +++ b/command/format/diff.go @@ -313,11 +313,16 @@ func (p *blockBodyDiffPrinter) writeNestedBlockDiffs(name string, blockS *config if blankBefore && (len(oldItems) > 0 || len(newItems) > 0) { p.buf.WriteRune('\n') } + for i := 0; i < commonLen; i++ { path := append(path, cty.IndexStep{Key: cty.NumberIntVal(int64(i))}) oldItem := oldItems[i] newItem := newItems[i] - p.writeNestedBlockDiff(name, nil, &blockS.Block, plans.Update, oldItem, newItem, indent, path) + action := plans.Update + if oldItem.RawEquals(newItem) { + action = plans.NoOp + } + p.writeNestedBlockDiff(name, nil, &blockS.Block, action, oldItem, newItem, indent, path) } for i := commonLen; i < len(oldItems); i++ { path := append(path, cty.IndexStep{Key: cty.NumberIntVal(int64(i))}) diff --git a/command/format/diff_test.go b/command/format/diff_test.go index 1d7740d654..2dc002a8ca 100644 --- a/command/format/diff_test.go +++ b/command/format/diff_test.go @@ -655,6 +655,59 @@ func TestResourceChange_map(t *testing.T) { func TestResourceChange_nestedList(t *testing.T) { testCases := map[string]testCase{ + "in-place update - equal": { + Action: plans.Update, + Mode: addrs.ManagedResourceMode, + Before: cty.ObjectVal(map[string]cty.Value{ + "id": cty.StringVal("i-02ae66f368e8518a9"), + "ami": cty.StringVal("ami-BEFORE"), + "root_block_device": cty.ListVal([]cty.Value{ + cty.ObjectVal(map[string]cty.Value{ + "volume_type": cty.StringVal("gp2"), + }), + }), + }), + After: cty.ObjectVal(map[string]cty.Value{ + "id": cty.StringVal("i-02ae66f368e8518a9"), + "ami": cty.StringVal("ami-AFTER"), + "root_block_device": cty.ListVal([]cty.Value{ + cty.ObjectVal(map[string]cty.Value{ + "volume_type": cty.StringVal("gp2"), + }), + }), + }), + RequiredReplace: cty.NewPathSet(), + Schema: &configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Optional: true, Computed: true}, + "ami": {Type: cty.String, Optional: true}, + }, + BlockTypes: map[string]*configschema.NestedBlock{ + "root_block_device": { + Block: configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "volume_type": { + Type: cty.String, + Optional: true, + Computed: true, + }, + }, + }, + Nesting: 2, + }, + }, + }, + ExpectedOutput: ` # test_instance.example will be updated in-place + ~ resource "test_instance" "example" { + ~ ami = "ami-BEFORE" -> "ami-AFTER" + id = "i-02ae66f368e8518a9" + + root_block_device { + volume_type = "gp2" + } + } +`, + }, "in-place update - creation": { Action: plans.Update, Mode: addrs.ManagedResourceMode,