-
Notifications
You must be signed in to change notification settings - Fork 20
/
affinitygroup_list.go
59 lines (48 loc) · 1.59 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
58
59
package cmd
import (
"fmt"
"strings"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"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() { output.JSON(o) }
func (o *affinityGroupListOutput) ToText() { output.Text(o) }
func (o *affinityGroupListOutput) ToTable() { output.Table(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(output.TemplateAnnotations(&affinityGroupListOutput{}), ", ")),
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
return printOutput(listAffinityGroups())
},
})
}
func listAffinityGroups() (output.Outputter, error) {
resp, err := globalstate.EgoscaleClient.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
}