zones: calculate Machine.ID on init

Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
2023-08-24 17:55:43 +00:00
parent 61374d4cc5
commit aca0a5e834
5 changed files with 25 additions and 31 deletions
+1 -23
View File
@@ -5,7 +5,6 @@ import (
"io/fs"
"net/netip"
"path/filepath"
"strconv"
"strings"
"sync"
)
@@ -15,7 +14,7 @@ type Machine struct {
mu sync.Mutex
zone *Zone
id int
ID int
Name string `toml:"name"`
PublicAddresses []netip.Addr `toml:"public,omitempty"`
@@ -26,27 +25,6 @@ func (m *Machine) String() string {
return m.Name
}
// ID return the index within the [Zone] associated to this [Machine]
func (m *Machine) ID() int {
m.mu.Lock()
defer m.mu.Unlock()
if m.id == 0 {
zoneName := m.zone.Name
s := m.Name[len(zoneName)+1:]
id, err := strconv.ParseInt(s, 10, 8)
if err != nil {
panic(err)
}
m.id = int(id)
}
return m.id
}
// FullName returns the Name of the machine including domain name
func (m *Machine) FullName() string {
if domain := m.zone.zones.domain; domain != "" {