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" + } + } + } + } + ] +}