@@ -24,9 +24,10 @@ type awsCWScrapeJobTFResourceModel struct {
24
24
// TODO(tristan): if the grafana provider is updated to use the Terraform v6 plugin protocol,
25
25
// we can consider adding additional support to use Set Nested Attributes, instead of Blocks.
26
26
// See https://developer.hashicorp.com/terraform/plugin/framework/handling-data/attributes#nested-attribute-types
27
- Services [] awsCWScrapeJobServiceTFModel `tfsdk:"service"`
28
- CustomNamespaces [] awsCWScrapeJobCustomNamespaceTFModel `tfsdk:"custom_namespace"`
27
+ Services types. List `tfsdk:"service"`
28
+ CustomNamespaces types. List `tfsdk:"custom_namespace"`
29
29
}
30
+
30
31
type awsCWScrapeJobTFDataSourceModel struct {
31
32
ID types.String `tfsdk:"id"`
32
33
StackID types.String `tfsdk:"stack_id"`
@@ -41,30 +42,82 @@ type awsCWScrapeJobTFDataSourceModel struct {
41
42
// TODO(tristan): if the grafana provider is updated to use the Terraform v6 plugin protocol,
42
43
// we can consider adding additional support to use Set Nested Attributes, instead of Blocks.
43
44
// See https://developer.hashicorp.com/terraform/plugin/framework/handling-data/attributes#nested-attribute-types
44
- Services [] awsCWScrapeJobServiceTFModel `tfsdk:"service"`
45
- CustomNamespaces [] awsCWScrapeJobCustomNamespaceTFModel `tfsdk:"custom_namespace"`
45
+ Services types. List `tfsdk:"service"`
46
+ CustomNamespaces types. List `tfsdk:"custom_namespace"`
46
47
}
48
+
47
49
type awsCWScrapeJobServiceTFModel struct {
48
- Name types.String `tfsdk:"name"`
49
- Metrics []awsCWScrapeJobMetricTFModel `tfsdk:"metric"`
50
- ScrapeIntervalSeconds types.Int64 `tfsdk:"scrape_interval_seconds"`
51
- ResourceDiscoveryTagFilters []awsCWScrapeJobTagFilterTFModel `tfsdk:"resource_discovery_tag_filter"`
52
- TagsToAddToMetrics types.Set `tfsdk:"tags_to_add_to_metrics"`
50
+ Name types.String `tfsdk:"name"`
51
+ Metrics types.List `tfsdk:"metric"`
52
+ ScrapeIntervalSeconds types.Int64 `tfsdk:"scrape_interval_seconds"`
53
+ ResourceDiscoveryTagFilters types.List `tfsdk:"resource_discovery_tag_filter"`
54
+ TagsToAddToMetrics types.Set `tfsdk:"tags_to_add_to_metrics"`
55
+ }
56
+
57
+ func (m awsCWScrapeJobServiceTFModel ) attrTypes () map [string ]attr.Type {
58
+ return map [string ]attr.Type {
59
+ "name" : types .StringType ,
60
+ "metric" : types.ListType {
61
+ ElemType : types.ObjectType {
62
+ AttrTypes : awsCWScrapeJobMetricTFModel {}.attrTypes (),
63
+ },
64
+ },
65
+ "scrape_interval_seconds" : types .Int64Type ,
66
+ "resource_discovery_tag_filter" : types.ListType {
67
+ ElemType : types.ObjectType {
68
+ AttrTypes : awsCWScrapeJobTagFilterTFModel {}.attrTypes (),
69
+ },
70
+ },
71
+ "tags_to_add_to_metrics" : types.SetType {
72
+ ElemType : types .StringType ,
73
+ },
74
+ }
53
75
}
76
+
54
77
type awsCWScrapeJobCustomNamespaceTFModel struct {
55
- Name types.String `tfsdk:"name"`
56
- Metrics []awsCWScrapeJobMetricTFModel `tfsdk:"metric"`
57
- ScrapeIntervalSeconds types.Int64 `tfsdk:"scrape_interval_seconds"`
78
+ Name types.String `tfsdk:"name"`
79
+ Metrics types.List `tfsdk:"metric"`
80
+ ScrapeIntervalSeconds types.Int64 `tfsdk:"scrape_interval_seconds"`
81
+ }
82
+
83
+ func (m awsCWScrapeJobCustomNamespaceTFModel ) attrTypes () map [string ]attr.Type {
84
+ return map [string ]attr.Type {
85
+ "name" : types .StringType ,
86
+ "metric" : types.ListType {
87
+ ElemType : types.ObjectType {
88
+ AttrTypes : awsCWScrapeJobMetricTFModel {}.attrTypes (),
89
+ },
90
+ },
91
+ "scrape_interval_seconds" : types .Int64Type ,
92
+ }
58
93
}
94
+
59
95
type awsCWScrapeJobMetricTFModel struct {
60
96
Name types.String `tfsdk:"name"`
61
97
Statistics types.Set `tfsdk:"statistics"`
62
98
}
99
+
100
+ func (m awsCWScrapeJobMetricTFModel ) attrTypes () map [string ]attr.Type {
101
+ return map [string ]attr.Type {
102
+ "name" : types .StringType ,
103
+ "statistics" : types.SetType {
104
+ ElemType : types .StringType ,
105
+ },
106
+ }
107
+ }
108
+
63
109
type awsCWScrapeJobTagFilterTFModel struct {
64
110
Key types.String `tfsdk:"key"`
65
111
Value types.String `tfsdk:"value"`
66
112
}
67
113
114
+ func (m awsCWScrapeJobTagFilterTFModel ) attrTypes () map [string ]attr.Type {
115
+ return map [string ]attr.Type {
116
+ "key" : types .StringType ,
117
+ "value" : types .StringType ,
118
+ }
119
+ }
120
+
68
121
type awsCWScrapeJobNoDuplicateServiceNamesValidator struct {}
69
122
70
123
func (v awsCWScrapeJobNoDuplicateServiceNamesValidator ) Description (ctx context.Context ) string {
@@ -76,15 +129,15 @@ func (v awsCWScrapeJobNoDuplicateServiceNamesValidator) MarkdownDescription(ctx
76
129
}
77
130
78
131
func (v awsCWScrapeJobNoDuplicateServiceNamesValidator ) ValidateList (ctx context.Context , req validator.ListRequest , resp * validator.ListResponse ) {
79
- seen := map [string ]struct {}{}
80
- elems := make ([]awsCWScrapeJobServiceTFModel , len (req .ConfigValue .Elements ()))
81
- diags := req .ConfigValue .ElementsAs (ctx , & elems , false )
132
+ var services []awsCWScrapeJobServiceTFModel
133
+ diags := req .ConfigValue .ElementsAs (ctx , & services , true )
82
134
resp .Diagnostics .Append (diags ... )
83
135
if diags .HasError () {
84
136
return
85
137
}
86
- for _ , elem := range elems {
87
- name := elem .Name .ValueString ()
138
+ seen := map [string ]struct {}{}
139
+ for _ , service := range services {
140
+ name := service .Name .ValueString ()
88
141
if _ , ok := seen [name ]; ok {
89
142
resp .Diagnostics .AddError ("Duplicate service name" , fmt .Sprintf ("Service name %q is duplicated." , name ))
90
143
}
@@ -103,15 +156,15 @@ func (v awsCWScrapeJobNoDuplicateCustomNamespaceNamesValidator) MarkdownDescript
103
156
}
104
157
105
158
func (v awsCWScrapeJobNoDuplicateCustomNamespaceNamesValidator ) ValidateList (ctx context.Context , req validator.ListRequest , resp * validator.ListResponse ) {
106
- seen := map [string ]struct {}{}
107
- elems := make ([]awsCWScrapeJobCustomNamespaceTFModel , len (req .ConfigValue .Elements ()))
108
- diags := req .ConfigValue .ElementsAs (ctx , & elems , false )
159
+ var customNamespaces []awsCWScrapeJobCustomNamespaceTFModel
160
+ diags := req .ConfigValue .ElementsAs (ctx , & customNamespaces , true )
109
161
resp .Diagnostics .Append (diags ... )
110
162
if diags .HasError () {
111
163
return
112
164
}
113
- for _ , elem := range elems {
114
- name := elem .Name .ValueString ()
165
+ seen := map [string ]struct {}{}
166
+ for _ , customNamespace := range customNamespaces {
167
+ name := customNamespace .Name .ValueString ()
115
168
if _ , ok := seen [name ]; ok {
116
169
resp .Diagnostics .AddError ("Duplicate custom namespace name" , fmt .Sprintf ("Custom namespace name %q is duplicated." , name ))
117
170
}
@@ -162,8 +215,14 @@ func (tfData awsCWScrapeJobTFResourceModel) toClientModel(ctx context.Context) (
162
215
return cloudproviderapi.AWSCloudWatchScrapeJobRequest {}, conversionDiags
163
216
}
164
217
165
- converted .Services = make ([]cloudproviderapi.AWSCloudWatchService , len (tfData .Services ))
166
- for i , service := range tfData .Services {
218
+ var services []awsCWScrapeJobServiceTFModel
219
+ diags = tfData .Services .ElementsAs (ctx , & services , false )
220
+ conversionDiags .Append (diags ... )
221
+ if conversionDiags .HasError () {
222
+ return cloudproviderapi.AWSCloudWatchScrapeJobRequest {}, conversionDiags
223
+ }
224
+ converted .Services = make ([]cloudproviderapi.AWSCloudWatchService , len (services ))
225
+ for i , service := range services {
167
226
converted .Services [i ] = cloudproviderapi.AWSCloudWatchService {
168
227
Name : service .Name .ValueString (),
169
228
ScrapeIntervalSeconds : service .ScrapeIntervalSeconds .ValueInt64 (),
@@ -175,8 +234,14 @@ func (tfData awsCWScrapeJobTFResourceModel) toClientModel(ctx context.Context) (
175
234
return cloudproviderapi.AWSCloudWatchScrapeJobRequest {}, conversionDiags
176
235
}
177
236
178
- converted .Services [i ].Metrics = make ([]cloudproviderapi.AWSCloudWatchMetric , len (service .Metrics ))
179
- for j , metric := range service .Metrics {
237
+ var metrics []awsCWScrapeJobMetricTFModel
238
+ diags = service .Metrics .ElementsAs (ctx , & metrics , false )
239
+ conversionDiags .Append (diags ... )
240
+ if conversionDiags .HasError () {
241
+ return cloudproviderapi.AWSCloudWatchScrapeJobRequest {}, conversionDiags
242
+ }
243
+ converted .Services [i ].Metrics = make ([]cloudproviderapi.AWSCloudWatchMetric , len (metrics ))
244
+ for j , metric := range metrics {
180
245
converted .Services [i ].Metrics [j ] = cloudproviderapi.AWSCloudWatchMetric {
181
246
Name : metric .Name .ValueString (),
182
247
}
@@ -188,24 +253,42 @@ func (tfData awsCWScrapeJobTFResourceModel) toClientModel(ctx context.Context) (
188
253
}
189
254
}
190
255
191
- converted .Services [i ].ResourceDiscoveryTagFilters = make ([]cloudproviderapi.AWSCloudWatchTagFilter , len (service .ResourceDiscoveryTagFilters ))
192
- for j , tagFilter := range service .ResourceDiscoveryTagFilters {
256
+ var tagFilters []awsCWScrapeJobTagFilterTFModel
257
+ diags = service .ResourceDiscoveryTagFilters .ElementsAs (ctx , & tagFilters , false )
258
+ conversionDiags .Append (diags ... )
259
+ if conversionDiags .HasError () {
260
+ return cloudproviderapi.AWSCloudWatchScrapeJobRequest {}, conversionDiags
261
+ }
262
+ converted .Services [i ].ResourceDiscoveryTagFilters = make ([]cloudproviderapi.AWSCloudWatchTagFilter , len (tagFilters ))
263
+ for j , tagFilter := range tagFilters {
193
264
converted .Services [i ].ResourceDiscoveryTagFilters [j ] = cloudproviderapi.AWSCloudWatchTagFilter {
194
265
Key : tagFilter .Key .ValueString (),
195
266
Value : tagFilter .Value .ValueString (),
196
267
}
197
268
}
198
269
}
199
270
200
- converted .CustomNamespaces = make ([]cloudproviderapi.AWSCloudWatchCustomNamespace , len (tfData .CustomNamespaces ))
201
- for i , customNamespace := range tfData .CustomNamespaces {
271
+ var customNamepsaces []awsCWScrapeJobCustomNamespaceTFModel
272
+ diags = tfData .CustomNamespaces .ElementsAs (ctx , & customNamepsaces , false )
273
+ conversionDiags .Append (diags ... )
274
+ if conversionDiags .HasError () {
275
+ return cloudproviderapi.AWSCloudWatchScrapeJobRequest {}, conversionDiags
276
+ }
277
+ converted .CustomNamespaces = make ([]cloudproviderapi.AWSCloudWatchCustomNamespace , len (customNamepsaces ))
278
+ for i , customNamespace := range customNamepsaces {
202
279
converted .CustomNamespaces [i ] = cloudproviderapi.AWSCloudWatchCustomNamespace {
203
280
Name : customNamespace .Name .ValueString (),
204
281
ScrapeIntervalSeconds : customNamespace .ScrapeIntervalSeconds .ValueInt64 (),
205
282
}
206
283
207
- converted .CustomNamespaces [i ].Metrics = make ([]cloudproviderapi.AWSCloudWatchMetric , len (customNamespace .Metrics ))
208
- for j , metric := range customNamespace .Metrics {
284
+ var metrics []awsCWScrapeJobMetricTFModel
285
+ diags = customNamespace .Metrics .ElementsAs (ctx , & metrics , false )
286
+ conversionDiags .Append (diags ... )
287
+ if conversionDiags .HasError () {
288
+ return cloudproviderapi.AWSCloudWatchScrapeJobRequest {}, conversionDiags
289
+ }
290
+ converted .CustomNamespaces [i ].Metrics = make ([]cloudproviderapi.AWSCloudWatchMetric , len (metrics ))
291
+ for j , metric := range metrics {
209
292
converted .CustomNamespaces [i ].Metrics [j ] = cloudproviderapi.AWSCloudWatchMetric {
210
293
Name : metric .Name .ValueString (),
211
294
}
@@ -301,80 +384,110 @@ func generateCloudWatchScrapeJobDataSourceTFModel(ctx context.Context, stackID s
301
384
return converted , conversionDiags
302
385
}
303
386
304
- func convertServicesClientToTFModel (ctx context.Context , services []cloudproviderapi.AWSCloudWatchService ) ([] awsCWScrapeJobServiceTFModel , diag.Diagnostics ) {
387
+ func convertServicesClientToTFModel (ctx context.Context , services []cloudproviderapi.AWSCloudWatchService ) (types. List , diag.Diagnostics ) {
305
388
conversionDiags := diag.Diagnostics {}
306
389
servicesTF := make ([]awsCWScrapeJobServiceTFModel , len (services ))
390
+ servicesListObjType := types.ObjectType {AttrTypes : awsCWScrapeJobServiceTFModel {}.attrTypes ()}
307
391
308
- for i , serviceData := range services {
309
- service := awsCWScrapeJobServiceTFModel {
310
- Name : types .StringValue (serviceData .Name ),
311
- ScrapeIntervalSeconds : types .Int64Value (serviceData .ScrapeIntervalSeconds ),
392
+ for i , service := range services {
393
+ serviceTF := awsCWScrapeJobServiceTFModel {
394
+ Name : types .StringValue (service .Name ),
395
+ ScrapeIntervalSeconds : types .Int64Value (service .ScrapeIntervalSeconds ),
312
396
}
313
397
314
- metricsData := make ([]awsCWScrapeJobMetricTFModel , len (serviceData .Metrics ))
315
- for j , metricData := range serviceData .Metrics {
316
- metricsData [j ] = awsCWScrapeJobMetricTFModel {
317
- Name : types .StringValue (metricData .Name ),
398
+ metricsTF := make ([]awsCWScrapeJobMetricTFModel , len (service .Metrics ))
399
+ for j , metric := range service .Metrics {
400
+ metricsTF [j ] = awsCWScrapeJobMetricTFModel {
401
+ Name : types .StringValue (metric .Name ),
318
402
}
319
403
statistics , diags := types .SetValueFrom (ctx , basetypes.StringType {}, services [i ].Metrics [j ].Statistics )
320
404
conversionDiags .Append (diags ... )
321
405
if conversionDiags .HasError () {
322
- return nil , conversionDiags
406
+ return types . ListNull ( servicesListObjType ) , conversionDiags
323
407
}
324
- metricsData [j ].Statistics = statistics
408
+ metricsTF [j ].Statistics = statistics
409
+ }
410
+ metricsTFList , diags := types .ListValueFrom (ctx , types.ObjectType {AttrTypes : awsCWScrapeJobMetricTFModel {}.attrTypes ()}, metricsTF )
411
+ conversionDiags .Append (diags ... )
412
+ if conversionDiags .HasError () {
413
+ return types .ListNull (servicesListObjType ), conversionDiags
325
414
}
326
- service .Metrics = metricsData
415
+ serviceTF .Metrics = metricsTFList
327
416
328
- tagFiltersData := make ([]awsCWScrapeJobTagFilterTFModel , len (serviceData .ResourceDiscoveryTagFilters ))
329
- for j , tagFilterData := range serviceData .ResourceDiscoveryTagFilters {
330
- tagFiltersData [j ] = awsCWScrapeJobTagFilterTFModel {
331
- Key : types .StringValue (tagFilterData .Key ),
332
- Value : types .StringValue (tagFilterData .Value ),
417
+ tagFiltersTF := make ([]awsCWScrapeJobTagFilterTFModel , len (service .ResourceDiscoveryTagFilters ))
418
+ for j , tagFilter := range service .ResourceDiscoveryTagFilters {
419
+ tagFiltersTF [j ] = awsCWScrapeJobTagFilterTFModel {
420
+ Key : types .StringValue (tagFilter .Key ),
421
+ Value : types .StringValue (tagFilter .Value ),
333
422
}
334
423
}
335
- service .ResourceDiscoveryTagFilters = tagFiltersData
424
+ tagFiltersTFList , diags := types .ListValueFrom (ctx , types.ObjectType {AttrTypes : awsCWScrapeJobTagFilterTFModel {}.attrTypes ()}, tagFiltersTF )
425
+ conversionDiags .Append (diags ... )
426
+ if conversionDiags .HasError () {
427
+ return types .ListNull (servicesListObjType ), conversionDiags
428
+ }
429
+ serviceTF .ResourceDiscoveryTagFilters = tagFiltersTFList
336
430
337
431
tagsToAdd , diags := types .SetValueFrom (ctx , basetypes.StringType {}, services [i ].TagsToAddToMetrics )
338
432
if tagsToAdd .IsNull () {
339
433
tagsToAdd = types .SetValueMust (basetypes.StringType {}, []attr.Value {})
340
434
}
341
435
conversionDiags .Append (diags ... )
342
436
if conversionDiags .HasError () {
343
- return nil , conversionDiags
437
+ return types . ListNull ( servicesListObjType ) , conversionDiags
344
438
}
345
- service .TagsToAddToMetrics = tagsToAdd
346
- servicesTF [i ] = service
439
+ serviceTF .TagsToAddToMetrics = tagsToAdd
440
+
441
+ servicesTF [i ] = serviceTF
442
+ }
443
+
444
+ servicesTFList , diags := types .ListValueFrom (ctx , servicesListObjType , servicesTF )
445
+ conversionDiags .Append (diags ... )
446
+ if conversionDiags .HasError () {
447
+ return types .ListNull (servicesListObjType ), conversionDiags
347
448
}
348
449
349
- return servicesTF , conversionDiags
450
+ return servicesTFList , conversionDiags
350
451
}
351
452
352
- func convertCustomNamespacesClientToTFModel (ctx context.Context , customNamespaces []cloudproviderapi.AWSCloudWatchCustomNamespace ) ([] awsCWScrapeJobCustomNamespaceTFModel , diag.Diagnostics ) {
453
+ func convertCustomNamespacesClientToTFModel (ctx context.Context , customNamespaces []cloudproviderapi.AWSCloudWatchCustomNamespace ) (types. List , diag.Diagnostics ) {
353
454
conversionDiags := diag.Diagnostics {}
354
455
customNamespacesTF := make ([]awsCWScrapeJobCustomNamespaceTFModel , len (customNamespaces ))
456
+ customNamspacesListObjType := types.ObjectType {AttrTypes : awsCWScrapeJobCustomNamespaceTFModel {}.attrTypes ()}
355
457
356
- for i , customNamespaceData := range customNamespaces {
357
- customNamespace := awsCWScrapeJobCustomNamespaceTFModel {
358
- Name : types .StringValue (customNamespaceData .Name ),
359
- ScrapeIntervalSeconds : types .Int64Value (customNamespaceData .ScrapeIntervalSeconds ),
458
+ for i , customNamespace := range customNamespaces {
459
+ customNamespaceTF := awsCWScrapeJobCustomNamespaceTFModel {
460
+ Name : types .StringValue (customNamespace .Name ),
461
+ ScrapeIntervalSeconds : types .Int64Value (customNamespace .ScrapeIntervalSeconds ),
360
462
}
361
463
362
- metricsData := make ([]awsCWScrapeJobMetricTFModel , len (customNamespaceData .Metrics ))
363
- for j , metricData := range customNamespaceData .Metrics {
364
- metricsData [j ] = awsCWScrapeJobMetricTFModel {
365
- Name : types .StringValue (metricData .Name ),
464
+ metricsTF := make ([]awsCWScrapeJobMetricTFModel , len (customNamespace .Metrics ))
465
+ for j , metric := range customNamespace .Metrics {
466
+ metricsTF [j ] = awsCWScrapeJobMetricTFModel {
467
+ Name : types .StringValue (metric .Name ),
366
468
}
367
469
statistics , diags := types .SetValueFrom (ctx , basetypes.StringType {}, customNamespaces [i ].Metrics [j ].Statistics )
368
470
conversionDiags .Append (diags ... )
369
471
if conversionDiags .HasError () {
370
- return nil , conversionDiags
472
+ return types . ListNull ( customNamspacesListObjType ) , conversionDiags
371
473
}
372
- metricsData [j ].Statistics = statistics
474
+ metricsTF [j ].Statistics = statistics
475
+ }
476
+ metricsTFList , diags := types .ListValueFrom (ctx , types.ObjectType {AttrTypes : awsCWScrapeJobMetricTFModel {}.attrTypes ()}, metricsTF )
477
+ conversionDiags .Append (diags ... )
478
+ if conversionDiags .HasError () {
479
+ return types .ListNull (customNamspacesListObjType ), conversionDiags
373
480
}
374
- customNamespace .Metrics = metricsData
481
+ customNamespaceTF .Metrics = metricsTFList
375
482
376
- customNamespacesTF [i ] = customNamespace
483
+ customNamespacesTF [i ] = customNamespaceTF
484
+ }
485
+
486
+ customNamespacesTFList , diags := types .ListValueFrom (ctx , customNamspacesListObjType , customNamespacesTF )
487
+ conversionDiags .Append (diags ... )
488
+ if conversionDiags .HasError () {
489
+ return types .ListNull (customNamspacesListObjType ), conversionDiags
377
490
}
378
491
379
- return customNamespacesTF , conversionDiags
492
+ return customNamespacesTFList , conversionDiags
380
493
}
0 commit comments