Commit 7dd0acc4 authored by James Bardin's avatar James Bardin
Browse files

don't count empty containers in diff.Apply

If there were no matching keys, and there was no diff at all, don't set
a zero count for the container. Normally Providers can't reliably detect
empty vs unset here, but there are some cases that worked.
parent 37b5e2dc
No related merge requests found
Showing with 109 additions and 13 deletions
+109 -13
......@@ -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(`
......
......@@ -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(),
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment