-
Notifications
You must be signed in to change notification settings - Fork 19
/
privnet_list.go
93 lines (80 loc) · 1.76 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
package cmd
import (
"fmt"
"os"
"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// listCmd represents the list command
var privnetListCmd = &cobra.Command{
Use: "list",
Short: "List private networks",
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
table := table.NewTable(os.Stdout)
table.SetHeader([]string{"zone", "Name", "ID", "Associated Virtual machine", "DHCP"})
if err := listPrivnets(zone, table); err != nil {
return err
}
table.Render()
return nil
},
}
func listPrivnets(zone string, table *table.Table) error {
pnReq := &egoscale.Network{}
if zone != "" {
var err error
pnReq.Type = "Isolated"
pnReq.ZoneID, err = getZoneIDByName(zone)
if err != nil {
return err
}
pnReq.CanUseForDeploy = true
pns, err := cs.ListWithContext(gContext, pnReq)
if err != nil {
return err
}
var zone string
for i, pNet := range pns {
pn := pNet.(*egoscale.Network)
if i == 0 {
zone = pn.ZoneName
}
vms, err := privnetDetails(pn)
if err != nil {
return err
}
vmNum := fmt.Sprintf("%d", len(vms))
table.Append([]string{
zone,
pn.Name,
pn.ID.String(),
vmNum,
dhcpRange(pn.StartIP, pn.EndIP, pn.Netmask),
})
zone = ""
}
return nil
}
zones := &egoscale.Zone{}
zs, err := cs.ListWithContext(gContext, zones)
if err != nil {
return err
}
for _, z := range zs {
zID := z.(*egoscale.Zone).Name
if err := listPrivnets(zID, table); err != nil {
return err
}
}
return nil
}
func init() {
privnetListCmd.Flags().StringP("zone", "z", "", "Show Private Network from given zone")
privnetCmd.AddCommand(privnetListCmd)
}