Missing ConfigResource checks in TargetContains

Adding some missing checks for ConfigResource, and check all
combinations of Resource types for consistency.
This commit is contained in:
James Bardin 2020-03-13 19:01:23 -04:00
parent e3ad9ffb77
commit e6bac359ed
4 changed files with 46 additions and 0 deletions

View File

@ -73,6 +73,9 @@ func (m Module) TargetContains(other Targetable) bool {
case ModuleInstance:
return m.TargetContains(to.Module())
case ConfigResource:
return m.TargetContains(to.Module)
case AbsResource:
return m.TargetContains(to.Module)

View File

@ -420,6 +420,9 @@ func (m ModuleInstance) TargetContains(other Targetable) bool {
}
return true
case ConfigResource:
return m.TargetContains(to.Module)
case AbsResource:
return m.TargetContains(to.Module)

View File

@ -145,6 +145,11 @@ func (r AbsResource) TargetContains(other Targetable) bool {
// We'll use our stringification as a cheat-ish way to test for equality.
return to.String() == r.String()
case ConfigResource:
// if an absolute resource from parsing a target address contains a
// ConfigResource, the string representation will match
return to.String() == r.String()
case AbsResourceInstance:
return r.TargetContains(to.ContainingResource())
@ -203,9 +208,15 @@ func (r AbsResourceInstance) ContainingResource() AbsResource {
func (r AbsResourceInstance) TargetContains(other Targetable) bool {
switch to := other.(type) {
// while we currently don't start with an AbsResourceInstance as a target
// address, check all resource types for consistency.
case AbsResourceInstance:
// We'll use our stringification as a cheat-ish way to test for equality.
return to.String() == r.String()
case ConfigResource:
return to.String() == r.String()
case AbsResource:
return to.String() == r.String()
default:
return false

View File

@ -94,6 +94,11 @@ func TestTargetContains(t *testing.T) {
mustParseTarget("module.bar[0].test_resource.foo[2]"),
false,
},
{
mustParseTarget("module.bar.test_resource.foo"),
mustParseTarget("module.bar.test_resource.foo[0]"),
true,
},
// Config paths, while never returned from parsing a target, must still
// be targetable
@ -109,6 +114,30 @@ func TestTargetContains(t *testing.T) {
mustParseTarget("module.bar.test_resource.foo[2]"),
true,
},
{
mustParseTarget("module.bar"),
ConfigResource{
Module: []string{"bar"},
Resource: Resource{
Mode: ManagedResourceMode,
Type: "test_resource",
Name: "foo",
},
},
true,
},
{
mustParseTarget("module.bar.test_resource.foo"),
ConfigResource{
Module: []string{"bar"},
Resource: Resource{
Mode: ManagedResourceMode,
Type: "test_resource",
Name: "foo",
},
},
true,
},
{
ConfigResource{
Resource: Resource{