terraform: StateFilter handles cases where ResourceState has no type

This was possible with test fixtures but it is also conceiably possible
with older states or corrupted states. We can also extract the type from
the key so we do that now so that StateFilter is more robust.
This commit is contained in:
Mitchell Hashimoto 2017-01-21 10:24:03 -08:00
parent 02b3fd289a
commit d0b7a4a072
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 42 additions and 8 deletions

View File

@ -85,15 +85,22 @@ func (f *StateFilter) filterSingle(a *ResourceAddress) []*StateFilterResult {
// the modules to find relevant resources.
for _, m := range modules {
for n, r := range m.Resources {
if f.relevant(a, r) {
// The name in the state contains valuable information. Parse.
key, err := ParseResourceStateKey(n)
if err != nil {
// If we get an error parsing, then just ignore it
// out of the state.
continue
}
// The name in the state contains valuable information. Parse.
key, err := ParseResourceStateKey(n)
if err != nil {
// If we get an error parsing, then just ignore it
// out of the state.
continue
}
// Older states and test fixtures often don't contain the
// type directly on the ResourceState. We add this so StateFilter
// is a bit more robust.
if r.Type == "" {
r.Type = key.Type
}
if f.relevant(a, r) {
if a.Name != "" && a.Name != key.Name {
// Name doesn't match
continue

View File

@ -38,6 +38,15 @@ func TestStateFilterFilter(t *testing.T) {
},
},
"single resource from minimal state": {
"single-minimal-resource.tfstate",
[]string{"aws_instance.web"},
[]string{
"*terraform.ResourceState: aws_instance.web",
"*terraform.InstanceState: aws_instance.web",
},
},
"single resource with similar names": {
"small_test_instance.tfstate",
[]string{"test_instance.foo"},

View File

@ -0,0 +1,18 @@
{
"version": 1,
"serial": 12,
"modules": [
{
"path": [
"root"
],
"resources": {
"aws_instance.web": {
"primary": {
"id": "onprem"
}
}
}
}
]
}