-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (146 loc) · 3.47 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/urfave/cli/v2"
"gopkg.in/aristanetworks/go-cvprac.v2/client"
)
// Cleanup Generated Configlets
func cleanup(c *cli.Context) error {
fmt.Println("Grabbing Configlets")
cvpClient := c.App.Metadata["client"].(*client.CvpClient)
reader := bufio.NewReader(os.Stdin)
lets, err := cvpClient.API.GetConfiglets()
if err != nil {
log.Fatalf("ERROR: %s", err)
}
for _, let := range lets {
if let.ContainerCount != 0 && let.NetElementCount != 0 {
continue
}
if let.Type != "Generated" {
continue
}
// Confirm with user before deleting, unless noconfirm set
if c.Bool("noconfirm") == true {
cvpClient.API.DeleteConfiglet(let.Name, let.Key)
fmt.Printf("Deleted %s!\n", let.Name)
} else {
for {
fmt.Printf("Delete %s ? (y/n): ", let.Name)
response, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
response = strings.TrimSpace(response)
response = strings.ToLower(response)
if response == "y" || response == "yes" {
cvpClient.API.DeleteConfiglet(let.Name, let.Key)
fmt.Printf("Deleted %s!\n", let.Name)
break
} else if response == "n" || response == "no" {
fmt.Println("k then.")
break
}
}
}
}
fmt.Println("Done cleaning generated configlets.")
return nil
}
// Export ConfigletBuilder
func export(c *cli.Context) error {
cvpClient := c.App.Metadata["client"].(*client.CvpClient)
reader := bufio.NewReader(os.Stdin)
fmt.Print("Builder Name: ")
response, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
response = strings.TrimSpace(response)
builder, err := cvpClient.API.GetConfigletBuilderByName(response)
if err != nil {
log.Fatal(err)
}
fmt.Println(builder)
fout, _ := json.MarshalIndent(builder, "", " ")
_ = ioutil.WriteFile("test.json", fout, 0644)
return nil
}
// Login to CVP
func login(c *cli.Context) error {
ip := c.String("ip")
user := c.String("user")
password := c.String("password")
hosts := []string{ip}
cvpClient, _ := client.NewCvpClient(
client.Protocol("https"),
client.Port(443),
client.Hosts(hosts...),
client.Debug(false),
)
if err := cvpClient.Connect(user, password); err != nil {
log.Fatalf("ERROR: %s", err)
}
fmt.Println("Logged in.")
cvpinfo, err := cvpClient.API.GetCvpInfo()
if err != nil {
log.Fatal(err)
}
fmt.Printf("CVP Version: %s\n", cvpinfo.Version)
c.App.Metadata["client"] = cvpClient
return nil
}
// CLI Options
func main() {
app := &cli.App{
Name: "cvp-cleanup",
Usage: "CVP Configlet Cleanupper - Usage Instructions:",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "ip",
Aliases: []string{"i"},
Usage: "IP Address (or fqdn) of CloudVision",
Required: true,
},
&cli.StringFlag{
Name: "user",
Aliases: []string{"u"},
Usage: "Username for CloudVision",
Required: true,
},
&cli.StringFlag{
Name: "password",
Aliases: []string{"p"},
Usage: "Password for CloudVision",
Required: true,
},
&cli.BoolFlag{
Name: "noconfirm",
Usage: "Do not prompt for configlet deletions",
},
},
Commands: []*cli.Command{
{
Before: login,
Name: "cleanup",
Usage: "Cleanup unused configlets in CloudVision",
Action: cleanup,
},
{
Before: login,
Name: "export",
Usage: "Export ConfigletBuilder",
Action: export,
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}