Skip to content

Commit 385595f

Browse files
authored
Add support for dataViewId (#1305)
* Add support for dataViewId * Changelog
1 parent 6487901 commit 385595f

File tree

5 files changed

+429
-263
lines changed

5 files changed

+429
-263
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
- Migrate `elasticstack_kibana_action_connector` to the Terraform plugin framework ([#1269](https://github.com/elastic/terraform-provider-elasticstack/pull/1269))
1414
- Migrate `elasticstack_elasticsearch_security_role_mapping` resource and data source to Terraform Plugin Framework ([#1279](https://github.com/elastic/terraform-provider-elasticstack/pull/1279))
1515
- Add support for `inactivity_timeout` in `elasticstack_fleet_agent_policy` ([#641](https://github.com/elastic/terraform-provider-elasticstack/issues/641))
16+
- [Refactor] Regenerate the SLO client using the current OpenAPI spec ([#1303](https://github.com/elastic/terraform-provider-elasticstack/pull/1303))
17+
- Add support for `data_view_id` in the `elasticstack_kibana_slo` resource ([#1305](https://github.com/elastic/terraform-provider-elasticstack/pull/1305))
1618
- Add support for `unenrollment_timeout` in `elasticstack_fleet_agent_policy` ([#1169](https://github.com/elastic/terraform-provider-elasticstack/issues/1169))
1719

1820
## [0.11.17] - 2025-07-21

docs/resources/kibana_slo.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ Required:
323323

324324
Optional:
325325

326+
- `data_view_id` (String) Optional data view id to use for this indicator.
326327
- `filter` (String)
327328
- `timestamp_field` (String)
328329

@@ -366,6 +367,7 @@ Required:
366367

367368
Optional:
368369

370+
- `data_view_id` (String) Optional data view id to use for this indicator.
369371
- `filter` (String)
370372
- `good` (String)
371373
- `timestamp_field` (String)
@@ -383,6 +385,7 @@ Required:
383385

384386
Optional:
385387

388+
- `data_view_id` (String) Optional data view id to use for this indicator.
386389
- `filter` (String)
387390
- `timestamp_field` (String)
388391

@@ -453,6 +456,7 @@ Required:
453456

454457
Optional:
455458

459+
- `data_view_id` (String) Optional data view id to use for this indicator.
456460
- `filter` (String)
457461

458462
<a id="nestedblock--timeslice_metric_indicator--metric"></a>

internal/clients/kibana/slo.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ func GetSlo(ctx context.Context, apiClient *clients.ApiClient, id, spaceID strin
2828
return nil, nil
2929
}
3030
if err != nil {
31-
return nil, diag.FromErr(err)
31+
diags := diag.FromErr(err)
32+
diags = append(diags, utils.CheckHttpError(res, "unable to create slo with id "+id)...)
33+
return nil, diags
3234
}
3335

3436
defer res.Body.Close()
@@ -46,7 +48,9 @@ func DeleteSlo(ctx context.Context, apiClient *clients.ApiClient, sloId string,
4648
req := client.DeleteSloOp(ctxWithAuth, sloId, spaceId).KbnXsrf("true")
4749
res, err := req.Execute()
4850
if err != nil && res == nil {
49-
return diag.FromErr(err)
51+
diags := diag.FromErr(err)
52+
diags = append(diags, utils.CheckHttpError(res, "unable to create slo with id "+sloId)...)
53+
return diags
5054
}
5155

5256
defer res.Body.Close()
@@ -80,7 +84,9 @@ func UpdateSlo(ctx context.Context, apiClient *clients.ApiClient, s models.Slo,
8084
_, res, err := req.Execute()
8185

8286
if err != nil {
83-
return nil, diag.FromErr(err)
87+
diags := diag.FromErr(err)
88+
diags = append(diags, utils.CheckHttpError(res, "unable to create slo with id "+s.SloID)...)
89+
return nil, diags
8490
}
8591

8692
defer res.Body.Close()
@@ -122,7 +128,9 @@ func CreateSlo(ctx context.Context, apiClient *clients.ApiClient, s models.Slo,
122128
req := client.CreateSloOp(ctxWithAuth, s.SpaceID).KbnXsrf("true").CreateSloRequest(reqModel)
123129
sloRes, res, err := req.Execute()
124130
if err != nil {
125-
return nil, diag.FromErr(err)
131+
diags := diag.FromErr(err)
132+
diags = append(diags, utils.CheckHttpError(res, "unable to create slo with id "+s.SloID)...)
133+
return nil, diags
126134
}
127135
defer res.Body.Close()
128136

internal/kibana/slo.go

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2124
func 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

Comments
 (0)