From 187b016d099257ec3d6c10604703ef375b8b500a Mon Sep 17 00:00:00 2001 From: Juan Font Date: Thu, 2 Sep 2021 16:59:50 +0200 Subject: [PATCH] Added helper function to get list of shared nodes --- namespaces.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/namespaces.go b/namespaces.go index ff9eeac..8d9543e 100644 --- a/namespaces.go +++ b/namespaces.go @@ -91,12 +91,30 @@ func (h *Headscale) ListMachinesInNamespace(name string) (*[]Machine, error) { } machines := []Machine{} - if err := h.db.Preload("AuthKey").Where(&Machine{NamespaceID: n.ID}).Find(&machines).Error; err != nil { + if err := h.db.Preload("AuthKey").Preload("Namespace").Where(&Machine{NamespaceID: n.ID}).Find(&machines).Error; err != nil { return nil, err } return &machines, nil } +// ListSharedMachinesInNamespaces returns all the machines that are shared to the specified namespace +func (h *Headscale) ListSharedMachinesInNamespace(name string) (*[]Machine, error) { + n, err := h.GetNamespace(name) + if err != nil { + return nil, err + } + sharedNodes := []SharedNode{} + if err := h.db.Preload("Namespace").Preload("Machine").Where(&SharedNode{NamespaceID: n.ID}).Find(&sharedNodes).Error; err != nil { + return nil, err + } + + machines := []Machine{} + for _, sn := range sharedNodes { + machines = append(machines, sn.Machine) + } + return &machines, nil +} + // SetMachineNamespace assigns a Machine to a namespace func (h *Headscale) SetMachineNamespace(m *Machine, namespaceName string) error { n, err := h.GetNamespace(namespaceName)