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:
parent
02b3fd289a
commit
d0b7a4a072
|
@ -85,15 +85,22 @@ func (f *StateFilter) filterSingle(a *ResourceAddress) []*StateFilterResult {
|
||||||
// the modules to find relevant resources.
|
// the modules to find relevant resources.
|
||||||
for _, m := range modules {
|
for _, m := range modules {
|
||||||
for n, r := range m.Resources {
|
for n, r := range m.Resources {
|
||||||
if f.relevant(a, r) {
|
// The name in the state contains valuable information. Parse.
|
||||||
// The name in the state contains valuable information. Parse.
|
key, err := ParseResourceStateKey(n)
|
||||||
key, err := ParseResourceStateKey(n)
|
if err != nil {
|
||||||
if err != nil {
|
// If we get an error parsing, then just ignore it
|
||||||
// If we get an error parsing, then just ignore it
|
// out of the state.
|
||||||
// out of the state.
|
continue
|
||||||
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 {
|
if a.Name != "" && a.Name != key.Name {
|
||||||
// Name doesn't match
|
// Name doesn't match
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -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": {
|
"single resource with similar names": {
|
||||||
"small_test_instance.tfstate",
|
"small_test_instance.tfstate",
|
||||||
[]string{"test_instance.foo"},
|
[]string{"test_instance.foo"},
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
{
|
||||||
|
"version": 1,
|
||||||
|
"serial": 12,
|
||||||
|
"modules": [
|
||||||
|
{
|
||||||
|
"path": [
|
||||||
|
"root"
|
||||||
|
],
|
||||||
|
"resources": {
|
||||||
|
"aws_instance.web": {
|
||||||
|
"primary": {
|
||||||
|
"id": "onprem"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in New Issue