-
Notifications
You must be signed in to change notification settings - Fork 19
/
privnet.go
80 lines (67 loc) · 1.59 KB
/
privnet.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
package cmd
import (
"fmt"
"net"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// privnetCmd represents the pn command
var privnetCmd = &cobra.Command{
Use: "privnet",
Short: "Private networks management",
}
func getNetworkByName(name string) (*egoscale.Network, error) {
net := &egoscale.Network{
Type: "Isolated",
CanUseForDeploy: true,
}
id, errUUID := egoscale.ParseUUID(name)
if errUUID != nil {
net.Name = name
} else {
net.ID = id
}
resp, err := cs.GetWithContext(gContext, net)
if err != nil {
return nil, err
}
return resp.(*egoscale.Network), nil
}
func getNetworkByNameAndZone(name string, zoneID *egoscale.UUID) (*egoscale.Network, error) {
net := &egoscale.Network{
ZoneID: zoneID,
Type: "Isolated",
CanUseForDeploy: true,
}
id, errUUID := egoscale.ParseUUID(name)
if errUUID != nil {
net.Name = name
} else {
net.ID = id
}
resp, err := cs.GetWithContext(gContext, net)
if err != nil {
return nil, err
}
return resp.(*egoscale.Network), nil
}
func init() {
RootCmd.AddCommand(privnetCmd)
}
// dhcpRange returns the string representation for a DHCP
func dhcpRange(network egoscale.Network) string {
if network.StartIP != nil && network.EndIP != nil && network.Netmask != nil {
mask := (net.IPMask)(network.Netmask.To4())
ones, _ := mask.Size()
return fmt.Sprintf("%s-%s /%d", network.StartIP, network.EndIP, ones)
}
return "n/a"
}
// dhcpRange returns the string representation for a DHCP
func nicIP(nic egoscale.Nic) string {
ip := nic.IPAddress
if ip != nil {
return ip.String()
}
return "n/a"
}