forked from exoscale/egoscale
/
privnet.go
58 lines (48 loc) · 1.12 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
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
}
if err := cs.GetWithContext(gContext, net); err != nil {
return nil, err
}
return net, nil
}
func init() {
RootCmd.AddCommand(privnetCmd)
}
// dhcpRange returns the string representation for a DHCP
func dhcpRange(startIP, endIP, netmask net.IP) string {
if startIP != nil && endIP != nil && netmask != nil {
mask := net.IPMask(netmask.To4())
prefixSize, _ := mask.Size()
return fmt.Sprintf("%s-%s /%d", startIP.String(), endIP.String(), prefixSize)
}
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"
}