-
Notifications
You must be signed in to change notification settings - Fork 19
/
instance_pool_list.go
107 lines (90 loc) · 2.56 KB
/
instance_pool_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
package cmd
import (
"fmt"
"strings"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
type instancePoolItem struct {
ID string `json:"id"`
Name string `json:"name"`
Zone string `json:"zone"`
Size int `json:"size"`
State egoscale.InstancePoolState `json:"state"`
}
type instancePoolListItemOutput []instancePoolItem
func (o *instancePoolListItemOutput) toJSON() { outputJSON(o) }
func (o *instancePoolListItemOutput) toText() { outputText(o) }
func (o *instancePoolListItemOutput) toTable() { outputTable(o) }
type instancePoolFetchResult struct {
instancePoolListItemOutput
error
}
var instancePoolListCmd = &cobra.Command{
Use: "list",
Short: "List instance pools",
Long: fmt.Sprintf(`This command lists instance pools.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&instancePoolItem{}), ", ")),
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
zoneFlag, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
zoneFlag = strings.ToLower(zoneFlag)
var zones []egoscale.Zone
if zoneFlag == "" {
resp, err := cs.Request(egoscale.ListZones{})
if err != nil {
return err
}
zones = resp.(*egoscale.ListZonesResponse).Zone
} else {
zone, err := getZoneByName(zoneFlag)
if err != nil {
return err
}
zones = append(zones, *zone)
}
results := make(chan instancePoolFetchResult, len(zones))
defer close(results)
for _, zone := range zones {
go getInstancePool(results, zone)
}
o := make(instancePoolListItemOutput, 0, len(zones))
for range zones {
result := <-results
if result.error != nil {
return err
}
o = append(o, result.instancePoolListItemOutput...)
}
return output(&o, nil)
},
}
func getInstancePool(result chan instancePoolFetchResult, zone egoscale.Zone) {
resp, err := cs.RequestWithContext(gContext, egoscale.ListInstancePools{
ZoneID: zone.ID,
})
if err != nil {
result <- instancePoolFetchResult{nil, err}
return
}
r := resp.(*egoscale.ListInstancePoolsResponse)
output := make(instancePoolListItemOutput, 0, r.Count)
for _, i := range r.InstancePools {
output = append(output, instancePoolItem{
ID: i.ID.String(),
Name: i.Name,
Zone: zone.Name,
Size: i.Size,
State: i.State,
})
}
result <- instancePoolFetchResult{output, nil}
}
func init() {
instancePoolListCmd.Flags().StringP("zone", "z", "", "List Instance pool by zone")
instancePoolCmd.AddCommand(instancePoolListCmd)
}