Skip to content

Commit

Permalink
feat(util/dcfilter): allow for creating stand-alone filter (#4061)
Browse files Browse the repository at this point in the history
Sometimes we don't have access to all dcs right away,
so it's more convenient to use Check instead of Apply.
It also allows for parsing dc filters just once.
  • Loading branch information
Michal-Leszczynski authored Oct 7, 2024
1 parent 78982d3 commit f7a2ea9
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions v3/pkg/util/inexlist/dcfilter/dcfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ func Apply(dcMap map[string][]string, filters []string) ([]string, error) {
return filtered, nil
}

// Filter that lets you filter datacenters.
type Filter struct {
filters []string
inex inexlist.InExList
}

func NewFilter(filters []string) (*Filter, error) {
// Decorate filters and create inexlist
inex, err := inexlist.ParseInExList(decorate(filters))
if err != nil {
return nil, errors.Wrapf(err, "parse dc filter %v", filters)
}
return &Filter{
filters: filters,
inex: inex,
}, nil
}

// Check returns true iff dc matches filter.
func (f *Filter) Check(dc string) bool {
return len(f.inex.Filter([]string{dc})) > 0
}

func decorate(filters []string) []string {
if len(filters) == 0 {
filters = append(filters, "*")
Expand Down

0 comments on commit f7a2ea9

Please sign in to comment.