From 9aef92f32d01ab25b6155ce05e8c146478bbab34 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Tue, 22 Aug 2023 21:01:29 +0000 Subject: [PATCH] zones: assign zoneID to zones inferable ID Signed-off-by: Alejandro Mery --- pkg/zones/scan.go | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/pkg/zones/scan.go b/pkg/zones/scan.go index 2cfdada..e051df4 100644 --- a/pkg/zones/scan.go +++ b/pkg/zones/scan.go @@ -5,6 +5,20 @@ import ( ) func (m *Zones) scan() error { + for _, fn := range []func() error{ + m.scanDirectory, + m.scanMachines, + m.scanZoneIDs, + } { + if err := fn(); err != nil { + return err + } + } + + return nil +} + +func (m *Zones) scanDirectory() error { // each directory is a zone entries, err := fs.ReadDir(m.dir, ".") if err != nil { @@ -26,7 +40,7 @@ func (m *Zones) scan() error { } } - return m.scanMachines() + return nil } func (m *Zones) scanMachines() error { @@ -38,6 +52,36 @@ func (m *Zones) scanMachines() error { return err } +func (m *Zones) scanZoneIDs() error { + var hasMissing bool + var lastZoneID int + + m.ForEachZone(func(z *Zone) bool { + switch { + case z.ID == 0: + hasMissing = true + case z.ID > lastZoneID: + lastZoneID = z.ID + } + + return false + }) + + if hasMissing { + next := lastZoneID + 1 + + m.ForEachZone(func(z *Zone) bool { + if z.ID == 0 { + z.ID, next = next, next+1 + } + + return false + }) + } + + return nil +} + func (z *Zone) scan() error { // each directory is a machine entries, err := fs.ReadDir(z.zones.dir, z.Name)