-
Notifications
You must be signed in to change notification settings - Fork 20
/
serviceoffering.go
95 lines (75 loc) · 2.23 KB
/
serviceoffering.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
package cmd
import (
"fmt"
"strings"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
const (
serviceOfferingHelp = "service offering NAME (micro|tiny|small|medium|large|extra-large|huge|mega|titan|jumbo)"
)
type serviceOfferingListItemOutput struct {
ID string `json:"id"`
Name string `json:"name"`
CPU string `json:"cpu"`
RAM string `json:"ram"`
}
type serviceOfferingListOutput []serviceOfferingListItemOutput
func (o *serviceOfferingListOutput) toJSON() { outputJSON(o) }
func (o *serviceOfferingListOutput) toText() { outputText(o) }
func (o *serviceOfferingListOutput) toTable() { outputTable(o) }
func init() {
vmCmd.AddCommand(&cobra.Command{
Use: "serviceoffering",
Short: "List available services offerings with details",
Long: fmt.Sprintf(`This command lists available Compute service offerings.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&serviceOfferingListOutput{}), ", ")),
RunE: func(cmd *cobra.Command, args []string) error {
return output(listServiceOfferings())
},
})
}
func listServiceOfferings() (outputter, error) {
serviceOffering, err := cs.ListWithContext(gContext, &egoscale.ServiceOffering{})
if err != nil {
return nil, err
}
out := serviceOfferingListOutput{}
for _, key := range serviceOffering {
so := key.(*egoscale.ServiceOffering)
ram := ""
if so.Memory > 1000 {
ram = fmt.Sprintf("%d GB", so.Memory>>10)
} else if so.Memory < 1000 {
ram = fmt.Sprintf("%d MB", so.Memory)
}
out = append(out, serviceOfferingListItemOutput{
ID: so.ID.String(),
Name: so.Name,
CPU: fmt.Sprintf("%d × %d MHz", so.CPUNumber, so.CPUSpeed),
RAM: ram,
})
}
return &out, nil
}
func getServiceOfferingByNameOrID(v string) (*egoscale.ServiceOffering, error) {
so := &egoscale.ServiceOffering{}
id, err := egoscale.ParseUUID(v)
if err != nil {
so.Name = v
} else {
so.ID = id
}
resp, err := cs.GetWithContext(gContext, so)
switch err {
case nil:
return resp.(*egoscale.ServiceOffering), nil
case egoscale.ErrNotFound:
return nil, fmt.Errorf("unknown Service Offering %q", v)
case egoscale.ErrTooManyFound:
return nil, fmt.Errorf("multiple Service Offerings match %q", v)
default:
return nil, err
}
}