From 04b8da4e054df87a5c10cad7f52a69490da2508f Mon Sep 17 00:00:00 2001 From: grahamhar Date: Wed, 18 Nov 2020 19:04:27 +0000 Subject: [PATCH] Fix change detection The change detection failed on maps, this introduces the use of go-cmp which is better suited to this purpose. Credit to https://github.com/gdavison for providing the tests. --- helper/schema/resource_data.go | 3 +- helper/schema/resource_data_test.go | 78 +++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/helper/schema/resource_data.go b/helper/schema/resource_data.go index d12e7546046..5519336c8a8 100644 --- a/helper/schema/resource_data.go +++ b/helper/schema/resource_data.go @@ -1,6 +1,7 @@ package schema import ( + "github.com/google/go-cmp/cmp" "log" "reflect" "strings" @@ -162,7 +163,7 @@ func (d *ResourceData) HasChange(key string) bool { return !eq.Equal(n) } - return !reflect.DeepEqual(o, n) + return !cmp.Equal(o, n) } // HasChangeExcept returns whether any keys outside the given key have been changed. diff --git a/helper/schema/resource_data_test.go b/helper/schema/resource_data_test.go index d3e06fa0ea6..c2c4d854e36 100644 --- a/helper/schema/resource_data_test.go +++ b/helper/schema/resource_data_test.go @@ -2073,6 +2073,84 @@ func TestResourceDataHasChange(t *testing.T) { Key: "ports", + Change: false, + }, + { + Schema: map[string]*Schema{ + "vpc_config": { + Type: TypeList, + Optional: true, + Elem: &Resource{ + Schema: map[string]*Schema{ + "subnet_ids": { + Type: TypeSet, + Required: true, + Elem: &Schema{Type: TypeString}, + Set: HashString, + }, + }, + }, + }, + }, + + State: &terraform.InstanceState{ + Attributes: map[string]string{ + "vpc_config.#": "1", + "vpc_config.0.subnet_ids.#": "1", + fmt.Sprintf("vpc_config.0.subnet_ids.%d", HashString("abc-123")): "abc-123", + }, + }, + + Diff: &terraform.InstanceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "vpc_config.0.subnet_ids.#": { + Old: "1", + New: "0", + }, + }, + }, + + Key: "vpc_config", + + Change: true, + }, + { + Schema: map[string]*Schema{ + "vpc_config": { + Type: TypeList, + Optional: true, + Elem: &Resource{ + Schema: map[string]*Schema{ + "subnet_ids": { + Type: TypeSet, + Required: true, + Elem: &Schema{Type: TypeString}, + Set: HashString, + }, + }, + }, + }, + }, + + State: &terraform.InstanceState{ + Attributes: map[string]string{ + "vpc_config.#": "1", + "vpc_config.0.subnet_ids.#": "1", + fmt.Sprintf("vpc_config.0.subnet_ids.%d", HashString("abc-123")): "abc-123", + }, + }, + + Diff: &terraform.InstanceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "tags.foo": { + Old: "", + New: "bar", + }, + }, + }, + + Key: "vpc_config", + Change: false, }, }