-
Notifications
You must be signed in to change notification settings - Fork 20
/
privnet_list.go
97 lines (78 loc) · 2.19 KB
/
privnet_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
94
95
96
97
package cmd
import (
"fmt"
"strings"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
type privnetListItemOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Zone string `json:"zone"`
DHCP string `json:"dhcp"`
NumInstances int `json:"num_instances" outputLabel:"Instances"`
}
type privnetListOutput []privnetListItemOutput
func (o *privnetListOutput) toJSON() { outputJSON(o) }
func (o *privnetListOutput) toText() { outputText(o) }
func (o *privnetListOutput) toTable() { outputTable(o) }
func init() {
privnetListCmd := &cobra.Command{
Use: "list",
Short: "List Private Networks",
Long: fmt.Sprintf(`This command lists existing Private Networks.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&privnetListOutput{}), ", ")),
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
return output(listPrivnets(zone))
},
}
privnetListCmd.Flags().StringP("zone", "z", "", "Show Private Networks only in specified zone")
privnetCmd.AddCommand(privnetListCmd)
}
func listPrivnets(zone string) (outputter, error) {
out := privnetListOutput{}
zones, err := cs.ListWithContext(gContext, &egoscale.Zone{})
if err != nil {
return nil, err
}
for _, z := range zones {
if zone != "" && z.(*egoscale.Zone).Name != zone {
continue
}
req := egoscale.Network{
ZoneID: z.(*egoscale.Zone).ID,
Type: "Isolated",
CanUseForDeploy: true,
}
privnets, err := cs.ListWithContext(gContext, &req)
if err != nil {
return nil, err
}
for _, p := range privnets {
privnet := p.(*egoscale.Network)
vms, err := privnetDetails(privnet)
if err != nil {
return nil, err
}
instances := make([]string, len(vms))
for i := range vms {
instances[i] = vms[i].Name
}
o := privnetListItemOutput{
ID: privnet.ID.String(),
Name: privnet.Name,
Zone: z.(*egoscale.Zone).Name,
DHCP: dhcpRange(*privnet),
NumInstances: len(instances),
}
out = append(out, o)
}
}
return &out, nil
}