Skip to content

Commit 7845a51

Browse files
authored
Support mutliple group_by fields in SLOs (#878)
* Allow group_by in updates * Support multi-group_by in SLOs * Generate docs * Changelog
1 parent d04f4bc commit 7845a51

File tree

11 files changed

+357
-82
lines changed

11 files changed

+357
-82
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## [Unreleased]
22

3+
- Support multiple group by fields in SLOs ([#870](https://github.com/elastic/terraform-provider-elasticstack/pull/878))
34
- Use the auto-generated OAS schema from elastic/kibana for the Fleet API. ([#834](https://github.com/elastic/terraform-provider-elasticstack/issues/834))
45
- Support description in `elasticstack_elasticsearch_security_role` data sources. ([#884](https://github.com/elastic/terraform-provider-elasticstack/pull/884))
56

docs/resources/kibana_slo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ resource "elasticstack_kibana_slo" "custom_metric" {
208208

209209
- `apm_availability_indicator` (Block List, Max: 1) (see [below for nested schema](#nestedblock--apm_availability_indicator))
210210
- `apm_latency_indicator` (Block List, Max: 1) (see [below for nested schema](#nestedblock--apm_latency_indicator))
211-
- `group_by` (String) Optional group by field to use to generate an SLO per distinct value.
211+
- `group_by` (List of String) Optional group by fields to use to generate an SLO per distinct value.
212212
- `histogram_custom_indicator` (Block List, Max: 1) (see [below for nested schema](#nestedblock--histogram_custom_indicator))
213213
- `kql_custom_indicator` (Block List, Max: 1) (see [below for nested schema](#nestedblock--kql_custom_indicator))
214214
- `metric_custom_indicator` (Block List, Max: 1) (see [below for nested schema](#nestedblock--metric_custom_indicator))

generated/slo-spec.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,14 @@ components:
13891389
$ref: '#/components/schemas/objective'
13901390
settings:
13911391
$ref: '#/components/schemas/settings'
1392+
groupBy:
1393+
description: optional group by field to use to generate an SLO per distinct value
1394+
oneOf:
1395+
- type: string
1396+
- type: array
1397+
items:
1398+
type: string
1399+
example: some.field
13921400
tags:
13931401
description: List of tags
13941402
type: array

generated/slo/api/openapi.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,8 @@ components:
13491349
$ref: '#/components/schemas/objective'
13501350
settings:
13511351
$ref: '#/components/schemas/settings'
1352+
groupBy:
1353+
$ref: '#/components/schemas/slo_response_groupBy'
13521354
tags:
13531355
description: List of tags
13541356
items:

generated/slo/docs/UpdateSloRequest.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Name | Type | Description | Notes
1111
**BudgetingMethod** | Pointer to [**BudgetingMethod**](BudgetingMethod.md) | | [optional]
1212
**Objective** | Pointer to [**Objective**](Objective.md) | | [optional]
1313
**Settings** | Pointer to [**Settings**](Settings.md) | | [optional]
14+
**GroupBy** | Pointer to [**SloResponseGroupBy**](SloResponseGroupBy.md) | | [optional]
1415
**Tags** | Pointer to **[]string** | List of tags | [optional]
1516

1617
## Methods
@@ -207,6 +208,31 @@ SetSettings sets Settings field to given value.
207208

208209
HasSettings returns a boolean if a field has been set.
209210

211+
### GetGroupBy
212+
213+
`func (o *UpdateSloRequest) GetGroupBy() SloResponseGroupBy`
214+
215+
GetGroupBy returns the GroupBy field if non-nil, zero value otherwise.
216+
217+
### GetGroupByOk
218+
219+
`func (o *UpdateSloRequest) GetGroupByOk() (*SloResponseGroupBy, bool)`
220+
221+
GetGroupByOk returns a tuple with the GroupBy field if it's non-nil, zero value otherwise
222+
and a boolean to check if the value has been set.
223+
224+
### SetGroupBy
225+
226+
`func (o *UpdateSloRequest) SetGroupBy(v SloResponseGroupBy)`
227+
228+
SetGroupBy sets GroupBy field to given value.
229+
230+
### HasGroupBy
231+
232+
`func (o *UpdateSloRequest) HasGroupBy() bool`
233+
234+
HasGroupBy returns a boolean if a field has been set.
235+
210236
### GetTags
211237

212238
`func (o *UpdateSloRequest) GetTags() []string`

generated/slo/model_slo_response_group_by.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/slo/model_update_slo_request.go

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/clients/kibana/slo.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func DeleteSlo(ctx context.Context, apiClient *clients.ApiClient, sloId string,
5353
return utils.CheckHttpError(res, "Unabled to delete slo with ID "+string(sloId))
5454
}
5555

56-
func UpdateSlo(ctx context.Context, apiClient *clients.ApiClient, s models.Slo) (*models.Slo, diag.Diagnostics) {
56+
func UpdateSlo(ctx context.Context, apiClient *clients.ApiClient, s models.Slo, supportsGroupByList bool) (*models.Slo, diag.Diagnostics) {
5757
client, err := apiClient.GetSloClient()
5858
if err != nil {
5959
return nil, diag.FromErr(err)
@@ -72,6 +72,7 @@ func UpdateSlo(ctx context.Context, apiClient *clients.ApiClient, s models.Slo)
7272
BudgetingMethod: (*slo.BudgetingMethod)(&s.BudgetingMethod),
7373
Objective: &s.Objective,
7474
Settings: s.Settings,
75+
GroupBy: transformGroupBy(s.GroupBy, supportsGroupByList),
7576
Tags: s.Tags,
7677
}
7778

@@ -90,7 +91,7 @@ func UpdateSlo(ctx context.Context, apiClient *clients.ApiClient, s models.Slo)
9091
return sloResponseToModel(s.SpaceID, slo), diag.Diagnostics{}
9192
}
9293

93-
func CreateSlo(ctx context.Context, apiClient *clients.ApiClient, s models.Slo) (*models.Slo, diag.Diagnostics) {
94+
func CreateSlo(ctx context.Context, apiClient *clients.ApiClient, s models.Slo, supportsGroupByList bool) (*models.Slo, diag.Diagnostics) {
9495
client, err := apiClient.GetSloClient()
9596
if err != nil {
9697
return nil, diag.FromErr(err)
@@ -109,7 +110,7 @@ func CreateSlo(ctx context.Context, apiClient *clients.ApiClient, s models.Slo)
109110
BudgetingMethod: slo.BudgetingMethod(s.BudgetingMethod),
110111
Objective: s.Objective,
111112
Settings: s.Settings,
112-
GroupBy: transformGroupBy(s.GroupBy),
113+
GroupBy: transformGroupBy(s.GroupBy, supportsGroupByList),
113114
Tags: s.Tags,
114115
}
115116

@@ -177,14 +178,33 @@ func sloResponseToModel(spaceID string, res *slo.SloResponse) *models.Slo {
177178
TimeWindow: res.TimeWindow,
178179
Objective: res.Objective,
179180
Settings: &res.Settings,
181+
GroupBy: transformGroupByFromResponse(res.GroupBy),
180182
Tags: res.Tags,
181183
}
182184
}
183185

184-
func transformGroupBy(groupBy *string) *slo.SloResponseGroupBy {
186+
func transformGroupBy(groupBy []string, supportsGroupByList bool) *slo.SloResponseGroupBy {
185187
if groupBy == nil {
186188
return nil
187189
}
188190

189-
return &slo.SloResponseGroupBy{String: groupBy}
191+
if !supportsGroupByList && len(groupBy) > 0 {
192+
return &slo.SloResponseGroupBy{
193+
String: &groupBy[0],
194+
}
195+
}
196+
197+
return &slo.SloResponseGroupBy{ArrayOfString: &groupBy}
198+
}
199+
200+
func transformGroupByFromResponse(groupBy slo.SloResponseGroupBy) []string {
201+
if groupBy.String != nil {
202+
return []string{*groupBy.String}
203+
}
204+
205+
if groupBy.ArrayOfString == nil {
206+
return nil
207+
}
208+
209+
return *groupBy.ArrayOfString
190210
}

0 commit comments

Comments
 (0)