-
Notifications
You must be signed in to change notification settings - Fork 39
/
import_lxd.go
204 lines (163 loc) · 4.13 KB
/
import_lxd.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"fmt"
"net"
"strconv"
"strings"
"github.com/lxc/lxd/client"
)
func importFromLXD(c lxd.ContainerServer) (Routers, error) {
// Load the LXD container list
containers, err := c.GetContainers()
if err != nil {
return nil, err
}
// Build the routers map
routers := Routers{}
for _, ctn := range containers {
// Skip the container we don't care about
if ctn.ExpandedConfig["user.internet.type"] != "router" {
continue
}
config := ctn.ExpandedConfig
// Generate the new record
router := Router{}
router.Configuration = &RouterConfiguration{}
// Text fields
router.Organization = config["user.internet.organization"]
router.Name = ctn.Name
router.Location = config["user.internet.location"]
router.Configuration.PasswordLogin = config["user.internet.router.password.login"]
router.Configuration.PasswordEnable = config["user.internet.router.password.enable"]
router.Configuration.FQDN = config["user.internet.router.fqdn"]
routerid := net.ParseIP(config["user.internet.router.routerid"])
router.Configuration.RouterID = &routerid
// Integers
tier, err := strconv.Atoi(config["user.internet.tier"])
if err != nil {
return nil, fmt.Errorf("Invalid tier: %s\n", config["user.internet.tier"])
}
router.Tier = tier
asn, err := strconv.Atoi(config["user.internet.router.asn"])
if err != nil {
return nil, fmt.Errorf("Invalid ASN: %s\n", config["user.internet.router.asn"])
}
router.Configuration.ASN = asn
priority, err := strconv.Atoi(config["user.internet.priority"])
if err != nil {
return nil, fmt.Errorf("Invalid priority: %s\n", config["user.internet.priority"])
}
router.Priority = priority
// Booleans
if config["user.internet.internal"] == "true" {
router.Internal = true
}
// DNS
dns := []string{}
for k, v := range config {
if !strings.HasPrefix(k, "user.internet.dns.") {
continue
}
dns = append(dns, v)
}
router.DNS = dns
// Loopback
addresses := []net.IP{}
routes := []RouterInterfaceRoute{}
for k, v := range config {
if strings.HasPrefix(k, "user.internet.router.loopback.address.") {
addresses = append(addresses, net.ParseIP(v))
continue
}
if strings.HasPrefix(k, "user.internet.router.loopback.route.") {
_, subnet, err := net.ParseCIDR(v)
if err != nil {
return nil, err
}
routes = append(routes, RouterInterfaceRoute{Subnet: subnet})
continue
}
}
// Peers
peersMap := map[string]*Peer{}
for k, v := range config {
if !strings.HasPrefix(k, "user.internet.peer.") {
continue
}
fields := strings.Split(k, ".")
name := fields[3]
key := fields[4]
peer, ok := peersMap[name]
if !ok {
peer = &Peer{Name: name}
}
switch key {
case "interface":
peer.Interface = v
case "mac":
peer.MAC = v
case "remote":
peer.Remote = v
case "speed":
speed, err := strconv.Atoi(v)
if err != nil {
return nil, err
}
peer.Speed = speed
case "delay":
delay, err := strconv.Atoi(v)
if err != nil {
return nil, err
}
peer.Delay = delay
case "asn":
asn, err := strconv.Atoi(v)
if err != nil {
return nil, err
}
peer.ASN = asn
case "weight":
weight, err := strconv.Atoi(v)
if err != nil {
return nil, err
}
peer.Weight = weight
}
peersMap[name] = peer
}
peers := []Peer{}
for _, p := range peersMap {
i := 0
routes := []RouterInterfaceRoute{}
for {
found := false
route := RouterInterfaceRoute{}
v, ok := config[fmt.Sprintf("user.internet.peer.%s.route.%d.subnet", p.Name, i)]
if ok {
found = true
_, subnet, err := net.ParseCIDR(v)
if err != nil {
return nil, err
}
route.Subnet = subnet
}
v, ok = config[fmt.Sprintf("user.internet.peer.%s.route.%d.gateway", p.Name, i)]
if ok {
found = true
gateway := net.ParseIP(v)
route.Gateway = &gateway
}
if !found {
break
}
i += 1
routes = append(routes, route)
}
p.Routes = routes
peers = append(peers, *p)
}
router.Peers = peers
routers[router.Name] = &router
}
return routers, nil
}