diff --git a/helper/schema/resource_data.go b/helper/schema/resource_data.go index 7ba554328..93ad72749 100644 --- a/helper/schema/resource_data.go +++ b/helper/schema/resource_data.go @@ -2,6 +2,7 @@ package schema import ( "fmt" + "reflect" "strconv" "strings" @@ -58,6 +59,12 @@ func (d *ResourceData) GetChange(key string) (interface{}, interface{}) { return o, n } +// HasChange returns whether or not the given key has been changed. +func (d *ResourceData) HasChange(key string) bool { + o, n := d.GetChange(key) + return !reflect.DeepEqual(o, n) +} + // Set sets the value for the given key. // // If the key is invalid or the value is not a correct type, an error diff --git a/helper/schema/resource_data_test.go b/helper/schema/resource_data_test.go index 2626da4d3..d67c1dc2a 100644 --- a/helper/schema/resource_data_test.go +++ b/helper/schema/resource_data_test.go @@ -401,6 +401,86 @@ func TestResourceDataGetChange(t *testing.T) { } } +func TestResourceDataHasChange(t *testing.T) { + cases := []struct { + Schema map[string]*Schema + State *terraform.ResourceState + Diff *terraform.ResourceDiff + Key string + Change bool + }{ + { + Schema: map[string]*Schema{ + "availability_zone": &Schema{ + Type: TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + + State: nil, + + Diff: &terraform.ResourceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "availability_zone": &terraform.ResourceAttrDiff{ + Old: "", + New: "foo", + RequiresNew: true, + }, + }, + }, + + Key: "availability_zone", + + Change: true, + }, + + { + Schema: map[string]*Schema{ + "availability_zone": &Schema{ + Type: TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + + State: &terraform.ResourceState{ + Attributes: map[string]string{ + "availability_zone": "foo", + }, + }, + + Diff: &terraform.ResourceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "availability_zone": &terraform.ResourceAttrDiff{ + Old: "", + New: "foo", + RequiresNew: true, + }, + }, + }, + + Key: "availability_zone", + + Change: false, + }, + } + + for i, tc := range cases { + d, err := schemaMap(tc.Schema).Data(tc.State, tc.Diff) + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := d.HasChange(tc.Key) + if actual != tc.Change { + t.Fatalf("Bad: %d %#v", i, actual) + } + } +} + func TestResourceDataSet(t *testing.T) { cases := []struct { Schema map[string]*Schema