forked from GoogleCloudPlatform/professional-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_access.go
388 lines (344 loc) · 10.2 KB
/
data_access.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"database/sql"
"fmt"
"log"
"net"
"strings"
"github.com/apparentlymart/go-cidr/cidr"
"github.com/jackc/pgtype"
_ "github.com/golang-migrate/migrate/v4/source/file"
)
type RoutingDomain struct {
Id int `db:"routing_domain_id"`
Name string `db:"name"`
Vpcs string `db:"vpcs"` // associated VPCs that should be tracked for subnet creation
}
type Range struct {
Subnet_id int `db:"subnet_id"`
Parent_id int `db:"parent_id"`
Routing_domain_id int `db:"routing_domain_id"`
Name string `db:"name"`
Cidr string `db:"cidr"`
}
func GetRangesFromDB() ([]Range, error) {
var ranges []Range
rows, err := db.Query("SELECT subnet_id, parent_id, routing_domain_id, name, cidr FROM subnets")
if err != nil {
return nil, err
}
for rows.Next() {
var subnet_id int
var routing_domain_id int
tmp := pgtype.Int4{}
var name string
var cidr string
err := rows.Scan(&subnet_id, &tmp, &routing_domain_id, &name, &cidr)
if err != nil {
return nil, err
}
parent_id := -1
if tmp.Status == pgtype.Present {
tmp.AssignTo(&parent_id)
}
ranges = append(ranges, Range{
Subnet_id: subnet_id,
Parent_id: parent_id,
Routing_domain_id: routing_domain_id,
Name: name,
Cidr: cidr,
})
}
return ranges, nil
}
func GetRangesForParentFromDB(tx *sql.Tx, parent_id int64) ([]Range, error) {
var ranges []Range
rows, err := tx.Query("SELECT subnet_id, parent_id, routing_domain_id, name, cidr FROM subnets WHERE parent_id = ? FOR UPDATE", parent_id)
if err != nil {
return nil, err
}
for rows.Next() {
var subnet_id int
var routing_domain_id int
tmp := pgtype.Int4{}
var name string
var cidr string
err := rows.Scan(&subnet_id, &tmp, &routing_domain_id, &name, &cidr)
if err != nil {
return nil, err
}
parent_id := -1
if tmp.Status == pgtype.Present {
tmp.AssignTo(&parent_id)
}
ranges = append(ranges, Range{
Subnet_id: subnet_id,
Parent_id: parent_id,
Routing_domain_id: routing_domain_id,
Name: name,
Cidr: cidr,
})
}
return ranges, nil
}
func GetRangeFromDB(id int64) (*Range, error) {
var subnet_id int
var routing_domain_id int
tmp := pgtype.Int4{}
var name string
var cidr string
err := db.QueryRow("SELECT subnet_id, parent_id, routing_domain_id, name, cidr FROM subnets WHERE subnet_id = ?", id).Scan(&subnet_id, &tmp, &routing_domain_id, &name, &cidr)
if err != nil {
return nil, err
}
parent_id := -1
if tmp.Status == pgtype.Present {
tmp.AssignTo(&parent_id)
}
return &Range{
Subnet_id: subnet_id,
Parent_id: parent_id,
Routing_domain_id: routing_domain_id,
Name: name,
Cidr: cidr,
}, nil
}
func GetRangeFromDBWithTx(tx *sql.Tx, id int64) (*Range, error) {
var subnet_id int
var routing_domain_id int
tmp := pgtype.Int4{}
var name string
var cidr string
err := tx.QueryRow("SELECT subnet_id, parent_id, routing_domain_id, name, cidr FROM subnets WHERE subnet_id = ? FOR UPDATE", id).Scan(&subnet_id, &tmp, &routing_domain_id, &name, &cidr)
if err != nil {
return nil, err
}
parent_id := -1
if tmp.Status == pgtype.Present {
tmp.AssignTo(&parent_id)
}
return &Range{
Subnet_id: subnet_id,
Parent_id: parent_id,
Routing_domain_id: routing_domain_id,
Name: name,
Cidr: cidr,
}, nil
}
func getRangeByCidrAndRoutingDomain(tx *sql.Tx, request_cidr string, routing_domain_id int) (*Range, error) {
var subnet_id int
tmp := pgtype.Int4{}
var name string
var cidr string
err := tx.QueryRow("SELECT subnet_id, parent_id, name, cidr FROM subnets WHERE cidr = ? and routing_domain_id = ? FOR UPDATE", request_cidr, routing_domain_id).Scan(&subnet_id, &tmp, &name, &cidr)
if err != nil {
return nil, err
}
parent_id := -1
if tmp.Status == pgtype.Present {
tmp.AssignTo(&parent_id)
}
return &Range{
Subnet_id: subnet_id,
Parent_id: parent_id,
Routing_domain_id: routing_domain_id,
Name: name,
Cidr: cidr,
}, nil
}
func GetRangeByCidrFromDB(tx *sql.Tx, routing_domain_id int, cidr_request string) (*Range, error) {
var subnet_id int
tmp := pgtype.Int4{}
var name string
var cidr string
if cidr_request != "" {
err := tx.QueryRow("SELECT subnet_id, parent_id, name, cidr FROM subnets WHERE cidr = ? and routing_domain_id = ? FOR UPDATE", cidr_request, routing_domain_id).Scan(&subnet_id, &tmp, &name, &cidr)
if err != nil {
return nil, err
}
} else {
err := tx.QueryRow("SELECT subnet_id, parent_id, name, cidr FROM subnets WHERE routing_domain_id = ? LIMIT 1 FOR UPDATE", routing_domain_id).Scan(&subnet_id, &tmp, &name, &cidr)
if err != nil {
return nil, err
}
}
parent_id := -1
if tmp.Status == pgtype.Present {
tmp.AssignTo(&parent_id)
}
return &Range{
Subnet_id: subnet_id,
Parent_id: parent_id,
Routing_domain_id: routing_domain_id,
Name: name,
Cidr: cidr,
}, nil
}
func DeleteRangeFromDb(id int64) error {
_, err := db.Query("DELETE FROM subnets WHERE subnet_id = ?", id)
if err != nil {
return err
}
return nil
}
func DeleteRoutingDomainFromDB(id int64) error {
_, err := db.Query("DELETE FROM routing_domains WHERE routing_domain_id = ?", id)
if err != nil {
return err
}
return nil
}
func CreateRangeInDb(tx *sql.Tx, parent_id int64, routing_domain_id int, name string, cidr string) (int64, error) {
if parent_id == -1 {
res, err := tx.Exec("INSERT INTO subnets (routing_domain_id, name, cidr) VALUES (?,?,?);", routing_domain_id, name, cidr)
if err != nil {
return -1, err
}
subnet_id, err := res.LastInsertId()
if err != nil {
return -1, err
}
return subnet_id, nil
} else {
res, err := tx.Exec("INSERT INTO subnets (parent_id, routing_domain_id, name, cidr) VALUES (?,?,?,?);", parent_id, routing_domain_id, name, cidr)
if err != nil {
return -1, err
}
subnet_id, err := res.LastInsertId()
if err != nil {
return -1, err
}
return subnet_id, nil
}
}
func createNewSubnetLease(prevCidr string, range_size int, subnetIndex int) (*net.IPNet, int, error) {
_, network, err := net.ParseCIDR(prevCidr)
if err != nil {
return nil, -1, fmt.Errorf("unable to calculate subnet %v", err)
}
ones, size := network.Mask.Size()
subnet, err := cidr.Subnet(network, int(range_size)-ones, subnetIndex)
if err != nil {
return nil, -1, fmt.Errorf("unable to calculate subnet %v", err)
}
subnet.Mask = net.CIDRMask(range_size, size)
return subnet, range_size, nil
}
func verifyNoOverlap(parentCidr string, subnetRanges []Range, newSubnet *net.IPNet) error {
_, parentNetwork, err := net.ParseCIDR(parentCidr)
if err != nil {
return fmt.Errorf("can't parse CIDR %v", err)
}
log.Printf("Checking Overlap\nparentCidr:\t%s", parentCidr)
var subnets []*net.IPNet
subnets = append(subnets, newSubnet)
log.Printf("newSubnet:\t%s/%d", newSubnet.IP.String(), netMask(newSubnet.Mask))
for i := 0; i < len(subnetRanges); i++ {
subnetRange := subnetRanges[i]
netAddr, subnetCidr, err := net.ParseCIDR(subnetRange.Cidr)
if err != nil {
return fmt.Errorf("can't parse CIDR %v", err)
}
if parentNetwork.Contains(netAddr) {
subnets = append(subnets, subnetCidr)
}
}
return cidr.VerifyNoOverlap(subnets, parentNetwork)
}
func netMask(mask net.IPMask) int {
ones, _ := mask.Size()
return ones
}
func GetDefaultRoutingDomainFromDB(tx *sql.Tx) (*RoutingDomain, error) {
var routing_domain_id int
var name string
var vpcs sql.NullString
err := tx.QueryRow("SELECT routing_domain_id, name, vpcs FROM routing_domains LIMIT 1 FOR UPDATE").Scan(&routing_domain_id, &name, &vpcs)
if err != nil {
return nil, err
}
return &RoutingDomain{
Id: routing_domain_id,
Name: name,
Vpcs: vpcs.String,
}, nil
}
func GetRoutingDomainsFromDB() ([]RoutingDomain, error) {
var domains []RoutingDomain
rows, err := db.Query("SELECT routing_domain_id, name, vpcs FROM routing_domains")
if err != nil {
return nil, err
}
for rows.Next() {
var routing_domain_id int
var name string
var vpcs sql.NullString
err := rows.Scan(&routing_domain_id, &name, &vpcs)
if err != nil {
return nil, err
}
domains = append(domains, RoutingDomain{
Id: routing_domain_id,
Name: name,
Vpcs: vpcs.String,
})
}
return domains, nil
}
func GetRoutingDomainFromDB(id int64) (*RoutingDomain, error) {
var routing_domain_id int
var name string
var vpcs sql.NullString
err := db.QueryRow("SELECT routing_domain_id, name, vpcs FROM routing_domains WHERE routing_domain_id = ?", id).Scan(&routing_domain_id, &name, &vpcs)
if err != nil {
return nil, err
}
return &RoutingDomain{
Id: routing_domain_id,
Name: name,
Vpcs: vpcs.String,
}, nil
}
func UpdateRoutingDomainOnDb(id int64, name JSONString, vpcs JSONStringArray) error {
if name.Set && vpcs.Set {
_, err := db.Query("UPDATE routing_domains SET name = ?, vpcs = ? WHERE routing_domain_id = ?", id, name.Value, strings.Join(vpcs.Value, ","))
if err != nil {
return err
}
} else if vpcs.Set {
_, err := db.Query("UPDATE routing_domains SET vpcs = ? WHERE routing_domain_id = ?", id, strings.Join(vpcs.Value, ","))
if err != nil {
return err
}
} else if name.Set {
_, err := db.Query("UPDATE routing_domains SET name = ? WHERE routing_domain_id = ?", id, name.Value)
if err != nil {
return err
}
}
return nil
}
func CreateRoutingDomainOnDb(name string, vpcs []string) (int64, error) {
res, err := db.Exec("INSERT INTO routing_domains (name, vpcs) VALUES (?,?);", name, strings.Join(vpcs, ","))
if err != nil {
return -1, err
}
domain_id, err := res.LastInsertId()
if err != nil {
return -1, err
}
return domain_id, nil
}