mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-30 13:35:23 +00:00
Implemented delete nodes (#52)
This commit is contained in:
parent
f3139d26c8
commit
9ca2ae7fc5
@ -3,6 +3,7 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -71,7 +72,7 @@ var ListNodesCmd = &cobra.Command{
|
|||||||
log.Fatalf("Error getting nodes: %s", err)
|
log.Fatalf("Error getting nodes: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("name\t\tlast seen\t\tephemeral\n")
|
fmt.Printf("ID\tname\t\tlast seen\t\tephemeral\n")
|
||||||
for _, m := range *machines {
|
for _, m := range *machines {
|
||||||
var ephemeral bool
|
var ephemeral bool
|
||||||
if m.AuthKey != nil && m.AuthKey.Ephemeral {
|
if m.AuthKey != nil && m.AuthKey.Ephemeral {
|
||||||
@ -81,8 +82,38 @@ var ListNodesCmd = &cobra.Command{
|
|||||||
if m.LastSeen != nil {
|
if m.LastSeen != nil {
|
||||||
lastSeen = *m.LastSeen
|
lastSeen = *m.LastSeen
|
||||||
}
|
}
|
||||||
fmt.Printf("%s\t%s\t%t\n", m.Name, lastSeen.Format("2006-01-02 15:04:05"), ephemeral)
|
fmt.Printf("%d\t%s\t%s\t%t\n", m.ID, m.Name, lastSeen.Format("2006-01-02 15:04:05"), ephemeral)
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var DeleteCmd = &cobra.Command{
|
||||||
|
Use: "delete ID",
|
||||||
|
Short: "Delete a node",
|
||||||
|
Args: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) < 1 {
|
||||||
|
return fmt.Errorf("Missing parameters")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
h, err := getHeadscaleApp()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error initializing: %s", err)
|
||||||
|
}
|
||||||
|
id, err := strconv.Atoi(args[0])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error converting ID to integer: %s", err)
|
||||||
|
}
|
||||||
|
m, err := h.GetMachineByID(uint64(id))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error getting node: %s", err)
|
||||||
|
}
|
||||||
|
err = h.DeleteMachine(m)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error deleting node: %s", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("Node deleted\n")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
@ -73,6 +73,7 @@ func main() {
|
|||||||
|
|
||||||
cli.NodeCmd.AddCommand(cli.ListNodesCmd)
|
cli.NodeCmd.AddCommand(cli.ListNodesCmd)
|
||||||
cli.NodeCmd.AddCommand(cli.RegisterCmd)
|
cli.NodeCmd.AddCommand(cli.RegisterCmd)
|
||||||
|
cli.NodeCmd.AddCommand(cli.DeleteCmd)
|
||||||
|
|
||||||
cli.RoutesCmd.AddCommand(cli.ListRoutesCmd)
|
cli.RoutesCmd.AddCommand(cli.ListRoutesCmd)
|
||||||
cli.RoutesCmd.AddCommand(cli.EnableRouteCmd)
|
cli.RoutesCmd.AddCommand(cli.EnableRouteCmd)
|
||||||
|
@ -198,7 +198,7 @@ func (h *Headscale) GetMachineByID(id uint64) (*Machine, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DeleteMachine softs deletes a Machine from the database
|
// DeleteMachine softs deletes a Machine from the database
|
||||||
func (h *Headscale) DeleteMachine(m Machine) error {
|
func (h *Headscale) DeleteMachine(m *Machine) error {
|
||||||
if err := h.db.Delete(&m).Error; err != nil {
|
if err := h.db.Delete(&m).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -206,7 +206,7 @@ func (h *Headscale) DeleteMachine(m Machine) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// HardDeleteMachine hard deletes a Machine from the database
|
// HardDeleteMachine hard deletes a Machine from the database
|
||||||
func (h *Headscale) HardDeleteMachine(m Machine) error {
|
func (h *Headscale) HardDeleteMachine(m *Machine) error {
|
||||||
if err := h.db.Unscoped().Delete(&m).Error; err != nil {
|
if err := h.db.Unscoped().Delete(&m).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ func (s *Suite) TestDeleteMachine(c *check.C) {
|
|||||||
AuthKeyID: uint(1),
|
AuthKeyID: uint(1),
|
||||||
}
|
}
|
||||||
h.db.Save(&m)
|
h.db.Save(&m)
|
||||||
h.DeleteMachine(m)
|
h.DeleteMachine(&m)
|
||||||
_, err = h.GetMachine(n.Name, "testmachine")
|
_, err = h.GetMachine(n.Name, "testmachine")
|
||||||
c.Assert(err, check.NotNil)
|
c.Assert(err, check.NotNil)
|
||||||
}
|
}
|
||||||
@ -99,7 +99,7 @@ func (s *Suite) TestHardDeleteMachine(c *check.C) {
|
|||||||
AuthKeyID: uint(1),
|
AuthKeyID: uint(1),
|
||||||
}
|
}
|
||||||
h.db.Save(&m)
|
h.db.Save(&m)
|
||||||
h.HardDeleteMachine(m)
|
h.HardDeleteMachine(&m)
|
||||||
_, err = h.GetMachine(n.Name, "testmachine3")
|
_, err = h.GetMachine(n.Name, "testmachine3")
|
||||||
c.Assert(err, check.NotNil)
|
c.Assert(err, check.NotNil)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user