From d3ce9ebc4729fe22fdd3e81167440a45b9bba05f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= <2000michal@wp.pl> Date: Mon, 7 Oct 2024 09:18:10 +0200 Subject: [PATCH] feat(util/dcfilter): allow for creating stand-alone filter 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. --- v3/pkg/util/inexlist/dcfilter/dcfilter.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/v3/pkg/util/inexlist/dcfilter/dcfilter.go b/v3/pkg/util/inexlist/dcfilter/dcfilter.go index 24a931b18..22f7843ae 100644 --- a/v3/pkg/util/inexlist/dcfilter/dcfilter.go +++ b/v3/pkg/util/inexlist/dcfilter/dcfilter.go @@ -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, "*")