|
1
|
+package device
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "fmt"
|
|
5
|
+
|
|
6
|
+ "gitlab.c3sl.ufpr.br/root/netboxadm/internal/models"
|
|
7
|
+ "gitlab.c3sl.ufpr.br/root/netboxadm/internal/nethuh"
|
|
8
|
+ "gitlab.c3sl.ufpr.br/root/netboxadm/internal/route"
|
|
9
|
+ "gitlab.c3sl.ufpr.br/root/netboxadm/pkg/rest"
|
|
10
|
+
|
|
11
|
+ "github.com/fatih/color"
|
|
12
|
+ "github.com/jedib0t/go-pretty/v6/list"
|
|
13
|
+ "github.com/spf13/cobra"
|
|
14
|
+ "github.com/spf13/viper"
|
|
15
|
+)
|
|
16
|
+
|
|
17
|
+var DeleteCmd = &cobra.Command{
|
|
18
|
+ Use: "delete",
|
|
19
|
+ Short: "Delete a Netbox device",
|
|
20
|
+ RunE: deleteDeviceCmd,
|
|
21
|
+}
|
|
22
|
+
|
|
23
|
+func deleteDeviceCmd(cmd *cobra.Command, args []string) error {
|
|
24
|
+ c := rest.NewClient(
|
|
25
|
+ viper.GetString("netbox.url"),
|
|
26
|
+ viper.GetString("netbox.token"),
|
|
27
|
+ )
|
|
28
|
+
|
|
29
|
+ selected, err := nethuh.SelectDevice(c)
|
|
30
|
+ if err != nil {
|
|
31
|
+ return err
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ color.Red("The following device will be deleted:")
|
|
35
|
+ printDevice(selected)
|
|
36
|
+ color.Yellow("\nPS: interfaces, ips, macs... will too, be deleted!\n\n")
|
|
37
|
+ ok := nethuh.Confirm("Are you sure?")
|
|
38
|
+ if !ok {
|
|
39
|
+ return fmt.Errorf("User Aborted")
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ err = rest.DELETE(c, route.Devices, selected.Id)
|
|
43
|
+ if err != nil {
|
|
44
|
+ return err
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ fmt.Println(color.GreenString("Device deleted sucessfully!"), color.RedString(">:)\n"))
|
|
48
|
+ return nil
|
|
49
|
+}
|
|
50
|
+
|
|
51
|
+func printDevice(d *models.Device) error {
|
|
52
|
+ l := list.NewWriter()
|
|
53
|
+ l.AppendItem("Device: " + d.Display)
|
|
54
|
+ l.Indent()
|
|
55
|
+ l.AppendItem("Type: " + d.DeviceType.Display)
|
|
56
|
+ l.AppendItem("Role: " + d.Role.Display)
|
|
57
|
+ l.AppendItem("Status: " + d.Status.Label)
|
|
58
|
+ if d.Serial != "" {
|
|
59
|
+ l.AppendItem("Serial: " + d.Serial)
|
|
60
|
+ }
|
|
61
|
+ if d.Description != "" {
|
|
62
|
+ l.AppendItem("Description: " + d.Description)
|
|
63
|
+ }
|
|
64
|
+ if d.Comments != "" {
|
|
65
|
+ l.AppendItem("Comments: " + d.Comments)
|
|
66
|
+ }
|
|
67
|
+ if d.PrimaryIp4 != nil {
|
|
68
|
+ l.AppendItem("IPv4: " + d.PrimaryIp4.Address)
|
|
69
|
+ }
|
|
70
|
+ if d.PrimaryIp6 != nil {
|
|
71
|
+ l.AppendItem("IPv6: " + d.PrimaryIp6.Address)
|
|
72
|
+ }
|
|
73
|
+ l.SetStyle(list.StyleConnectedRounded)
|
|
74
|
+ fmt.Println(l.Render())
|
|
75
|
+ return nil
|
|
76
|
+} |