-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathmodels.go
521 lines (459 loc) · 21.5 KB
/
models.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
package cloudprovider
import (
"context"
"fmt"
"github.com/grafana/terraform-provider-grafana/v3/internal/common/cloudproviderapi"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)
type awsCWScrapeJobTFResourceModel struct {
ID types.String `tfsdk:"id"`
StackID types.String `tfsdk:"stack_id"`
Name types.String `tfsdk:"name"`
Enabled types.Bool `tfsdk:"enabled"`
AWSAccountResourceID types.String `tfsdk:"aws_account_resource_id"`
RegionsSubsetOverride types.Set `tfsdk:"regions_subset_override"`
ExportTags types.Bool `tfsdk:"export_tags"`
DisabledReason types.String `tfsdk:"disabled_reason"`
// TODO(tristan): if the grafana provider is updated to use the Terraform v6 plugin protocol,
// we can consider adding additional support to use Set Nested Attributes, instead of Blocks.
// See https://developer.hashicorp.com/terraform/plugin/framework/handling-data/attributes#nested-attribute-types
Services types.List `tfsdk:"service"`
CustomNamespaces types.List `tfsdk:"custom_namespace"`
StaticLabels types.Map `tfsdk:"static_labels"`
}
type awsCWScrapeJobTFDataSourceModel struct {
ID types.String `tfsdk:"id"`
StackID types.String `tfsdk:"stack_id"`
Name types.String `tfsdk:"name"`
Enabled types.Bool `tfsdk:"enabled"`
AWSAccountResourceID types.String `tfsdk:"aws_account_resource_id"`
Regions types.Set `tfsdk:"regions"`
RegionsSubsetOverrideUsed types.Bool `tfsdk:"regions_subset_override_used"`
RoleARN types.String `tfsdk:"role_arn"`
ExportTags types.Bool `tfsdk:"export_tags"`
DisabledReason types.String `tfsdk:"disabled_reason"`
// TODO(tristan): if the grafana provider is updated to use the Terraform v6 plugin protocol,
// we can consider adding additional support to use Set Nested Attributes, instead of Blocks.
// See https://developer.hashicorp.com/terraform/plugin/framework/handling-data/attributes#nested-attribute-types
Services types.List `tfsdk:"service"`
CustomNamespaces types.List `tfsdk:"custom_namespace"`
StaticLabels types.Map `tfsdk:"static_labels"`
}
type awsCWScrapeJobServiceTFModel struct {
Name types.String `tfsdk:"name"`
Metrics types.List `tfsdk:"metric"`
ScrapeIntervalSeconds types.Int64 `tfsdk:"scrape_interval_seconds"`
ResourceDiscoveryTagFilters types.List `tfsdk:"resource_discovery_tag_filter"`
TagsToAddToMetrics types.Set `tfsdk:"tags_to_add_to_metrics"`
}
func (m awsCWScrapeJobServiceTFModel) attrTypes() map[string]attr.Type {
return map[string]attr.Type{
"name": types.StringType,
"metric": types.ListType{
ElemType: types.ObjectType{
AttrTypes: awsCWScrapeJobMetricTFModel{}.attrTypes(),
},
},
"scrape_interval_seconds": types.Int64Type,
"resource_discovery_tag_filter": types.ListType{
ElemType: types.ObjectType{
AttrTypes: awsCWScrapeJobTagFilterTFModel{}.attrTypes(),
},
},
"tags_to_add_to_metrics": types.SetType{
ElemType: types.StringType,
},
}
}
type awsCWScrapeJobCustomNamespaceTFModel struct {
Name types.String `tfsdk:"name"`
Metrics types.List `tfsdk:"metric"`
ScrapeIntervalSeconds types.Int64 `tfsdk:"scrape_interval_seconds"`
}
func (m awsCWScrapeJobCustomNamespaceTFModel) attrTypes() map[string]attr.Type {
return map[string]attr.Type{
"name": types.StringType,
"metric": types.ListType{
ElemType: types.ObjectType{
AttrTypes: awsCWScrapeJobMetricTFModel{}.attrTypes(),
},
},
"scrape_interval_seconds": types.Int64Type,
}
}
type awsCWScrapeJobMetricTFModel struct {
Name types.String `tfsdk:"name"`
Statistics types.Set `tfsdk:"statistics"`
}
func (m awsCWScrapeJobMetricTFModel) attrTypes() map[string]attr.Type {
return map[string]attr.Type{
"name": types.StringType,
"statistics": types.SetType{
ElemType: types.StringType,
},
}
}
type awsCWScrapeJobTagFilterTFModel struct {
Key types.String `tfsdk:"key"`
Value types.String `tfsdk:"value"`
}
func (m awsCWScrapeJobTagFilterTFModel) attrTypes() map[string]attr.Type {
return map[string]attr.Type{
"key": types.StringType,
"value": types.StringType,
}
}
type awsCWScrapeJobNoDuplicateServiceNamesValidator struct{}
func (v awsCWScrapeJobNoDuplicateServiceNamesValidator) Description(ctx context.Context) string {
return "No duplicate service names are allowed."
}
func (v awsCWScrapeJobNoDuplicateServiceNamesValidator) MarkdownDescription(ctx context.Context) string {
return "No duplicate service names are allowed."
}
func (v awsCWScrapeJobNoDuplicateServiceNamesValidator) ValidateList(ctx context.Context, req validator.ListRequest, resp *validator.ListResponse) {
var services []awsCWScrapeJobServiceTFModel
diags := req.ConfigValue.ElementsAs(ctx, &services, true)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
seen := map[string]struct{}{}
for _, service := range services {
name := service.Name.ValueString()
if _, ok := seen[name]; ok {
resp.Diagnostics.AddError("Duplicate service name", fmt.Sprintf("Service name %q is duplicated.", name))
}
seen[name] = struct{}{}
}
}
type awsCWScrapeJobNoDuplicateCustomNamespaceNamesValidator struct{}
func (v awsCWScrapeJobNoDuplicateCustomNamespaceNamesValidator) Description(ctx context.Context) string {
return "No duplicate custom namespace names are allowed."
}
func (v awsCWScrapeJobNoDuplicateCustomNamespaceNamesValidator) MarkdownDescription(ctx context.Context) string {
return "No duplicate custom namespace names are allowed."
}
func (v awsCWScrapeJobNoDuplicateCustomNamespaceNamesValidator) ValidateList(ctx context.Context, req validator.ListRequest, resp *validator.ListResponse) {
var customNamespaces []awsCWScrapeJobCustomNamespaceTFModel
diags := req.ConfigValue.ElementsAs(ctx, &customNamespaces, true)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
seen := map[string]struct{}{}
for _, customNamespace := range customNamespaces {
name := customNamespace.Name.ValueString()
if _, ok := seen[name]; ok {
resp.Diagnostics.AddError("Duplicate custom namespace name", fmt.Sprintf("Custom namespace name %q is duplicated.", name))
}
seen[name] = struct{}{}
}
}
type awsCWScrapeJobNoDuplicateMetricNamesValidator struct{}
func (v awsCWScrapeJobNoDuplicateMetricNamesValidator) Description(ctx context.Context) string {
return "Metric names must be unique (case-insensitive) within the same service or custom namespace."
}
func (v awsCWScrapeJobNoDuplicateMetricNamesValidator) MarkdownDescription(ctx context.Context) string {
return "Metric names must be unique (case-insensitive) within the same service or custom namespace."
}
func (v awsCWScrapeJobNoDuplicateMetricNamesValidator) ValidateList(ctx context.Context, req validator.ListRequest, resp *validator.ListResponse) {
seen := map[string]struct{}{}
elems := make([]awsCWScrapeJobMetricTFModel, len(req.ConfigValue.Elements()))
diags := req.ConfigValue.ElementsAs(ctx, &elems, true)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
for _, elem := range elems {
name := elem.Name.ValueString()
if _, ok := seen[name]; ok {
resp.Diagnostics.AddError("Duplicate metric name for service or custom namespace", fmt.Sprintf("Metric name %q is duplicated within the service or custom namespace.", name))
}
seen[name] = struct{}{}
}
}
// toClientModel converts a awsCWScrapeJobTFModel instance to a cloudproviderapi.AWSCloudWatchScrapeJobRequest instance.
func (tfData awsCWScrapeJobTFResourceModel) toClientModel(ctx context.Context) (cloudproviderapi.AWSCloudWatchScrapeJobRequest, diag.Diagnostics) {
conversionDiags := diag.Diagnostics{}
converted := cloudproviderapi.AWSCloudWatchScrapeJobRequest{
Name: tfData.Name.ValueString(),
Enabled: tfData.Enabled.ValueBool(),
AWSAccountResourceID: tfData.AWSAccountResourceID.ValueString(),
ExportTags: tfData.ExportTags.ValueBool(),
}
diags := tfData.RegionsSubsetOverride.ElementsAs(ctx, &converted.RegionsSubsetOverride, false)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return cloudproviderapi.AWSCloudWatchScrapeJobRequest{}, conversionDiags
}
var services []awsCWScrapeJobServiceTFModel
diags = tfData.Services.ElementsAs(ctx, &services, false)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return cloudproviderapi.AWSCloudWatchScrapeJobRequest{}, conversionDiags
}
converted.Services = make([]cloudproviderapi.AWSCloudWatchService, len(services))
for i, service := range services {
converted.Services[i] = cloudproviderapi.AWSCloudWatchService{
Name: service.Name.ValueString(),
ScrapeIntervalSeconds: service.ScrapeIntervalSeconds.ValueInt64(),
}
diags = service.TagsToAddToMetrics.ElementsAs(ctx, &converted.Services[i].TagsToAddToMetrics, false)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return cloudproviderapi.AWSCloudWatchScrapeJobRequest{}, conversionDiags
}
var metrics []awsCWScrapeJobMetricTFModel
diags = service.Metrics.ElementsAs(ctx, &metrics, false)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return cloudproviderapi.AWSCloudWatchScrapeJobRequest{}, conversionDiags
}
converted.Services[i].Metrics = make([]cloudproviderapi.AWSCloudWatchMetric, len(metrics))
for j, metric := range metrics {
converted.Services[i].Metrics[j] = cloudproviderapi.AWSCloudWatchMetric{
Name: metric.Name.ValueString(),
}
diags = metric.Statistics.ElementsAs(ctx, &converted.Services[i].Metrics[j].Statistics, false)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return cloudproviderapi.AWSCloudWatchScrapeJobRequest{}, conversionDiags
}
}
var tagFilters []awsCWScrapeJobTagFilterTFModel
diags = service.ResourceDiscoveryTagFilters.ElementsAs(ctx, &tagFilters, false)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return cloudproviderapi.AWSCloudWatchScrapeJobRequest{}, conversionDiags
}
converted.Services[i].ResourceDiscoveryTagFilters = make([]cloudproviderapi.AWSCloudWatchTagFilter, len(tagFilters))
for j, tagFilter := range tagFilters {
converted.Services[i].ResourceDiscoveryTagFilters[j] = cloudproviderapi.AWSCloudWatchTagFilter{
Key: tagFilter.Key.ValueString(),
Value: tagFilter.Value.ValueString(),
}
}
}
var customNamepsaces []awsCWScrapeJobCustomNamespaceTFModel
diags = tfData.CustomNamespaces.ElementsAs(ctx, &customNamepsaces, false)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return cloudproviderapi.AWSCloudWatchScrapeJobRequest{}, conversionDiags
}
converted.CustomNamespaces = make([]cloudproviderapi.AWSCloudWatchCustomNamespace, len(customNamepsaces))
for i, customNamespace := range customNamepsaces {
converted.CustomNamespaces[i] = cloudproviderapi.AWSCloudWatchCustomNamespace{
Name: customNamespace.Name.ValueString(),
ScrapeIntervalSeconds: customNamespace.ScrapeIntervalSeconds.ValueInt64(),
}
var metrics []awsCWScrapeJobMetricTFModel
diags = customNamespace.Metrics.ElementsAs(ctx, &metrics, false)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return cloudproviderapi.AWSCloudWatchScrapeJobRequest{}, conversionDiags
}
converted.CustomNamespaces[i].Metrics = make([]cloudproviderapi.AWSCloudWatchMetric, len(metrics))
for j, metric := range metrics {
converted.CustomNamespaces[i].Metrics[j] = cloudproviderapi.AWSCloudWatchMetric{
Name: metric.Name.ValueString(),
}
diags = metric.Statistics.ElementsAs(ctx, &converted.CustomNamespaces[i].Metrics[j].Statistics, false)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return cloudproviderapi.AWSCloudWatchScrapeJobRequest{}, conversionDiags
}
}
}
diags = tfData.StaticLabels.ElementsAs(ctx, &converted.StaticLabels, false)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return cloudproviderapi.AWSCloudWatchScrapeJobRequest{}, conversionDiags
}
return converted, conversionDiags
}
// generateCloudWatchScrapeJobTFResourceModel generates a new awsCWScrapeJobTFResourceModel based on the provided cloudproviderapi.AWSCloudWatchScrapeJobResponse
func generateCloudWatchScrapeJobTFResourceModel(ctx context.Context, stackID string, scrapeJobData cloudproviderapi.AWSCloudWatchScrapeJobResponse) (awsCWScrapeJobTFResourceModel, diag.Diagnostics) {
conversionDiags := diag.Diagnostics{}
converted := awsCWScrapeJobTFResourceModel{
ID: types.StringValue(resourceAWSCloudWatchScrapeJobTerraformID.Make(stackID, scrapeJobData.Name)),
StackID: types.StringValue(stackID),
Name: types.StringValue(scrapeJobData.Name),
Enabled: types.BoolValue(scrapeJobData.Enabled),
AWSAccountResourceID: types.StringValue(scrapeJobData.AWSAccountResourceID),
ExportTags: types.BoolValue(scrapeJobData.ExportTags),
DisabledReason: types.StringValue(scrapeJobData.DisabledReason),
}
regionsSubsetOverride := types.SetValueMust(basetypes.StringType{}, []attr.Value{})
if scrapeJobData.RegionsSubsetOverrideUsed {
regions, diags := types.SetValueFrom(ctx, basetypes.StringType{}, scrapeJobData.Regions)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return awsCWScrapeJobTFResourceModel{}, conversionDiags
}
regionsSubsetOverride = regions
}
converted.RegionsSubsetOverride = regionsSubsetOverride
services, diags := convertServicesClientToTFModel(ctx, scrapeJobData.Services)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return awsCWScrapeJobTFResourceModel{}, conversionDiags
}
converted.Services = services
customNamespaces, diags := convertCustomNamespacesClientToTFModel(ctx, scrapeJobData.CustomNamespaces)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return awsCWScrapeJobTFResourceModel{}, conversionDiags
}
converted.CustomNamespaces = customNamespaces
staticLabelsMap := types.MapValueMust(types.StringType, map[string]attr.Value{})
if scrapeJobData.StaticLabels != nil {
staticLabelsMap, diags = types.MapValueFrom(ctx, basetypes.StringType{}, scrapeJobData.StaticLabels)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return awsCWScrapeJobTFResourceModel{}, conversionDiags
}
}
converted.StaticLabels = staticLabelsMap
return converted, conversionDiags
}
// generateCloudWatchScrapeJobTFDataSourceModel generates a new awsCWScrapeJobTFDataSourceModel based on the provided cloudproviderapi.AWSCloudWatchScrapeJobResponse
func generateCloudWatchScrapeJobDataSourceTFModel(ctx context.Context, stackID string, scrapeJobData cloudproviderapi.AWSCloudWatchScrapeJobResponse) (awsCWScrapeJobTFDataSourceModel, diag.Diagnostics) {
conversionDiags := diag.Diagnostics{}
converted := awsCWScrapeJobTFDataSourceModel{
ID: types.StringValue(resourceAWSCloudWatchScrapeJobTerraformID.Make(stackID, scrapeJobData.Name)),
StackID: types.StringValue(stackID),
Name: types.StringValue(scrapeJobData.Name),
Enabled: types.BoolValue(scrapeJobData.Enabled),
AWSAccountResourceID: types.StringValue(scrapeJobData.AWSAccountResourceID),
RoleARN: types.StringValue(scrapeJobData.RoleARN),
RegionsSubsetOverrideUsed: types.BoolValue(scrapeJobData.RegionsSubsetOverrideUsed),
ExportTags: types.BoolValue(scrapeJobData.ExportTags),
DisabledReason: types.StringValue(scrapeJobData.DisabledReason),
}
regions, diags := types.SetValueFrom(ctx, basetypes.StringType{}, scrapeJobData.Regions)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return awsCWScrapeJobTFDataSourceModel{}, conversionDiags
}
converted.Regions = regions
services, diags := convertServicesClientToTFModel(ctx, scrapeJobData.Services)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return awsCWScrapeJobTFDataSourceModel{}, conversionDiags
}
converted.Services = services
customNamespaces, diags := convertCustomNamespacesClientToTFModel(ctx, scrapeJobData.CustomNamespaces)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return awsCWScrapeJobTFDataSourceModel{}, conversionDiags
}
converted.CustomNamespaces = customNamespaces
staticLabelsMap := types.MapValueMust(types.StringType, map[string]attr.Value{})
if scrapeJobData.StaticLabels != nil {
staticLabelsMap, diags = types.MapValueFrom(ctx, basetypes.StringType{}, scrapeJobData.StaticLabels)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return awsCWScrapeJobTFDataSourceModel{}, conversionDiags
}
}
converted.StaticLabels = staticLabelsMap
return converted, conversionDiags
}
func convertServicesClientToTFModel(ctx context.Context, services []cloudproviderapi.AWSCloudWatchService) (types.List, diag.Diagnostics) {
conversionDiags := diag.Diagnostics{}
servicesTF := make([]awsCWScrapeJobServiceTFModel, len(services))
servicesListObjType := types.ObjectType{AttrTypes: awsCWScrapeJobServiceTFModel{}.attrTypes()}
for i, service := range services {
serviceTF := awsCWScrapeJobServiceTFModel{
Name: types.StringValue(service.Name),
ScrapeIntervalSeconds: types.Int64Value(service.ScrapeIntervalSeconds),
}
metricsTF := make([]awsCWScrapeJobMetricTFModel, len(service.Metrics))
for j, metric := range service.Metrics {
metricsTF[j] = awsCWScrapeJobMetricTFModel{
Name: types.StringValue(metric.Name),
}
statistics, diags := types.SetValueFrom(ctx, basetypes.StringType{}, services[i].Metrics[j].Statistics)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return types.ListNull(servicesListObjType), conversionDiags
}
metricsTF[j].Statistics = statistics
}
metricsTFList, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: awsCWScrapeJobMetricTFModel{}.attrTypes()}, metricsTF)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return types.ListNull(servicesListObjType), conversionDiags
}
serviceTF.Metrics = metricsTFList
tagFiltersTF := make([]awsCWScrapeJobTagFilterTFModel, len(service.ResourceDiscoveryTagFilters))
for j, tagFilter := range service.ResourceDiscoveryTagFilters {
tagFiltersTF[j] = awsCWScrapeJobTagFilterTFModel{
Key: types.StringValue(tagFilter.Key),
Value: types.StringValue(tagFilter.Value),
}
}
tagFiltersTFList, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: awsCWScrapeJobTagFilterTFModel{}.attrTypes()}, tagFiltersTF)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return types.ListNull(servicesListObjType), conversionDiags
}
serviceTF.ResourceDiscoveryTagFilters = tagFiltersTFList
tagsToAdd, diags := types.SetValueFrom(ctx, basetypes.StringType{}, services[i].TagsToAddToMetrics)
if tagsToAdd.IsNull() {
tagsToAdd = types.SetValueMust(basetypes.StringType{}, []attr.Value{})
}
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return types.ListNull(servicesListObjType), conversionDiags
}
serviceTF.TagsToAddToMetrics = tagsToAdd
servicesTF[i] = serviceTF
}
servicesTFList, diags := types.ListValueFrom(ctx, servicesListObjType, servicesTF)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return types.ListNull(servicesListObjType), conversionDiags
}
return servicesTFList, conversionDiags
}
func convertCustomNamespacesClientToTFModel(ctx context.Context, customNamespaces []cloudproviderapi.AWSCloudWatchCustomNamespace) (types.List, diag.Diagnostics) {
conversionDiags := diag.Diagnostics{}
customNamespacesTF := make([]awsCWScrapeJobCustomNamespaceTFModel, len(customNamespaces))
customNamspacesListObjType := types.ObjectType{AttrTypes: awsCWScrapeJobCustomNamespaceTFModel{}.attrTypes()}
for i, customNamespace := range customNamespaces {
customNamespaceTF := awsCWScrapeJobCustomNamespaceTFModel{
Name: types.StringValue(customNamespace.Name),
ScrapeIntervalSeconds: types.Int64Value(customNamespace.ScrapeIntervalSeconds),
}
metricsTF := make([]awsCWScrapeJobMetricTFModel, len(customNamespace.Metrics))
for j, metric := range customNamespace.Metrics {
metricsTF[j] = awsCWScrapeJobMetricTFModel{
Name: types.StringValue(metric.Name),
}
statistics, diags := types.SetValueFrom(ctx, basetypes.StringType{}, customNamespaces[i].Metrics[j].Statistics)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return types.ListNull(customNamspacesListObjType), conversionDiags
}
metricsTF[j].Statistics = statistics
}
metricsTFList, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: awsCWScrapeJobMetricTFModel{}.attrTypes()}, metricsTF)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return types.ListNull(customNamspacesListObjType), conversionDiags
}
customNamespaceTF.Metrics = metricsTFList
customNamespacesTF[i] = customNamespaceTF
}
customNamespacesTFList, diags := types.ListValueFrom(ctx, customNamspacesListObjType, customNamespacesTF)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return types.ListNull(customNamspacesListObjType), conversionDiags
}
return customNamespacesTFList, conversionDiags
}