-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_logs_e2m.go
497 lines (466 loc) · 16.6 KB
/
data_source_ibm_logs_e2m.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
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package logs
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/logs-go-sdk/logsv0"
)
func DataSourceIbmLogsE2m() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIbmLogsE2mRead,
Schema: map[string]*schema.Schema{
"logs_e2m_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "ID of e2m to be deleted.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Name of the E2M.",
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Description of the E2M.",
},
"create_time": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "E2M create time.",
},
"update_time": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "E2M update time.",
},
"permutations": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Represents the limit of the permutations and if the limit was exceeded.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"limit": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "E2M permutation limit.",
},
"has_exceeded_limit": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Flag to indicate if limit was exceeded.",
},
},
},
},
"metric_labels": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "E2M metric labels.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target_label": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Metric label target alias name.",
},
"source_field": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Metric label source field.",
},
},
},
},
"metric_fields": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "E2M metric fields.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target_base_metric_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Target metric field alias name.",
},
"source_field": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Source field.",
},
"aggregations": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Represents Aggregation type list.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Is enabled.",
},
"agg_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Aggregation type.",
},
"target_metric_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Target metric field alias name.",
},
"samples": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "E2M sample type metadata.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"sample_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Sample type min/max.",
},
},
},
},
"histogram": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "E2M aggregate histogram type metadata.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"buckets": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Buckets of the E2M.",
Elem: &schema.Schema{
Type: schema.TypeFloat,
},
},
},
},
},
},
},
},
},
},
},
"type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "E2M type.",
},
"is_internal": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "A flag that represents if the e2m is for internal usage.",
},
"logs_query": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "E2M logs query.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"lucene": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Lucene query.",
},
"alias": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Alias.",
},
"applicationname_filters": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Application name filters.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"subsystemname_filters": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Subsystem names filters.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"severity_filters": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Severity type filters.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
}
}
func dataSourceIbmLogsE2mRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
logsClient, err := meta.(conns.ClientSession).LogsV0()
if err != nil {
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_logs_e2m", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
region := getLogsInstanceRegion(logsClient, d)
instanceId := d.Get("instance_id").(string)
logsClient = getClientWithLogsInstanceEndpoint(logsClient, instanceId, region, getLogsInstanceEndpointType(logsClient, d))
getE2mOptions := &logsv0.GetE2mOptions{}
getE2mOptions.SetID(d.Get("logs_e2m_id").(string))
event2MetricIntf, _, err := logsClient.GetE2mWithContext(context, getE2mOptions)
if err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetE2mWithContext failed: %s", err.Error()), "(Data) ibm_logs_e2m", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
event2Metric := event2MetricIntf.(*logsv0.Event2Metric)
d.SetId(fmt.Sprintf("%s", *getE2mOptions.ID))
if err = d.Set("name", event2Metric.Name); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting name: %s", err), "(Data) ibm_logs_e2m", "read")
return tfErr.GetDiag()
}
if err = d.Set("description", event2Metric.Description); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting description: %s", err), "(Data) ibm_logs_e2m", "read")
return tfErr.GetDiag()
}
if err = d.Set("create_time", event2Metric.CreateTime); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting create_time: %s", err), "(Data) ibm_logs_e2m", "read")
return tfErr.GetDiag()
}
if err = d.Set("update_time", event2Metric.UpdateTime); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting update_time: %s", err), "(Data) ibm_logs_e2m", "read")
return tfErr.GetDiag()
}
permutations := []map[string]interface{}{}
if event2Metric.Permutations != nil {
modelMap, err := DataSourceIbmLogsE2mApisEvents2metricsV2E2mPermutationsToMap(event2Metric.Permutations)
if err != nil {
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_logs_e2m", "read")
return tfErr.GetDiag()
}
permutations = append(permutations, modelMap)
}
if err = d.Set("permutations", permutations); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting permutations: %s", err), "(Data) ibm_logs_e2m", "read")
return tfErr.GetDiag()
}
metricLabels := []map[string]interface{}{}
if event2Metric.MetricLabels != nil {
for _, modelItem := range event2Metric.MetricLabels {
modelMap, err := DataSourceIbmLogsE2mApisEvents2metricsV2MetricLabelToMap(&modelItem)
if err != nil {
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_logs_e2m", "read")
return tfErr.GetDiag()
}
metricLabels = append(metricLabels, modelMap)
}
}
if err = d.Set("metric_labels", metricLabels); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting metric_labels: %s", err), "(Data) ibm_logs_e2m", "read")
return tfErr.GetDiag()
}
metricFields := []map[string]interface{}{}
if event2Metric.MetricFields != nil {
for _, modelItem := range event2Metric.MetricFields {
modelMap, err := DataSourceIbmLogsE2mApisEvents2metricsV2MetricFieldToMap(&modelItem)
if err != nil {
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_logs_e2m", "read")
return tfErr.GetDiag()
}
metricFields = append(metricFields, modelMap)
}
}
if err = d.Set("metric_fields", metricFields); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting metric_fields: %s", err), "(Data) ibm_logs_e2m", "read")
return tfErr.GetDiag()
}
if err = d.Set("type", event2Metric.Type); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting type: %s", err), "(Data) ibm_logs_e2m", "read")
return tfErr.GetDiag()
}
if err = d.Set("is_internal", event2Metric.IsInternal); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting is_internal: %s", err), "(Data) ibm_logs_e2m", "read")
return tfErr.GetDiag()
}
logsQuery := []map[string]interface{}{}
if event2Metric.LogsQuery != nil {
modelMap, err := DataSourceIbmLogsE2mApisLogs2metricsV2LogsQueryToMap(event2Metric.LogsQuery)
if err != nil {
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_logs_e2m", "read")
return tfErr.GetDiag()
}
logsQuery = append(logsQuery, modelMap)
}
if err = d.Set("logs_query", logsQuery); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting logs_query: %s", err), "(Data) ibm_logs_e2m", "read")
return tfErr.GetDiag()
}
return nil
}
func DataSourceIbmLogsE2mApisEvents2metricsV2E2mPermutationsToMap(model *logsv0.ApisEvents2metricsV2E2mPermutations) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Limit != nil {
modelMap["limit"] = flex.IntValue(model.Limit)
}
if model.HasExceededLimit != nil {
modelMap["has_exceeded_limit"] = *model.HasExceededLimit
}
return modelMap, nil
}
func DataSourceIbmLogsE2mApisEvents2metricsV2MetricLabelToMap(model *logsv0.ApisEvents2metricsV2MetricLabel) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.TargetLabel != nil {
modelMap["target_label"] = *model.TargetLabel
}
if model.SourceField != nil {
modelMap["source_field"] = *model.SourceField
}
return modelMap, nil
}
func DataSourceIbmLogsE2mApisEvents2metricsV2MetricFieldToMap(model *logsv0.ApisEvents2metricsV2MetricField) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.TargetBaseMetricName != nil {
modelMap["target_base_metric_name"] = *model.TargetBaseMetricName
}
if model.SourceField != nil {
modelMap["source_field"] = *model.SourceField
}
if model.Aggregations != nil {
aggregations := []map[string]interface{}{}
for _, aggregationsItem := range model.Aggregations {
aggregationsItemMap, err := DataSourceIbmLogsE2mApisEvents2metricsV2AggregationToMap(aggregationsItem)
if err != nil {
return modelMap, err
}
aggregations = append(aggregations, aggregationsItemMap)
}
modelMap["aggregations"] = aggregations
}
return modelMap, nil
}
func DataSourceIbmLogsE2mApisEvents2metricsV2AggregationToMap(model logsv0.ApisEvents2metricsV2AggregationIntf) (map[string]interface{}, error) {
if _, ok := model.(*logsv0.ApisEvents2metricsV2AggregationAggMetadataSamples); ok {
return DataSourceIbmLogsE2mApisEvents2metricsV2AggregationAggMetadataSamplesToMap(model.(*logsv0.ApisEvents2metricsV2AggregationAggMetadataSamples))
} else if _, ok := model.(*logsv0.ApisEvents2metricsV2AggregationAggMetadataHistogram); ok {
return DataSourceIbmLogsE2mApisEvents2metricsV2AggregationAggMetadataHistogramToMap(model.(*logsv0.ApisEvents2metricsV2AggregationAggMetadataHistogram))
} else if _, ok := model.(*logsv0.ApisEvents2metricsV2Aggregation); ok {
modelMap := make(map[string]interface{})
model := model.(*logsv0.ApisEvents2metricsV2Aggregation)
if model.Enabled != nil {
modelMap["enabled"] = *model.Enabled
}
if model.AggType != nil {
modelMap["agg_type"] = *model.AggType
}
if model.TargetMetricName != nil {
modelMap["target_metric_name"] = *model.TargetMetricName
}
if model.Samples != nil {
samplesMap, err := DataSourceIbmLogsE2mApisEvents2metricsV2E2mAggSamplesToMap(model.Samples)
if err != nil {
return modelMap, err
}
modelMap["samples"] = []map[string]interface{}{samplesMap}
}
if model.Histogram != nil {
histogramMap, err := DataSourceIbmLogsE2mApisEvents2metricsV2E2mAggHistogramToMap(model.Histogram)
if err != nil {
return modelMap, err
}
modelMap["histogram"] = []map[string]interface{}{histogramMap}
}
return modelMap, nil
} else {
return nil, fmt.Errorf("Unrecognized logsv0.ApisEvents2metricsV2AggregationIntf subtype encountered")
}
}
func DataSourceIbmLogsE2mApisEvents2metricsV2E2mAggSamplesToMap(model *logsv0.ApisEvents2metricsV2E2mAggSamples) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.SampleType != nil {
modelMap["sample_type"] = *model.SampleType
}
return modelMap, nil
}
func DataSourceIbmLogsE2mApisEvents2metricsV2E2mAggHistogramToMap(model *logsv0.ApisEvents2metricsV2E2mAggHistogram) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Buckets != nil {
var buckets []interface{}
for _, item := range model.Buckets {
buckets = append(buckets, float64(item))
}
modelMap["buckets"] = buckets
}
return modelMap, nil
}
func DataSourceIbmLogsE2mApisEvents2metricsV2AggregationAggMetadataSamplesToMap(model *logsv0.ApisEvents2metricsV2AggregationAggMetadataSamples) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Enabled != nil {
modelMap["enabled"] = *model.Enabled
}
if model.AggType != nil {
modelMap["agg_type"] = *model.AggType
}
if model.TargetMetricName != nil {
modelMap["target_metric_name"] = *model.TargetMetricName
}
if model.Samples != nil {
samplesMap, err := DataSourceIbmLogsE2mApisEvents2metricsV2E2mAggSamplesToMap(model.Samples)
if err != nil {
return modelMap, err
}
modelMap["samples"] = []map[string]interface{}{samplesMap}
}
return modelMap, nil
}
func DataSourceIbmLogsE2mApisEvents2metricsV2AggregationAggMetadataHistogramToMap(model *logsv0.ApisEvents2metricsV2AggregationAggMetadataHistogram) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Enabled != nil {
modelMap["enabled"] = *model.Enabled
}
if model.AggType != nil {
modelMap["agg_type"] = *model.AggType
}
if model.TargetMetricName != nil {
modelMap["target_metric_name"] = *model.TargetMetricName
}
if model.Histogram != nil {
histogramMap, err := DataSourceIbmLogsE2mApisEvents2metricsV2E2mAggHistogramToMap(model.Histogram)
if err != nil {
return modelMap, err
}
modelMap["histogram"] = []map[string]interface{}{histogramMap}
}
return modelMap, nil
}
func DataSourceIbmLogsE2mApisLogs2metricsV2LogsQueryToMap(model *logsv0.ApisLogs2metricsV2LogsQuery) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Lucene != nil {
modelMap["lucene"] = *model.Lucene
}
if model.Alias != nil {
modelMap["alias"] = *model.Alias
}
if model.ApplicationnameFilters != nil {
modelMap["applicationname_filters"] = model.ApplicationnameFilters
}
if model.SubsystemnameFilters != nil {
modelMap["subsystemname_filters"] = model.SubsystemnameFilters
}
if model.SeverityFilters != nil {
modelMap["severity_filters"] = model.SeverityFilters
}
return modelMap, nil
}