/
two_stage_l3_clos_virtual_networks.go
863 lines (764 loc) · 23.2 KB
/
two_stage_l3_clos_virtual_networks.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
package apstra
import (
"context"
"fmt"
"net"
"net/http"
"strconv"
)
const (
// Do not use apiUrlVirtualNetworks directly. The rawVirtualNetwork objects
// in the returned map do not match the objects when retrieved using
// apiUrlVirtualNetworkById
apiUrlVirtualNetworks = apiUrlBlueprintById + apiUrlPathDelim + "virtual-networks"
apiUrlVirtualNetworkById = apiUrlVirtualNetworks + apiUrlPathDelim + "%s"
dhcpServiceDisabled = dhcpServiceMode("dhcpServiceDisabled")
dhcpServiceEnabled = dhcpServiceMode("dhcpServiceEnabled")
)
type DhcpServiceEnabled bool
type dhcpServiceMode string
func (o DhcpServiceEnabled) raw() dhcpServiceMode {
if o {
return dhcpServiceEnabled
}
return dhcpServiceDisabled
}
func (o dhcpServiceMode) polish() DhcpServiceEnabled {
return o == dhcpServiceEnabled
}
//const (
// l3ConnectivityEnabled = l3ConnectivityMode("l3Enabled")
// l3ConnectivityDisabled = l3ConnectivityMode("l3Disabled")
//)
//
//type L3ConnectivityMode bool
//type l3ConnectivityMode string
//
//func (o L3ConnectivityMode) raw() l3ConnectivityMode {
// if o {
// return l3ConnectivityEnabled
// }
// return l3ConnectivityDisabled
//}
//
//func (o l3ConnectivityMode) polish() L3ConnectivityMode {
// return o == l3ConnectivityEnabled
//}
type SviIpRequirement int
type sviIpRequirement string
const (
SviIpRequirementNone = SviIpRequirement(iota)
SviIpRequirementOptional
SviIpRequirementForbidden
SviIpRequirementMandatory
SviIpRequirementIntentionConflict
SviIpRequirementUnknown = "SVI IP requirement mode '%s' unknown"
sviIpRequirementNone = sviIpRequirement("")
sviIpRequirementOptional = sviIpRequirement("optional")
sviIpRequirementForbidden = sviIpRequirement("forbidden")
sviIpRequirementMandatory = sviIpRequirement("mandatory")
sviIpRequirementIntentionConflict = sviIpRequirement("intention_conflict")
sviIpRequirementUnknown = "SVI IP requirement mode %d unknown"
)
func (o SviIpRequirement) String() string {
return string(o.raw())
}
func (o SviIpRequirement) int() int {
return int(o)
}
func (o SviIpRequirement) raw() sviIpRequirement {
switch o {
case SviIpRequirementNone:
return sviIpRequirementNone
case SviIpRequirementOptional:
return sviIpRequirementOptional
case SviIpRequirementForbidden:
return sviIpRequirementForbidden
case SviIpRequirementMandatory:
return sviIpRequirementMandatory
case SviIpRequirementIntentionConflict:
return sviIpRequirementIntentionConflict
default:
return sviIpRequirement(fmt.Sprintf(sviIpRequirementUnknown, o))
}
}
func (o sviIpRequirement) string() string {
return string(o)
}
func (o sviIpRequirement) parse() (int, error) {
switch o {
case sviIpRequirementNone:
return int(SviIpRequirementNone), nil
case sviIpRequirementOptional:
return int(SviIpRequirementOptional), nil
case sviIpRequirementForbidden:
return int(SviIpRequirementForbidden), nil
case sviIpRequirementMandatory:
return int(SviIpRequirementMandatory), nil
case sviIpRequirementIntentionConflict:
return int(SviIpRequirementIntentionConflict), nil
default:
return 0, fmt.Errorf(SviIpRequirementUnknown, o)
}
}
type Ipv4Mode int
type ipv4Mode string
const (
Ipv4ModeNone = Ipv4Mode(iota)
Ipv4ModeDisabled
Ipv4ModeEnabled
Ipv4ModeForced
Ipv4ModeUnknown = "unknown IPv4 mode '%s'"
ipv4ModeNone = ipv4Mode("")
ipv4ModeDisabled = ipv4Mode("disabled")
ipv4ModeEnabled = ipv4Mode("enabled")
ipv4ModeForced = ipv4Mode("forced")
ipv4ModeUnknown = "unknown IPv4 mode %d"
)
func (o Ipv4Mode) String() string {
return string(o.raw())
}
func (o Ipv4Mode) int() int {
return int(o)
}
func (o Ipv4Mode) raw() ipv4Mode {
switch o {
case Ipv4ModeNone:
return ipv4ModeNone
case Ipv4ModeDisabled:
return ipv4ModeDisabled
case Ipv4ModeEnabled:
return ipv4ModeEnabled
case Ipv4ModeForced:
return ipv4ModeForced
default:
return ipv4Mode(fmt.Sprintf(ipv4ModeUnknown, o))
}
}
func (o ipv4Mode) string() string {
return string(o)
}
func (o ipv4Mode) parse() (int, error) {
switch o {
case ipv4ModeNone:
return int(Ipv4ModeNone), nil
case ipv4ModeDisabled:
return int(Ipv4ModeDisabled), nil
case ipv4ModeEnabled:
return int(Ipv4ModeEnabled), nil
case ipv4ModeForced:
return int(Ipv4ModeForced), nil
default:
return 0, fmt.Errorf(Ipv4ModeUnknown, o)
}
}
type Ipv6Mode int
type ipv6Mode string
const (
Ipv6ModeNone = Ipv6Mode(iota)
Ipv6ModeDisabled
Ipv6ModeEnabled
Ipv6ModeForced
Ipv6ModeLinkLocal
Ipv6ModeUnknown = "unknown IPv6 mode '%s'"
ipv6ModeNone = ipv6Mode("")
ipv6ModeDisabled = ipv6Mode("disabled")
ipv6ModeEnabled = ipv6Mode("enabled")
ipv6ModeForced = ipv6Mode("forced")
ipv6ModeLinkLocal = ipv6Mode("link_local")
ipv6ModeUnknown = "unknown IPv6 mode %d"
)
func (o Ipv6Mode) String() string {
return string(o.raw())
}
func (o Ipv6Mode) int() int {
return int(o)
}
func (o Ipv6Mode) raw() ipv6Mode {
switch o {
case Ipv6ModeNone:
return ipv6ModeNone
case Ipv6ModeDisabled:
return ipv6ModeDisabled
case Ipv6ModeEnabled:
return ipv6ModeEnabled
case Ipv6ModeLinkLocal:
return ipv6ModeLinkLocal
case Ipv6ModeForced:
return ipv6ModeForced
default:
return ipv6Mode(fmt.Sprintf(ipv6ModeUnknown, o))
}
}
func (o ipv6Mode) string() string {
return string(o)
}
func (o ipv6Mode) parse() (int, error) {
switch o {
case ipv6ModeNone:
return int(Ipv6ModeNone), nil
case ipv6ModeDisabled:
return int(Ipv6ModeDisabled), nil
case ipv6ModeEnabled:
return int(Ipv6ModeEnabled), nil
case ipv6ModeLinkLocal:
return int(Ipv6ModeLinkLocal), nil
case ipv6ModeForced:
return int(Ipv6ModeForced), nil
default:
return 0, fmt.Errorf(Ipv6ModeUnknown, o)
}
}
type VnType int
type vnType string
const (
VnTypeExternal = VnType(iota)
VnTypeVlan
VnTypeVxlan
VnTypeUnknown = "unknown VN type '%s'"
vnTypeExternal = vnType("external")
vnTypeVlan = vnType("vlan")
vnTypeVxlan = vnType("vxlan")
vnTypeUnknown = "unknown VN type '%d'"
)
func (o VnType) String() string {
return string(o.raw())
}
func (o VnType) int() int {
return int(o)
}
func (o *VnType) FromString(s string) error {
i, err := vnType(s).parse()
if err != nil {
return err
}
*o = VnType(i)
return nil
}
func (o VnType) raw() vnType {
switch o {
case VnTypeExternal:
return vnTypeExternal
case VnTypeVlan:
return vnTypeVlan
case VnTypeVxlan:
return vnTypeVxlan
default:
return vnType(fmt.Sprintf(vnTypeUnknown, o))
}
}
func (o vnType) string() string {
return string(o)
}
func (o vnType) parse() (int, error) {
switch o {
case vnTypeExternal:
return int(VnTypeExternal), nil
case vnTypeVlan:
return int(VnTypeVlan), nil
case vnTypeVxlan:
return int(VnTypeVxlan), nil
default:
return 0, fmt.Errorf(VnTypeUnknown, o)
}
}
// AllVirtualNetworkTypes returns the []VnType representing
// each supported VnType
func AllVirtualNetworkTypes() []VnType {
i := 0
var result []VnType
for {
var os VnType
err := os.FromString(VnType(i).String())
if err != nil {
return result[:i]
}
result = append(result, os)
i++
}
}
type SystemRole int
type systemRole string
const (
SystemRoleNone = SystemRole(iota)
SystemRoleAccess
SystemRoleGeneric
SystemRoleLeaf
SystemRoleSpine
SystemRoleSuperSpine
SystemRoleRedundancyGroup
SystemRoleUnknown = "unknown System Role '%s'"
systemRoleNone = systemRole("")
systemRoleAccess = systemRole("access")
systemRoleGeneric = systemRole("generic")
systemRoleLeaf = systemRole("leaf")
systemRoleSpine = systemRole("spine")
systemRoleSuperSpine = systemRole("superspine")
systemRoleRedundancyGroup = systemRole("redundancy_group")
systemRoleUnknown = "unknown System Role '%d'"
)
func (o SystemRole) String() string {
return string(o.raw())
}
func (o SystemRole) int() int {
return int(o)
}
func (o SystemRole) raw() systemRole {
switch o {
case SystemRoleNone:
return systemRoleNone
case SystemRoleAccess:
return systemRoleAccess
case SystemRoleGeneric:
return systemRoleGeneric
case SystemRoleLeaf:
return systemRoleLeaf
case SystemRoleSpine:
return systemRoleSpine
case SystemRoleSuperSpine:
return systemRoleSuperSpine
case SystemRoleRedundancyGroup:
return systemRoleRedundancyGroup
default:
return systemRole(fmt.Sprintf(systemRoleUnknown, o))
}
}
func (o *SystemRole) FromString(in string) error {
i, err := systemRole(in).parse()
if err != nil {
return err
}
*o = SystemRole(i)
return nil
}
func (o systemRole) string() string {
return string(o)
}
func (o systemRole) parse() (int, error) {
switch o {
case systemRoleNone:
return int(SystemRoleNone), nil
case systemRoleAccess:
return int(SystemRoleAccess), nil
case systemRoleGeneric:
return int(SystemRoleGeneric), nil
case systemRoleLeaf:
return int(SystemRoleLeaf), nil
case systemRoleSpine:
return int(SystemRoleSpine), nil
case systemRoleSuperSpine:
return int(SystemRoleSuperSpine), nil
case systemRoleRedundancyGroup:
return int(SystemRoleRedundancyGroup), nil
default:
return 0, fmt.Errorf(SystemRoleUnknown, o)
}
}
type SviIp struct {
SystemId ObjectId `json:"system_id"`
Ipv4Addr net.IP `json:"ipv4_addr"`
Ipv4Mode Ipv4Mode `json:"ipv4_mode"`
Ipv4Requirement SviIpRequirement `json:"ipv4_requirement"`
Ipv6Addr net.IP `json:"ipv6_addr"`
Ipv6Mode Ipv6Mode `json:"ipv6_mode"`
Ipv6Requirement SviIpRequirement `json:"ipv6_requirement"`
}
func (o *SviIp) raw() *rawSviIp {
var ipv4Addr, ipv6Addr string
if len(o.Ipv4Addr) != 0 {
ipv4Addr = o.Ipv4Addr.String()
}
if len(o.Ipv6Addr) != 0 {
ipv6Addr = o.Ipv6Addr.String()
}
return &rawSviIp{
SystemId: o.SystemId,
Ipv4Addr: ipv4Addr,
Ipv4Mode: o.Ipv4Mode.raw(),
Ipv4Requirement: o.Ipv4Requirement.raw(),
Ipv6Addr: ipv6Addr,
Ipv6Mode: o.Ipv6Mode.raw(),
Ipv6Requirement: o.Ipv6Requirement.raw(),
}
}
type rawSviIp struct {
Ipv4Addr string `json:"ipv4_addr,omitempty"`
Ipv4Mode ipv4Mode `json:"ipv4_mode,omitempty"`
Ipv4Requirement sviIpRequirement `json:"ipv4_requirement,omitempty"` // not present in swagger example, not present in GET
Ipv6Addr string `json:"ipv6_addr,omitempty"`
Ipv6Mode ipv6Mode `json:"ipv6_mode,omitempty"`
Ipv6Requirement sviIpRequirement `json:"ipv6_requirement,omitempty"` // not present in swagger example, not present in GET
SystemId ObjectId `json:"system_id"`
}
func (o *rawSviIp) parse() (*SviIp, error) {
var ipv4Addr, ipv6Addr net.IP
var err error
if o.Ipv4Addr != "" {
ipv4Addr, _, err = net.ParseCIDR(o.Ipv4Addr)
if err != nil {
return nil, err
}
}
if o.Ipv6Addr != "" {
ipv6Addr, _, err = net.ParseCIDR(o.Ipv6Addr)
if err != nil {
return nil, err
}
}
ipv4mode, err := o.Ipv4Mode.parse()
if err != nil {
return nil, err
}
ipv6mode, err := o.Ipv6Mode.parse()
if err != nil {
return nil, err
}
ipv4Requirement, err := o.Ipv4Requirement.parse()
if err != nil {
return nil, err
}
ipv6Requirement, err := o.Ipv6Requirement.parse()
if err != nil {
return nil, err
}
return &SviIp{
SystemId: o.SystemId,
Ipv4Addr: ipv4Addr,
Ipv4Mode: Ipv4Mode(ipv4mode),
Ipv4Requirement: SviIpRequirement(ipv4Requirement),
Ipv6Addr: ipv6Addr,
Ipv6Mode: Ipv6Mode(ipv6mode),
Ipv6Requirement: SviIpRequirement(ipv6Requirement),
}, nil
}
type VnBinding struct {
//AccessSwitches []interface `json:"access_switches"`
AccessSwitchNodeIds []ObjectId `json:"access_switch_node_ids"`
//Role string `json:"role"` // so far: "leaf", possibly graphdb "role" element
SystemId ObjectId `json:"system_id"` // graphdb node id of a leaf (so far) switch
VlanId *Vlan `json:"vlan_id"` // optional (auto-assign)
//Selected bool `json:"selected?"`
// Tags []interface `json:"tags"` //sent as empty string by 4.1.2 web UI, not seen in 4.1.0 or 4.1.1
//PodData struct {
// Description interface{} `json:"description"`
// GlobalCatalogId interface{} `json:"global_catalog_id"`
// Label string `json:"label"`
// Position int `json:"position"`
// Type string `json:"type"`
// Id string `json:"id"`
//} `json:"pod-data"`
}
type VirtualNetwork struct {
Id ObjectId
Data *VirtualNetworkData
}
type Endpoint struct {
InterfaceId ObjectId `json:"interface_id"`
TagType string `json:"tag_type"`
Label string `json:"label"`
}
type VirtualNetworkData struct {
DhcpService DhcpServiceEnabled
Ipv4Enabled bool
Ipv4Subnet *net.IPNet
Ipv6Enabled bool
Ipv6Subnet *net.IPNet
L3Mtu *int
Label string
ReservedVlanId *Vlan
RouteTarget string
RtPolicy *RtPolicy
SecurityZoneId ObjectId
SviIps []SviIp
VirtualGatewayIpv4 net.IP
VirtualGatewayIpv6 net.IP
VirtualGatewayIpv4Enabled bool
VirtualGatewayIpv6Enabled bool
VnBindings []VnBinding
VnId *VNI
VnType VnType
VirtualMac net.HardwareAddr
}
func (o *VirtualNetworkData) raw() *rawVirtualNetwork {
var ipv4Subnet, ipv6Subnet string
if o.Ipv4Subnet != nil {
ipv4Subnet = o.Ipv4Subnet.String()
}
if o.Ipv6Subnet != nil {
ipv6Subnet = o.Ipv6Subnet.String()
}
var l3Mtu *int
if o.L3Mtu != nil {
mtu := *o.L3Mtu
l3Mtu = &mtu
}
sviIps := make([]rawSviIp, len(o.SviIps))
for i := range o.SviIps {
sviIps[i] = *o.SviIps[i].raw()
}
var virtualGatewayIpv4, virtualGatewayIpv6 string
if len(o.VirtualGatewayIpv4.To4()) == net.IPv4len {
virtualGatewayIpv4 = o.VirtualGatewayIpv4.String()
}
if len(o.VirtualGatewayIpv6) == net.IPv6len {
virtualGatewayIpv6 = o.VirtualGatewayIpv6.String()
}
var vnId string
if o.VnId != nil {
vnId = strconv.Itoa(int(*o.VnId))
}
return &rawVirtualNetwork{
DhcpService: o.DhcpService.raw(),
Ipv4Enabled: o.Ipv4Enabled,
Ipv4Subnet: ipv4Subnet,
Ipv6Enabled: o.Ipv6Enabled,
Ipv6Subnet: ipv6Subnet,
L3Mtu: l3Mtu,
Label: o.Label,
ReservedVlanId: o.ReservedVlanId,
RouteTarget: o.RouteTarget,
RtPolicy: o.RtPolicy,
SecurityZoneId: o.SecurityZoneId,
SviIps: sviIps,
VirtualGatewayIpv4: virtualGatewayIpv4,
VirtualGatewayIpv6: virtualGatewayIpv6,
VirtualGatewayIpv4Enabled: o.VirtualGatewayIpv4Enabled,
VirtualGatewayIpv6Enabled: o.VirtualGatewayIpv6Enabled,
VnBindings: o.VnBindings,
VnId: vnId,
VnType: o.VnType.raw(),
VirtualMac: o.VirtualMac.String(),
}
}
type rawVirtualNetwork struct {
Id ObjectId `json:"id,omitempty"`
DhcpService dhcpServiceMode `json:"dhcp_service"`
Ipv4Enabled bool `json:"ipv4_enabled"`
Ipv4Subnet string `json:"ipv4_subnet,omitempty"`
Ipv6Enabled bool `json:"ipv6_enabled"`
Ipv6Subnet string `json:"ipv6_subnet,omitempty"`
L3Mtu *int `json:"l3_mtu,omitempty"`
Label string `json:"label"`
ReservedVlanId *Vlan `json:"reserved_vlan_id,omitempty"`
RouteTarget string `json:"route_target,omitempty"` // not mentioned in swagger, seen in 4.1.1: "10000:1"
RtPolicy *RtPolicy `json:"rt_policy"`
SecurityZoneId ObjectId `json:"security_zone_id,omitempty"`
SviIps []rawSviIp `json:"svi_ips"`
VirtualGatewayIpv4 string `json:"virtual_gateway_ipv4,omitempty"`
VirtualGatewayIpv6 string `json:"virtual_gateway_ipv6,omitempty"`
VirtualGatewayIpv4Enabled bool `json:"virtual_gateway_ipv4_enabled"`
VirtualGatewayIpv6Enabled bool `json:"virtual_gateway_ipv6_enabled"`
VnBindings []VnBinding `json:"bound_to"`
VnId string `json:"vn_id,omitempty"` // VNI as a string, null when unset
VnType vnType `json:"vn_type"`
VirtualMac string `json:"virtual_mac,omitempty"`
//CreatePolicyTagged bool `json:"create_policy_tagged"`
//CreatePolicyUntagged bool `json:"create_policy_untagged"`
//DefaultEndpointTagTypes interface{} `json:"default_endpoint_tag_types"` // what is this? not present in 4.1.1 api response
//Description string `json:"description"` // not used in the web UI
//FloatingIps []interface{} `json:"floating_ips"` // seen in 4.1.1 api response
//ForceMoveUntaggedEndpoints bool `json:"force_move_untagged_endpoints"` // not used in post/get with web UI
//L3Connectivity *l3ConnectivityMode `json:"l3_connectivity,omitempty"` // does not appear in 4.1.2 swagger
//VniIds []interface{} `json:"vni_ids,omitempty"` // unknown, sent by web UI as empty list
//Endpoints []interface{} `json:"endpoints"` // unknown, maybe relates to servers, etc?
}
func (o rawVirtualNetwork) polish() (*VirtualNetwork, error) {
var err error
var ipv4Subnet *net.IPNet
if o.Ipv4Subnet != "" {
_, ipv4Subnet, err = net.ParseCIDR(o.Ipv4Subnet)
if err != nil {
return nil, err
}
}
var ipv6Subnet *net.IPNet
if o.Ipv6Subnet != "" {
_, ipv6Subnet, err = net.ParseCIDR(o.Ipv6Subnet)
if err != nil {
return nil, err
}
}
var l3mtu *int
if o.L3Mtu != nil {
mtu := *o.L3Mtu
l3mtu = &mtu
}
sviIps := make([]SviIp, len(o.SviIps))
for i, sviIp := range o.SviIps {
SviIp, err := sviIp.parse()
if err != nil {
return nil, err
}
sviIps[i] = *SviIp
}
var vnId *VNI
if o.VnId != "" {
vniUint64, err := strconv.ParseUint(o.VnId, 10, 32)
if err != nil {
return nil, fmt.Errorf("error parsing VNID from string %q - %w", o.VnId, err)
}
vni := VNI(uint32(vniUint64))
vnId = &vni
}
vntype, err := o.VnType.parse()
if err != nil {
return nil, err
}
var virtualMac net.HardwareAddr
if o.VirtualMac != "" {
virtualMac, err = net.ParseMAC(o.VirtualMac)
if err != nil {
return nil, fmt.Errorf("error parsing mac address %q - %w", o.VirtualMac, err)
}
}
return &VirtualNetwork{
Id: o.Id,
Data: &VirtualNetworkData{
DhcpService: o.DhcpService.polish(),
Ipv4Enabled: o.Ipv4Enabled,
Ipv4Subnet: ipv4Subnet,
Ipv6Enabled: o.Ipv6Enabled,
Ipv6Subnet: ipv6Subnet,
L3Mtu: l3mtu,
Label: o.Label,
ReservedVlanId: o.ReservedVlanId,
RouteTarget: o.RouteTarget,
RtPolicy: o.RtPolicy,
SecurityZoneId: o.SecurityZoneId,
SviIps: sviIps,
VirtualGatewayIpv4: net.ParseIP(o.VirtualGatewayIpv4),
VirtualGatewayIpv6: net.ParseIP(o.VirtualGatewayIpv6),
VirtualGatewayIpv4Enabled: o.VirtualGatewayIpv4Enabled,
VirtualGatewayIpv6Enabled: o.VirtualGatewayIpv6Enabled,
VnBindings: o.VnBindings,
VnId: vnId,
VnType: VnType(vntype),
VirtualMac: virtualMac,
},
}, nil
}
func (o *TwoStageL3ClosClient) listAllVirtualNetworkIds(ctx context.Context) ([]ObjectId, error) {
apstraUrl, err := o.urlWithParam(fmt.Sprintf(apiUrlVirtualNetworks, o.blueprintId))
if err != nil {
return nil, err
}
response := &struct {
VirtualNetworks map[ObjectId]rawVirtualNetwork `json:"virtual_networks"`
}{}
err = o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
url: apstraUrl,
apiResponse: response,
unsynchronized: true,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
result := make([]ObjectId, len(response.VirtualNetworks))
i := 0
for id := range response.VirtualNetworks {
result[i] = id
i++
}
return result, nil
}
func (o *TwoStageL3ClosClient) getVirtualNetwork(ctx context.Context, vnId ObjectId) (*rawVirtualNetwork, error) {
apstraUrl, err := o.urlWithParam(fmt.Sprintf(apiUrlVirtualNetworkById, o.blueprintId, vnId))
if err != nil {
return nil, err
}
response := &rawVirtualNetwork{}
err = o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
url: apstraUrl,
apiResponse: response,
unsynchronized: true,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
return response, nil
}
func (o *TwoStageL3ClosClient) getVirtualNetworkByName(ctx context.Context, name string) (*rawVirtualNetwork, error) {
rawVns, err := o.getAllVirtualNetworks(ctx)
if err != nil {
return nil, err
}
var found int
var rawVn rawVirtualNetwork
for i := range rawVns {
if rawVns[i].Label == name {
found++
rawVn = rawVns[i]
}
}
switch found {
case 0:
return nil, ClientErr{
errType: ErrNotfound,
err: fmt.Errorf("virtual network with label %q not found", name),
}
case 1:
// re-fetch is required here because data is missing when we "get all".
return o.getVirtualNetwork(ctx, rawVn.Id)
default:
return nil, ClientErr{
errType: ErrMultipleMatch,
err: fmt.Errorf("found %d virtual networks with label %q", found, name),
}
}
}
func (o *TwoStageL3ClosClient) getAllVirtualNetworks(ctx context.Context) (map[ObjectId]rawVirtualNetwork, error) {
var response struct {
VirtualNetworks map[ObjectId]rawVirtualNetwork `json:"virtual_networks"`
}
err := o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: fmt.Sprintf(apiUrlVirtualNetworks, o.blueprintId),
apiResponse: &response,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
return response.VirtualNetworks, nil
}
func (o *TwoStageL3ClosClient) createVirtualNetwork(ctx context.Context, cfg *rawVirtualNetwork) (ObjectId, error) {
if cfg.Id != "" {
return "", fmt.Errorf("refusing to create virtual network using input data with a populated ID field")
}
response := &objectIdResponse{}
err := o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodPost,
urlStr: fmt.Sprintf(apiUrlVirtualNetworks, o.blueprintId),
apiInput: cfg,
apiResponse: response,
})
if err != nil {
return "", convertTtaeToAceWherePossible(err)
}
return response.Id, nil
}
func (o *TwoStageL3ClosClient) updateVirtualNetwork(ctx context.Context, id ObjectId, cfg *rawVirtualNetwork) error {
if cfg.Id != "" {
return fmt.Errorf("refusing to update virtual network using input data with a populated ID field")
}
err := o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodPut,
urlStr: fmt.Sprintf(apiUrlVirtualNetworkById, o.blueprintId, id),
apiInput: cfg,
})
if err != nil {
return convertTtaeToAceWherePossible(err)
}
return nil
}
func (o *TwoStageL3ClosClient) deleteVirtualNetwork(ctx context.Context, id ObjectId) error {
err := o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodDelete,
urlStr: fmt.Sprintf(apiUrlVirtualNetworkById, o.blueprintId, id),
})
if err != nil {
return convertTtaeToAceWherePossible(err)
}
return nil
}