diff --git a/builtin/providers/test/resource_test.go b/builtin/providers/test/resource_test.go index 12bf568fa..05fd5c0c5 100644 --- a/builtin/providers/test/resource_test.go +++ b/builtin/providers/test/resource_test.go @@ -24,9 +24,95 @@ resource "test_resource" "foo" { } } `), - Check: func(s *terraform.State) error { - return nil - }, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckNoResourceAttr( + "test_resource.foo", "list.#", + ), + ), + }, + }, + }) +} + +func TestResource_changedList(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + Providers: testAccProviders, + CheckDestroy: testAccCheckResourceDestroy, + Steps: []resource.TestStep{ + { + Config: strings.TrimSpace(` +resource "test_resource" "foo" { + required = "yep" + required_map = { + key = "value" + } +} + `), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckNoResourceAttr( + "test_resource.foo", "list.#", + ), + ), + }, + { + Config: strings.TrimSpace(` +resource "test_resource" "foo" { + required = "yep" + required_map = { + key = "value" + } + list = ["a"] +} + `), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "test_resource.foo", "list.#", "1", + ), + resource.TestCheckResourceAttr( + "test_resource.foo", "list.0", "a", + ), + ), + }, + { + Config: strings.TrimSpace(` +resource "test_resource" "foo" { + required = "yep" + required_map = { + key = "value" + } + list = ["a", "b"] +} + `), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "test_resource.foo", "list.#", "2", + ), + resource.TestCheckResourceAttr( + "test_resource.foo", "list.0", "a", + ), + resource.TestCheckResourceAttr( + "test_resource.foo", "list.1", "b", + ), + ), + }, + { + Config: strings.TrimSpace(` +resource "test_resource" "foo" { + required = "yep" + required_map = { + key = "value" + } + list = ["b"] +} + `), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "test_resource.foo", "list.#", "1", + ), + resource.TestCheckResourceAttr( + "test_resource.foo", "list.0", "b", + ), + ), }, }, }) @@ -165,9 +251,6 @@ resource "test_resource" "foo" { } } `), - Check: func(s *terraform.State) error { - return nil - }, }, resource.TestStep{ Config: strings.TrimSpace(` diff --git a/terraform/diff.go b/terraform/diff.go index bd886ebea..7751794ac 100644 --- a/terraform/diff.go +++ b/terraform/diff.go @@ -454,10 +454,10 @@ func (d *InstanceDiff) Apply(attrs map[string]string, schema *configschema.Block return result, nil } - return d.blockDiff(nil, attrs, schema) + return d.applyBlockDiff(nil, attrs, schema) } -func (d *InstanceDiff) blockDiff(path []string, attrs map[string]string, schema *configschema.Block) (map[string]string, error) { +func (d *InstanceDiff) applyBlockDiff(path []string, attrs map[string]string, schema *configschema.Block) (map[string]string, error) { result := map[string]string{} name := "" @@ -564,7 +564,7 @@ func (d *InstanceDiff) blockDiff(path []string, attrs map[string]string, schema } for k := range candidateKeys { - newAttrs, err := d.blockDiff(append(path, n, k), attrs, &block.Block) + newAttrs, err := d.applyBlockDiff(append(path, n, k), attrs, &block.Block) if err != nil { return result, err } @@ -735,24 +735,37 @@ func (d *InstanceDiff) applyCollectionDiff(path []string, attrs map[string]strin } // collect all the keys from the diff and the old state + noDiff := true keys := map[string]bool{} for k := range d.Attributes { + if !strings.HasPrefix(k, currentKey+".") { + continue + } + noDiff = false keys[k] = true } + + noAttrs := true for k := range attrs { + if !strings.HasPrefix(k, currentKey+".") { + continue + } + noAttrs = false keys[k] = true } + // If there's no diff and no attrs, then there's no value at all. + // This prevents an unexpected zero-count attribute in the attributes. + if noDiff && noAttrs { + return result, nil + } + idx := "#" if attrSchema.Type.IsMapType() { idx = "%" } for k := range keys { - if !strings.HasPrefix(k, currentKey+".") { - continue - } - // generate an schema placeholder for the values elSchema := &configschema.Attribute{ Type: attrSchema.Type.ElementType(),