From 0158dc3a3c209bcc6ce11f5ca2c8d1fd37461dae Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sat, 2 Mar 2024 21:51:05 +0000 Subject: [PATCH] cluster: add recursive RegionIterator to Zone Signed-off-by: Alejandro Mery --- pkg/cluster/regions.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/cluster/regions.go b/pkg/cluster/regions.go index c40ab8c..bf29137 100644 --- a/pkg/cluster/regions.go +++ b/pkg/cluster/regions.go @@ -9,6 +9,7 @@ var ( _ MachineIterator = (*Region)(nil) _ ZoneIterator = (*Region)(nil) + _ RegionIterator = (*Zone)(nil) _ RegionIterator = (*Cluster)(nil) ) @@ -152,6 +153,28 @@ func (m *Cluster) appendRegionRegions(name string, subs ...string) { }) } +// ForEachRegion calls a function on all regions this zone belongs to. +func (z *Zone) ForEachRegion(fn func(*Region) bool) { + if fn == nil { + return + } + + z.zones.ForEachRegion(func(r *Region) bool { + var match bool + + r.ForEachZone(func(z2 *Zone) bool { + match = (z == z2) + return match + }) + + if match && fn(r) { + return true + } + + return false + }) +} + func (z *Zone) appendRegions(regions ...string) error { for _, s := range regions { // TODO: validate