-
Notifications
You must be signed in to change notification settings - Fork 2
/
command_servers.go
260 lines (226 loc) · 6.53 KB
/
command_servers.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main
import (
"fmt"
"github.com/1and1/soma/internal/help"
"github.com/1and1/soma/internal/adm"
"github.com/1and1/soma/internal/cmpl"
"github.com/1and1/soma/lib/proto"
"github.com/codegangsta/cli"
)
func registerServers(app cli.App) *cli.App {
app.Commands = append(app.Commands,
[]cli.Command{
// servers
{
Name: "servers",
Usage: "SUBCOMMANDS for servers",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "Create a new physical server",
Description: help.CmdServerCreate,
Action: runtime(cmdServerCreate),
BashComplete: cmpl.ServerCreate,
},
{
Name: "delete",
Usage: "Mark an existing physical server as deleted",
Action: runtime(cmdServerMarkAsDeleted),
},
/*
{
Name: "purge",
Usage: "Remove all unreferenced servers marked as deleted",
Action: runtime(cmdServerPurgeDeleted),
Flags: []cli.Flag{
cli.BoolFlag{
Name: "all, a",
Usage: "Purge all deleted servers",
},
},
},
*/
{
Name: "update",
Usage: "Full update of server attributes (replace, not merge)",
Description: help.CmdServerUpdate,
Action: runtime(cmdServerUpdate),
BashComplete: cmpl.ServerUpdate,
},
/*
{
Name: "rename",
Usage: "Rename an existing server",
Action: runtime(cmdServerRename),
},
{
Name: "online",
Usage: "Set an existing server to online",
Action: runtime(cmdServerOnline),
},
{
Name: "offline",
Usage: "Set an existing server to offline",
Action: runtime(cmdServerOffline),
},
{
Name: "move",
Usage: "Change a server's registered location",
Action: runtime(cmdServerMove),
},
*/
{
Name: "list",
Usage: "List all servers, see full description for possible filters",
Action: runtime(cmdServerList),
},
{
Name: "synclist",
Usage: "Export a list of all servers suitable for sync",
Action: runtime(cmdServerSync),
},
{
Name: "show",
Usage: "Show details about a specific server",
Action: runtime(cmdServerShow),
},
/*
{
Name: "sync",
Usage: "Request a data sync for a server",
Action: runtime(cmdServerSyncRequest),
},
*/
{
Name: "null",
Usage: "Bootstrap the null server",
Action: runtime(cmdServerNull),
BashComplete: cmpl.Datacenter,
},
},
}, // end servers
}...,
)
return &app
}
func cmdServerCreate(c *cli.Context) error {
utl.ValidateCliMinArgumentCount(c, 7)
multiple := []string{}
unique := []string{`assetid`, `datacenter`, `location`, `online`}
required := []string{`assetid`, `datacenter`, `location`}
opts := utl.ParseVariadicArguments(multiple, unique, required,
c.Args().Tail())
req := proto.NewServerRequest()
req.Server.Name = c.Args().First()
req.Server.AssetId = utl.GetValidatedUint64(opts[`assetid`][0], 1)
req.Server.Datacenter = opts[`datacenter`][0]
req.Server.Location = opts[`location`][0]
// optional argument: online
if ov, ok := opts[`online`]; ok {
req.Server.IsOnline = utl.GetValidatedBool(ov[0])
} else {
// online defaults to true
req.Server.IsOnline = true
}
resp := utl.PostRequestWithBody(Client, req, `/servers/`)
fmt.Println(resp)
return nil
}
func cmdServerMarkAsDeleted(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 1)
sid := utl.TryGetServerByUUIDOrName(&store, Client, c.Args().First())
path := fmt.Sprintf("/servers/%d", sid)
resp := utl.DeleteRequest(Client, path)
fmt.Println(resp)
return nil
}
func cmdServerPurgeDeleted(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 1)
// TODO this will currently never return a deleted server
sid := utl.TryGetServerByUUIDOrName(&store, Client, c.Args().First())
path := fmt.Sprintf("/servers/%d", sid)
req := proto.NewServerRequest()
req.Flags.Purge = true
resp := utl.DeleteRequestWithBody(Client, req, path)
fmt.Println(resp)
return nil
}
func cmdServerUpdate(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 13)
if !utl.IsUUID(c.Args().First()) {
utl.Abort(
fmt.Sprintf("Server to update not referenced by UUID: %s",
c.Args().First()))
}
multiple := []string{}
unique := []string{`name`, `assetid`, `datacenter`, `location`, `online`, `deleted`}
required := []string{`name`, `assetid`, `datacenter`, `location`, `online`, `deleted`}
opts := utl.ParseVariadicArguments(multiple, unique, required,
c.Args().Tail())
req := proto.NewServerRequest()
req.Server.Id = c.Args().First()
req.Server.Name = opts[`name`][0]
req.Server.AssetId = utl.GetValidatedUint64(opts[`assetid`][0], 1)
req.Server.Datacenter = opts[`datacenter`][0]
req.Server.Location = opts[`location`][0]
req.Server.IsOnline = utl.GetValidatedBool(opts[`online`][0])
req.Server.IsDeleted = utl.GetValidatedBool(opts[`deleted`][0])
path := fmt.Sprintf("/servers/%s", c.Args().First())
resp := utl.PutRequestWithBody(Client, req, path)
fmt.Println(resp)
return nil
}
func cmdServerRename(c *cli.Context) error {
return nil
}
func cmdServerOnline(c *cli.Context) error {
return nil
}
func cmdServerOffline(c *cli.Context) error {
return nil
}
func cmdServerMove(c *cli.Context) error {
return nil
}
func cmdServerList(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 0)
resp, err := adm.GetReq(`/servers/`)
if err != nil {
return err
}
fmt.Println(resp)
return nil
}
func cmdServerSync(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 0)
resp, err := adm.GetReq(`/sync/servers/`)
if err != nil {
return err
}
fmt.Println(resp)
return nil
}
func cmdServerShow(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 1)
serverId := utl.TryGetServerByUUIDOrName(&store, Client, c.Args().First())
path := fmt.Sprintf("/servers/%s", serverId)
resp := utl.GetRequest(Client, path)
fmt.Println(resp)
return nil
}
func cmdServerSyncRequest(c *cli.Context) error {
return nil
}
func cmdServerNull(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 2)
key := []string{"datacenter"}
opts := utl.ParseVariadicArguments(key, key, key, c.Args())
req := proto.Request{}
req.Server = &proto.Server{}
req.Server.Id = "00000000-0000-0000-0000-000000000000"
req.Server.Datacenter = opts["datacenter"][0]
resp := utl.PostRequestWithBody(Client, req, "/servers/null")
fmt.Println(resp)
return nil
}
// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix