-
Notifications
You must be signed in to change notification settings - Fork 19
/
instance_pool_list.go
105 lines (88 loc) · 2.69 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
package cmd
import (
"fmt"
"os"
"strings"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
type instancePoolListItemOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Zone string `json:"zone"`
Size int `json:"size"`
State egoscale.InstancePoolState `json:"state"`
}
type instancePoolListOutput []instancePoolListItemOutput
func (o *instancePoolListOutput) toJSON() { outputJSON(o) }
func (o *instancePoolListOutput) toText() { outputText(o) }
func (o *instancePoolListOutput) toTable() { outputTable(o) }
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(&instancePoolListItemOutput{}), ", ")),
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
zone = strings.ToLower(zone)
return output(listInstancePools(zone))
},
}
func listInstancePools(zone string) (outputter, error) {
var (
zonesIndex = make(map[string]egoscale.Zone)
instancePoolZones = make([]string, 0)
)
// We have to index existing zones per name in advance, as forEachZone()
// expects a zone name but we'll need the zone UUID to perform the CS-style
// calls in the callback function.
resp, err := cs.RequestWithContext(gContext, egoscale.ListZones{})
if err != nil {
return nil, err
}
for _, z := range resp.(*egoscale.ListZonesResponse).Zone {
if zone != "" && z.Name != zone {
continue
}
zonesIndex[z.Name] = z
instancePoolZones = append(instancePoolZones, z.Name)
}
out := make(instancePoolListOutput, 0)
res := make(chan instancePoolListItemOutput)
defer close(res)
go func() {
for instancePool := range res {
out = append(out, instancePool)
}
}()
err = forEachZone(instancePoolZones, func(zone string) error {
resp, err := cs.RequestWithContext(gContext, egoscale.ListInstancePools{ZoneID: zonesIndex[zone].ID})
if err != nil {
return err
}
for _, i := range resp.(*egoscale.ListInstancePoolsResponse).InstancePools {
res <- instancePoolListItemOutput{
ID: i.ID.String(),
Name: i.Name,
Zone: zone,
Size: i.Size,
State: i.State,
}
}
return nil
})
if err != nil {
fmt.Fprintf(os.Stderr,
"warning: errors during listing, results might be incomplete.\n%s\n", err) // nolint:golint
}
return &out, nil
}
func init() {
instancePoolListCmd.Flags().StringP("zone", "z", "", "Zone to filter results to")
instancePoolCmd.AddCommand(instancePoolListCmd)
}