diff --git a/pkg/cluster/regions.go b/pkg/cluster/regions.go index 134cb2e..c3b716d 100644 --- a/pkg/cluster/regions.go +++ b/pkg/cluster/regions.go @@ -1,5 +1,10 @@ package cluster +var ( + _ MachineIterator = (*Region)(nil) + _ ZoneIterator = (*Region)(nil) +) + // Region represents a group of zones geographically related type Region struct { m *Cluster @@ -9,6 +14,43 @@ type Region struct { Regions []string `json:",omitempty" yaml:",omitempty"` } +// ForEachRegion calls a function for each Region of the cluster +// until instructed to terminate the loop +func (m *Cluster) ForEachRegion(fn func(r *Region) bool) { + for i := range m.Regions { + r := &m.Regions[i] + if fn(r) { + return + } + } +} + +// ForEachMachine calls a function for each Machine in the region +// until instructed to terminate the loop +func (r *Region) ForEachMachine(fn func(*Machine) bool) { + r.ForEachZone(func(z *Zone) bool { + var term bool + + z.ForEachMachine(func(p *Machine) bool { + term = fn(p) + return term + }) + + return term + }) +} + +// ForEachZone calls a function for each Zone in the region +// until instructed to terminate the loop +func (r *Region) ForEachZone(fn func(*Zone) bool) { + for _, p := range r.zones { + if fn(p) { + // terminate + return + } + } +} + func (m *Cluster) initRegions(_ *ScanOptions) error { regions := make(map[string][]*Zone)