-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstorage.go
109 lines (100 loc) · 2.75 KB
/
storage.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
package cmd
import (
"bytes"
"context"
"fmt"
"strconv"
"github.com/gridscale/gsclient-go/v3"
"github.com/gridscale/gscloud/render"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// storageOperator is used for testing purpose,
// we can mock data return from the gsclient via interface.
type storageOperator interface {
GetStorageList(ctx context.Context) ([]gsclient.Storage, error)
DeleteStorage(ctx context.Context, id string) error
}
// Storage action enums
const (
storageListAction = iota
storageDeleteAction
)
// produceStorageCmdRunFunc takes an instance of a struct that implements `storageOperator`
// returns a `cmdRunFunc`
func produceStorageCmdRunFunc(o storageOperator, action int) cmdRunFunc {
switch action {
case storageListAction:
return func(cmd *cobra.Command, args []string) {
ctx := context.Background()
out := new(bytes.Buffer)
storages, err := o.GetStorageList(ctx)
if err != nil {
log.Fatalf("Couldn't get storage list: %s", err)
}
var storageinfo [][]string
if !jsonFlag {
heading := []string{"id", "name", "capacity", "changetime", "status"}
for _, stor := range storages {
fill := [][]string{
{
stor.Properties.ObjectUUID,
stor.Properties.Name,
strconv.FormatInt(int64(stor.Properties.Capacity), 10),
strconv.FormatInt(int64(stor.Properties.ChangeTime.Hour()), 10),
stor.Properties.Status,
},
}
storageinfo = append(storageinfo, fill...)
}
if quietFlag {
for _, info := range storageinfo {
fmt.Println(info[0])
}
return
}
render.Table(out, heading[:], storageinfo)
} else {
for _, storage := range storages {
render.AsJSON(out, storage)
}
}
fmt.Print(out)
}
case storageDeleteAction:
return func(cmd *cobra.Command, args []string) {
ctx := context.Background()
err := o.DeleteStorage(ctx, args[0])
if err != nil {
log.Fatalf("Removing Storage failed: %s", err)
}
}
default:
}
return nil
}
// initStorageCmd adds storage cmd to the root cmd
func initStorageCmd() {
var storageCmd = &cobra.Command{
Use: "storage",
Short: "Operations on storages",
Long: `List, create, or remove storages.`,
}
var storageLsCmd = &cobra.Command{
Use: "ls [flags]",
Aliases: []string{"list"},
Short: "List storages",
Long: `List storage objects.`,
Run: produceStorageCmdRunFunc(client, storageListAction),
}
var removeCmd = &cobra.Command{
Use: "rm [flags] [ID]",
Aliases: []string{"remove"},
Short: "Remove storage",
Long: `Remove an existing storage.`,
Args: cobra.ExactArgs(1),
Run: produceStorageCmdRunFunc(client, storageDeleteAction),
}
storageCmd.AddCommand(storageLsCmd, removeCmd)
rootCmd.AddCommand(storageCmd)
}