-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_arrays_cmd.go
278 lines (236 loc) · 8.35 KB
/
server_arrays_cmd.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2017-2021 Ingram Micro Inc.
package cmd
import (
"fmt"
"github.com/ingrammicro/cio/api/cloud"
"github.com/ingrammicro/cio/api/types"
"github.com/ingrammicro/cio/utils"
"github.com/ingrammicro/cio/utils/format"
"github.com/urfave/cli"
)
// WireUpServerArray prepares common resources to send request to Concerto API
func WireUpServerArray(c *cli.Context) (ds *cloud.ServerArrayService, f format.Formatter) {
f = format.GetFormatter()
config, err := utils.GetConcertoConfig()
if err != nil {
f.PrintFatal("Couldn't wire up config", err)
}
hcs, err := utils.NewHTTPConcertoService(config)
if err != nil {
f.PrintFatal("Couldn't wire up concerto service", err)
}
ds, err = cloud.NewServerArrayService(hcs)
if err != nil {
f.PrintFatal("Couldn't wire up server array service", err)
}
return ds, f
}
// ServerArrayList subcommand function
func ServerArrayList(c *cli.Context) error {
debugCmdFuncInfo(c)
serverArraySvc, formatter := WireUpServerArray(c)
serverArrays, err := serverArraySvc.ListServerArrays()
if err != nil {
formatter.PrintFatal("Couldn't receive server array data", err)
}
labelables := make([]types.Labelable, len(serverArrays))
for i := 0; i < len(serverArrays); i++ {
labelables[i] = types.Labelable(serverArrays[i])
}
labelIDsByName, labelNamesByID := LabelLoadsMapping(c)
filteredLabelables := LabelFiltering(c, labelables, labelIDsByName)
LabelAssignNamesForIDs(c, filteredLabelables, labelNamesByID)
serverArrays = make([]*types.ServerArray, len(filteredLabelables))
for i, labelable := range filteredLabelables {
sa, ok := labelable.(*types.ServerArray)
if !ok {
formatter.PrintFatal(LabelFilteringUnexpected,
fmt.Errorf("expected labelable to be a *types.ServerArray, got a %T", labelable))
}
serverArrays[i] = sa
}
if err = formatter.PrintList(serverArrays); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// ServerArrayShow subcommand function
func ServerArrayShow(c *cli.Context) error {
debugCmdFuncInfo(c)
serverArraySvc, formatter := WireUpServerArray(c)
checkRequiredFlags(c, []string{"id"}, formatter)
serverArray, err := serverArraySvc.GetServerArray(c.String("id"))
if err != nil {
formatter.PrintFatal("Couldn't receive server array data", err)
}
_, labelNamesByID := LabelLoadsMapping(c)
serverArray.FillInLabelNames(labelNamesByID)
if err = formatter.PrintItem(*serverArray); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// ServerArrayCreate subcommand function
func ServerArrayCreate(c *cli.Context) error {
debugCmdFuncInfo(c)
serverArraySvc, formatter := WireUpServerArray(c)
checkRequiredFlags(c, []string{"name", "template-id", "cloud-account-id", "server-plan-id"}, formatter)
serverArrayIn := map[string]interface{}{
"name": c.String("name"),
"template_id": c.String("template-id"),
"cloud_account_id": c.String("cloud-account-id"),
"server_plan_id": c.String("server-plan-id"),
}
if c.IsSet("size") {
serverArrayIn["size"] = c.Int("size")
}
if c.IsSet("firewall-profile-id") {
serverArrayIn["firewall_profile_id"] = c.String("firewall-profile-id")
}
if c.IsSet("ssh-profile-id") {
serverArrayIn["ssh_profile_id"] = c.String("ssh-profile-id")
}
if c.IsSet("subnet-id") {
serverArrayIn["subnet_id"] = c.String("subnet-id")
}
if c.IsSet("privateness") {
serverArrayIn["privateness"] = c.Bool("privateness")
}
labelIDsByName, labelNamesByID := LabelLoadsMapping(c)
if c.IsSet("labels") {
serverArrayIn["label_ids"] = LabelResolution(c, c.String("labels"), &labelNamesByID, &labelIDsByName)
}
serverArray, err := serverArraySvc.CreateServerArray(&serverArrayIn)
if err != nil {
formatter.PrintFatal("Couldn't create server array", err)
}
serverArray.FillInLabelNames(labelNamesByID)
if err = formatter.PrintItem(*serverArray); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// ServerArrayUpdate subcommand function
func ServerArrayUpdate(c *cli.Context) error {
debugCmdFuncInfo(c)
serverArraySvc, formatter := WireUpServerArray(c)
checkRequiredFlags(c, []string{"id"}, formatter)
serverArray, err := serverArraySvc.UpdateServerArray(c.String("id"), utils.FlagConvertParams(c))
if err != nil {
formatter.PrintFatal("Couldn't update server array", err)
}
_, labelNamesByID := LabelLoadsMapping(c)
serverArray.FillInLabelNames(labelNamesByID)
if err = formatter.PrintItem(*serverArray); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// ServerArrayBoot subcommand function
func ServerArrayBoot(c *cli.Context) error {
debugCmdFuncInfo(c)
serverArraySvc, formatter := WireUpServerArray(c)
checkRequiredFlags(c, []string{"id"}, formatter)
serverArray, err := serverArraySvc.BootServerArray(c.String("id"), utils.FlagConvertParams(c))
if err != nil {
formatter.PrintFatal("Couldn't boot server array", err)
}
_, labelNamesByID := LabelLoadsMapping(c)
serverArray.FillInLabelNames(labelNamesByID)
if err = formatter.PrintItem(*serverArray); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// ServerArrayShutdown subcommand function
func ServerArrayShutdown(c *cli.Context) error {
debugCmdFuncInfo(c)
serverArraySvc, formatter := WireUpServerArray(c)
checkRequiredFlags(c, []string{"id"}, formatter)
serverArray, err := serverArraySvc.ShutdownServerArray(c.String("id"), utils.FlagConvertParams(c))
if err != nil {
formatter.PrintFatal("Couldn't shutdown server array", err)
}
_, labelNamesByID := LabelLoadsMapping(c)
serverArray.FillInLabelNames(labelNamesByID)
if err = formatter.PrintItem(*serverArray); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// ServerArrayEmpty subcommand function
func ServerArrayEmpty(c *cli.Context) error {
debugCmdFuncInfo(c)
serverArraySvc, formatter := WireUpServerArray(c)
checkRequiredFlags(c, []string{"id"}, formatter)
serverArray, err := serverArraySvc.EmptyServerArray(c.String("id"), utils.FlagConvertParams(c))
if err != nil {
formatter.PrintFatal("Couldn't empty server array", err)
}
_, labelNamesByID := LabelLoadsMapping(c)
serverArray.FillInLabelNames(labelNamesByID)
if err = formatter.PrintItem(*serverArray); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// ServerArrayEnlarge subcommand function
func ServerArrayEnlarge(c *cli.Context) error {
debugCmdFuncInfo(c)
serverArraySvc, formatter := WireUpServerArray(c)
checkRequiredFlags(c, []string{"id", "size"}, formatter)
serverArrayEnlargeIn := map[string]interface{}{
"size_increase": c.Int("size"),
}
serverArray, err := serverArraySvc.EnlargeServerArray(c.String("id"), &serverArrayEnlargeIn)
if err != nil {
formatter.PrintFatal("Couldn't enlarge server array", err)
}
_, labelNamesByID := LabelLoadsMapping(c)
serverArray.FillInLabelNames(labelNamesByID)
if err = formatter.PrintItem(*serverArray); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// ServerArrayServerList subcommand function
func ServerArrayServerList(c *cli.Context) error {
debugCmdFuncInfo(c)
serverArraySvc, formatter := WireUpServerArray(c)
checkRequiredFlags(c, []string{"id"}, formatter)
servers, err := serverArraySvc.ListServerArrayServers(c.String("id"))
if err != nil {
formatter.PrintFatal("Couldn't receive server data", err)
}
labelables := make([]types.Labelable, len(servers))
for i := 0; i < len(servers); i++ {
labelables[i] = types.Labelable(servers[i])
}
labelIDsByName, labelNamesByID := LabelLoadsMapping(c)
filteredLabelables := LabelFiltering(c, labelables, labelIDsByName)
LabelAssignNamesForIDs(c, filteredLabelables, labelNamesByID)
servers = make([]*types.Server, len(filteredLabelables))
for i, labelable := range filteredLabelables {
s, ok := labelable.(*types.Server)
if !ok {
formatter.PrintFatal(LabelFilteringUnexpected,
fmt.Errorf("expected labelable to be a *types.Server, got a %T", labelable))
}
servers[i] = s
}
if err = formatter.PrintList(servers); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// ServerArrayDelete subcommand function
func ServerArrayDelete(c *cli.Context) error {
debugCmdFuncInfo(c)
serverArraySvc, formatter := WireUpServerArray(c)
checkRequiredFlags(c, []string{"id"}, formatter)
err := serverArraySvc.DeleteServerArray(c.String("id"))
if err != nil {
formatter.PrintFatal("Couldn't delete server array", err)
}
return nil
}