diff --git a/cmd/headscale/cli/nodes.go b/cmd/headscale/cli/nodes.go index 171e98d1..623f7f8c 100644 --- a/cmd/headscale/cli/nodes.go +++ b/cmd/headscale/cli/nodes.go @@ -25,7 +25,7 @@ func init() { nodeCmd.AddCommand(listNodesCmd) nodeCmd.AddCommand(registerNodeCmd) nodeCmd.AddCommand(deleteNodeCmd) - nodeCmd.AddCommand(shareNodeCmd) + nodeCmd.AddCommand(shareMachineCmd) } var nodeCmd = &cobra.Command{ @@ -81,7 +81,7 @@ var listNodesCmd = &cobra.Command{ log.Fatalf("Error initializing: %s", err) } - ns, err := h.GetNamespace(n) + namespace, err := h.GetNamespace(n) if err != nil { log.Fatalf("Error fetching namespace: %s", err) } @@ -107,7 +107,7 @@ var listNodesCmd = &cobra.Command{ log.Fatalf("Error getting nodes: %s", err) } - d, err := nodesToPtables(*ns, allMachines) + d, err := nodesToPtables(*namespace, allMachines) if err != nil { log.Fatalf("Error converting to table: %s", err) } @@ -163,7 +163,7 @@ var deleteNodeCmd = &cobra.Command{ }, } -var shareNodeCmd = &cobra.Command{ +var shareMachineCmd = &cobra.Command{ Use: "share ID namespace", Short: "Shares a node from the current namespace to the specified one", Args: func(cmd *cobra.Command, args []string) error { @@ -173,23 +173,23 @@ var shareNodeCmd = &cobra.Command{ return nil }, Run: func(cmd *cobra.Command, args []string) { - n, err := cmd.Flags().GetString("namespace") + namespace, err := cmd.Flags().GetString("namespace") if err != nil { log.Fatalf("Error getting namespace: %s", err) } - o, _ := cmd.Flags().GetString("output") + output, _ := cmd.Flags().GetString("output") h, err := getHeadscaleApp() if err != nil { log.Fatalf("Error initializing: %s", err) } - _, err = h.GetNamespace(n) + _, err = h.GetNamespace(namespace) if err != nil { log.Fatalf("Error fetching origin namespace: %s", err) } - destNs, err := h.GetNamespace(args[1]) + destinationNamespace, err := h.GetNamespace(args[1]) if err != nil { log.Fatalf("Error fetching destination namespace: %s", err) } @@ -198,12 +198,12 @@ var shareNodeCmd = &cobra.Command{ if err != nil { log.Fatalf("Error converting ID to integer: %s", err) } - m, err := h.GetMachineByID(uint64(id)) + machine, err := h.GetMachineByID(uint64(id)) if err != nil { log.Fatalf("Error getting node: %s", err) } - err = h.AddSharedMachineToNamespace(m, destNs) + err = h.AddSharedMachineToNamespace(machine, destinationNamespace) if strings.HasPrefix(o, "json") { JsonOutput(map[string]string{"Result": "Node shared"}, err, o) return @@ -217,10 +217,10 @@ var shareNodeCmd = &cobra.Command{ }, } -func nodesToPtables(currNs headscale.Namespace, m []headscale.Machine) (pterm.TableData, error) { +func nodesToPtables(currentNamespace headscale.Namespace, machines []headscale.Machine) (pterm.TableData, error) { d := pterm.TableData{{"ID", "Name", "NodeKey", "Namespace", "IP address", "Ephemeral", "Last seen", "Online"}} - for _, m := range m { + for _, machine := range machines { var ephemeral bool if m.AuthKey != nil && m.AuthKey.Ephemeral { ephemeral = true @@ -243,12 +243,12 @@ func nodesToPtables(currNs headscale.Namespace, m []headscale.Machine) (pterm.Ta } var namespace string - if currNs.ID == m.NamespaceID { - namespace = pterm.LightMagenta(m.Namespace.Name) + if currentNamespace.ID == machine.NamespaceID { + namespace = pterm.LightMagenta(machine.Namespace.Name) } else { - namespace = pterm.LightYellow(m.Namespace.Name) + namespace = pterm.LightYellow(machine.Namespace.Name) } - d = append(d, []string{strconv.FormatUint(m.ID, 10), m.Name, nodeKey.ShortString(), namespace, m.IPAddress, strconv.FormatBool(ephemeral), lastSeen.Format("2006-01-02 15:04:05"), online}) + d = append(d, []string{strconv.FormatUint(machine.ID, 10), machine.Name, nodeKey.ShortString(), namespace, machine.IPAddress, strconv.FormatBool(ephemeral), lastSeen.Format("2006-01-02 15:04:05"), online}) } return d, nil } diff --git a/machine.go b/machine.go index c5a8a2a9..40fabee6 100644 --- a/machine.go +++ b/machine.go @@ -98,8 +98,8 @@ func (m Machine) toNode(includeRoutes bool) (*tailcfg.Node, error) { } } - for _, aip := range routesStr { - ip, err := netaddr.ParseIPPrefix(aip) + for _, routeStr := range routesStr { + ip, err := netaddr.ParseIPPrefix(routeStr) if err != nil { return nil, err } @@ -183,9 +183,9 @@ func (h *Headscale) getPeers(m Machine) (*[]*tailcfg.Node, error) { } // We fetch here machines that are shared to the `Namespace` of the machine we are getting peers for - sharedNodes := []SharedMachine{} + sharedMachines := []SharedMachine{} if err := h.db.Preload("Namespace").Preload("Machine").Where("namespace_id = ?", - m.NamespaceID).Find(&sharedNodes).Error; err != nil { + m.NamespaceID).Find(&sharedMachines).Error; err != nil { return nil, err } @@ -197,8 +197,8 @@ func (h *Headscale) getPeers(m Machine) (*[]*tailcfg.Node, error) { } peers = append(peers, peer) } - for _, sn := range sharedNodes { - peer, err := sn.Machine.toNode(false) // shared nodes do not expose their routes + for _, sharedMachine := range sharedMachines { + peer, err := sharedMachine.Machine.toNode(false) // shared nodes do not expose their routes if err != nil { return nil, err } diff --git a/namespaces.go b/namespaces.go index e7d207b2..8204f96d 100644 --- a/namespaces.go +++ b/namespaces.go @@ -99,22 +99,22 @@ func (h *Headscale) ListMachinesInNamespace(name string) (*[]Machine, error) { // ListSharedMachinesInNamespace returns all the machines that are shared to the specified namespace func (h *Headscale) ListSharedMachinesInNamespace(name string) (*[]Machine, error) { - n, err := h.GetNamespace(name) + namespace, err := h.GetNamespace(name) if err != nil { return nil, err } - sharedNodes := []SharedMachine{} - if err := h.db.Preload("Namespace").Where(&SharedMachine{NamespaceID: n.ID}).Find(&sharedNodes).Error; err != nil { + sharedMachines := []SharedMachine{} + if err := h.db.Preload("Namespace").Where(&SharedMachine{NamespaceID: namespace.ID}).Find(&sharedMachines).Error; err != nil { return nil, err } machines := []Machine{} - for _, sn := range sharedNodes { - m, err := h.GetMachineByID(sn.MachineID) // otherwise not everything comes filled + for _, sharedMachine := range sharedMachines { + machine, err := h.GetMachineByID(sharedMachine.MachineID) // otherwise not everything comes filled if err != nil { return nil, err } - machines = append(machines, *m) + machines = append(machines, *machine) } return &machines, nil } diff --git a/sharing.go b/sharing.go index db16ef33..c507707a 100644 --- a/sharing.go +++ b/sharing.go @@ -21,17 +21,17 @@ func (h *Headscale) AddSharedMachineToNamespace(m *Machine, ns *Namespace) error } sn := SharedMachine{} - if err := h.db.Where("machine_id = ? AND namespace_id", m.ID, ns.ID).First(&sn).Error; err == nil { - return errorNodeAlreadyShared + if err := h.db.Where("machine_id = ? AND namespace_id", m.ID, ns.ID).First(&sharedMachine).Error; err == nil { + return errorMachineAlreadyShared } - sn = SharedMachine{ + sharedMachine = SharedMachine{ MachineID: m.ID, Machine: *m, NamespaceID: ns.ID, Namespace: *ns, } - h.db.Save(&sn) + h.db.Save(&sharedMachine) return nil } diff --git a/sharing_test.go b/sharing_test.go index 7c3ff828..ec4951de 100644 --- a/sharing_test.go +++ b/sharing_test.go @@ -183,7 +183,7 @@ func (s *Suite) TestAlreadyShared(c *check.C) { err = h.AddSharedMachineToNamespace(&m2, n1) c.Assert(err, check.IsNil) err = h.AddSharedMachineToNamespace(&m2, n1) - c.Assert(err, check.Equals, errorNodeAlreadyShared) + c.Assert(err, check.Equals, errorMachineAlreadyShared) } func (s *Suite) TestDoNotIncludeRoutesOnShared(c *check.C) {