terraform: modify Same to return true if list is computed

This commit is contained in:
Mitchell Hashimoto 2014-10-09 20:52:38 -07:00
parent dcaf653d6f
commit 75e79da9c3
2 changed files with 81 additions and 8 deletions

View File

@ -358,19 +358,55 @@ func (d *InstanceDiff) Same(d2 *InstanceDiff) bool {
if d.RequiresNew() != d2.RequiresNew() { if d.RequiresNew() != d2.RequiresNew() {
return false return false
} }
if len(d.Attributes) != len(d2.Attributes) {
// Go through the old diff and make sure the new diff has all the
// same attributes. To start, build up the check map to be all the keys.
checkOld := make(map[string]struct{})
checkNew := make(map[string]struct{})
for k, _ := range d.Attributes {
checkOld[k] = struct{}{}
}
for k, _ := range d2.Attributes {
checkNew[k] = struct{}{}
}
for k, diffOld := range d.Attributes {
if _, ok := checkOld[k]; !ok {
// We're not checking this key for whatever reason (see where
// check is modified).
continue
}
// Remove this key since we'll never hit it again
delete(checkOld, k)
delete(checkNew, k)
_, ok := d2.Attributes[k]
if !ok {
// The matching attribute was not found, we're different
return false return false
} }
ks := make(map[string]struct{}) if diffOld.NewComputed && strings.HasSuffix(k, ".#") {
for k, _ := range d.Attributes { // This is a computed list, so remove any keys with this
ks[k] = struct{}{} // prefix from the check list.
kprefix := k[0:len(k)-2] + "."
for k2, _ := range checkOld {
if strings.HasPrefix(k2, kprefix) {
delete(checkOld, k2)
}
}
for k2, _ := range checkNew {
if strings.HasPrefix(k2, kprefix) {
delete(checkNew, k2)
}
} }
for k, _ := range d2.Attributes {
delete(ks, k)
} }
if len(ks) > 0 { // TODO: check for the same value if not computed
}
// Check for leftover attributes
if len(checkNew) > 0 {
return false return false
} }

View File

@ -351,6 +351,22 @@ func TestInstanceDiffSame(t *testing.T) {
false, false,
}, },
// Extra attributes
{
&InstanceDiff{
Attributes: map[string]*ResourceAttrDiff{
"foo": &ResourceAttrDiff{},
},
},
&InstanceDiff{
Attributes: map[string]*ResourceAttrDiff{
"foo": &ResourceAttrDiff{},
"bar": &ResourceAttrDiff{},
},
},
false,
},
{ {
&InstanceDiff{ &InstanceDiff{
Attributes: map[string]*ResourceAttrDiff{ Attributes: map[string]*ResourceAttrDiff{
@ -364,12 +380,33 @@ func TestInstanceDiffSame(t *testing.T) {
}, },
false, false,
}, },
{
&InstanceDiff{
Attributes: map[string]*ResourceAttrDiff{
"foo.#": &ResourceAttrDiff{NewComputed: true},
},
},
&InstanceDiff{
Attributes: map[string]*ResourceAttrDiff{
"foo.#": &ResourceAttrDiff{
Old: "0",
New: "1",
},
"foo.0": &ResourceAttrDiff{
Old: "",
New: "12",
},
},
},
true,
},
} }
for i, tc := range cases { for i, tc := range cases {
actual := tc.One.Same(tc.Two) actual := tc.One.Same(tc.Two)
if actual != tc.Same { if actual != tc.Same {
t.Fatalf("Fail %d", i) t.Fatalf("%d:\n\n%#v\n\n%#v", i, tc.One, tc.Two)
} }
} }
} }