Skip to content

Commit

Permalink
Add support for multi-value filters
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Winther <[email protected]>
  • Loading branch information
jippi committed Sep 3, 2018
1 parent efb249d commit efd6076
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
8 changes: 6 additions & 2 deletions backend/consul/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (b *Backend) CatalogReader(state *config.CatalogState, consulNodeName strin
continue
}

logger.Debug("Wait index is changed (%d <> %d)", localWaitIndex, remoteWaitIndex)
logger.Debugf("Wait index is changed (%d <> %d)", localWaitIndex, remoteWaitIndex)
q.WaitIndex = remoteWaitIndex

state.Lock()
Expand All @@ -78,8 +78,12 @@ func processCatalog(n internalNode) config.Services {
}

for _, check := range n.Checks {
if check.CheckID == "serfHealth" {
continue
}

if _, ok := services[check.ServiceID]; !ok {
log.Fatalf("Could not find a service %s for check %s", check.ServiceID, check.CheckID)
log.Fatalf("Could not find a service '%s' for check '%s'", check.ServiceID, check.CheckID)
}

services[check.ServiceID].CheckID = check.CheckID
Expand Down
4 changes: 3 additions & 1 deletion config/filters.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"log"
"strings"
)
Expand All @@ -18,7 +19,8 @@ func ProcessFilters(userFilters []string) Filters {
name := split[0]

if _, ok := results[name]; ok {
log.Fatalf("Duplicate filer name %s", name)
results[name] = fmt.Sprintf("%s,%s", results[name], split[1])
continue
}

results[name] = split[1]
Expand Down
32 changes: 22 additions & 10 deletions service/rds/filter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package rds

import (
"strings"

"github.com/aws/aws-sdk-go/aws"
observer "github.com/imkira/go-observer"
"github.com/seatgeek/aws-dynamic-consul-catalog/config"
Expand Down Expand Up @@ -49,32 +51,42 @@ func (r *RDS) filterByInstanceData(instance *config.DBInstance, filters config.F
return true
}

for k, v := range filters {
for k, filter := range filters {
switch k {
case "AvailabilityZone":
return v == aws.StringValue(instance.AvailabilityZone)
return r.matches(filter, aws.StringValue(instance.AvailabilityZone))
case "DBInstanceArn":
return v == aws.StringValue(instance.DBInstanceArn)
return r.matches(filter, aws.StringValue(instance.DBInstanceArn))
case "DBInstanceClass":
return v == aws.StringValue(instance.DBInstanceClass)
return r.matches(filter, aws.StringValue(instance.DBInstanceClass))
case "DBInstanceIdentifier":
return v == aws.StringValue(instance.DBInstanceIdentifier)
return r.matches(filter, aws.StringValue(instance.DBInstanceIdentifier))
case "DBInstanceStatus":
return v == aws.StringValue(instance.DBInstanceStatus)
return r.matches(filter, aws.StringValue(instance.DBInstanceStatus))
case "Engine":
return v == aws.StringValue(instance.Engine)
return r.matches(filter, aws.StringValue(instance.Engine))
case "EngineVersion":
return v == aws.StringValue(instance.EngineVersion)
return r.matches(filter, aws.StringValue(instance.EngineVersion))
case "VpcId":
return v == aws.StringValue(instance.DBSubnetGroup.VpcId)
return r.matches(filter, aws.StringValue(instance.DBSubnetGroup.VpcId))
default:
log.Fatalf("Unknown instance filter key %s (%s)", k, v)
log.Fatalf("Unknown instance filter key %s (%s)", k, filter)
}
}

return true
}

func (r *RDS) matches(filter, value string) bool {
for _, v := range strings.Split(filter, ",") {
if v == value {
return true
}
}

return false
}

func (r *RDS) filterByInstanceTags(instance *config.DBInstance, filters config.Filters) bool {
if len(filters) == 0 {
return true
Expand Down

0 comments on commit efd6076

Please sign in to comment.