Simplify DomainFilterInterface

This commit is contained in:
John Gardiner Myers 2023-06-05 12:49:20 -07:00
parent 7efecc711f
commit d1fb3423c4
5 changed files with 8 additions and 24 deletions

View File

@ -24,41 +24,22 @@ import (
// DomainFilterInterface defines the interface to select matching domains for a specific provider or runtime // DomainFilterInterface defines the interface to select matching domains for a specific provider or runtime
type DomainFilterInterface interface { type DomainFilterInterface interface {
Match(domain string) bool Match(domain string) bool
IsConfigured() bool
} }
type MatchAllDomainFilters []DomainFilterInterface type MatchAllDomainFilters []DomainFilterInterface
func (f MatchAllDomainFilters) Match(domain string) bool { func (f MatchAllDomainFilters) Match(domain string) bool {
if !f.IsConfigured() {
return true
}
for _, filter := range f { for _, filter := range f {
if filter == nil { if filter == nil {
continue continue
} }
if filter.IsConfigured() && !filter.Match(domain) { if !filter.Match(domain) {
return false return false
} }
} }
return true return true
} }
func (f MatchAllDomainFilters) IsConfigured() bool {
if f == nil {
return false
}
for _, filter := range f {
if filter == nil {
continue
}
if filter.IsConfigured() {
return true
}
}
return len(f) > 0
}
// DomainFilter holds a lists of valid domain names // DomainFilter holds a lists of valid domain names
type DomainFilter struct { type DomainFilter struct {
// Filters define what domains to match // Filters define what domains to match

View File

@ -199,7 +199,7 @@ func (p AkamaiProvider) fetchZones() (akamaiZones, error) {
} }
for _, zone := range resp.Zones { for _, zone := range resp.Zones {
if p.domainFilter.Match(zone.Zone) || !p.domainFilter.IsConfigured() { if p.domainFilter.Match(zone.Zone) {
filteredZones.Zones = append(filteredZones.Zones, akamaiZone{ContractID: zone.ContractId, Zone: zone.Zone}) filteredZones.Zones = append(filteredZones.Zones, akamaiZone{ContractID: zone.ContractId, Zone: zone.Zone})
log.Debugf("Fetched zone: '%s' (ZoneID: %s)", zone.Zone, zone.ContractId) log.Debugf("Fetched zone: '%s' (ZoneID: %s)", zone.Zone, zone.ContractId)
} }

View File

@ -148,7 +148,7 @@ func (p *piholeClient) listRecords(ctx context.Context, rtype string) ([]*endpoi
for _, rec := range data { for _, rec := range data {
name := rec[0] name := rec[0]
target := rec[1] target := rec[1]
if p.cfg.DomainFilter.IsConfigured() && !p.cfg.DomainFilter.Match(name) { if !p.cfg.DomainFilter.Match(name) {
log.Debugf("Skipping %s that does not match domain filter", name) log.Debugf("Skipping %s that does not match domain filter", name)
continue continue
} }
@ -195,7 +195,7 @@ type actionResponse struct {
} }
func (p *piholeClient) apply(ctx context.Context, action string, ep *endpoint.Endpoint) error { func (p *piholeClient) apply(ctx context.Context, action string, ep *endpoint.Endpoint) error {
if p.cfg.DomainFilter.IsConfigured() && !p.cfg.DomainFilter.Match(ep.DNSName) { if !p.cfg.DomainFilter.Match(ep.DNSName) {
log.Debugf("Skipping %s %s that does not match domain filter", action, ep.DNSName) log.Debugf("Skipping %s %s that does not match domain filter", action, ep.DNSName)
return nil return nil
} }

View File

@ -119,7 +119,7 @@ func (p *TencentCloudProvider) getPrivateZones() ([]*privatedns.PrivateZone, err
privateZonesFilter := make([]*privatedns.PrivateZone, 0) privateZonesFilter := make([]*privatedns.PrivateZone, 0)
for _, privateZone := range privateZones { for _, privateZone := range privateZones {
if p.domainFilter.IsConfigured() && !p.domainFilter.Match(*privateZone.Domain) { if !p.domainFilter.Match(*privateZone.Domain) {
continue continue
} }
privateZonesFilter = append(privateZonesFilter, privateZone) privateZonesFilter = append(privateZonesFilter, privateZone)

View File

@ -34,6 +34,9 @@ func (f ZoneIDFilter) Match(zoneID string) bool {
if len(f.ZoneIDs) == 0 { if len(f.ZoneIDs) == 0 {
return true return true
} }
if len(f.ZoneIDs) == 1 && f.ZoneIDs[0] == "" {
return true
}
for _, id := range f.ZoneIDs { for _, id := range f.ZoneIDs {
if strings.HasSuffix(zoneID, id) { if strings.HasSuffix(zoneID, id) {