helper/schema: HasChange

This commit is contained in:
Mitchell Hashimoto 2014-08-18 10:00:41 -07:00
parent f1f4836c99
commit 66d7003e6f
2 changed files with 87 additions and 0 deletions

View File

@ -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

View File

@ -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