/
data_source_sdwan_cisco_ospf_feature_template.go
524 lines (503 loc) · 20.3 KB
/
data_source_sdwan_cisco_ospf_feature_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
// Copyright © 2023 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Mozilla Public 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
//
// https://mozilla.org/MPL/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.
//
// SPDX-License-Identifier: MPL-2.0
// Code generated by "gen/generator.go"; DO NOT EDIT.
package provider
import (
"context"
"fmt"
"net/url"
"github.com/CiscoDevNet/terraform-provider-sdwan/internal/provider/helpers"
"github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/netascode/go-sdwan"
"github.com/tidwall/gjson"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &CiscoOSPFFeatureTemplateDataSource{}
_ datasource.DataSourceWithConfigure = &CiscoOSPFFeatureTemplateDataSource{}
)
func NewCiscoOSPFFeatureTemplateDataSource() datasource.DataSource {
return &CiscoOSPFFeatureTemplateDataSource{}
}
type CiscoOSPFFeatureTemplateDataSource struct {
client *sdwan.Client
}
func (d *CiscoOSPFFeatureTemplateDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_cisco_ospf_feature_template"
}
func (d *CiscoOSPFFeatureTemplateDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "This data source can read the Cisco OSPF feature template.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The id of the feature template",
Optional: true,
Computed: true,
},
"version": schema.Int64Attribute{
MarkdownDescription: "The version of the feature template",
Computed: true,
},
"template_type": schema.StringAttribute{
MarkdownDescription: "The template type",
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the feature template",
Optional: true,
Computed: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "The description of the feature template",
Computed: true,
},
"device_types": schema.SetAttribute{
MarkdownDescription: "List of supported device types",
ElementType: types.StringType,
Computed: true,
},
"router_id": schema.StringAttribute{
MarkdownDescription: "Set OSPF router ID to override system IP address",
Computed: true,
},
"router_id_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"auto_cost_reference_bandwidth": schema.Int64Attribute{
MarkdownDescription: "Set reference bandwidth method to assign OSPF cost",
Computed: true,
},
"auto_cost_reference_bandwidth_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"compatible_rfc1583": schema.BoolAttribute{
MarkdownDescription: "Calculate summary route cost based on RFC 1583",
Computed: true,
},
"compatible_rfc1583_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"default_information_originate": schema.BoolAttribute{
MarkdownDescription: "Distribute default external route into OSPF",
Computed: true,
},
"default_information_originate_always": schema.BoolAttribute{
MarkdownDescription: "Always advertise default route",
Computed: true,
},
"default_information_originate_always_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"default_information_originate_metric": schema.Int64Attribute{
MarkdownDescription: "Set metric used to generate default route <0..16777214>",
Computed: true,
},
"default_information_originate_metric_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"default_information_originate_metric_type": schema.StringAttribute{
MarkdownDescription: "Set default route type",
Computed: true,
},
"default_information_originate_metric_type_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"distance_external": schema.Int64Attribute{
MarkdownDescription: "Set distance for external routes",
Computed: true,
},
"distance_external_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"distance_inter_area": schema.Int64Attribute{
MarkdownDescription: "Set distance for inter-area routes",
Computed: true,
},
"distance_inter_area_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"distance_intra_area": schema.Int64Attribute{
MarkdownDescription: "Set distance for intra-area routes",
Computed: true,
},
"distance_intra_area_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"timers_spf_delay": schema.Int64Attribute{
MarkdownDescription: "Set delay from first change received until performing SPF calculation",
Computed: true,
},
"timers_spf_delay_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"timers_spf_initial_hold": schema.Int64Attribute{
MarkdownDescription: "Set initial hold time between consecutive SPF calculations",
Computed: true,
},
"timers_spf_initial_hold_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"timers_spf_max_hold": schema.Int64Attribute{
MarkdownDescription: "Set maximum hold time between consecutive SPF calculations",
Computed: true,
},
"timers_spf_max_hold_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"redistribute": schema.ListNestedAttribute{
MarkdownDescription: "Redistribute routes",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"protocol": schema.StringAttribute{
MarkdownDescription: "Set the protocol",
Computed: true,
},
"protocol_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"route_policy": schema.StringAttribute{
MarkdownDescription: "Set route policy to apply to redistributed routes",
Computed: true,
},
"route_policy_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"nat_dia": schema.BoolAttribute{
MarkdownDescription: "Enable NAT DIA for redistributed routes",
Computed: true,
},
"nat_dia_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"optional": schema.BoolAttribute{
MarkdownDescription: "Indicates if list item is considered optional.",
Computed: true,
},
},
},
},
"max_metric_router_lsa": schema.ListNestedAttribute{
MarkdownDescription: "Advertise own router LSA with infinite distance",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"ad_type": schema.StringAttribute{
MarkdownDescription: "Set the router LSA advertisement type",
Computed: true,
},
"time": schema.Int64Attribute{
MarkdownDescription: "Set how long to advertise maximum metric after router starts up",
Computed: true,
},
"time_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"optional": schema.BoolAttribute{
MarkdownDescription: "Indicates if list item is considered optional.",
Computed: true,
},
},
},
},
"route_policies": schema.ListNestedAttribute{
MarkdownDescription: "Set route policy to apply",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"direction": schema.StringAttribute{
MarkdownDescription: "Set direction to apply policy",
Computed: true,
},
"direction_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"policy_name": schema.StringAttribute{
MarkdownDescription: "Name of route policy",
Computed: true,
},
"policy_name_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"optional": schema.BoolAttribute{
MarkdownDescription: "Indicates if list item is considered optional.",
Computed: true,
},
},
},
},
"areas": schema.ListNestedAttribute{
MarkdownDescription: "Configure OSPF area",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"area_number": schema.Int64Attribute{
MarkdownDescription: "Set OSPF area number",
Computed: true,
},
"area_number_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"stub": schema.BoolAttribute{
MarkdownDescription: "Stub area",
Computed: true,
},
"stub_no_summary": schema.BoolAttribute{
MarkdownDescription: "Do not inject interarea routes into stub",
Computed: true,
},
"stub_no_summary_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"nssa": schema.BoolAttribute{
MarkdownDescription: "NSSA area",
Computed: true,
},
"nssa_no_summary": schema.BoolAttribute{
MarkdownDescription: "Do not inject interarea routes into NSSA",
Computed: true,
},
"nssa_no_summary_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"interfaces": schema.ListNestedAttribute{
MarkdownDescription: "Set OSPF interface parameters",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "Set interface name",
Computed: true,
},
"name_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"hello_interval": schema.Int64Attribute{
MarkdownDescription: "Set interval between OSPF hello packets",
Computed: true,
},
"hello_interval_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"dead_interval": schema.Int64Attribute{
MarkdownDescription: "Set interval after which neighbor is declared to be down",
Computed: true,
},
"dead_interval_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"retransmit_interval": schema.Int64Attribute{
MarkdownDescription: "Set time between retransmitting LSAs",
Computed: true,
},
"retransmit_interval_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"cost": schema.Int64Attribute{
MarkdownDescription: "Set cost of OSPF interface",
Computed: true,
},
"cost_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"priority": schema.Int64Attribute{
MarkdownDescription: "Set router’s priority to be elected as designated router",
Computed: true,
},
"priority_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"network": schema.StringAttribute{
MarkdownDescription: "Set the OSPF network type",
Computed: true,
},
"network_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"passive_interface": schema.BoolAttribute{
MarkdownDescription: "Set the interface to advertise its address, but not to actively run OSPF",
Computed: true,
},
"passive_interface_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"authentication_type": schema.StringAttribute{
MarkdownDescription: "Set OSPF interface authentication type",
Computed: true,
},
"authentication_type_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"authentication_message_digest_key_id": schema.Int64Attribute{
MarkdownDescription: "Set MD5 message digest key",
Computed: true,
},
"authentication_message_digest_key_id_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"authentication_message_digest_key": schema.StringAttribute{
MarkdownDescription: "Set MD5 authentication key",
Computed: true,
},
"authentication_message_digest_key_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"optional": schema.BoolAttribute{
MarkdownDescription: "Indicates if list item is considered optional.",
Computed: true,
},
},
},
},
"ranges": schema.ListNestedAttribute{
MarkdownDescription: "Summarize OSPF routes at an area boundary",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"address": schema.StringAttribute{
MarkdownDescription: "Set matching prefix",
Computed: true,
},
"address_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"cost": schema.Int64Attribute{
MarkdownDescription: "Set cost for this range",
Computed: true,
},
"cost_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"no_advertise": schema.BoolAttribute{
MarkdownDescription: "Do not advertise this range",
Computed: true,
},
"no_advertise_variable": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Variable name").String,
Computed: true,
},
"optional": schema.BoolAttribute{
MarkdownDescription: "Indicates if list item is considered optional.",
Computed: true,
},
},
},
},
"optional": schema.BoolAttribute{
MarkdownDescription: "Indicates if list item is considered optional.",
Computed: true,
},
},
},
},
},
}
}
func (d *CiscoOSPFFeatureTemplateDataSource) ConfigValidators(ctx context.Context) []datasource.ConfigValidator {
return []datasource.ConfigValidator{
datasourcevalidator.ExactlyOneOf(
path.MatchRoot("id"),
path.MatchRoot("name"),
),
}
}
func (d *CiscoOSPFFeatureTemplateDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
d.client = req.ProviderData.(*SdwanProviderData).Client
}
func (d *CiscoOSPFFeatureTemplateDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config CiscoOSPF
// Read config
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", config.Id.String()))
if config.Id.IsNull() && !config.Name.IsNull() {
res, err := d.client.Get("/template/feature")
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve existing templates, got error: %s", err))
return
}
if value := res.Get("data"); len(value.Array()) > 0 {
value.ForEach(func(k, v gjson.Result) bool {
if config.Name.ValueString() == v.Get("templateName").String() {
config.Id = types.StringValue(v.Get("templateId").String())
tflog.Debug(ctx, fmt.Sprintf("%s: Found feature template with name '%v', id: %v", config.Id.String(), config.Name.ValueString(), config.Id.String()))
return false
}
return true
})
}
if config.Id.IsNull() {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to find feature template with name: %s", config.Name.ValueString()))
return
}
}
res, err := d.client.Get("/template/feature/object/" + url.QueryEscape(config.Id.ValueString()))
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object, got error: %s", err))
return
}
config.fromBody(ctx, res)
tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", config.Name.ValueString()))
diags = resp.State.Set(ctx, &config)
resp.Diagnostics.Append(diags...)
}