-
Notifications
You must be signed in to change notification settings - Fork 20
/
blockstorage_list.go
105 lines (81 loc) · 2.7 KB
/
blockstorage_list.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
package cmd
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
v3 "github.com/exoscale/egoscale/v3"
)
type blockStorageListItemOutput struct {
ID v3.UUID `json:"id"`
Name string `json:"name"`
Zone v3.ZoneName `json:"zone"`
Size string `json:"size"`
State v3.BlockStorageVolumeState `json:"state"`
}
type blockStorageListOutput []blockStorageListItemOutput
func (o *blockStorageListOutput) ToJSON() { output.JSON(o) }
func (o *blockStorageListOutput) ToText() { output.Text(o) }
func (o *blockStorageListOutput) ToTable() { output.Table(o) }
type blockStorageListCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"list"`
Zone v3.ZoneName `cli-short:"z" cli-usage:"zone to filter results to"`
}
func (c *blockStorageListCmd) cmdAliases() []string { return gListAlias }
func (c *blockStorageListCmd) cmdShort() string { return "List Block Storage Volumes" }
func (c *blockStorageListCmd) cmdLong() string {
return fmt.Sprintf(`This command lists Block Storage Volumes.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&blockStorageListOutput{}), ", "))
}
func (c *blockStorageListCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *blockStorageListCmd) cmdRun(_ *cobra.Command, _ []string) error {
client := globalstate.EgoscaleV3Client
ctx := gContext
resp, err := client.ListZones(ctx)
if err != nil {
return err
}
zones := resp.Zones
if c.Zone != "" {
endpoint, err := client.GetZoneAPIEndpoint(ctx, c.Zone)
if err != nil {
return err
}
zones = []v3.Zone{{APIEndpoint: endpoint}}
}
output := make(blockStorageListOutput, 0)
for _, zone := range zones {
c := client.WithEndpoint(zone.APIEndpoint)
resp, err := c.ListBlockStorageVolumes(ctx)
if err != nil {
// TODO(pej): remove it once Block Storage is deployed in every zone.
if strings.Contains(err.Error(), "Availability of the block storage volumes") {
continue
}
_, _ = fmt.Fprintf(os.Stderr,
"warning: errors during listing, results might be incomplete.\n%s\n", err) // nolint:golint
continue
}
for _, volume := range resp.BlockStorageVolumes {
output = append(output, blockStorageListItemOutput{
ID: volume.ID,
Name: volume.Name,
Zone: zone.Name,
Size: fmt.Sprintf("%d GiB", volume.Size),
State: volume.State,
})
}
}
return c.outputFunc(&output, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(blockstorageCmd, &blockStorageListCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}