-
Notifications
You must be signed in to change notification settings - Fork 19
/
eip_show.go
89 lines (74 loc) · 1.81 KB
/
eip_show.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
package cmd
import (
"os"
"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// showCmd represents the show command
var eipShowCmd = &cobra.Command{
Use: "show <ip address | eip id>",
Short: "Show an eip details",
Aliases: gShowAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
id, err := egoscale.ParseUUID(args[0])
if err != nil {
id, err = getEIPIDByIP(args[0])
if err != nil {
return err
}
}
ip, vms, err := eipDetails(id)
if err != nil {
return err
}
table := table.NewTable(os.Stdout)
table.SetHeader([]string{"Zone", "IP", "Virtual Machine", "Virtual Machine ID"})
zone := ip.ZoneName
ipaddr := ip.IPAddress.String()
if len(vms) > 0 {
for _, vm := range vms {
table.Append([]string{zone, ipaddr, vm.Name, vm.ID.String()})
zone = ""
ipaddr = ""
}
} else {
table.Append([]string{zone, ipaddr})
}
table.Render()
return nil
},
}
func eipDetails(eip *egoscale.UUID) (*egoscale.IPAddress, []egoscale.VirtualMachine, error) {
var eipID = eip
query := &egoscale.IPAddress{ID: eipID, IsElastic: true}
resp, err := cs.GetWithContext(gContext, query)
if err != nil {
return nil, nil, err
}
addr := resp.(*egoscale.IPAddress)
vms, err := cs.ListWithContext(gContext, &egoscale.VirtualMachine{ZoneID: addr.ZoneID})
if err != nil {
return nil, nil, err
}
vmAssociated := []egoscale.VirtualMachine{}
for _, value := range vms {
vm := value.(*egoscale.VirtualMachine)
nic := vm.DefaultNic()
if nic == nil {
continue
}
for _, sIP := range nic.SecondaryIP {
if sIP.IPAddress.Equal(addr.IPAddress) {
vmAssociated = append(vmAssociated, *vm)
}
}
}
return addr, vmAssociated, nil
}
func init() {
eipCmd.AddCommand(eipShowCmd)
}