-
Notifications
You must be signed in to change notification settings - Fork 20
/
instance_pool.go
62 lines (50 loc) · 1.28 KB
/
instance_pool.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
package cmd
import (
"fmt"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
var instancePoolCmd = &cobra.Command{
Use: "instancepool",
Short: "Instance Pools management",
}
func init() {
RootCmd.AddCommand(instancePoolCmd)
}
func getInstancePoolByID(id, zone *egoscale.UUID) (*egoscale.InstancePool, error) {
resp, err := cs.RequestWithContext(gContext, egoscale.GetInstancePool{
ID: id,
ZoneID: zone,
})
if err != nil {
return nil, err
}
r := resp.(*egoscale.GetInstancePoolResponse)
return &r.InstancePools[0], nil
}
func getInstancePoolByName(name string, zone *egoscale.UUID) (*egoscale.InstancePool, error) {
instancePools := []egoscale.InstancePool{}
id, err := egoscale.ParseUUID(name)
if err == nil {
return getInstancePoolByID(id, zone)
}
resp, err := cs.RequestWithContext(gContext, egoscale.ListInstancePools{
ZoneID: zone,
})
if err != nil {
return nil, err
}
r := resp.(*egoscale.ListInstancePoolsResponse)
for _, i := range r.InstancePools {
if i.Name == name {
instancePools = append(instancePools, i)
}
}
switch count := len(instancePools); {
case count == 0:
return nil, fmt.Errorf("not found: %q", name)
case count > 1:
return nil, fmt.Errorf(`more than one element found: %d`, count)
}
return &instancePools[0], nil
}