From 7e6c53c5f52734a637945ef81cdc99bb07dcff0a Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 21 Aug 2023 22:16:39 +0000 Subject: [PATCH] zones: introduce Machine.PublicAddresses() Signed-off-by: Alejandro Mery --- pkg/zones/machine.go | 3 +++ pkg/zones/machine_scan.go | 26 ++++++++++++++++++++++++++ pkg/zones/scan.go | 4 ++++ 3 files changed, 33 insertions(+) create mode 100644 pkg/zones/machine_scan.go diff --git a/pkg/zones/machine.go b/pkg/zones/machine.go index c238857..ca863e3 100644 --- a/pkg/zones/machine.go +++ b/pkg/zones/machine.go @@ -1,6 +1,7 @@ package zones import ( + "net/netip" "strconv" "strings" "sync" @@ -13,6 +14,8 @@ type Machine struct { zone *Zone id int Name string + + PublicAddresses []netip.Addr } func (m *Machine) String() string { diff --git a/pkg/zones/machine_scan.go b/pkg/zones/machine_scan.go new file mode 100644 index 0000000..9ab8513 --- /dev/null +++ b/pkg/zones/machine_scan.go @@ -0,0 +1,26 @@ +package zones + +import ( + "context" + "net/netip" + "time" +) + +func (m *Machine) lookupNetIP() ([]netip.Addr, error) { + timeout := 2 * time.Second + ctx, cancel := context.WithTimeout(context.Background(), timeout) + + defer cancel() + + return m.zone.zones.resolver.LookupNetIP(ctx, "ip", m.FullName()) +} + +func (m *Machine) updatePublicAddresses() error { + addrs, err := m.lookupNetIP() + if err != nil { + return err + } + + m.PublicAddresses = addrs + return nil +} diff --git a/pkg/zones/scan.go b/pkg/zones/scan.go index cbe1ff3..6ab2a7e 100644 --- a/pkg/zones/scan.go +++ b/pkg/zones/scan.go @@ -43,6 +43,10 @@ func (z *Zone) scan() error { Name: e.Name(), } + if err := m.updatePublicAddresses(); err != nil { + return err + } + z.Machines = append(z.Machines, m) } }