-
Notifications
You must be signed in to change notification settings - Fork 18
/
privnet_create.go
153 lines (124 loc) · 3.49 KB
/
privnet_create.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package cmd
import (
"fmt"
"net"
"strconv"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// createCmd represents the create command
var privnetCreateCmd = &cobra.Command{
Use: "create <name>",
Short: "Create private network",
Aliases: gCreateAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return cmd.Usage()
}
name := args[0]
desc, err := cmd.Flags().GetString("description")
if err != nil {
return err
}
startIP, err := getIPValue(cmd, "startip")
if err != nil {
return err
}
endIP, err := getIPValue(cmd, "endip")
if err != nil {
return err
}
netmask, err := getIPValue(cmd, "netmask")
if err != nil {
return err
}
cidrmask, err := cmd.Flags().GetString("cidrmask")
if err != nil {
return err
}
if netmask.IP != nil && cidrmask != "" {
return fmt.Errorf("netmask %q and cidrmask %q are mutually exclusive", netmask, cidrmask)
}
if cidrmask != "" {
c, err := strconv.ParseInt(cidrmask, 10, 32)
if err != nil {
return err
}
ipmask := net.CIDRMask(int(c), 32)
netmask.IP = (*net.IP)(&ipmask)
}
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
if zone == "" {
zone = gCurrentAccount.DefaultZone
}
if isEmptyArgs(name, zone) {
return cmd.Usage()
}
newNet, err := privnetCreate(name, desc, zone, startIP.Value(), endIP.Value(), netmask.Value())
if err != nil {
return err
}
return privnetShow(*newNet)
},
}
func isEmptyArgs(args ...string) bool {
for _, arg := range args {
if arg == "" {
return true
}
}
return false
}
func privnetCreate(name, desc, zoneName string, startIP, endIP, netmask net.IP) (*egoscale.Network, error) {
zoneID, err := getZoneIDByName(zoneName)
if err != nil {
return nil, err
}
// NetworkOffering are cross zones
listReq := &egoscale.ListNetworkOfferings{
Name: "PrivNet",
ZoneID: zoneID,
Page: 1,
PageSize: 1,
}
resp, err := cs.RequestWithContext(gContext, listReq)
if err != nil {
return nil, err
}
nos := resp.(*egoscale.ListNetworkOfferingsResponse)
if len(nos.NetworkOffering) != 1 {
return nil, fmt.Errorf("missing Network Offering %q in %q", listReq.Name, zoneName)
}
if startIP != nil && endIP != nil && netmask == nil {
netmask = net.IPv4(255, 255, 255, 0)
}
req := &egoscale.CreateNetwork{
Name: name,
DisplayText: desc,
ZoneID: zoneID,
StartIP: startIP,
EndIP: endIP,
Netmask: netmask,
NetworkOfferingID: nos.NetworkOffering[0].ID,
}
resp, err = cs.RequestWithContext(gContext, req)
if err != nil {
return nil, err
}
return resp.(*egoscale.Network), nil
}
func init() {
privnetCreateCmd.Flags().StringP("description", "d", "", "Private network description")
privnetCreateCmd.Flags().StringP("cidrmask", "c", "", "the cidrmask of the network. Required for managed networks.")
privnetCreateCmd.Flags().StringP("zone", "z", "", "Assign private network to a zone")
startIP := new(ipValue)
endIP := new(ipValue)
netmask := new(ipValue)
privnetCreateCmd.Flags().VarP(startIP, "startip", "s", "the beginning IP address in the network IP range. Required for managed networks.")
privnetCreateCmd.Flags().VarP(endIP, "endip", "e", "the ending IP address in the network IP range. Required for managed networks.")
privnetCreateCmd.Flags().VarP(netmask, "netmask", "m", "the netmask of the network. Required for managed networks.")
privnetCmd.AddCommand(privnetCreateCmd)
}