diff --git a/helper/schema/resource_data.go b/helper/schema/resource_data.go index 5bb09ca5e..b00c4b154 100644 --- a/helper/schema/resource_data.go +++ b/helper/schema/resource_data.go @@ -532,7 +532,9 @@ func (d *ResourceData) stateList( count := countRaw.(int) result := make(map[string]string) - result[prefix+".#"] = strconv.FormatInt(int64(count), 10) + if count > 0 { + result[prefix+".#"] = strconv.FormatInt(int64(count), 10) + } for i := 0; i < count; i++ { key := fmt.Sprintf("%s.%d", prefix, i) diff --git a/helper/schema/resource_data_test.go b/helper/schema/resource_data_test.go index 4578d7574..e07daab08 100644 --- a/helper/schema/resource_data_test.go +++ b/helper/schema/resource_data_test.go @@ -406,6 +406,44 @@ func TestResourceDataGet(t *testing.T) { }, }, }, + + // List of maps with removal in diff + { + Schema: map[string]*Schema{ + "config_vars": &Schema{ + Type: TypeList, + Optional: true, + Computed: true, + Elem: &Schema{ + Type: TypeMap, + }, + }, + }, + + State: &terraform.ResourceState{ + Attributes: map[string]string{ + "config_vars.#": "1", + "config_vars.0.FOO": "bar", + }, + }, + + Diff: &terraform.ResourceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "config_vars.#": &terraform.ResourceAttrDiff{ + Old: "1", + New: "0", + }, + "config_vars.0.FOO": &terraform.ResourceAttrDiff{ + Old: "bar", + NewRemoved: true, + }, + }, + }, + + Key: "config_vars", + + Value: []interface{}{}, + }, } for i, tc := range cases { @@ -1171,6 +1209,44 @@ func TestResourceDataState(t *testing.T) { }, }, }, + + // List of maps with removal in diff + { + Schema: map[string]*Schema{ + "config_vars": &Schema{ + Type: TypeList, + Optional: true, + Computed: true, + Elem: &Schema{ + Type: TypeMap, + }, + }, + }, + + State: &terraform.ResourceState{ + Attributes: map[string]string{ + "config_vars.#": "1", + "config_vars.0.FOO": "bar", + }, + }, + + Diff: &terraform.ResourceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "config_vars.#": &terraform.ResourceAttrDiff{ + Old: "1", + New: "0", + }, + "config_vars.0.FOO": &terraform.ResourceAttrDiff{ + Old: "bar", + NewRemoved: true, + }, + }, + }, + + Result: &terraform.ResourceState{ + Attributes: map[string]string{}, + }, + }, } for i, tc := range cases {