-
Notifications
You must be signed in to change notification settings - Fork 20
/
eip_list.go
93 lines (73 loc) · 2.04 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
86
87
88
89
90
91
92
93
package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/egoscale"
)
type eipListItemOutput struct {
ID string `json:"id"`
Zone string `json:"zone"`
IPAddress string `json:"ip_address"`
Description string `json:"description"`
Managed bool `json:"managed"`
}
type eipListOutput []eipListItemOutput
func (o *eipListOutput) ToJSON() { output.JSON(o) }
func (o *eipListOutput) ToText() { output.Text(o) }
func (o *eipListOutput) ToTable() { output.Table(o) }
func init() {
eipListCmd := &cobra.Command{
Use: "list",
Short: "List Elastic IP addresses",
Long: fmt.Sprintf(`This command lists existing Elastic IP addresses.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&eipListOutput{}), ", ")),
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
return printOutput(listEIP(zone))
},
}
eipListCmd.Flags().StringP(zoneFlagLong, zoneFlagShort, "", "Show IPs from given zone")
eipCmd.AddCommand(eipListCmd)
}
func listEIP(zone string) (output.Outputter, error) {
out := eipListOutput{}
zones, err := globalstate.EgoscaleClient.ListWithContext(gContext, &egoscale.Zone{})
if err != nil {
return nil, err
}
for _, z := range zones {
if zone != "" && z.(*egoscale.Zone).Name != zone {
continue
}
req := egoscale.IPAddress{
ZoneID: z.(*egoscale.Zone).ID,
IsElastic: true,
}
ips, err := globalstate.EgoscaleClient.ListWithContext(gContext, &req)
if err != nil {
return nil, err
}
for _, ip := range ips {
eip := ip.(*egoscale.IPAddress)
o := eipListItemOutput{
Description: eip.Description,
ID: eip.ID.String(),
IPAddress: eip.IPAddress.String(),
Zone: z.(*egoscale.Zone).Name,
}
if eip.Healthcheck != nil {
o.Managed = true
}
out = append(out, o)
}
}
return &out, nil
}