-
Notifications
You must be signed in to change notification settings - Fork 20
/
affinitygroup_list.go
57 lines (46 loc) · 1.47 KB
/
affinitygroup_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
package cmd
import (
"fmt"
"strings"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
type affinityGroupListItemOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
NumInstances int `json:"num_instances" outputLabel:"Instances"`
}
type affinityGroupListOutput []affinityGroupListItemOutput
func (o *affinityGroupListOutput) toJSON() { outputJSON(o) }
func (o *affinityGroupListOutput) toText() { outputText(o) }
func (o *affinityGroupListOutput) toTable() { outputTable(o) }
func init() {
affinitygroupCmd.AddCommand(&cobra.Command{
Use: "list",
Short: "List Anti-Affinity Groups",
Long: fmt.Sprintf(`This command lists existing Anti-Affinity Groups.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&affinityGroupListOutput{}), ", ")),
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
return output(listAffinityGroups())
},
})
}
func listAffinityGroups() (outputter, error) {
resp, err := cs.RequestWithContext(gContext, &egoscale.ListAffinityGroups{})
if err != nil {
return nil, err
}
out := affinityGroupListOutput{}
for _, ag := range resp.(*egoscale.ListAffinityGroupsResponse).AffinityGroup {
out = append(out, affinityGroupListItemOutput{
ID: ag.ID.String(),
Name: ag.Name,
Description: ag.Description,
NumInstances: len(ag.VirtualMachineIDs),
})
}
return &out, nil
}