@@ -16,7 +16,10 @@ import (
1616 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1717)
1818
19- var SLOSupportsMultipleGroupByMinVersion = version .Must (version .NewVersion ("8.14.0" ))
19+ var (
20+ SLOSupportsMultipleGroupByMinVersion = version .Must (version .NewVersion ("8.14.0" ))
21+ SLOSupportsDataViewIDMinVersion = version .Must (version .NewVersion ("8.15.0" ))
22+ )
2023
2124func ResourceSlo () * schema.Resource {
2225 return & schema.Resource {
@@ -114,6 +117,11 @@ func getSchema() map[string]*schema.Schema {
114117 Type : schema .TypeString ,
115118 Required : true ,
116119 },
120+ "data_view_id" : {
121+ Type : schema .TypeString ,
122+ Optional : true ,
123+ Description : "Optional data view id to use for this indicator." ,
124+ },
117125 "filter" : {
118126 Type : schema .TypeString ,
119127 Optional : true ,
@@ -215,6 +223,11 @@ func getSchema() map[string]*schema.Schema {
215223 Type : schema .TypeString ,
216224 Required : true ,
217225 },
226+ "data_view_id" : {
227+ Type : schema .TypeString ,
228+ Optional : true ,
229+ Description : "Optional data view id to use for this indicator." ,
230+ },
218231 "filter" : {
219232 Type : schema .TypeString ,
220233 Optional : true ,
@@ -375,6 +388,11 @@ func getSchema() map[string]*schema.Schema {
375388 Type : schema .TypeString ,
376389 Required : true ,
377390 },
391+ "data_view_id" : {
392+ Type : schema .TypeString ,
393+ Optional : true ,
394+ Description : "Optional data view id to use for this indicator." ,
395+ },
378396 "filter" : {
379397 Type : schema .TypeString ,
380398 Optional : true ,
@@ -408,6 +426,11 @@ func getSchema() map[string]*schema.Schema {
408426 Type : schema .TypeString ,
409427 Required : true ,
410428 },
429+ "data_view_id" : {
430+ Type : schema .TypeString ,
431+ Optional : true ,
432+ Description : "Optional data view id to use for this indicator." ,
433+ },
411434 "timestamp_field" : {
412435 Type : schema .TypeString ,
413436 Required : true ,
@@ -609,7 +632,8 @@ func getSloFromResourceData(d *schema.ResourceData) (models.Slo, diag.Diagnostic
609632 IndicatorPropertiesCustomKql : & slo.IndicatorPropertiesCustomKql {
610633 Type : indicatorAddressToType [indicatorType ],
611634 Params : slo.IndicatorPropertiesCustomKqlParams {
612- Index : d .Get (indicatorType + ".0.index" ).(string ),
635+ Index : d .Get (indicatorType + ".0.index" ).(string ),
636+ DataViewId : getOrNil [string ](indicatorType + ".0.data_view_id" , d ),
613637 Filter : transformOrNil [slo.KqlWithFilters ](
614638 indicatorType + ".0.filter" , d ,
615639 func (v interface {}) slo.KqlWithFilters {
@@ -666,6 +690,7 @@ func getSloFromResourceData(d *schema.ResourceData) (models.Slo, diag.Diagnostic
666690 Params : slo.IndicatorPropertiesHistogramParams {
667691 Filter : getOrNil [string ](indicatorType + ".0.filter" , d ),
668692 Index : d .Get (indicatorType + ".0.index" ).(string ),
693+ DataViewId : getOrNil [string ](indicatorType + ".0.data_view_id" , d ),
669694 TimestampField : d .Get (indicatorType + ".0.timestamp_field" ).(string ),
670695 Good : slo.IndicatorPropertiesHistogramParamsGood {
671696 Field : d .Get (indicatorType + ".0.good.0.field" ).(string ),
@@ -714,6 +739,7 @@ func getSloFromResourceData(d *schema.ResourceData) (models.Slo, diag.Diagnostic
714739 Params : slo.IndicatorPropertiesCustomMetricParams {
715740 Filter : getOrNil [string ](indicatorType + ".0.filter" , d ),
716741 Index : d .Get (indicatorType + ".0.index" ).(string ),
742+ DataViewId : getOrNil [string ](indicatorType + ".0.data_view_id" , d ),
717743 TimestampField : d .Get (indicatorType + ".0.timestamp_field" ).(string ),
718744 Good : slo.IndicatorPropertiesCustomMetricParamsGood {
719745 Equation : d .Get (indicatorType + ".0.good.0.equation" ).(string ),
@@ -769,6 +795,7 @@ func getSloFromResourceData(d *schema.ResourceData) (models.Slo, diag.Diagnostic
769795 Type : indicatorAddressToType [indicatorType ],
770796 Params : slo.IndicatorPropertiesTimesliceMetricParams {
771797 Index : params ["index" ].(string ),
798+ DataViewId : getOrNil [string ]("timeslice_metric_indicator.0.data_view_id" , d ),
772799 TimestampField : params ["timestamp_field" ].(string ),
773800 Filter : getOrNil [string ]("timeslice_metric_indicator.0.filter" , d ),
774801 Metric : slo.IndicatorPropertiesTimesliceMetricParamsMetric {
@@ -850,6 +877,16 @@ func resourceSloCreate(ctx context.Context, d *schema.ResourceData, meta interfa
850877 return diags
851878 }
852879
880+ // Version check for data_view_id support
881+ if ! serverVersion .GreaterThanOrEqual (SLOSupportsDataViewIDMinVersion ) {
882+ // Check all indicator types that support data_view_id
883+ for _ , indicatorType := range []string {"metric_custom_indicator" , "histogram_custom_indicator" , "kql_custom_indicator" , "timeslice_metric_indicator" } {
884+ if v , ok := d .GetOk (indicatorType + ".0.data_view_id" ); ok && v != "" {
885+ return diag .Errorf ("data_view_id is not supported for %s on Elastic Stack versions < %s" , indicatorType , SLOSupportsDataViewIDMinVersion )
886+ }
887+ }
888+ }
889+
853890 supportsMultipleGroupBy := serverVersion .GreaterThanOrEqual (SLOSupportsMultipleGroupByMinVersion )
854891 if len (slo .GroupBy ) > 1 && ! supportsMultipleGroupBy {
855892 return diag .Errorf ("multiple group_by fields are not supported in this version of the Elastic Stack. Multiple group_by fields requires %s" , SLOSupportsMultipleGroupByMinVersion )
@@ -882,6 +919,15 @@ func resourceSloUpdate(ctx context.Context, d *schema.ResourceData, meta interfa
882919 return diags
883920 }
884921
922+ // Version check for data_view_id support
923+ if ! serverVersion .GreaterThanOrEqual (SLOSupportsDataViewIDMinVersion ) {
924+ for _ , indicatorType := range []string {"metric_custom_indicator" , "histogram_custom_indicator" , "kql_custom_indicator" , "timeslice_metric_indicator" } {
925+ if v , ok := d .GetOk (indicatorType + ".0.data_view_id" ); ok && v != "" {
926+ return diag .Errorf ("data_view_id is not supported for %s on Elastic Stack versions < %s" , indicatorType , SLOSupportsDataViewIDMinVersion )
927+ }
928+ }
929+ }
930+
885931 supportsMultipleGroupBy := serverVersion .GreaterThanOrEqual (SLOSupportsMultipleGroupByMinVersion )
886932 if len (slo .GroupBy ) > 1 && ! supportsMultipleGroupBy {
887933 return diag .Errorf ("multiple group_by fields are not supported in this version of the Elastic Stack. Multiple group_by fields requires %s" , SLOSupportsMultipleGroupByMinVersion )
@@ -950,13 +996,17 @@ func resourceSloRead(ctx context.Context, d *schema.ResourceData, meta interface
950996 case s .Indicator .IndicatorPropertiesCustomKql != nil :
951997 indicatorAddress = indicatorTypeToAddress [s .Indicator .IndicatorPropertiesCustomKql .Type ]
952998 params := s .Indicator .IndicatorPropertiesCustomKql .Params
953- indicator = append ( indicator , map [string ]interface {}{
999+ indicatorMap := map [string ]interface {}{
9541000 "index" : params .Index ,
9551001 "filter" : params .Filter .String ,
9561002 "good" : params .Good .String ,
9571003 "total" : params .Total .String ,
9581004 "timestamp_field" : params .TimestampField ,
959- })
1005+ }
1006+ if params .DataViewId != nil {
1007+ indicatorMap ["data_view_id" ] = * params .DataViewId
1008+ }
1009+ indicator = append (indicator , indicatorMap )
9601010
9611011 case s .Indicator .IndicatorPropertiesHistogram != nil :
9621012 indicatorAddress = indicatorTypeToAddress [s .Indicator .IndicatorPropertiesHistogram .Type ]
@@ -975,13 +1025,17 @@ func resourceSloRead(ctx context.Context, d *schema.ResourceData, meta interface
9751025 "from" : params .Total .From ,
9761026 "to" : params .Total .To ,
9771027 }}
978- indicator = append ( indicator , map [string ]interface {}{
1028+ indicatorMap := map [string ]interface {}{
9791029 "index" : params .Index ,
9801030 "filter" : params .Filter ,
9811031 "timestamp_field" : params .TimestampField ,
9821032 "good" : good ,
9831033 "total" : total ,
984- })
1034+ }
1035+ if params .DataViewId != nil {
1036+ indicatorMap ["data_view_id" ] = * params .DataViewId
1037+ }
1038+ indicator = append (indicator , indicatorMap )
9851039
9861040 case s .Indicator .IndicatorPropertiesCustomMetric != nil :
9871041 indicatorAddress = indicatorTypeToAddress [s .Indicator .IndicatorPropertiesCustomMetric .Type ]
@@ -1012,13 +1066,17 @@ func resourceSloRead(ctx context.Context, d *schema.ResourceData, meta interface
10121066 "equation" : params .Total .Equation ,
10131067 "metrics" : totalMetrics ,
10141068 }}
1015- indicator = append ( indicator , map [string ]interface {}{
1069+ indicatorMap := map [string ]interface {}{
10161070 "index" : params .Index ,
10171071 "filter" : params .Filter ,
10181072 "timestamp_field" : params .TimestampField ,
10191073 "good" : good ,
10201074 "total" : total ,
1021- })
1075+ }
1076+ if params .DataViewId != nil {
1077+ indicatorMap ["data_view_id" ] = * params .DataViewId
1078+ }
1079+ indicator = append (indicator , indicatorMap )
10221080
10231081 case s .Indicator .IndicatorPropertiesTimesliceMetric != nil :
10241082 indicatorAddress = indicatorTypeToAddress [s .Indicator .IndicatorPropertiesTimesliceMetric .Type ]
@@ -1049,12 +1107,16 @@ func resourceSloRead(ctx context.Context, d *schema.ResourceData, meta interface
10491107 "comparator" : params .Metric .Comparator ,
10501108 "threshold" : params .Metric .Threshold ,
10511109 }
1052- indicator = append ( indicator , map [string ]interface {}{
1110+ indicatorMap := map [string ]interface {}{
10531111 "index" : params .Index ,
10541112 "timestamp_field" : params .TimestampField ,
10551113 "filter" : params .Filter ,
10561114 "metric" : []interface {}{metricBlock },
1057- })
1115+ }
1116+ if params .DataViewId != nil {
1117+ indicatorMap ["data_view_id" ] = * params .DataViewId
1118+ }
1119+ indicator = append (indicator , indicatorMap )
10581120
10591121 default :
10601122 return diag .Errorf ("indicator not set" )
0 commit comments