From 0a2fd1a5e5a939a4bc8679933bf9b9e4e7b7c94c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 31 Mar 2016 09:24:23 -0700 Subject: [PATCH] terraform: filtering on name actually matches name --- terraform/state_filter.go | 5 ++++ terraform/state_filter_test.go | 9 +++++++ terraform/state_test.go | 18 +++++++++++-- .../state-filter/small_test_instance.tfstate | 27 +++++++++++++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 terraform/test-fixtures/state-filter/small_test_instance.tfstate diff --git a/terraform/state_filter.go b/terraform/state_filter.go index 89cf0d898..1b41a3b7e 100644 --- a/terraform/state_filter.go +++ b/terraform/state_filter.go @@ -94,6 +94,11 @@ func (f *StateFilter) filterSingle(a *ResourceAddress) []*StateFilterResult { continue } + if a.Name != "" && a.Name != key.Name { + // Name doesn't match + continue + } + if a.Index >= 0 && key.Index != a.Index { // Index doesn't match continue diff --git a/terraform/state_filter_test.go b/terraform/state_filter_test.go index f9187b4e9..5e66d9176 100644 --- a/terraform/state_filter_test.go +++ b/terraform/state_filter_test.go @@ -38,6 +38,15 @@ func TestStateFilterFilter(t *testing.T) { }, }, + "single resource with similar names": { + "small_test_instance.tfstate", + []string{"test_instance.foo"}, + []string{ + "*terraform.ResourceState: test_instance.foo", + "*terraform.InstanceState: test_instance.foo", + }, + }, + "single instance": { "small.tfstate", []string{"aws_key_pair.onprem.primary"}, diff --git a/terraform/state_test.go b/terraform/state_test.go index b9e727457..a27615ede 100644 --- a/terraform/state_test.go +++ b/terraform/state_test.go @@ -590,6 +590,13 @@ func TestStateRemove(t *testing.T) { ID: "foo", }, }, + + "test_instance.bar": &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, }, }, }, @@ -597,8 +604,15 @@ func TestStateRemove(t *testing.T) { &State{ Modules: []*ModuleState{ &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{}, + Path: rootModulePath, + Resources: map[string]*ResourceState{ + "test_instance.bar": &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + }, }, }, }, diff --git a/terraform/test-fixtures/state-filter/small_test_instance.tfstate b/terraform/test-fixtures/state-filter/small_test_instance.tfstate new file mode 100644 index 000000000..7d6283389 --- /dev/null +++ b/terraform/test-fixtures/state-filter/small_test_instance.tfstate @@ -0,0 +1,27 @@ +{ + "version": 1, + "serial": 12, + "modules": [ + { + "path": [ + "root" + ], + "resources": { + "test_instance.foo": { + "type": "test_instance", + "primary": { + "id": "foo" + } + } + }, + "resources": { + "test_instance.bar": { + "type": "test_instance", + "primary": { + "id": "foo" + } + } + } + } + ] +}