-
Notifications
You must be signed in to change notification settings - Fork 19
/
eip_show.go
119 lines (102 loc) · 2.51 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package cmd
import (
"fmt"
"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)
zone := ip.ZoneName
ipaddr := ip.IPAddress.String()
table.SetHeader([]string{ipaddr})
table.Append([]string{"ID", id.String()})
table.Append([]string{"Zone", zone})
if ip.Healthcheck != nil {
table.Append([]string{
"Healthcheck Mode",
ip.Healthcheck.Mode})
table.Append([]string{
"Healthcheck Path",
ip.Healthcheck.Path})
table.Append([]string{
"Healthcheck Port",
fmt.Sprintf("%d", ip.Healthcheck.Port)})
table.Append([]string{
"Healthcheck Interval",
fmt.Sprintf("%d", ip.Healthcheck.Interval)})
table.Append([]string{
"Healthcheck Strikes Fail",
fmt.Sprintf("%d", ip.Healthcheck.StrikesFail)})
table.Append([]string{
"Healthcheck Strikes OK",
fmt.Sprintf("%d", ip.Healthcheck.StrikesOk)})
table.Append([]string{
"Healthcheck Timeout",
fmt.Sprintf("%d", ip.Healthcheck.Timeout)})
}
if len(vms) > 0 {
table.Append([]string{
"Instances",
vms[0].Name,
})
for _, vm := range vms[1:] {
table.Append([]string{
"",
vm.Name})
}
}
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)
}