/
two_stage_l3_clos_connectivity_template.go
731 lines (622 loc) · 22 KB
/
two_stage_l3_clos_connectivity_template.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
package apstra
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
)
const (
apiUrlBlueprintObjPolicyImport = apiUrlBlueprintById + apiUrlPathDelim + "obj-policy-import"
apiUrlBlueprintObjPolicyExport = apiUrlBlueprintById + apiUrlPathDelim + "obj-policy-export"
apiUrlBlueprintObjPolicyExportById = apiUrlBlueprintObjPolicyExport + apiUrlPathDelim + "%s"
apiUrlBlueprintEndpointPolicies = apiUrlBlueprintById + apiUrlPathDelim + "endpoint-policies"
apiUrlBlueprintEndpointPolicyById = apiUrlBlueprintEndpointPolicies + apiUrlPathDelim + "%s"
deleteRecursive = "delete_recursive"
policyTypeBatchSuffix = " (" + ctPrimitivePolicyTypeNameBatch + ")"
policyTypePipelineSuffix = " (" + ctPrimitivePolicyTypeNamePipeline + ")"
xInitialPosition = 290
yInitialPosition = 80
xSpacing = 200
ySpacing = 70
)
type rawConnectivityTemplateState struct {
Id ObjectId `json:"id"`
Status ctPrimitiveStatus `json:"status"`
AppPointsCount int `json:"app_points_count"`
MissingAttributes map[string]string `json:"missing_attributes"`
Visible bool `json:"visible"`
}
func (o rawConnectivityTemplateState) polish() (*ConnectivityTemplateState, error) {
if !o.Visible {
return nil, fmt.Errorf("attempt to polish rawConnectivityTemplateState %q which is not visible", o.Id)
}
status, err := o.Status.parse()
if err != nil {
return nil, err
}
return &ConnectivityTemplateState{
Id: o.Id,
Status: CtPrimitiveStatus(status),
AppPointsCount: o.AppPointsCount,
MissingAttributes: o.MissingAttributes,
}, nil
}
type ConnectivityTemplateState struct {
Id ObjectId
Status CtPrimitiveStatus
AppPointsCount int
MissingAttributes map[string]string
}
type ConnectivityTemplate struct {
Id *ObjectId
Label string
Description string
Subpolicies []*ConnectivityTemplatePrimitive // batch pointers
Tags []string
UserData *ConnectivityTemplatePrimitiveUserData
}
func (o *ConnectivityTemplate) raw() (*rawConnectivityTemplate, error) {
err := o.SetIds()
if err != nil {
return nil, err
}
subpolicyIds := make([]ObjectId, len(o.Subpolicies))
for i, primitivePtr := range o.Subpolicies {
err = primitivePtr.setIds()
if err != nil {
return nil, err
}
subpolicyIds[i] = *primitivePtr.PipelineId
}
rawPolicies, err := rawBatch(*o.Id, "", "", o.Subpolicies)
if err != nil {
return nil, err
}
if o.Tags == nil {
o.Tags = []string{}
}
userDataBytes, err := json.Marshal(o.UserData)
if err != nil {
return nil, fmt.Errorf("failed marshaling user data - %w", err)
}
userDataString := string(userDataBytes)
// special handling for root batch fields
rawPolicies[0].Description = o.Description
rawPolicies[0].Label = o.Label
rawPolicies[0].Visible = true
rawPolicies[0].Tags = o.Tags
rawPolicies[0].UserData = &userDataString
return &rawConnectivityTemplate{
Policies: rawPolicies,
}, nil
}
// SetIds walks the Connectivity Template tree and sets all "batch", "pipeline"
// and "actual" object IDs which aren't set, but need to be. Batch IDs where
// no children exist will not be set.
func (o *ConnectivityTemplate) SetIds() error {
if o.Id == nil {
uuid, err := uuid1AsObjectId()
if err != nil {
return err
}
o.Id = &uuid
}
for _, subpolicy := range o.Subpolicies {
err := subpolicy.setIds()
if err != nil {
return err
}
}
return nil
}
// SetUserData builds the top level `user_data` struct. It tries to lay the
// primitive "sausages" out sensibly.
func (o *ConnectivityTemplate) SetUserData() error {
o.UserData = &ConnectivityTemplatePrimitiveUserData{
IsSausage: true,
Positions: make(map[ObjectId][]float64),
}
var width int
for _, subpolicy := range o.Subpolicies {
xPosition := float64(xInitialPosition + (width * xSpacing))
additionalPositions, subpolicyWidth, err := subpolicy.positions(xPosition, yInitialPosition)
if err != nil {
return err
}
mergePositionMaps(&o.UserData.Positions, &additionalPositions)
if subpolicyWidth > width {
width += subpolicyWidth
}
}
return nil
}
type rawConnectivityTemplate struct {
Policies []rawConnectivityTemplatePolicy `json:"policies"`
}
func (o *rawConnectivityTemplate) rootBatchIds() []ObjectId {
var result []ObjectId
for _, rawPolicy := range o.Policies {
if rawPolicy.Visible {
result = append(result, rawPolicy.Id)
}
}
return result
}
func (o *rawConnectivityTemplate) policyMap() map[ObjectId]rawConnectivityTemplatePolicy {
result := make(map[ObjectId]rawConnectivityTemplatePolicy, len(o.Policies))
for _, policy := range o.Policies {
result[policy.Id] = policy
}
return result
}
func (o *rawConnectivityTemplate) polish(id ObjectId) (*ConnectivityTemplate, error) {
if len(o.Policies) == 0 {
return nil, fmt.Errorf("cannot polish a rawConnectivityTemplate with no policies")
}
policyMap := o.policyMap()
rootBatch, ok := policyMap[id]
if !ok {
return nil, fmt.Errorf("root batch policy %q not found", id)
}
if rootBatch.UserData == nil {
return nil, fmt.Errorf("connectivity template root batch has no user data")
}
if ctPrimitivePolicyTypeNameBatch != rootBatch.PolicyTypeName {
return nil, fmt.Errorf("expected policy %q to be type %q, got %q",
rootBatch.Id, ctPrimitivePolicyTypeNameBatch, rootBatch.PolicyTypeName)
}
delete(policyMap, rootBatch.Id)
var userData ConnectivityTemplatePrimitiveUserData
err := json.Unmarshal([]byte(*rootBatch.UserData), &userData)
if err != nil {
return nil, fmt.Errorf("error unmarshaling root batch %q user data %q - %w",
rootBatch.Id, *rootBatch.UserData, err)
}
var attributes rawBatchAttributes
err = json.Unmarshal(rootBatch.Attributes, &attributes)
if err != nil {
return nil, fmt.Errorf("error unmarshaling root batch %q attributes %q - %w",
rootBatch.Id, *rootBatch.UserData, err)
}
subpolicies := make([]*ConnectivityTemplatePrimitive, len(attributes.Subpolicies))
for i, policyId := range attributes.Subpolicies {
subpolicies[i], err = parsePrimitiveTreeByPipelineId(policyId, policyMap)
if err != nil {
return nil, err
}
}
return &ConnectivityTemplate{
Id: &rootBatch.Id,
Label: rootBatch.Label,
Description: rootBatch.Description,
Subpolicies: subpolicies,
Tags: rootBatch.Tags,
UserData: &userData,
}, nil
}
type ConnectivityTemplatePrimitiveUserData struct {
IsSausage bool `json:"isSausage"`
Positions map[ObjectId][]float64 `json:"positions"`
}
type ConnectivityTemplatePrimitive struct {
Id *ObjectId
Label string
Attributes ConnectivityTemplatePrimitiveAttributes
Subpolicies []*ConnectivityTemplatePrimitive // batch of pointers to pipelines
BatchId *ObjectId
PipelineId *ObjectId
}
// positions returns position data suitable for Web UI layout and an integer
// which indicates the maximum width in "sausages" of the subpolicy tree.
func (o *ConnectivityTemplatePrimitive) positions(x, y float64) (map[ObjectId][]float64, int, error) {
if o.Id == nil {
return nil, 0, errors.New("attempt to define connectivity template primitive position without setting ID")
}
var width int
positions := make(map[ObjectId][]float64)
positions[*o.Id] = []float64{x, y, 1}
for _, subpolicy := range o.Subpolicies {
additionalPositions, subpolicyWidth, err := subpolicy.positions(x+float64(width*xSpacing), y+ySpacing)
if err != nil {
return nil, 0, err
}
mergePositionMaps(&positions, &additionalPositions)
width += subpolicyWidth
}
if width == 0 {
width++
}
return positions, width, nil
}
// rawPipeline returns []rawConnectivityTemplatePolicy consisting of:
// - a pipeline policy element
// - the actual policy element
// - if there are any children, a batch policy element containing downstream primitives
func (o *ConnectivityTemplatePrimitive) rawPipeline() ([]rawConnectivityTemplatePolicy, error) {
if o.Attributes == nil {
return nil, errors.New("rawPipeline() invoked with nil attributes")
}
err := o.setIds()
if err != nil {
return nil, err
}
attributes := o.Attributes
rawAttributes, err := attributes.raw()
if err != nil {
return nil, err
}
// "actual"
actual := rawConnectivityTemplatePolicy{
Description: attributes.Description(),
Tags: []string{}, // always empty slice
Label: o.Label,
PolicyTypeName: attributes.PolicyTypeName().raw(),
Attributes: rawAttributes,
Id: *o.Id,
}
var secondSubpolicy *ObjectId
if len(o.Subpolicies) > 0 {
secondSubpolicy = o.BatchId
}
pipelineAttributes := rawPipelineAttributes{
FirstSubpolicy: *o.Id,
SecondSubpolicy: secondSubpolicy,
Resolver: nil,
}
rawPipelineAttribtes, err := json.Marshal(&pipelineAttributes)
if err != nil {
return nil, fmt.Errorf("failed marshaling pipelineAttributes - %w", err)
}
pipeline := rawConnectivityTemplatePolicy{
Description: attributes.Description(),
Tags: []string{}, // always empty slice
Label: o.Label + string(policyTypePipelineSuffix),
PolicyTypeName: ctPrimitivePolicyTypeNamePipeline,
Attributes: rawPipelineAttribtes,
Id: *o.PipelineId,
}
result := []rawConnectivityTemplatePolicy{pipeline, actual}
if len(o.Subpolicies) > 0 {
batchPolicies, err := rawBatch(*o.BatchId, attributes.Description(), o.Label, o.Subpolicies)
if err != nil {
return nil, err
}
result = append(result, batchPolicies...)
}
return result, nil
}
func (o *ConnectivityTemplatePrimitive) setIds() error {
if o.Id == nil {
uuid, err := uuid1AsObjectId()
if err != nil {
return err
}
o.Id = &uuid
}
if o.PipelineId == nil {
uuid, err := uuid1AsObjectId()
if err != nil {
return err
}
o.PipelineId = &uuid
}
if o.BatchId == nil && len(o.Subpolicies) > 0 {
uuid, err := uuid1AsObjectId()
if err != nil {
return err
}
o.BatchId = &uuid
}
for _, subpolicy := range o.Subpolicies {
err := subpolicy.setIds()
if err != nil {
return err
}
}
return nil
}
// A rawConnectivityTemplatePolicy is the base building block of a CT primitive
// (CT building block in the web UI) in the Apstra API. Each CT primitive is
// composed of 2 or 3 (when it has children) rawConnectivityTemplatePolicy
// structs.
//
// The Attributes element can take any of 12 forms: "pipeline", "batch", or
// one of the 10 implementations of ConnectivityTemplatePrimitiveAttributes.
// "pipeline" and "batch" are used to provide the tree structure which forms a
// CT as seen in the web UI.
type rawConnectivityTemplatePolicy struct {
Id ObjectId `json:"id"`
Label string `json:"label"`
Description string `json:"description"`
Tags []string `json:"tags"`
UserData *string `json:"user_data,omitempty"`
Visible bool `json:"visible"`
PolicyTypeName ctPrimitivePolicyTypeName `json:"policy_type_name"`
Attributes json.RawMessage `json:"attributes"`
}
func (o rawConnectivityTemplatePolicy) attributes() (ConnectivityTemplatePrimitiveAttributes, error) {
var result ConnectivityTemplatePrimitiveAttributes
switch o.PolicyTypeName {
case ctPrimitivePolicyTypeNameAttachSingleVlan:
result = new(ConnectivityTemplatePrimitiveAttributesAttachSingleVlan)
case ctPrimitivePolicyTypeNameAttachMultipleVlan:
result = new(ConnectivityTemplatePrimitiveAttributesAttachMultipleVlan)
case ctPrimitivePolicyTypeNameAttachLogicalLink:
result = new(ConnectivityTemplatePrimitiveAttributesAttachLogicalLink)
case ctPrimitivePolicyTypeNameAttachStaticRoute:
result = new(ConnectivityTemplatePrimitiveAttributesAttachStaticRoute)
case ctPrimitivePolicyTypeNameAttachCustomStaticRoute:
result = new(ConnectivityTemplatePrimitiveAttributesAttachCustomStaticRoute)
case ctPrimitivePolicyTypeNameAttachIpEndpointWithBgpNsxt:
result = new(ConnectivityTemplatePrimitiveAttributesAttachIpEndpointWithBgpNsxt)
case ctPrimitivePolicyTypeNameAttachBgpOverSubinterfacesOrSvi:
result = new(ConnectivityTemplatePrimitiveAttributesAttachBgpOverSubinterfacesOrSvi)
case ctPrimitivePolicyTypeNameAttachBgpWithPrefixPeeringForSviOrSubinterface:
result = new(ConnectivityTemplatePrimitiveAttributesAttachBgpWithPrefixPeeringForSviOrSubinterface)
case ctPrimitivePolicyTypeNameAttachExistingRoutingPolicy:
result = new(ConnectivityTemplatePrimitiveAttributesAttachExistingRoutingPolicy)
case ctPrimitivePolicyTypeNameAttachRoutingZoneConstraint:
result = new(ConnectivityTemplatePrimitiveAttributesAttachRoutingZoneConstraint)
default:
return nil, fmt.Errorf("unhandled connectivity template type %q", o.PolicyTypeName)
}
err := result.fromRawJson(o.Attributes)
if err != nil {
return nil, err
}
return result, err
}
// rawBatchAttributes
// Each "batch" policy, including the root batch keeps a list of child policies.
// These sub-policies are identified by ID. Each one is a "pipeline" policy.
type rawBatchAttributes struct {
Subpolicies []ObjectId `json:"subpolicies"`
}
// rawPipelineAttributes
// each "pipeline" policy identifies an actual CT primitive policy (VLAN, BGP
// stuff, static route, etc...) by ID in the FirstSubpolicy element. When the
// CT primitive has child primitives, the SecondSubpolicy element identifies a
// downstream "batch" policy. When there are no child primitives, SeconSubpolicy
// is nil.
type rawPipelineAttributes struct {
FirstSubpolicy ObjectId `json:"first_subpolicy"`
SecondSubpolicy *ObjectId `json:"second_subpolicy"`
Resolver interface{} `json:"resolver"` // what is this?
}
func rawBatch(id ObjectId, description, label string, subpolicies []*ConnectivityTemplatePrimitive) ([]rawConnectivityTemplatePolicy, error) {
// build downstream pipelines and collect their IDs
var pipelines []rawConnectivityTemplatePolicy
subpolicyIds := make([]ObjectId, len(subpolicies))
for i, subpolicy := range subpolicies {
pipelineSlice, err := subpolicy.rawPipeline()
if err != nil {
return nil, err
}
subpolicyIds[i] = pipelineSlice[0].Id
pipelines = append(pipelines, pipelineSlice...)
}
rawAttributes, err := json.Marshal(&rawBatchAttributes{Subpolicies: subpolicyIds})
if err != nil {
return nil, fmt.Errorf("failed marshaling subpolicy ids for batch - %w", err)
}
batch := rawConnectivityTemplatePolicy{
Description: description,
Tags: []string{},
Label: label + string(policyTypeBatchSuffix),
PolicyTypeName: ctPrimitivePolicyTypeNameBatch,
Attributes: rawAttributes,
Id: id,
}
return append([]rawConnectivityTemplatePolicy{batch}, pipelines...), nil
}
func mergePositionMaps(dst, src *map[ObjectId][]float64) {
t := *dst
for k, v := range *src {
t[k] = v
}
}
// parsePrimitiveTreeByPipelineId takes an entrypoint ObjectId representing a
// "pipeline" policy and a map of rawConnectivityTemplatePolicy including
// the specified pipeline and all of its children.
//
// The returned *ConnectivityTemplatePrimitive is a tree built by recursive
// invocations of parsePrimitiveTreeByPipelineId until the tree is complete.
//
// parsePrimitiveTreeByPipelineId should be invoked once for each sub-policy in
// a connectivity template's root batch.
func parsePrimitiveTreeByPipelineId(pipelineId ObjectId, policyMap map[ObjectId]rawConnectivityTemplatePolicy) (*ConnectivityTemplatePrimitive, error) {
var actual, batch, pipeline rawConnectivityTemplatePolicy
var ok bool
if pipeline, ok = policyMap[pipelineId]; !ok {
return nil, fmt.Errorf("raw policy map doesn't include pipeline policy %q", pipelineId)
}
if ctPrimitivePolicyTypeNamePipeline != pipeline.PolicyTypeName {
return nil, fmt.Errorf("expected policy %q to be type %q, got %q",
pipeline.Id, ctPrimitivePolicyTypeNamePipeline, pipeline.PolicyTypeName)
}
var pipelineAttributes rawPipelineAttributes
err := json.Unmarshal(pipeline.Attributes, &pipelineAttributes)
if err != nil {
return nil, fmt.Errorf("failed unmarshaling pipeline attributes %q for policy %q - %w",
pipeline.Attributes, pipeline.Id, err)
}
if actual, ok = policyMap[pipelineAttributes.FirstSubpolicy]; !ok {
return nil, fmt.Errorf("raw policy map doesn't include actual policy %q", pipelineAttributes.FirstSubpolicy)
}
var actualType CtPrimitivePolicyTypeName
err = actualType.FromString(string(actual.PolicyTypeName))
if err != nil {
return nil, fmt.Errorf("failed to parse policy type from CT policy %q - %w", actual.Id, err)
}
var batchId *ObjectId
var subpolicies []*ConnectivityTemplatePrimitive
if pipelineAttributes.SecondSubpolicy != nil {
// a batch ID appears in the pipeline
if batch, ok = policyMap[*pipelineAttributes.SecondSubpolicy]; ok {
// the batch was found in the map
if ctPrimitivePolicyTypeNameBatch != batch.PolicyTypeName {
// batch ID has wrong policy type (not batch)
return nil, fmt.Errorf("expected policy %q to be type %q, got %q",
batch.Id, ctPrimitivePolicyTypeNameBatch, batch.PolicyTypeName)
}
var batchAttributes rawBatchAttributes
err = json.Unmarshal(batch.Attributes, &batchAttributes)
if err != nil {
return nil, fmt.Errorf("failed unmarshaling batch attributes %q for policy %q - %w",
batch.Attributes, batch.Id, err)
}
batchId = &batch.Id
subpolicies = make([]*ConnectivityTemplatePrimitive, len(batchAttributes.Subpolicies))
for i, subpolicyId := range batchAttributes.Subpolicies {
subpolicies[i], err = parsePrimitiveTreeByPipelineId(subpolicyId, policyMap)
if err != nil {
return nil, err
}
}
}
}
attributes, err := actual.attributes()
if err != nil {
return nil, fmt.Errorf("failed to load attributes \"%s\" for policy %q - %w",
actual.Attributes, actual.Id, err)
}
return &ConnectivityTemplatePrimitive{
Id: &actual.Id,
Label: actual.Label,
Attributes: attributes,
Subpolicies: subpolicies,
BatchId: batchId,
PipelineId: &pipeline.Id,
}, nil
}
func (o *TwoStageL3ClosClient) ListConnectivityTemplates(ctx context.Context) ([]ObjectId, error) {
var apiResponse struct {
Policies []rawConnectivityTemplatePolicy `json:"policies"`
}
err := o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: fmt.Sprintf(apiUrlBlueprintObjPolicyExport, o.blueprintId),
apiResponse: &apiResponse,
doNotLogin: false,
unsynchronized: false,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
var result []ObjectId
for _, policy := range apiResponse.Policies {
if policy.Visible {
result = append(result, policy.Id)
}
}
return result, nil
}
func (o *TwoStageL3ClosClient) CreateConnectivityTemplate(ctx context.Context, in *ConnectivityTemplate) error {
apiInput, err := in.raw()
if err != nil {
return err
}
err = o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodPut,
urlStr: fmt.Sprintf(apiUrlBlueprintObjPolicyImport, o.blueprintId),
apiInput: &apiInput,
})
if err != nil {
return convertTtaeToAceWherePossible(err)
}
return nil
}
func (o *TwoStageL3ClosClient) UpdateConnectivityTemplate(ctx context.Context, in *ConnectivityTemplate) error {
return o.CreateConnectivityTemplate(ctx, in)
}
func (o *TwoStageL3ClosClient) DeleteConnectivityTemplate(ctx context.Context, id ObjectId) error {
urlStr := fmt.Sprintf(apiUrlBlueprintEndpointPolicyById, o.blueprintId, id)
urlObj, err := url.Parse(urlStr)
if err != nil {
return fmt.Errorf("failed parsing url %q - %w", urlStr, err)
}
params := urlObj.Query()
params.Set(deleteRecursive, "true")
urlObj.RawQuery = params.Encode()
err = o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodDelete,
url: urlObj,
})
if err != nil {
return convertTtaeToAceWherePossible(err)
}
return nil
}
func (o *TwoStageL3ClosClient) GetConnectivityTemplate(ctx context.Context, id ObjectId) (*ConnectivityTemplate, error) {
var response rawConnectivityTemplate
err := o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: fmt.Sprintf(apiUrlBlueprintObjPolicyExportById, o.blueprintId, id),
apiResponse: &response,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
return response.polish(id)
}
func (o *TwoStageL3ClosClient) GetAllConnectivityTemplates(ctx context.Context) ([]ConnectivityTemplate, error) {
var response rawConnectivityTemplate
err := o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: fmt.Sprintf(apiUrlBlueprintObjPolicyExport, o.blueprintId),
apiResponse: &response,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
ids := response.rootBatchIds()
result := make([]ConnectivityTemplate, len(ids))
for i, id := range ids {
polished, err := response.polish(id)
if err != nil {
return nil, err
}
result[i] = *polished
}
return result, nil
}
func (o *TwoStageL3ClosClient) GetConnectivityTemplateState(ctx context.Context, id ObjectId) (*ConnectivityTemplateState, error) {
var response struct {
EndpointPolicy rawConnectivityTemplateState `json:"endpoint_policy"`
}
err := o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: fmt.Sprintf(apiUrlBlueprintEndpointPolicyById, o.blueprintId, id),
apiResponse: &response,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
return response.EndpointPolicy.polish()
}
func (o *TwoStageL3ClosClient) GetAllConnectivityTemplateStates(ctx context.Context) ([]ConnectivityTemplateState, error) {
var response struct {
EndpointPolicies []rawConnectivityTemplateState `json:"endpoint_policies"`
}
err := o.client.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: fmt.Sprintf(apiUrlBlueprintEndpointPolicies, o.blueprintId),
apiResponse: &response,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
result := make([]ConnectivityTemplateState, 0, len(response.EndpointPolicies)/3)
for _, rawPolicy := range response.EndpointPolicies {
if rawPolicy.Visible {
polished, err := rawPolicy.polish()
if err != nil {
return nil, err
}
result = append(result, *polished)
}
}
return result, nil
}