From ab6741f6fc09c3cd8f13db16a0d6326c385734d2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 10 Oct 2014 13:46:44 -0700 Subject: [PATCH] terraform: require the prefix match with a "." in Diff.Instances --- terraform/diff.go | 6 ++-- terraform/diff_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/terraform/diff.go b/terraform/diff.go index e01deb8564..3770a1d471 100644 --- a/terraform/diff.go +++ b/terraform/diff.go @@ -172,8 +172,10 @@ func (d *ModuleDiff) Empty() bool { func (d *ModuleDiff) Instances(id string) []*InstanceDiff { var result []*InstanceDiff for k, diff := range d.Resources { - if strings.HasPrefix(k, id) && !diff.Empty() { - result = append(result, diff) + if k == id || strings.HasPrefix(k, id+".") { + if !diff.Empty() { + result = append(result, diff) + } } } diff --git a/terraform/diff_test.go b/terraform/diff_test.go index aaa5e4da62..dcee2b3357 100644 --- a/terraform/diff_test.go +++ b/terraform/diff_test.go @@ -1,6 +1,7 @@ package terraform import ( + "reflect" "strings" "testing" ) @@ -268,6 +269,68 @@ func TestInstanceDiff_Empty(t *testing.T) { } } +func TestModuleDiff_Instances(t *testing.T) { + yesDiff := &InstanceDiff{Destroy: true} + noDiff := &InstanceDiff{Destroy: true, DestroyTainted: true} + + cases := []struct { + Diff *ModuleDiff + Id string + Result []*InstanceDiff + }{ + { + &ModuleDiff{ + Resources: map[string]*InstanceDiff{ + "foo": yesDiff, + "bar": noDiff, + }, + }, + "foo", + []*InstanceDiff{ + yesDiff, + }, + }, + + { + &ModuleDiff{ + Resources: map[string]*InstanceDiff{ + "foo": yesDiff, + "foo.0": yesDiff, + "bar": noDiff, + }, + }, + "foo", + []*InstanceDiff{ + yesDiff, + yesDiff, + }, + }, + + { + &ModuleDiff{ + Resources: map[string]*InstanceDiff{ + "foo": yesDiff, + "foo.0": yesDiff, + "foo_bar": noDiff, + "bar": noDiff, + }, + }, + "foo", + []*InstanceDiff{ + yesDiff, + yesDiff, + }, + }, + } + + for i, tc := range cases { + actual := tc.Diff.Instances(tc.Id) + if !reflect.DeepEqual(actual, tc.Result) { + t.Fatalf("%d: %#v", i, actual) + } + } +} + func TestInstanceDiff_RequiresNew(t *testing.T) { rd := &InstanceDiff{ Attributes: map[string]*ResourceAttrDiff{