-
Notifications
You must be signed in to change notification settings - Fork 19
/
eip_list.go
85 lines (73 loc) · 1.62 KB
/
eip_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
package cmd
import (
"fmt"
"os"
"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// listCmd represents the list command
var eipListCmd = &cobra.Command{
Use: "list",
Short: "List elastic IP",
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
table := table.NewTable(os.Stdout)
table.SetHeader([]string{"zone", "IP", "ID", "Associated Virtual machine"})
if err := listIPs(zone, table); err != nil {
return err
}
table.Render()
return nil
},
}
func listIPs(zone string, table *table.Table) error {
zReq := egoscale.IPAddress{}
if zone != "" {
var err error
zReq.ZoneID, err = getZoneIDByName(zone)
if err != nil {
return err
}
zReq.IsElastic = true
ips, err := cs.ListWithContext(gContext, &zReq)
if err != nil {
return err
}
var zone string
for i, ipaddr := range ips {
ip := ipaddr.(*egoscale.IPAddress)
if i == 0 {
zone = ip.ZoneName
}
_, vms, err := eipDetails(ip.ID)
if err != nil {
return err
}
nbVM := fmt.Sprintf("%d", len(vms))
table.Append([]string{zone, ip.IPAddress.String(), ip.ID.String(), nbVM})
zone = ""
}
return nil
}
zones := &egoscale.Zone{}
zs, err := cs.ListWithContext(gContext, zones)
if err != nil {
return err
}
for _, z := range zs {
zID := z.(*egoscale.Zone).Name
if err := listIPs(zID, table); err != nil {
return err
}
}
return nil
}
func init() {
eipListCmd.Flags().StringP("zone", "z", "", "Show IPs from given zone")
eipCmd.AddCommand(eipListCmd)
}