-
Notifications
You must be signed in to change notification settings - Fork 7
/
volume.go
383 lines (359 loc) · 12.1 KB
/
volume.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
Copyright © (2020-2021) Bizfly Cloud
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/bizflycloud/bizflyctl/formatter"
"github.com/bizflycloud/gobizfly"
"github.com/spf13/cobra"
)
var (
volumeHeaderList = []string{"ID", "Name", "Description", "Status", "Size", "Created At", "Volume Type", "Snapshot ID", "Billing Plan", "Zone", "Attached Server"}
volumeTypeHeaderList = []string{"Type", "Category", "Availability Zones"}
volumeName string
volumeSize int
volumeType string
volumeCategory string
volumeBillingPlan string
serverID string
category string
)
// volumeCmd represents the volume command
var volumeCmd = &cobra.Command{
Use: "volume",
Short: "Bizfly Cloud Volume Interaction",
Long: `Bizfly Cloud Volume Action: Create, List, Delete, Extend Volume`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("volume called")
},
}
// volumeDeleteCmd represents the delete command
var volumeDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete volume",
Long: `Delete volume with volume ID as input
Example: bizfly volume delete fd554aac-9ab1-11ea-b09d-bbaf82f02f58
You can delete multiple volumes with list of volume ID
Example: bizfly volume delete fd554aac-9ab1-11ea-b09d-bbaf82f02f58 f5869e9c-9ab2-11ea-b9e3-e353a4f04836`,
Run: func(cmd *cobra.Command, args []string) {
client, ctx := getApiClient(cmd)
for _, volumeID := range args {
fmt.Printf("Deleting volume %s \n", volumeID)
err := client.Volume.Delete(ctx, volumeID)
if err != nil {
if errors.Is(err, gobizfly.ErrNotFound) {
fmt.Printf("Volume %s is not found", volumeID)
return
}
}
}
},
}
// volumeGetCmd represents the get command
var volumeGetCmd = &cobra.Command{
Use: "get",
Short: "Get detail a volume",
Long: `Get detail a volume in your account
Example: bizfly volume get 9e580b1a-0526-460b-9a6f-d8f80130bda8
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
fmt.Printf("Unknow variable %s", strings.Join(args[1:], ""))
}
client, ctx := getApiClient(cmd)
volume, err := client.Volume.Get(ctx, args[0])
if err != nil {
if errors.Is(err, gobizfly.ErrNotFound) {
fmt.Printf("Volume %s not found.", args[0])
return
}
log.Fatal(err)
}
var data [][]string
serverID := ""
if (len(volume.Attachments)) > 0 {
serverID = volume.Attachments[0].ServerID
}
data = append(data, []string{volume.ID, volume.Name, volume.Description, volume.Status,
strconv.Itoa(volume.Size), volume.CreatedAt, volume.VolumeType, volume.SnapshotID, volume.BillingPlan,
volume.AvailabilityZone, serverID})
formatter.Output(volumeHeaderList, data)
},
}
// volumeListCmd represents the list command
var volumeListCmd = &cobra.Command{
Use: "list",
Short: "List all volumes in your account",
Long: `List all volumes in your Bizfly Cloud account
Example: bizfly volume list
`,
Run: func(cmd *cobra.Command, args []string) {
client, ctx := getApiClient(cmd)
volumes, err := client.Volume.List(ctx, &gobizfly.VolumeListOptions{})
if err != nil {
log.Fatal(err)
}
var data [][]string
for _, volume := range volumes {
serverID := ""
if (len(volume.Attachments)) > 0 {
serverID = volume.Attachments[0].ServerID
}
data = append(data, []string{volume.ID, volume.Name, volume.Description, volume.Status,
strconv.Itoa(volume.Size), volume.CreatedAt, volume.VolumeType, volume.SnapshotID, volume.BillingPlan,
volume.AvailabilityZone, serverID})
}
formatter.Output(volumeHeaderList, data)
},
}
// volumeCreateCmd represents the create command
var volumeCreateCmd = &cobra.Command{
Use: "create",
Short: "Create a new volume",
Long: `
Create a new volume
Use: bizfly volume create
`,
Run: func(cmd *cobra.Command, args []string) {
client, ctx := getApiClient(cmd)
vcr := gobizfly.VolumeCreateRequest{
Name: volumeName,
Size: volumeSize,
VolumeType: volumeType,
SnapshotID: snapshotID,
ServerID: serverID,
AvailabilityZone: availabilityZone,
VolumeCategory: volumeCategory,
Description: description,
BillingPlan: volumeBillingPlan,
}
volume, err := client.Volume.Create(ctx, &vcr)
if err != nil {
fmt.Printf("Create a new volume error: %v", err)
os.Exit(1)
}
var data [][]string
serverID := ""
if (len(volume.Attachments)) > 0 {
serverID = volume.Attachments[0].ServerID
}
data = append(data, []string{volume.ID, volume.Name, volume.Description, volume.Status,
strconv.Itoa(volume.Size), volume.CreatedAt, volume.VolumeType, volume.SnapshotID, volume.BillingPlan,
volume.AvailabilityZone, serverID})
formatter.Output(volumeHeaderList, data)
},
}
// volumeAttachCmd represents the volume attach command
var volumeAttachCmd = &cobra.Command{
Use: "attach",
Short: "Attach a volume to a server",
Long: `
Attach a volume to a server
Use: bizfly volume attach <volume-id> <server-id>
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 2 {
fmt.Println("Command error: use bizfly volume attach <volume-id> <server-id>")
os.Exit(1)
}
volumeID := args[0]
if volumeID == "" {
fmt.Println("You need to specify volume-id in the command")
os.Exit(1)
}
serverID := args[1]
if serverID == "" {
fmt.Println("You need to specify server-id in the command")
os.Exit(1)
}
client, ctx := getApiClient(cmd)
res, err := client.Volume.Attach(ctx, volumeID, serverID)
if err != nil {
fmt.Printf("Attach a volume to a server error: %v", err)
os.Exit(1)
}
fmt.Println(res.Message)
},
}
// volumeDetachCmd represents the volume detach command
var volumeDetachCmd = &cobra.Command{
Use: "detach",
Short: "Detach a volume from a server",
Long: `
Detach a volume from a server
Use: bizfly volume detach <volume-id> <server-id>
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 2 {
fmt.Println("Command error: use bizfly volume attach <volume-id> <server-id>")
os.Exit(1)
}
volumeID := args[0]
if volumeID == "" {
fmt.Println("You need to specify volume-id in the command")
os.Exit(1)
}
serverID := args[1]
if serverID == "" {
fmt.Println("You need to specify server-id in the command")
os.Exit(1)
}
client, ctx := getApiClient(cmd)
res, err := client.Volume.Detach(ctx, volumeID, serverID)
if err != nil {
fmt.Printf("Detach a volume from a server error: %v", err)
os.Exit(1)
}
fmt.Println(res.Message)
},
}
// extendVolumeCmd represent the resize volume command
var extendVolumeCmd = &cobra.Command{
Use: "extend",
Short: "Extend size of a volume",
Long: `
Extend size of a volume
Use: bizfly volume extend <volume-id> --size <new-size>
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("You need to specify the volume-id in the command. Use: bizfly volume extend <volume-id> --size <new size>")
os.Exit(1)
}
volumeID := args[0]
client, ctx := getApiClient(cmd)
_, err := client.Volume.ExtendVolume(ctx, volumeID, volumeSize)
if err != nil {
fmt.Printf("Extend volume error: %v\n", err)
}
fmt.Printf("Extending volume %v\n", volumeID)
},
}
var restoreVolumeCmd = &cobra.Command{
Use: "restore",
Short: "Restore volume by using its snapshot",
Long: `
Restore volume by using its snapshot
Use: bizfly volume restore <volume-id> --snapshot-id <snapshot-id>
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("You need to specify the volume-id in the command. Use: bizfly volume restore <volume-id> --snapshot-id <snapshot-id>")
os.Exit(1)
}
volumeID := args[0]
client, ctx := getApiClient(cmd)
_, err := client.Volume.Restore(ctx, volumeID, snapshotID)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Restoring volume %s using snapshot %s", volumeID, snapshotID)
},
}
var patchVolumeCmd = &cobra.Command{
Use: "patch",
Short: "Patch Volume",
Long: `
Patch volume
Use: bizfly volume patch <volume-id> [--name <vol_name>] [--description <description>]`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
log.Fatal("You need to specify the volume-id in the command. Use: bizfly volume patch <volume-id> [--name <vol_name>] [--description <description>]")
}
volumeID := args[0]
client, ctx := getApiClient(cmd)
req := &gobizfly.VolumePatchRequest{}
req.Description = description
volume, err := client.Volume.Patch(ctx, volumeID, req)
if err != nil {
log.Fatal(err)
}
var data [][]string
serverID := ""
if (len(volume.Attachments)) > 0 {
serverID = volume.Attachments[0].ServerID
}
data = append(data, []string{volume.ID, volume.Name, volume.Description, volume.Status,
strconv.Itoa(volume.Size), volume.CreatedAt, volume.VolumeType, volume.SnapshotID, volume.BillingPlan,
volume.AvailabilityZone, serverID})
formatter.Output(volumeHeaderList, data)
},
}
var listVolumeTypesCmd = &cobra.Command{
Use: "list-types",
Short: "List volume types",
Long: `
List volume types
Use: bizfly volume list-types --category <category> --availability-zone <availability-zone>`,
Run: func(cmd *cobra.Command, args []string) {
client, ctx := getApiClient(cmd)
opts := &gobizfly.ListVolumeTypesOptions{}
if category != "" {
opts.Category = category
}
if availabilityZone != "" {
opts.AvailabilityZone = availabilityZone
}
volumeTypes, err := client.Volume.ListVolumeTypes(ctx, opts)
if err != nil {
log.Fatal(err)
}
var data [][]string
for _, volumeType := range volumeTypes {
data = append(data, []string{volumeType.Type, volumeType.Category,
strings.Join(volumeType.AvailabilityZones, ",")})
}
formatter.Output(volumeTypeHeaderList, data)
},
}
func init() {
rootCmd.AddCommand(volumeCmd)
volumeCmd.AddCommand(volumeListCmd)
volumeCmd.AddCommand(volumeGetCmd)
volumeCmd.AddCommand(volumeDeleteCmd)
vspf := restoreVolumeCmd.PersistentFlags()
vspf.StringVar(&snapshotID, "snapshot-id", "", "Restore volume by using snapshot")
_ = cobra.MarkFlagRequired(vspf, "snapshot-id")
volumeCmd.AddCommand(restoreVolumeCmd)
vcpf := volumeCreateCmd.PersistentFlags()
vcpf.StringVar(&volumeName, "name", "", "Volume name")
_ = cobra.MarkFlagRequired(vcpf, "name")
vcpf.StringVar(&description, "description", "", "Volume description")
vcpf.StringVar(&volumeType, "type", "HDD", "Volume type: SSD or HDD.")
vcpf.StringVar(&volumeCategory, "category", "premium", "Volume category: premium, enterprise or basic.")
vcpf.IntVar(&volumeSize, "size", 0, "Volume size")
_ = cobra.MarkFlagRequired(vcpf, "size")
vcpf.StringVar(&availabilityZone, "availability-zone", "HN1", "Avaialability Zone of volume.")
vcpf.StringVar(&snapshotID, "snapshot-id", "", "Create a volume from a snapshot")
vcpf.StringVar(&serverID, "server-id", "", "Create a new volume and attach to a server")
vcpf.StringVar(&volumeBillingPlan, "billing-plan", "saving_plan", "Billing plan of volume: saving_plan, on_demand")
volumeCmd.AddCommand(volumeCreateCmd)
volumeCmd.AddCommand(volumeAttachCmd)
volumeCmd.AddCommand(volumeDetachCmd)
extendVolumeCmd.PersistentFlags().IntVar(&volumeSize, "size", 0, "Volume size")
_ = cobra.MarkFlagRequired(extendVolumeCmd.PersistentFlags(), "size")
volumeCmd.AddCommand(extendVolumeCmd)
pvpf := patchVolumeCmd.PersistentFlags()
pvpf.StringVar(&description, "description", "", "Patched volume description")
_ = cobra.MarkFlagRequired(pvpf, "description")
volumeCmd.AddCommand(patchVolumeCmd)
volumeCmd.AddCommand(listVolumeTypesCmd)
lvtpf := listVolumeTypesCmd.PersistentFlags()
lvtpf.StringVar(&category, "category", "", "Volume category")
lvtpf.StringVar(&availabilityZone, "availability-zone", "", "Availability Zone of volume.")
}