-
Notifications
You must be signed in to change notification settings - Fork 19
/
instance_type_list.go
117 lines (93 loc) · 2.83 KB
/
instance_type_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
106
107
108
109
110
111
112
113
114
115
116
117
package cmd
import (
"fmt"
"os"
"strings"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/table"
exoapi "github.com/exoscale/egoscale/v2/api"
)
type instanceTypeListItemOutput struct {
ID string `json:"id"`
Family string `json:"family"`
Size string `json:"name"`
CPUs int64 `json:"cpus"`
Memory int64 `json:"memory"`
Authorized bool `json:"authorized"`
}
type instanceTypeListOutput struct {
data []instanceTypeListItemOutput
verbose bool
}
func (o *instanceTypeListOutput) ToJSON() { output.JSON(o.data) }
func (o *instanceTypeListOutput) ToText() { output.Text(o.data) }
func (o *instanceTypeListOutput) ToTable() {
header := []string{"ID", "Family", "Size"}
if o.verbose {
header = append(header, "# CPUs", "Memory", "Authorized")
}
t := table.NewTable(os.Stdout)
t.SetHeader(header)
defer t.Render()
for _, p := range o.data {
cols := []string{p.ID, p.Family, p.Size}
if o.verbose {
cols = append(
cols,
fmt.Sprint(p.CPUs),
humanize.Bytes(uint64(p.Memory)),
fmt.Sprint(p.Authorized),
)
}
t.Append(cols)
}
}
type instanceTypeListCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"list"`
Verbose bool `cli-short:"v" cli-help:"show additional information about Compute instance types"`
}
func (c *instanceTypeListCmd) cmdAliases() []string { return nil }
func (c *instanceTypeListCmd) cmdShort() string { return "List Compute instance types" }
func (c *instanceTypeListCmd) cmdLong() string {
return fmt.Sprintf(`This command lists available Compute instance types.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&instanceTypeListItemOutput{}), ", "))
}
func (c *instanceTypeListCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *instanceTypeListCmd) cmdRun(_ *cobra.Command, _ []string) error {
ctx := exoapi.WithEndpoint(
gContext,
exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone),
)
instanceTypes, err := globalstate.EgoscaleClient.ListInstanceTypes(ctx, account.CurrentAccount.DefaultZone)
if err != nil {
return err
}
out := instanceTypeListOutput{
data: make([]instanceTypeListItemOutput, 0),
verbose: c.Verbose,
}
for _, t := range instanceTypes {
out.data = append(out.data, instanceTypeListItemOutput{
ID: *t.ID,
Family: *t.Family,
Size: *t.Size,
Memory: *t.Memory,
CPUs: *t.CPUs,
Authorized: *t.Authorized,
})
}
return c.outputFunc(&out, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(instanceTypeCmd, &instanceTypeListCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}