Use an interface for zone type matching

This commit is contained in:
Falcon Taylor-Carter 2021-07-11 19:50:29 -04:00
parent 62011f09aa
commit b8cee43d73
3 changed files with 24 additions and 33 deletions

View File

@ -241,7 +241,7 @@ func (p *AWSProvider) Zones(ctx context.Context) (map[string]*route53.HostedZone
continue
}
if !p.zoneTypeFilter.MatchAWS(zone) {
if !p.zoneTypeFilter.Match(zone) {
continue
}

View File

@ -37,43 +37,34 @@ func NewZoneTypeFilter(zoneType string) ZoneTypeFilter {
}
// Match checks whether a zone matches the zone type that's filtered for.
func (f ZoneTypeFilter) Match(visibility string) bool {
func (f ZoneTypeFilter) Match(rawZoneType interface{}) bool {
// An empty zone filter includes all hosted zones.
if f.zoneType == "" {
return true
}
switch zoneType := rawZoneType.(type) {
// Given a zone type we return true if the given zone matches this type.
switch f.zoneType {
case zoneTypePublic:
return visibility == zoneTypePublic
case zoneTypePrivate:
return visibility == zoneTypePrivate
}
// We return false on any other path, e.g. unknown zone type filter value.
return false
}
// Match checks whether a zone matches the zone type that's filtered for.
func (f ZoneTypeFilter) MatchAWS(zone *route53.HostedZone) bool {
// An empty zone filter includes all hosted zones.
if f.zoneType == "" {
return true
}
// If the zone has no config we assume it's a public zone since the config's field
// `PrivateZone` is false by default in go.
if zone.Config == nil {
return f.zoneType == zoneTypePublic
}
// Given a zone type we return true if the given zone matches this type.
switch f.zoneType {
case zoneTypePublic:
return !aws.BoolValue(zone.Config.PrivateZone)
case zoneTypePrivate:
return aws.BoolValue(zone.Config.PrivateZone)
case string:
switch f.zoneType {
case zoneTypePublic:
return zoneType == zoneTypePublic
case zoneTypePrivate:
return zoneType == zoneTypePrivate
}
case *route53.HostedZone:
// If the zone has no config we assume it's a public zone since the config's field
// `PrivateZone` is false by default in go.
if zoneType.Config == nil {
return f.zoneType == zoneTypePublic
}
switch f.zoneType {
case zoneTypePublic:
return !aws.BoolValue(zoneType.Config.PrivateZone)
case zoneTypePrivate:
return aws.BoolValue(zoneType.Config.PrivateZone)
}
}
// We return false on any other path, e.g. unknown zone type filter value.

View File

@ -66,7 +66,7 @@ func TestZoneTypeFilterMatchAWS(t *testing.T) {
},
} {
zoneTypeFilter := NewZoneTypeFilter(tc.zoneTypeFilter)
assert.Equal(t, tc.matches, zoneTypeFilter.MatchAWS(tc.zone))
assert.Equal(t, tc.matches, zoneTypeFilter.Match(tc.zone))
}
}