-
Notifications
You must be signed in to change notification settings - Fork 19
/
nlb_list.go
91 lines (75 loc) · 2.03 KB
/
nlb_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
package cmd
import (
"fmt"
"os"
"strings"
apiv2 "github.com/exoscale/egoscale/api/v2"
"github.com/spf13/cobra"
)
type nlbListItemOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Zone string `json:"zone"`
IPAddress string `json:"ip_address"`
}
type nlbListOutput []nlbListItemOutput
func (o *nlbListOutput) toJSON() { outputJSON(o) }
func (o *nlbListOutput) toText() { outputText(o) }
func (o *nlbListOutput) toTable() { outputTable(o) }
var nlbListCmd = &cobra.Command{
Use: "list",
Short: "List Network Load Balancers",
Long: fmt.Sprintf(`This command lists Network Load Balancers.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&nlbListItemOutput{}), ", ")),
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
zone = strings.ToLower(zone)
return output(listNLBs(zone), nil)
},
}
func listNLBs(zone string) outputter {
var nlbZones []string
if zone != "" {
nlbZones = []string{zone}
} else {
nlbZones = zones
}
out := make(nlbListOutput, 0)
res := make(chan nlbListItemOutput)
defer close(res)
go func() {
for nlb := range res {
out = append(out, nlb)
}
}()
ctx := apiv2.WithEndpoint(gContext, apiv2.NewReqEndpoint(gCurrentAccount.Environment, zone))
err := forEachZone(nlbZones, func(zone string) error {
list, err := cs.ListNetworkLoadBalancers(ctx, zone)
if err != nil {
return fmt.Errorf("unable to list Network Load Balancers in zone %s: %v", zone, err)
}
for _, nlb := range list {
res <- nlbListItemOutput{
ID: nlb.ID,
Name: nlb.Name,
Zone: zone,
IPAddress: nlb.IPAddress.String(),
}
}
return nil
})
if err != nil {
fmt.Fprintf(os.Stderr,
"warning: errors during listing, results might be incomplete.\n%s\n", err) // nolint:golint
}
return &out
}
func init() {
nlbListCmd.Flags().StringP("zone", "z", "", "Zone to filter results to")
nlbCmd.AddCommand(nlbListCmd)
}