-
Notifications
You must be signed in to change notification settings - Fork 2
/
datacenter_generic_system.go
1131 lines (1001 loc) · 41.3 KB
/
datacenter_generic_system.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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package blueprint
import (
"context"
"errors"
"fmt"
"math"
"net"
"regexp"
"sort"
"github.com/Juniper/terraform-provider-apstra/apstra/constants"
"github.com/Juniper/apstra-go-sdk/apstra"
apiversions "github.com/Juniper/terraform-provider-apstra/apstra/api_versions"
"github.com/Juniper/terraform-provider-apstra/apstra/design"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
"github.com/hashicorp/terraform-plugin-framework-nettypes/cidrtypes"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
resourceSchema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)
type DatacenterGenericSystem struct {
Id types.String `tfsdk:"id"`
BlueprintId types.String `tfsdk:"blueprint_id"`
Name types.String `tfsdk:"name"`
Hostname types.String `tfsdk:"hostname"`
Tags types.Set `tfsdk:"tags"`
Links types.Set `tfsdk:"links"`
Asn types.Int64 `tfsdk:"asn"`
LoopbackIpv4 cidrtypes.IPv4Prefix `tfsdk:"loopback_ipv4"`
LoopbackIpv6 cidrtypes.IPv6Prefix `tfsdk:"loopback_ipv6"`
PortChannelIdMin types.Int64 `tfsdk:"port_channel_id_min"`
PortChannelIdMax types.Int64 `tfsdk:"port_channel_id_max"`
External types.Bool `tfsdk:"external"`
DeployMode types.String `tfsdk:"deploy_mode"`
ClearCtsOnDestroy types.Bool `tfsdk:"clear_cts_on_destroy"`
}
func (o DatacenterGenericSystem) ResourceAttributes() map[string]resourceSchema.Attribute {
return map[string]resourceSchema.Attribute{
"id": resourceSchema.StringAttribute{
MarkdownDescription: "Apstra graph node ID.",
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"blueprint_id": resourceSchema.StringAttribute{
MarkdownDescription: "Apstra Blueprint ID.",
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
},
"name": resourceSchema.StringAttribute{
MarkdownDescription: "Name displayed in the Apstra web UI.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
Validators: []validator.String{stringvalidator.LengthBetween(1, 64)},
},
"hostname": resourceSchema.StringAttribute{
MarkdownDescription: "System hostname.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile("^[A-Za-z0-9.-]+$"),
"only alphanumeric characters, '.' and '-' allowed."),
stringvalidator.LengthBetween(1, 32),
},
},
"tags": resourceSchema.SetAttribute{
MarkdownDescription: "Tag labels to be applied to this Generic System. If a Tag doesn't exist " +
"in the Blueprint it will be created automatically.",
ElementType: types.StringType,
Optional: true,
Validators: []validator.Set{setvalidator.ValueStringsAre(stringvalidator.LengthAtLeast(1))},
},
"links": resourceSchema.SetNestedAttribute{
MarkdownDescription: fmt.Sprintf("Generic System link details. Note that tagging Links requires "+
"Apstra %s or newer.", apiversions.Apstra412),
Required: true,
NestedObject: resourceSchema.NestedAttributeObject{
Attributes: DatacenterGenericSystemLink{}.ResourceAttributes(),
},
Validators: []validator.Set{
setvalidator.SizeAtLeast(1),
genericSystemLinkSetValidator{},
},
},
"asn": resourceSchema.Int64Attribute{
MarkdownDescription: "AS number of the Generic System. Note that in some circumstances Apstra may assign " +
"an ASN to the generic system even when none is supplied via this attribute. The automatically" +
"assigned value will be overwritten by Terraform during a subsequent apply operation.",
Optional: true,
Validators: []validator.Int64{int64validator.Between(1, math.MaxUint32)},
},
"loopback_ipv4": resourceSchema.StringAttribute{
MarkdownDescription: "IPv4 address of loopback interface in CIDR notation",
CustomType: cidrtypes.IPv4PrefixType{},
Optional: true,
},
"loopback_ipv6": resourceSchema.StringAttribute{
MarkdownDescription: "IPv6 address of loopback interface in CIDR notation",
CustomType: cidrtypes.IPv6PrefixType{},
Optional: true,
},
"port_channel_id_min": resourceSchema.Int64Attribute{
MarkdownDescription: fmt.Sprintf("Omit this attribute to allow any available port-channel to be "+
"used. In Apstra version %s and earlier, all port channel min/max constraints had to be unique per "+
"blueprint. Port channel ranges could not overlap. This requirement has been relaxed, and now they "+
"need only be unique per system.", apiversions.Apstra412),
Optional: true,
Computed: true,
Default: int64default.StaticInt64(design.PoIdMin),
Validators: []validator.Int64{
int64validator.Between(design.PoIdMin, design.PoIdMax),
int64validator.AlsoRequires(path.MatchRelative().AtParent().AtName("port_channel_id_max")),
},
},
"port_channel_id_max": resourceSchema.Int64Attribute{
MarkdownDescription: fmt.Sprintf("Omit this attribute to allow any available port-channel to be "+
"used. In Apstra version %s and earlier, all port channel min/max constraints had to be unique per "+
"blueprint. Port channel ranges could not overlap. This requirement has been relaxed, and now they "+
"need only be unique per system.", apiversions.Apstra412),
Optional: true,
Computed: true,
Default: int64default.StaticInt64(design.PoIdMin),
Validators: []validator.Int64{
int64validator.Between(design.PoIdMin, design.PoIdMax),
int64validator.AtLeastSumOf(path.MatchRelative().AtParent().AtName("port_channel_id_min")),
int64validator.AlsoRequires(path.MatchRelative().AtParent().AtName("port_channel_id_min")),
},
},
"external": resourceSchema.BoolAttribute{
MarkdownDescription: "Set `true` to create an External Generic System",
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
PlanModifiers: []planmodifier.Bool{boolplanmodifier.RequiresReplace()},
},
"deploy_mode": resourceSchema.StringAttribute{
MarkdownDescription: fmt.Sprintf("Set the Apstra Deploy Mode for this Generic System. Default: `%s`",
apstra.DeployModeDeploy),
Optional: true,
Computed: true,
Default: stringdefault.StaticString(apstra.DeployModeDeploy.String()),
Validators: []validator.String{stringvalidator.OneOf(utils.AllNodeDeployModes()...)},
},
"clear_cts_on_destroy": resourceSchema.BoolAttribute{
MarkdownDescription: "When `true`, Connectivity Templates associated with this Generic System will be " +
"automatically cleared in a variety of circumstances where they would ordinarily block Generic System " +
"changes, including:\n" +
" - Deletion of the Generic System\n" +
" - Deletion of a Generic System Link or LAG interface\n" +
" - Orphaning a LAG interface by reassigning all of its member links to new roles by changing their " +
"`group_label` attribute\n",
Optional: true,
},
}
}
func (o *DatacenterGenericSystem) CreateRequest(ctx context.Context, diags *diag.Diagnostics) *apstra.CreateLinksWithNewSystemRequest {
bogusLdTemplateUsedInEveryRequest := apstra.LogicalDevice{
Id: "tf-ld-template",
Data: &apstra.LogicalDeviceData{
DisplayName: "TF LD template",
Panels: []apstra.LogicalDevicePanel{{
PanelLayout: apstra.LogicalDevicePanelLayout{RowCount: 1, ColumnCount: 1},
PortIndexing: apstra.LogicalDevicePortIndexing{
Order: apstra.PortIndexingHorizontalFirst,
StartIndex: 1,
Schema: apstra.PortIndexingSchemaAbsolute,
},
PortGroups: []apstra.LogicalDevicePortGroup{{Count: 1, Speed: "100M", Roles: 0}},
}},
},
}
// extract []DatacenterGenericSystemLink from the plan
planLinks := o.links(ctx, diags)
if diags.HasError() {
return nil
}
var systemType apstra.SystemType
if o.External.ValueBool() {
systemType = apstra.SystemTypeExternal
} else {
systemType = apstra.SystemTypeServer
}
// start building the request object
request := apstra.CreateLinksWithNewSystemRequest{
Links: make([]apstra.CreateLinkRequest, len(planLinks)),
System: apstra.CreateLinksWithNewSystemRequestSystem{
Hostname: o.Hostname.ValueString(),
Label: o.Name.ValueString(),
LogicalDevice: &bogusLdTemplateUsedInEveryRequest,
Type: systemType,
PortChannelIdMin: int(o.PortChannelIdMin.ValueInt64()),
PortChannelIdMax: int(o.PortChannelIdMax.ValueInt64()),
},
}
// populate the tags in the request object without checking diags for errors
diags.Append(o.Tags.ElementsAs(ctx, &request.System.Tags, false)...)
// populate each link in the request object
for i, link := range planLinks {
request.Links[i] = *link.request(ctx, diags) // collect all errors
}
return &request
}
func (o *DatacenterGenericSystem) links(ctx context.Context, diags *diag.Diagnostics) []DatacenterGenericSystemLink {
var result []DatacenterGenericSystemLink
diags.Append(o.Links.ElementsAs(ctx, &result, false)...)
return result
}
func (o *DatacenterGenericSystem) ReadTags(ctx context.Context, bp *apstra.TwoStageL3ClosClient, diags *diag.Diagnostics) {
tags, err := bp.GetNodeTags(ctx, apstra.ObjectId(o.Id.ValueString()))
if err != nil {
diags.AddError(fmt.Sprintf("couldn't get tags for node %s", o.Id), err.Error())
return
}
o.Tags = utils.SetValueOrNull(ctx, types.StringType, tags, diags)
}
func (o *DatacenterGenericSystem) ReadLinks(ctx context.Context, bp *apstra.TwoStageL3ClosClient, diags *diag.Diagnostics) {
// Extract the prior state into a map (stateLinks) of links keyed by
// endpoint digest (switch_id:interface_name).
// We use a map for quick access to the prior data. We're looking at prior
// state data so that we know whether to populate the `group_label` (an
// optional field) in our result. If `group_label` isn't found in the
// prior state, that means the user omitted it, so we should leave it `null`
// regardless of the value returned by the API.
stateLinks := o.links(ctx, diags)
if diags.HasError() {
return
}
stateLinksMap := make(map[string]*DatacenterGenericSystemLink, len(stateLinks))
for i, link := range stateLinks {
stateLinksMap[link.digest()] = &stateLinks[i]
}
// get the list of links from the API and filter out non-Ethernet links
apiLinks, err := bp.GetCablingMapLinksBySystem(ctx, apstra.ObjectId(o.Id.ValueString()))
if err != nil {
diags.AddError(fmt.Sprintf("failed to fetch generic system %s links", o.Id), err.Error())
return
}
for i := len(apiLinks) - 1; i >= 0; i-- { // loop backwards through the slice
if apiLinks[i].Type != apstra.LinkTypeEthernet { // target non-Ethernet links for deletion
apiLinks[i] = apiLinks[len(apiLinks)-1] // overwrite unwanted element with last element
apiLinks = apiLinks[:len(apiLinks)-1] // shorten the slice to eliminate the newly dup'ed last item.
}
}
oLinks := make([]attr.Value, len(apiLinks)) // oLinks will populate o.Links
for i, apiLink := range apiLinks {
var dcgsl DatacenterGenericSystemLink
// loadApiData handles every detail except for the transform ID
dcgsl.loadApiData(ctx, &apiLink, apstra.ObjectId(o.Id.ValueString()), diags)
if diags.HasError() {
return
}
// check the state's record of this link to see if the user previously
// specified `group_label`. The `group_label` attribute is not
// "Computed", so we must return `null` to avoid state churn if the
// user opted for `null` by not setting it.
if link, ok := stateLinksMap[dcgsl.digest()]; ok {
if link.GroupLabel.IsNull() {
dcgsl.GroupLabel = types.StringNull()
}
}
// read the switch interface transform ID from the API
dcgsl.getTransformId(ctx, bp, diags)
if diags.HasError() {
return
}
var d diag.Diagnostics
oLinks[i], d = types.ObjectValueFrom(ctx, dcgsl.attrTypes(), &dcgsl)
diags.Append(d...) // collect all errors
}
if diags.HasError() {
return
}
// pack the result slice into o.Links
o.Links = types.SetValueMust(types.ObjectType{AttrTypes: DatacenterGenericSystemLink{}.attrTypes()}, oLinks)
}
// ReadSystemProperties returns an error rather than appending to a
// []diag.Diagnosics because some callers might need to invoke RemoveResource()
// based on the error type.
func (o *DatacenterGenericSystem) ReadSystemProperties(ctx context.Context, bp *apstra.TwoStageL3ClosClient, overwriteKnownValues bool) error {
nodeInfo, err := bp.GetSystemNodeInfo(ctx, apstra.ObjectId(o.Id.ValueString()))
if err != nil {
return err
}
if overwriteKnownValues || o.Hostname.IsUnknown() {
o.Hostname = types.StringValue(nodeInfo.Hostname)
}
if overwriteKnownValues || o.Name.IsUnknown() {
o.Name = types.StringValue(nodeInfo.Label)
}
if overwriteKnownValues || o.External.IsUnknown() {
o.External = types.BoolValue(nodeInfo.External)
}
if overwriteKnownValues || o.DeployMode.IsUnknown() {
deployMode, err := utils.GetNodeDeployMode(ctx, bp, o.Id.ValueString())
if err != nil {
return err
}
o.DeployMode = types.StringValue(deployMode)
}
// asn isn't computed, so will never be unknown
if overwriteKnownValues && nodeInfo.Asn != nil {
o.Asn = types.Int64Value(int64(*nodeInfo.Asn))
}
// v4 loopback isn't computed, so will never be unknown
if overwriteKnownValues && nodeInfo.LoopbackIpv4 != nil {
o.LoopbackIpv4 = cidrtypes.NewIPv4PrefixValue(nodeInfo.LoopbackIpv4.String())
}
// v6 loopback isn't computed, so will never be unknown
if overwriteKnownValues && nodeInfo.LoopbackIpv6 != nil {
o.LoopbackIpv6 = cidrtypes.NewIPv6PrefixValue(nodeInfo.LoopbackIpv6.String())
}
// Port Channel Min & Max
if overwriteKnownValues {
o.PortChannelIdMin = types.Int64Value(int64(nodeInfo.PortChannelIdMin))
o.PortChannelIdMax = types.Int64Value(int64(nodeInfo.PortChannelIdMax))
}
return nil
}
// UpdateHostnameAndName uses the node patch API to set the label and
// hostname fields.
func (o *DatacenterGenericSystem) UpdateHostnameAndName(ctx context.Context, bp *apstra.TwoStageL3ClosClient, state *DatacenterGenericSystem, diags *diag.Diagnostics) {
if o.Hostname.Equal(state.Hostname) && o.Name.Equal(state.Name) {
// no planned changes to hostname or name attributes
return
}
// node is an apstra-compatible system struct fragment suitable for patching
node := struct {
Hostname string `json:"hostname,omitempty"`
Label string `json:"label,omitempty"`
}{
Hostname: o.Hostname.ValueString(),
Label: o.Name.ValueString(),
}
// update the system node
err := bp.Client().PatchNode(ctx, bp.Id(), apstra.ObjectId(o.Id.ValueString()), &node, nil)
if err != nil {
diags.AddError(
fmt.Sprintf("error patching blueprint %q node %s with hostname: %s and label %s",
bp.Id(), o.Id, o.Hostname, o.Name),
err.Error())
return
}
if !o.Hostname.IsUnknown() && !o.Name.IsUnknown() {
// no need to retrieve learned values from Apstra
return
}
// retrieve values from Apstra
err = bp.Client().GetNode(ctx, bp.Id(), apstra.ObjectId(o.Id.ValueString()), &node)
if err != nil {
diags.AddError(
fmt.Sprintf("error fetching blueprint %q node %s", bp.Id(), o.Id),
err.Error())
return
}
o.Hostname = types.StringValue(node.Hostname)
o.Name = types.StringValue(node.Label)
}
// UpdateTags uses the tagging API to set the tag set
func (o *DatacenterGenericSystem) UpdateTags(ctx context.Context, bp *apstra.TwoStageL3ClosClient, state *DatacenterGenericSystem, diags *diag.Diagnostics) {
var planTags, stateTags []string
diags.Append(o.Tags.ElementsAs(ctx, &planTags, false)...)
diags.Append(state.Tags.ElementsAs(ctx, &stateTags, false)...)
if diags.HasError() {
return
}
sort.Strings(planTags)
sort.Strings(stateTags)
if utils.SlicesMatch(planTags, stateTags) {
// no planned changes to tag set
return
}
// update node tags
err := bp.SetNodeTags(ctx, apstra.ObjectId(o.Id.ValueString()), planTags)
if err != nil {
diags.AddError(fmt.Sprintf("failed to update tags on %s", o.Id), err.Error())
}
}
// UpdateLinkSet uses the old state to determine which links in the plan should
// be added, removed and kept. Individual links are then added or removed and
// the "keeper" links are updated with the correct tags, modes, etc...
func (o *DatacenterGenericSystem) UpdateLinkSet(ctx context.Context, state *DatacenterGenericSystem, bp *apstra.TwoStageL3ClosClient, diags *diag.Diagnostics) {
// extract links from plan (o) and state objects
var planLinks, stateLinks []DatacenterGenericSystemLink
diags.Append(o.Links.ElementsAs(ctx, &planLinks, false)...)
diags.Append(state.Links.ElementsAs(ctx, &stateLinks, false)...)
if diags.HasError() {
return
}
// transform plan and state links into a map keyed by link digest (device:port)
planLinksMap := make(map[string]*DatacenterGenericSystemLink, len(planLinks))
for i, link := range planLinks {
planLinksMap[link.digest()] = &planLinks[i]
}
stateLinksMap := make(map[string]*DatacenterGenericSystemLink, len(stateLinks))
for i, link := range stateLinks {
stateLinksMap[link.digest()] = &stateLinks[i]
}
// compare plan and state, make lists of links to add / check+update / delete
var addLinks, updateLinksPlan, updateLinksState, delLinks []*DatacenterGenericSystemLink
for digest := range planLinksMap {
if _, ok := stateLinksMap[digest]; !ok {
addLinks = append(addLinks, planLinksMap[digest])
} else {
// "updateLinks" is two slices: plan and state, so that we can
// compare and change only required attributes.
updateLinksPlan = append(updateLinksPlan, planLinksMap[digest])
updateLinksState = append(updateLinksState, stateLinksMap[digest])
}
}
for digest := range stateLinksMap {
if _, ok := planLinksMap[digest]; !ok {
delLinks = append(delLinks, stateLinksMap[digest])
}
}
o.addLinksToSystem(ctx, addLinks, bp, diags)
if diags.HasError() {
return
}
o.deleteLinksFromSystem(ctx, delLinks, bp, diags)
if diags.HasError() {
return
}
o.updateLinkParams(ctx, updateLinksPlan, updateLinksState, bp, diags)
if diags.HasError() {
return
}
}
func (o *DatacenterGenericSystem) addLinksToSystem(ctx context.Context, links []*DatacenterGenericSystemLink, bp *apstra.TwoStageL3ClosClient, diags *diag.Diagnostics) {
if len(links) == 0 {
return
}
linkRequests := make([]apstra.CreateLinkRequest, len(links))
for i, link := range links {
linkRequests[i] = *link.request(ctx, diags)
if diags.HasError() {
return
}
linkRequests[i].SystemEndpoint.SystemId = apstra.ObjectId(o.Id.ValueString())
err := linkRequests[i].LagMode.FromString(link.LagMode.ValueString())
if err != nil {
diags.AddError(fmt.Sprintf("failed parsing lag mode %s", link.LagMode), err.Error())
}
}
if diags.HasError() {
return
}
_, err := bp.AddLinksToSystem(ctx, linkRequests)
if err != nil {
diags.AddError(fmt.Sprintf("failed adding links to generic system %s", o.Id), err.Error())
}
}
func (o *DatacenterGenericSystem) deleteLinksFromSystem(ctx context.Context, links []*DatacenterGenericSystemLink, bp *apstra.TwoStageL3ClosClient, diags *diag.Diagnostics) {
if len(links) == 0 {
return
}
linkIdsToDelete := o.linkIds(ctx, links, bp, diags)
if diags.HasError() {
return
}
// try to delete the links
err := bp.DeleteLinksFromSystem(ctx, linkIdsToDelete)
if err == nil {
return // success!
}
// see if we can handle this error...
var ace apstra.ClientErr
if !errors.As(err, &ace) || ace.Type() != apstra.ErrCtAssignedToLink || ace.Detail() == nil {
// cannot handle error
diags.AddError("failed while deleting Links from Generic System", err.Error())
return
}
// the error detail has to be the correct type...
detail, ok := ace.Detail().(apstra.ErrCtAssignedToLinkDetail)
if !ok {
diags.AddError(
constants.ErrProviderBug+fmt.Sprintf(" - ErrCtAssignedToLink has unexpected detail type: %T", detail),
err.Error(),
)
return
}
// see if the user could have avoided this problem...
if !o.ClearCtsOnDestroy.ValueBool() {
diags.AddWarning(
fmt.Sprintf("Cannot delete links with Connectivity Templates assigned: %v", detail.LinkIds),
"You can set 'clear_cts_on_destroy = true' to override this behavior",
)
return
}
// prep an error diagnostic in case we can't figure this out
var pendingDiags diag.Diagnostics
pendingDiags.AddError(
fmt.Sprintf("failed deleting links %v from generic system %s", linkIdsToDelete, o.Id),
err.Error())
// try to clear the connectivity templates from the problem links
o.ClearConnectivityTemplatesFromLinks(ctx, ace.Detail().(apstra.ErrCtAssignedToLinkDetail).LinkIds, bp, diags)
if diags.HasError() {
diags.Append(pendingDiags...) // throw the pending diagnostic on the pile and give up
return
}
// try deleting the links again
err = bp.DeleteLinksFromSystem(ctx, linkIdsToDelete)
if err != nil {
diags.AddError("failed second attempt to delete links after attempting to handle the link deletion error",
err.Error())
diags.Append(pendingDiags...) // throw the pending diagnostic on the pile and give up
return
}
}
// updateLinkParams is a method on DatacenterGenericSystem (which has links
// embedded), but it does not operate on those links (all of the links). Rather
// it operates only on the links passed as a function argument because only
// those links need to be updated/validated.
func (o *DatacenterGenericSystem) updateLinkParams(ctx context.Context, planLinks, stateLinks []*DatacenterGenericSystemLink, bp *apstra.TwoStageL3ClosClient, diags *diag.Diagnostics) {
// create an empty request with room for each link to be modified
request := make(apstra.SetLinkLagParamsRequest, len(planLinks))
for i, link := range planLinks {
// we don't keep the link ID, but we have each link's target switch and
// interface name. That's enough to uniquely identify the link from the
// graph data store
linkId := o.linkId(ctx, link, bp, diags)
if diags.HasError() {
return
}
linkParams := link.lagParams(ctx, linkId, stateLinks[i], bp, diags)
if linkParams != nil {
request[linkId] = *linkParams
}
}
if len(request) != 0 {
err := bp.SetLinkLagParams(ctx, &request)
if err != nil {
// we may be able to figure this out...
var pendingDiags diag.Diagnostics
pendingDiags.AddError("failed updating generic system link parameters", err.Error())
var ace apstra.ClientErr
if !errors.As(err, &ace) || ace.Type() != apstra.ErrLagHasAssignedStructrues || ace.Detail() == nil {
diags.Append(pendingDiags...) // cannot handle error
return
}
detail, ok := ace.Detail().(apstra.ErrLagHasAssignedStructuresDetail)
if !ok || len(detail.GroupLabels) == 0 {
diags.Append(pendingDiags...) // cannot handle error
return
}
var lagIds []apstra.ObjectId
for _, groupLabel := range detail.GroupLabels {
lagId, err := lagLinkIdFromGsIdAndGroupLabel(ctx, bp, apstra.ObjectId(o.Id.ValueString()), groupLabel)
if err != nil {
// return both errors
diags.Append(pendingDiags...)
diags.AddError("failed to determine upstream switch LAG port ID", err.Error())
continue
}
lagIds = append(lagIds, lagId)
}
if !o.ClearCtsOnDestroy.ValueBool() {
diags.Append(pendingDiags...) // cannot handle error
diags.AddWarning(
fmt.Sprintf("Cannot orphan LAGs with Connectivity Templates assigned: %v", lagIds),
"You can set 'clear_cts_on_destroy = true' to override this behavior",
)
return
}
o.ClearConnectivityTemplatesFromLinks(ctx, lagIds, bp, diags)
// try again...
err = bp.SetLinkLagParams(ctx, &request)
if err != nil {
diags.AddError("failed updating generic system LAG parameters after clearing CTs", err.Error()) // cannot handle error
return
}
}
}
// one at a time, check/update each link transform ID
for i, link := range planLinks {
link.updateTransformId(ctx, stateLinks[i], bp, diags)
}
}
func lagLinkIdFromGsIdAndGroupLabel(ctx context.Context, bp *apstra.TwoStageL3ClosClient, gsId apstra.ObjectId, groupLabel string) (apstra.ObjectId, error) {
query := new(apstra.PathQuery).SetBlueprintId(bp.Id()).SetClient(bp.Client()).
Node([]apstra.QEEAttribute{{Key: "id", Value: apstra.QEStringVal(gsId.String())}}).
Out([]apstra.QEEAttribute{apstra.RelationshipTypeHostedInterfaces.QEEAttribute()}).
Node([]apstra.QEEAttribute{
apstra.NodeTypeInterface.QEEAttribute(),
{Key: "if_type", Value: apstra.QEStringVal("port_channel")},
}).
Out([]apstra.QEEAttribute{apstra.RelationshipTypeLink.QEEAttribute()}).
Node([]apstra.QEEAttribute{
apstra.NodeTypeLink.QEEAttribute(),
{Key: "group_label", Value: apstra.QEStringVal(groupLabel)},
{Key: "link_type", Value: apstra.QEStringVal("aggregate_link")},
{Key: "name", Value: apstra.QEStringVal("n_link")},
})
var result struct {
Items []struct {
Link struct {
Id apstra.ObjectId `json:"id"`
} `json:"n_link"`
} `json:"items"`
}
if err := query.Do(ctx, &result); err != nil {
return "", err
}
switch len(result.Items) {
case 0:
return "", fmt.Errorf("query failed to find LAG link ID for system %q group label %q - %s", gsId, groupLabel, query.String())
case 1:
return result.Items[0].Link.Id, nil
default:
return "", fmt.Errorf("query found multiple find LAG link IDs for system %q group label %q - %s", gsId, groupLabel, query.String())
}
}
//func switchLagIdFromGsIdAndGroupLabel(ctx context.Context, bp *apstra.TwoStageL3ClosClient, gsId apstra.ObjectId, groupLabel string) (apstra.ObjectId, error) {
// query := new(apstra.PathQuery).SetBlueprintId(bp.Id()).SetClient(bp.Client()).
// Node([]apstra.QEEAttribute{{Key: "id", Value: apstra.QEStringVal(gsId.String())}}).
// Out([]apstra.QEEAttribute{apstra.RelationshipTypeHostedInterfaces.QEEAttribute()}).
// Node([]apstra.QEEAttribute{
// apstra.NodeTypeInterface.QEEAttribute(),
// {Key: "if_type", Value: apstra.QEStringVal("port_channel")},
// }).
// Out([]apstra.QEEAttribute{apstra.RelationshipTypeLink.QEEAttribute()}).
// Node([]apstra.QEEAttribute{
// apstra.NodeTypeLink.QEEAttribute(),
// {Key: "group_label", Value: apstra.QEStringVal(groupLabel)},
// {Key: "link_type", Value: apstra.QEStringVal("aggregate_link")},
// }).
// In([]apstra.QEEAttribute{apstra.RelationshipTypeLink.QEEAttribute()}).
// Node([]apstra.QEEAttribute{
// apstra.NodeTypeInterface.QEEAttribute(),
// {Key: "if_type", Value: apstra.QEStringVal("port_channel")},
// {Key: "name", Value: apstra.QEStringVal("n_application_point")},
// })
//
// var result struct {
// Items []struct {
// ApplicationPoint struct {
// Id apstra.ObjectId `json:"id"`
// } `json:"n_application_point"`
// } `json:"items"`
// }
//
// if err := query.Do(ctx, &result); err != nil {
// return "", err
// }
//
// switch len(result.Items) {
// case 0:
// return "", fmt.Errorf("query failed to find upstream interface ID for system %q group label %q - %s", gsId, groupLabel, query.String())
// case 1:
// return result.Items[0].ApplicationPoint.Id, nil
// default:
// return "", fmt.Errorf("query found multiple find upstream interface IDs for system %q group label %q - %s", gsId, groupLabel, query.String())
// }
//}
// linkIds performs the graph queries necessary to return the link IDs which
// connect this Generic System (o) to the systems+interfaces specified by links.
func (o *DatacenterGenericSystem) linkIds(ctx context.Context, links []*DatacenterGenericSystemLink, bp *apstra.TwoStageL3ClosClient, diags *diag.Diagnostics) []apstra.ObjectId {
if len(links) == 0 {
return nil
}
// make a map keyed by switch ID to facilitate graph queries:
// switchId123 -> []string{"xe-0/0/1", "xe-0/0/5"}
// switchId456 -> []string{"xe-0/0/3", "xe-0/0/6"}
swIdToIfNames := make(map[string][]string)
for _, link := range links {
swIdToIfNames[link.TargetSwitchId.ValueString()] = append(
swIdToIfNames[link.TargetSwitchId.ValueString()], link.TargetSwitchIfName.ValueString())
}
var queryResponse struct {
Items []struct {
Link struct {
Id apstra.ObjectId `json:"id"`
} `json:"n_link"`
} `json:"items"`
}
// get link IDs from each switch
var result []apstra.ObjectId
for switchId, ifNames := range swIdToIfNames {
query := new(apstra.PathQuery).
SetBlueprintType(apstra.BlueprintTypeStaging).
SetBlueprintId(bp.Id()).
SetClient(bp.Client()).
Node([]apstra.QEEAttribute{
apstra.NodeTypeSystem.QEEAttribute(),
{Key: "id", Value: apstra.QEStringVal(switchId)},
}).
Out([]apstra.QEEAttribute{apstra.RelationshipTypeHostedInterfaces.QEEAttribute()}).
Node([]apstra.QEEAttribute{apstra.NodeTypeInterface.QEEAttribute()}).
Out([]apstra.QEEAttribute{apstra.RelationshipTypeLink.QEEAttribute()}).
Node([]apstra.QEEAttribute{
apstra.NodeTypeLink.QEEAttribute(),
{Key: "link_type", Value: apstra.QEStringVal("ethernet")},
{Key: "name", Value: apstra.QEStringVal("n_link")},
}).
In([]apstra.QEEAttribute{apstra.RelationshipTypeLink.QEEAttribute()}).
Node([]apstra.QEEAttribute{
apstra.NodeTypeInterface.QEEAttribute(),
{Key: "if_name", Value: apstra.QEStringValIsIn(ifNames)},
}).
In([]apstra.QEEAttribute{apstra.RelationshipTypeHostedInterfaces.QEEAttribute()}).
Node([]apstra.QEEAttribute{
apstra.NodeTypeSystem.QEEAttribute(),
{Key: "id", Value: apstra.QEStringVal(switchId)},
})
err := query.Do(ctx, &queryResponse)
if err != nil {
diags.AddError(
fmt.Sprintf("failed querying for link IDs from node %s: %q ports %v",
o.Id, switchId, ifNames),
err.Error())
return nil
}
resultLinkIds := make([]apstra.ObjectId, len(queryResponse.Items))
for i, item := range queryResponse.Items {
resultLinkIds[i] = item.Link.Id
}
if len(ifNames) != len(resultLinkIds) {
diags.AddError(
fmt.Sprintf("link ID query for node %s interfaces %v returned only %d link IDs", switchId, ifNames, len(resultLinkIds)),
fmt.Sprintf("got the following link IDs: %v", resultLinkIds))
return nil
}
result = append(result, resultLinkIds...)
}
return result
}
// linkId performs the graph queries necessary to return the link IDs which
// connect this Generic System (o) to the systems+interfaces specified by links.
func (o *DatacenterGenericSystem) linkId(ctx context.Context, link *DatacenterGenericSystemLink, bp *apstra.TwoStageL3ClosClient, diags *diag.Diagnostics) apstra.ObjectId {
linkIds := o.linkIds(ctx, []*DatacenterGenericSystemLink{link}, bp, diags)
if diags.HasError() {
return ""
}
if len(linkIds) != 1 {
diags.AddError("provider bug", fmt.Sprintf("expected 1 link ID, got %d", len(linkIds)))
return ""
}
return linkIds[0]
}
// this validator is here because (a) it's just for one attribute of this
// resource and (2) it uses structs from the blueprint package and would cause
// an import cycle if we put it there.
var _ validator.Set = genericSystemLinkSetValidator{}
type genericSystemLinkSetValidator struct{}
func (o genericSystemLinkSetValidator) Description(_ context.Context) string {
return "ensures that link sets use a valid combination of values"
}
func (o genericSystemLinkSetValidator) MarkdownDescription(ctx context.Context) string {
return o.Description(ctx)
}
func (o genericSystemLinkSetValidator) ValidateSet(ctx context.Context, req validator.SetRequest, resp *validator.SetResponse) {
if req.ConfigValue.IsUnknown() {
return // cannot validate an unknown value
}
var links []DatacenterGenericSystemLink
resp.Diagnostics.Append(req.ConfigValue.ElementsAs(ctx, &links, false)...)
if resp.Diagnostics.HasError() {
return
}
digests := make(map[string]bool, len(links)) // track switch interfaces in use
groupModes := make(map[string]string, len(links)) // track lag modes per group
for _, link := range links {
digest := link.digest()
if digests[digest] {
resp.Diagnostics.Append(
validatordiag.InvalidAttributeCombinationDiagnostic(
req.Path,
fmt.Sprintf("multiple links claim interface %s on switch %s",
link.TargetSwitchIfName, link.TargetSwitchId),
),
)
return
}
lagMode := link.LagMode.ValueString()
if groupMode, ok := groupModes[link.GroupLabel.ValueString()]; ok && !link.GroupLabel.IsNull() {
// we have seen this group label before
if link.LagMode.IsNull() {
resp.Diagnostics.Append(
validatordiag.InvalidAttributeCombinationDiagnostic(
req.Path,
fmt.Sprintf("because multiple interfaces share group label %q, lag_mode must be set",
link.GroupLabel.ValueString())))
return
}
if groupMode != lagMode {
resp.Diagnostics.Append(
validatordiag.InvalidAttributeCombinationDiagnostic(
req.Path,
fmt.Sprintf("interfaces with group label %q have mismatched 'lag_mode': %q and %q",
link.GroupLabel.ValueString(), groupMode, lagMode)))
return
}
} else {
groupModes[link.GroupLabel.ValueString()] = lagMode
}
}
}
func (o *DatacenterGenericSystem) SetProperties(ctx context.Context, bp *apstra.TwoStageL3ClosClient, state *DatacenterGenericSystem, diags *diag.Diagnostics) {
// set ASN if we don't have prior state or the ASN needs to be updated
if state == nil || !o.Asn.Equal(state.Asn) {
o.setAsn(ctx, bp, diags)
if diags.HasError() {
return
}
}
// set loopback v4 if we don't have prior state or the v4 address needs to be updated
if state == nil || !o.LoopbackIpv4.Equal(state.LoopbackIpv4) {
o.setLoopbackIPv4(ctx, bp, diags)
if diags.HasError() {
return
}
}
// set loopback v6 if we don't have prior state or the v6 address needs to be updated
if state == nil || !o.LoopbackIpv6.Equal(state.LoopbackIpv6) {
o.setLoopbackIPv6(ctx, bp, diags)
if diags.HasError() {
return
}
}
// Set Port Channel Min and Max if prior state indicates update is needed
if state != nil && (!o.PortChannelIdMax.Equal(state.PortChannelIdMax) || !o.PortChannelIdMin.Equal(state.PortChannelIdMin)) {
o.setPortChannelIdMinMax(ctx, bp, diags)
if diags.HasError() {
return
}
}
// set deploy mode if we don't have prior state or the deploy mode needs to be updated
if state == nil || !o.DeployMode.Equal(state.DeployMode) {
err := utils.SetNodeDeployMode(ctx, bp, o.Id.ValueString(), o.DeployMode.ValueString())
if err != nil {
diags.AddError("failed to set node deploy mode", err.Error())
}
}
}
// setAsn sets or clears the generic system ASN attribute depending on o.Asn.IsNull()
func (o *DatacenterGenericSystem) setAsn(ctx context.Context, bp *apstra.TwoStageL3ClosClient, diags *diag.Diagnostics) {
var asn *uint32
if !o.Asn.IsNull() {
a := uint32(o.Asn.ValueInt64())
asn = &a
}
err := bp.SetGenericSystemAsn(ctx, apstra.ObjectId(o.Id.ValueString()), asn)
if err != nil {
diags.AddError("failed setting generic system ASN", err.Error())
}
}
// setLoopbackIPv4 sets or clears the generic system loopback IPv4 attribute depending on o.LoopbackIpv4.IsNull()
func (o *DatacenterGenericSystem) setLoopbackIPv4(ctx context.Context, bp *apstra.TwoStageL3ClosClient, diags *diag.Diagnostics) {
var ipNet *net.IPNet
var err error
if !o.LoopbackIpv4.IsNull() {
var ip net.IP
ip, ipNet, err = net.ParseCIDR(o.LoopbackIpv4.ValueString())
if err != nil {
diags.AddError("failed parsing generic system IPv4 loopback address", err.Error())
return
}
ipNet.IP = ip // overwrite subnet address in the parsed object with host address
}
err = bp.SetGenericSystemLoopbackIpv4(ctx, apstra.ObjectId(o.Id.ValueString()), ipNet, 0)
if err != nil {
diags.AddError("failed setting generic system IPv4 loopback address", err.Error())
}
}
// setLoopbackIPv6 sets or clears the generic system loopback IPv6 attribute depending on o.LoopbackIpv6.IsNull()
func (o *DatacenterGenericSystem) setLoopbackIPv6(ctx context.Context, bp *apstra.TwoStageL3ClosClient, diags *diag.Diagnostics) {
var ipNet *net.IPNet
var err error
if !o.LoopbackIpv6.IsNull() {
var ip net.IP
ip, ipNet, err = net.ParseCIDR(o.LoopbackIpv6.ValueString())
if err != nil {
diags.AddError("failed parsing generic system IPv6 loopback address", err.Error())
return
}