From 7ce4738d8a2f0a9233f15f3f300cbc190e48c2e5 Mon Sep 17 00:00:00 2001 From: Juan Font Date: Fri, 3 Sep 2021 10:23:26 +0200 Subject: [PATCH] Preload namespace so the name can be shown --- machine.go | 2 +- namespaces.go | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/machine.go b/machine.go index a6f8d1fb..bbacee5a 100644 --- a/machine.go +++ b/machine.go @@ -231,7 +231,7 @@ func (h *Headscale) GetMachine(namespace string, name string) (*Machine, error) // GetMachineByID finds a Machine by ID and returns the Machine struct func (h *Headscale) GetMachineByID(id uint64) (*Machine, error) { m := Machine{} - if result := h.db.Find(&Machine{ID: id}).First(&m); result.Error != nil { + if result := h.db.Preload("Namespace").Find(&Machine{ID: id}).First(&m); result.Error != nil { return nil, result.Error } return &m, nil diff --git a/namespaces.go b/namespaces.go index fb2db01a..57674ff3 100644 --- a/namespaces.go +++ b/namespaces.go @@ -104,13 +104,17 @@ func (h *Headscale) ListSharedMachinesInNamespace(name string) (*[]Machine, erro return nil, err } sharedNodes := []SharedNode{} - if err := h.db.Preload("Namespace").Preload("Machine").Where(&SharedNode{NamespaceID: n.ID}).Find(&sharedNodes).Error; err != nil { + if err := h.db.Preload("Namespace").Where(&SharedNode{NamespaceID: n.ID}).Find(&sharedNodes).Error; err != nil { return nil, err } machines := []Machine{} for _, sn := range sharedNodes { - machines = append(machines, sn.Machine) + m, err := h.GetMachineByID(sn.MachineID) // otherwise not everything comes filled + if err != nil { + return nil, err + } + machines = append(machines, *m) } return &machines, nil }