-
Notifications
You must be signed in to change notification settings - Fork 20
/
privnet.go
116 lines (96 loc) · 2.91 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
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
package cmd
import (
"fmt"
"net"
"os"
"time"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/egoscale"
)
var privnetCmd = &cobra.Command{
Use: "privnet",
Short: "Private networks management",
PersistentPreRun: func(_ *cobra.Command, _ []string) {
fmt.Fprintln(os.Stderr,
`**********************************************************************
The "exo privnet" commands are deprecated and will be removed in a future
version, please use "exo compute private-network" replacement commands.
**********************************************************************`)
time.Sleep(3 * time.Second)
},
Hidden: true,
}
// getNetwork returns a Private Network by name or ID, and optionally a zone to restrict search to.
// Since the Exoscale API doesn't support searching by unique name, we have to list all networks and
// match results ourselves. In case the caller provides a network name and there are multiple matching
// results an error is returned.
func getNetwork(net string, zoneID *egoscale.UUID) (*egoscale.Network, error) {
var found *egoscale.Network
req := &egoscale.Network{
ZoneID: zoneID,
Type: "Isolated",
CanUseForDeploy: true,
}
id, errUUID := egoscale.ParseUUID(net)
if errUUID != nil {
req.Name = net
} else {
req.ID = id
}
resp, err := globalstate.EgoscaleClient.ListWithContext(gContext, req)
if err != nil {
return nil, err
}
for _, item := range resp {
network := item.(*egoscale.Network)
// If search criterion is an unique ID, return the first (i.e. only) match
if id != nil && network.ID.Equal(*id) {
return network, nil
}
// If search criterion is a name, check that there isn't multiple networks named
// identically before returning a match
if network.Name == net {
// We already found a match before -> multiple results
if found != nil {
return nil, fmt.Errorf("found multiple networks named %q, please specify a unique ID instead", net)
}
found = network
}
}
if found != nil {
return found, nil
}
return nil, fmt.Errorf("network %q not found", net)
}
func getPrivnetIDs(params []string, zoneID *egoscale.UUID) ([]egoscale.UUID, error) {
ids := make([]egoscale.UUID, len(params))
for i, sg := range params {
n, err := getNetwork(sg, zoneID)
if err != nil {
return nil, err
}
ids[i] = *n.ID
}
return ids, 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"
}