Skip to content

Commit d6d32a4

Browse files
authored
Fix metric_custom_indicator's good and total metrics array (#545)
1 parent 06d87a6 commit d6d32a4

File tree

2 files changed

+70
-34
lines changed

2 files changed

+70
-34
lines changed

internal/kibana/slo.go

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package kibana
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/elastic/terraform-provider-elasticstack/generated/slo"
78
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
@@ -541,34 +542,42 @@ func getSloFromResourceData(d *schema.ResourceData) (models.Slo, diag.Diagnostic
541542
}
542543

543544
case "metric_custom_indicator":
545+
goodMetricsRaw := d.Get(indicatorType + ".0.good.0.metrics").([]interface{})
546+
var goodMetrics []slo.IndicatorPropertiesCustomMetricParamsGoodMetricsInner
547+
for n := range goodMetricsRaw {
548+
idx := fmt.Sprint(n)
549+
goodMetrics = append(goodMetrics, slo.IndicatorPropertiesCustomMetricParamsGoodMetricsInner{
550+
Name: d.Get(indicatorType + ".0.good.0.metrics." + idx + ".name").(string),
551+
Field: d.Get(indicatorType + ".0.good.0.metrics." + idx + ".field").(string),
552+
Aggregation: d.Get(indicatorType + ".0.good.0.metrics." + idx + ".aggregation").(string),
553+
Filter: getOrNilString(indicatorType+".0.good.0.metrics."+idx+".filter", d),
554+
})
555+
}
556+
totalMetricsRaw := d.Get(indicatorType + ".0.total.0.metrics").([]interface{})
557+
var totalMetrics []slo.IndicatorPropertiesCustomMetricParamsTotalMetricsInner
558+
for n := range totalMetricsRaw {
559+
idx := fmt.Sprint(n)
560+
totalMetrics = append(totalMetrics, slo.IndicatorPropertiesCustomMetricParamsTotalMetricsInner{
561+
Name: d.Get(indicatorType + ".0.total.0.metrics." + idx + ".name").(string),
562+
Field: d.Get(indicatorType + ".0.total.0.metrics." + idx + ".field").(string),
563+
Aggregation: d.Get(indicatorType + ".0.total.0.metrics." + idx + ".aggregation").(string),
564+
Filter: getOrNilString(indicatorType+".0.total.0.metrics."+idx+".filter", d),
565+
})
566+
}
544567
indicator = slo.SloResponseIndicator{
545568
IndicatorPropertiesCustomMetric: &slo.IndicatorPropertiesCustomMetric{
546569
Type: indicatorAddressToType[indicatorType],
547570
Params: slo.IndicatorPropertiesCustomMetricParams{
548571
Filter: getOrNilString(indicatorType+".0.filter", d),
549572
Index: d.Get(indicatorType + ".0.index").(string),
550573
TimestampField: d.Get(indicatorType + ".0.timestamp_field").(string),
551-
Total: slo.IndicatorPropertiesCustomMetricParamsTotal{
552-
Equation: d.Get(indicatorType + ".0.total.0.equation").(string),
553-
Metrics: []slo.IndicatorPropertiesCustomMetricParamsTotalMetricsInner{ //are there actually instances where there are more than one 'good' / 'total'? Need to build array if so.
554-
{
555-
Name: d.Get(indicatorType + ".0.total.0.metrics.0.name").(string),
556-
Field: d.Get(indicatorType + ".0.total.0.metrics.0.field").(string),
557-
Aggregation: d.Get(indicatorType + ".0.total.0.metrics.0.aggregation").(string),
558-
Filter: getOrNilString(indicatorType+".0.total.0.metrics.0.filter", d),
559-
},
560-
},
561-
},
562574
Good: slo.IndicatorPropertiesCustomMetricParamsGood{
563575
Equation: d.Get(indicatorType + ".0.good.0.equation").(string),
564-
Metrics: []slo.IndicatorPropertiesCustomMetricParamsGoodMetricsInner{ //are there actually instances where there are more than one 'good' / 'total'? Need to build array if so.
565-
{
566-
Name: d.Get(indicatorType + ".0.good.0.metrics.0.name").(string),
567-
Field: d.Get(indicatorType + ".0.good.0.metrics.0.field").(string),
568-
Aggregation: d.Get(indicatorType + ".0.good.0.metrics.0.aggregation").(string),
569-
Filter: getOrNilString(indicatorType+".0.good.0.metrics.0.filter", d),
570-
},
571-
},
576+
Metrics: goodMetrics,
577+
},
578+
Total: slo.IndicatorPropertiesCustomMetricParamsTotal{
579+
Equation: d.Get(indicatorType + ".0.total.0.equation").(string),
580+
Metrics: totalMetrics,
572581
},
573582
},
574583
},
@@ -757,23 +766,31 @@ func resourceSloRead(ctx context.Context, d *schema.ResourceData, meta interface
757766
case s.Indicator.IndicatorPropertiesCustomMetric != nil:
758767
indicatorAddress = indicatorTypeToAddress[s.Indicator.IndicatorPropertiesCustomMetric.Type]
759768
params := s.Indicator.IndicatorPropertiesCustomMetric.Params
769+
goodMetrics := []map[string]interface{}{}
770+
for _, m := range params.Good.Metrics {
771+
goodMetrics = append(goodMetrics, map[string]interface{}{
772+
"name": m.Name,
773+
"aggregation": m.Aggregation,
774+
"field": m.Field,
775+
"filter": m.Filter,
776+
})
777+
}
760778
good := []map[string]interface{}{{
761779
"equation": params.Good.Equation,
762-
"metrics": []map[string]interface{}{{
763-
"name": params.Good.Metrics[0].Name, //this is only getting the first one? Does this really need to be an array?
764-
"aggregation": params.Good.Metrics[0].Aggregation,
765-
"field": params.Good.Metrics[0].Field,
766-
"filter": params.Good.Metrics[0].Filter,
767-
}},
780+
"metrics": goodMetrics,
768781
}}
782+
totalMetrics := []map[string]interface{}{}
783+
for _, m := range params.Total.Metrics {
784+
totalMetrics = append(totalMetrics, map[string]interface{}{
785+
"name": m.Name,
786+
"aggregation": m.Aggregation,
787+
"field": m.Field,
788+
"filter": m.Filter,
789+
})
790+
}
769791
total := []map[string]interface{}{{
770792
"equation": params.Total.Equation,
771-
"metrics": []map[string]interface{}{{
772-
"name": params.Total.Metrics[0].Name, //this is only getting the first one? Does this really need to be an array?
773-
"aggregation": params.Total.Metrics[0].Aggregation,
774-
"field": params.Total.Metrics[0].Field,
775-
"filter": params.Total.Metrics[0].Filter,
776-
}},
793+
"metrics": totalMetrics,
777794
}}
778795
indicator = append(indicator, map[string]interface{}{
779796
"index": params.Index,

internal/kibana/slo_test.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,18 @@ func TestAccResourceSlo(t *testing.T) {
109109
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "metric_custom_indicator.0.good.0.metrics.0.name", "A"),
110110
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "metric_custom_indicator.0.good.0.metrics.0.aggregation", "sum"),
111111
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "metric_custom_indicator.0.good.0.metrics.0.field", "processor.processed"),
112+
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "metric_custom_indicator.0.good.0.metrics.1.name", "B"),
113+
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "metric_custom_indicator.0.good.0.metrics.1.aggregation", "sum"),
114+
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "metric_custom_indicator.0.good.0.metrics.1.field", "processor.processed"),
115+
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "metric_custom_indicator.0.good.0.equation", "A + B"),
116+
112117
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "metric_custom_indicator.0.total.0.metrics.0.name", "A"),
113118
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "metric_custom_indicator.0.total.0.metrics.0.aggregation", "sum"),
114119
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "metric_custom_indicator.0.total.0.metrics.0.field", "processor.accepted"),
120+
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "metric_custom_indicator.0.total.0.metrics.1.name", "B"),
121+
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "metric_custom_indicator.0.total.0.metrics.1.aggregation", "sum"),
122+
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "metric_custom_indicator.0.total.0.metrics.1.field", "processor.accepted"),
123+
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "metric_custom_indicator.0.total.0.equation", "A + B"),
115124
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "group_by", "some.field"),
116125
),
117126
},
@@ -389,7 +398,12 @@ func getSLOConfig(name string, indicatorType string, settingsEnabled bool, tags
389398
aggregation = "sum"
390399
field = "processor.processed"
391400
}
392-
equation = "A"
401+
metrics {
402+
name = "B"
403+
aggregation = "sum"
404+
field = "processor.processed"
405+
}
406+
equation = "A + B"
393407
}
394408
395409
total {
@@ -398,9 +412,14 @@ func getSLOConfig(name string, indicatorType string, settingsEnabled bool, tags
398412
aggregation = "sum"
399413
field = "processor.accepted"
400414
}
401-
equation = "A"
415+
metrics {
416+
name = "B"
417+
aggregation = "sum"
418+
field = "processor.accepted"
419+
}
420+
equation = "A + B"
402421
}
403-
}
422+
}
404423
`
405424
}
406425
return indicator

0 commit comments

Comments
 (0)