/
utils_network.go
88 lines (75 loc) · 2.43 KB
/
utils_network.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
package edgecenter
import (
"encoding/json"
"net"
edgecloudV2 "github.com/Edge-Center/edgecentercloud-go/v2"
)
// findNetworkByName searches for a network with the given name among the given networks.
// Returns the found network and a flag indicating the success of the search.
func findNetworkByName(name string, nets []edgecloudV2.Network) (edgecloudV2.Network, bool) {
for _, n := range nets {
if n.Name == name {
return n, true
}
}
return edgecloudV2.Network{}, false
}
// findSharedNetworkByName searches for a shared network with the given name among the given networks.
// Returns the found network and a flag indicating the success of the search.
func findSharedNetworkByName(name string, nets []edgecloudV2.NetworkSubnetwork) (edgecloudV2.NetworkSubnetwork, bool) {
for _, n := range nets {
if n.Name == name {
return n, true
}
}
return edgecloudV2.NetworkSubnetwork{}, false
}
// StructToMap converts the struct to map[string]interface{}.
// Returns an error if the conversion fails.
func StructToMap(obj interface{}) (map[string]interface{}, error) {
data, err := json.Marshal(obj)
if err != nil {
return nil, err
}
var newMap map[string]interface{}
err = json.Unmarshal(data, &newMap)
if err != nil {
return nil, err
}
return newMap, nil
}
func dnsNameserversToStringList(dnsNameservers []net.IP) []string {
dns := make([]string, len(dnsNameservers))
for i, ns := range dnsNameservers {
dns[i] = ns.String()
}
return dns
}
func hostRoutesToListOfMapsV2(hostRoutes []edgecloudV2.HostRoute) []map[string]string {
hrs := make([]map[string]string, len(hostRoutes))
for i, hr := range hostRoutes {
hR := map[string]string{"destination": "", "nexthop": ""}
hR["destination"] = hr.Destination.String()
hR["nexthop"] = hr.NextHop.String()
hrs[i] = hR
}
return hrs
}
func prepareSubnets(subs []edgecloudV2.Subnetwork) []map[string]interface{} {
subnetList := make([]map[string]interface{}, 0, len(subs))
for _, s := range subs {
subnetList = append(subnetList, map[string]interface{}{
"id": s.ID,
"name": s.Name,
"enable_dhcp": s.EnableDHCP,
"cidr": s.CIDR,
"available_ips": s.AvailableIps,
"total_ips": s.TotalIps,
"has_router": s.HasRouter,
"dns_nameservers": dnsNameserversToStringList(s.DNSNameservers),
"host_routes": hostRoutesToListOfMapsV2(s.HostRoutes),
"gateway_ip": s.GatewayIP.String(),
})
}
return subnetList
}