diff --git a/pkg/zones/zones.go b/pkg/zones/zones.go index 7601919..a8fb2ea 100644 --- a/pkg/zones/zones.go +++ b/pkg/zones/zones.go @@ -66,6 +66,26 @@ func (m Machines) Swap(i, j int) { m[i], m[j] = m[j], m[i] } +// FilterMachines produces a subset of the machines offered by the given +// iterator fulfilling a condition +func FilterMachines(m MachineIterator, cond func(*Machine) bool) (Machines, int) { + var out []*Machine + + if cond == nil { + // unconditional + cond = func(*Machine) bool { return true } + } + + m.ForEachMachine(func(p *Machine) bool { + if cond(p) { + out = append(out, p) + } + return false + }) + + return out, len(out) +} + // Zone represents one zone in a cluster type Zone struct { zones *Zones