-
Notifications
You must be signed in to change notification settings - Fork 19
/
blockstorage_show.go
88 lines (71 loc) · 2.78 KB
/
blockstorage_show.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
package cmd
import (
"fmt"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
v3 "github.com/exoscale/egoscale/v3"
)
type blockStorageShowOutput struct {
BlockStorageSnapshots []v3.BlockStorageSnapshotTarget `json:"block-storage-snapshots"`
Blocksize int64 `json:"blocksize"`
CreatedAT time.Time `json:"created-at"`
ID v3.UUID `json:"id"`
Instance *v3.InstanceTarget `json:"instance"`
Name string `json:"name"`
Size string `json:"size"`
State v3.BlockStorageVolumeState `json:"state"`
}
func (o *blockStorageShowOutput) Type() string { return "Block Storage Volume" }
func (o *blockStorageShowOutput) ToJSON() { output.JSON(o) }
func (o *blockStorageShowOutput) ToText() { output.Text(o) }
func (o *blockStorageShowOutput) ToTable() { output.Table(o) }
type blockStorageShowCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"show"`
Name string `cli-arg:"#" cli-usage:"NAME|ID"`
Zone v3.ZoneName `cli-short:"z" cli-usage:"block storage volume zone"`
}
func (c *blockStorageShowCmd) cmdAliases() []string { return gShowAlias }
func (c *blockStorageShowCmd) cmdShort() string { return "Show a Block Storage Volume details" }
func (c *blockStorageShowCmd) cmdLong() string {
return fmt.Sprintf(`This command shows a Block Storage Volume details.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&instanceShowOutput{}), ", "))
}
func (c *blockStorageShowCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *blockStorageShowCmd) cmdRun(cmd *cobra.Command, _ []string) error {
ctx := gContext
client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, c.Zone)
if err != nil {
return err
}
volumes, err := client.ListBlockStorageVolumes(ctx)
if err != nil {
return err
}
volume, err := volumes.FindBlockStorageVolume(c.Name)
if err != nil {
return err
}
return c.outputFunc(&blockStorageShowOutput{
ID: volume.ID,
Name: volume.Name,
Size: fmt.Sprintf("%d GiB", volume.Size),
Blocksize: volume.Blocksize,
CreatedAT: volume.CreatedAT,
State: volume.State,
Instance: volume.Instance,
BlockStorageSnapshots: volume.BlockStorageSnapshots,
}, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(blockstorageCmd, &blockStorageShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}