From 765b318c06b935cd75e3a5d54c0205568ac07f36 Mon Sep 17 00:00:00 2001 From: Dominik Giger Date: Wed, 7 Aug 2024 17:43:18 +0200 Subject: [PATCH 1/3] Bugfix: Update SLO api type for groupBy to match latest stack versions. - Starting with 8.14.0, the API can now return a list of strings instead of just a string. - To be able to handle both old and new APIs, the spec has been updated to accept either a string or a list of strings. - This change only ensures that the provider doesn't crash with the new API, there is no support yet for multiple group-by arguments. --- generated/slo-spec.yml | 12 +- generated/slo/.openapi-generator/FILES | 2 + generated/slo/README.md | 1 + generated/slo/api/openapi.yaml | 19 ++- generated/slo/docs/CreateSloRequest.md | 8 +- generated/slo/docs/SloResponse.md | 10 +- generated/slo/docs/SloResponseGroupBy.md | 30 ++++ generated/slo/model_create_slo_request.go | 13 +- generated/slo/model_slo_response.go | 15 +- generated/slo/model_slo_response_group_by.go | 145 +++++++++++++++++++ internal/clients/kibana/slo.go | 2 +- 11 files changed, 222 insertions(+), 35 deletions(-) create mode 100644 generated/slo/docs/SloResponseGroupBy.md create mode 100644 generated/slo/model_slo_response_group_by.go diff --git a/generated/slo-spec.yml b/generated/slo-spec.yml index 5aa20726b..5529502e2 100644 --- a/generated/slo-spec.yml +++ b/generated/slo-spec.yml @@ -1173,7 +1173,11 @@ components: example: true groupBy: description: optional group by field to use to generate an SLO per distinct value - type: string + oneOf: + - type: string + - type: array + items: + type: string example: some.field instanceId: description: the value derived from the groupBy field, if present, otherwise '*' @@ -1320,7 +1324,11 @@ components: $ref: '#/components/schemas/settings' groupBy: description: optional group by field to use to generate an SLO per distinct value - type: string + oneOf: + - type: string + - type: array + items: + type: string example: some.field tags: description: List of tags diff --git a/generated/slo/.openapi-generator/FILES b/generated/slo/.openapi-generator/FILES index 6c78451d1..9145efdc6 100644 --- a/generated/slo/.openapi-generator/FILES +++ b/generated/slo/.openapi-generator/FILES @@ -45,6 +45,7 @@ docs/Objective.md docs/Settings.md docs/SloAPI.md docs/SloResponse.md +docs/SloResponseGroupBy.md docs/SloResponseIndicator.md docs/Summary.md docs/SummaryStatus.md @@ -94,6 +95,7 @@ model_indicator_properties_timeslice_metric_params_metric_metrics_inner.go model_objective.go model_settings.go model_slo_response.go +model_slo_response_group_by.go model_slo_response_indicator.go model_summary.go model_summary_status.go diff --git a/generated/slo/README.md b/generated/slo/README.md index cea692cc2..261fc2d0e 100644 --- a/generated/slo/README.md +++ b/generated/slo/README.md @@ -128,6 +128,7 @@ Class | Method | HTTP request | Description - [Objective](docs/Objective.md) - [Settings](docs/Settings.md) - [SloResponse](docs/SloResponse.md) + - [SloResponseGroupBy](docs/SloResponseGroupBy.md) - [SloResponseIndicator](docs/SloResponseIndicator.md) - [Summary](docs/Summary.md) - [SummaryStatus](docs/SummaryStatus.md) diff --git a/generated/slo/api/openapi.yaml b/generated/slo/api/openapi.yaml index d2796a419..33bc46518 100644 --- a/generated/slo/api/openapi.yaml +++ b/generated/slo/api/openapi.yaml @@ -1062,10 +1062,7 @@ components: example: true type: boolean groupBy: - description: optional group by field to use to generate an SLO per distinct - value - example: some.field - type: string + $ref: '#/components/schemas/slo_response_groupBy' instanceId: description: "the value derived from the groupBy field, if present, otherwise\ \ '*'" @@ -1289,10 +1286,7 @@ components: settings: $ref: '#/components/schemas/settings' groupBy: - description: optional group by field to use to generate an SLO per distinct - value - example: some.field - type: string + $ref: '#/components/schemas/slo_response_groupBy' tags: description: List of tags items: @@ -1783,6 +1777,15 @@ components: - $ref: '#/components/schemas/indicator_properties_custom_metric' - $ref: '#/components/schemas/indicator_properties_histogram' - $ref: '#/components/schemas/indicator_properties_timeslice_metric' + slo_response_groupBy: + description: optional group by field to use to generate an SLO per distinct + value + example: some.field + oneOf: + - type: string + - items: + type: string + type: array create_slo_request_indicator: oneOf: - $ref: '#/components/schemas/indicator_properties_custom_kql' diff --git a/generated/slo/docs/CreateSloRequest.md b/generated/slo/docs/CreateSloRequest.md index c56190f41..e8b5ec1bc 100644 --- a/generated/slo/docs/CreateSloRequest.md +++ b/generated/slo/docs/CreateSloRequest.md @@ -12,7 +12,7 @@ Name | Type | Description | Notes **BudgetingMethod** | [**BudgetingMethod**](BudgetingMethod.md) | | **Objective** | [**Objective**](Objective.md) | | **Settings** | Pointer to [**Settings**](Settings.md) | | [optional] -**GroupBy** | Pointer to **string** | optional group by field to use to generate an SLO per distinct value | [optional] +**GroupBy** | Pointer to [**SloResponseGroupBy**](SloResponseGroupBy.md) | | [optional] **Tags** | Pointer to **[]string** | List of tags | [optional] ## Methods @@ -206,20 +206,20 @@ HasSettings returns a boolean if a field has been set. ### GetGroupBy -`func (o *CreateSloRequest) GetGroupBy() string` +`func (o *CreateSloRequest) GetGroupBy() SloResponseGroupBy` GetGroupBy returns the GroupBy field if non-nil, zero value otherwise. ### GetGroupByOk -`func (o *CreateSloRequest) GetGroupByOk() (*string, bool)` +`func (o *CreateSloRequest) GetGroupByOk() (*SloResponseGroupBy, bool)` GetGroupByOk returns a tuple with the GroupBy field if it's non-nil, zero value otherwise and a boolean to check if the value has been set. ### SetGroupBy -`func (o *CreateSloRequest) SetGroupBy(v string)` +`func (o *CreateSloRequest) SetGroupBy(v SloResponseGroupBy)` SetGroupBy sets GroupBy field to given value. diff --git a/generated/slo/docs/SloResponse.md b/generated/slo/docs/SloResponse.md index b3a1d6bda..071b3ecd8 100644 --- a/generated/slo/docs/SloResponse.md +++ b/generated/slo/docs/SloResponse.md @@ -15,7 +15,7 @@ Name | Type | Description | Notes **Revision** | **float64** | The SLO revision | **Summary** | [**Summary**](Summary.md) | | **Enabled** | **bool** | Indicate if the SLO is enabled | -**GroupBy** | **string** | optional group by field to use to generate an SLO per distinct value | +**GroupBy** | [**SloResponseGroupBy**](SloResponseGroupBy.md) | | **InstanceId** | **string** | the value derived from the groupBy field, if present, otherwise '*' | **Tags** | **[]string** | List of tags | **CreatedAt** | **string** | The creation date | @@ -25,7 +25,7 @@ Name | Type | Description | Notes ### NewSloResponse -`func NewSloResponse(id string, name string, description string, indicator SloResponseIndicator, timeWindow TimeWindow, budgetingMethod BudgetingMethod, objective Objective, settings Settings, revision float64, summary Summary, enabled bool, groupBy string, instanceId string, tags []string, createdAt string, updatedAt string, ) *SloResponse` +`func NewSloResponse(id string, name string, description string, indicator SloResponseIndicator, timeWindow TimeWindow, budgetingMethod BudgetingMethod, objective Objective, settings Settings, revision float64, summary Summary, enabled bool, groupBy SloResponseGroupBy, instanceId string, tags []string, createdAt string, updatedAt string, ) *SloResponse` NewSloResponse instantiates a new SloResponse object This constructor will assign default values to properties that have it defined, @@ -262,20 +262,20 @@ SetEnabled sets Enabled field to given value. ### GetGroupBy -`func (o *SloResponse) GetGroupBy() string` +`func (o *SloResponse) GetGroupBy() SloResponseGroupBy` GetGroupBy returns the GroupBy field if non-nil, zero value otherwise. ### GetGroupByOk -`func (o *SloResponse) GetGroupByOk() (*string, bool)` +`func (o *SloResponse) GetGroupByOk() (*SloResponseGroupBy, bool)` GetGroupByOk returns a tuple with the GroupBy field if it's non-nil, zero value otherwise and a boolean to check if the value has been set. ### SetGroupBy -`func (o *SloResponse) SetGroupBy(v string)` +`func (o *SloResponse) SetGroupBy(v SloResponseGroupBy)` SetGroupBy sets GroupBy field to given value. diff --git a/generated/slo/docs/SloResponseGroupBy.md b/generated/slo/docs/SloResponseGroupBy.md new file mode 100644 index 000000000..81a44275f --- /dev/null +++ b/generated/slo/docs/SloResponseGroupBy.md @@ -0,0 +1,30 @@ +# SloResponseGroupBy + +## Properties + +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- + +## Methods + +### NewSloResponseGroupBy + +`func NewSloResponseGroupBy() *SloResponseGroupBy` + +NewSloResponseGroupBy instantiates a new SloResponseGroupBy object +This constructor will assign default values to properties that have it defined, +and makes sure properties required by API are set, but the set of arguments +will change when the set of required properties is changed + +### NewSloResponseGroupByWithDefaults + +`func NewSloResponseGroupByWithDefaults() *SloResponseGroupBy` + +NewSloResponseGroupByWithDefaults instantiates a new SloResponseGroupBy object +This constructor will only assign default values to properties that have it defined, +but it doesn't guarantee that properties required by API are set + + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/generated/slo/model_create_slo_request.go b/generated/slo/model_create_slo_request.go index b96f904a4..02210deb0 100644 --- a/generated/slo/model_create_slo_request.go +++ b/generated/slo/model_create_slo_request.go @@ -30,8 +30,7 @@ type CreateSloRequest struct { BudgetingMethod BudgetingMethod `json:"budgetingMethod"` Objective Objective `json:"objective"` Settings *Settings `json:"settings,omitempty"` - // optional group by field to use to generate an SLO per distinct value - GroupBy *string `json:"groupBy,omitempty"` + GroupBy *SloResponseGroupBy `json:"groupBy,omitempty"` // List of tags Tags []string `json:"tags,omitempty"` } @@ -268,9 +267,9 @@ func (o *CreateSloRequest) SetSettings(v Settings) { } // GetGroupBy returns the GroupBy field value if set, zero value otherwise. -func (o *CreateSloRequest) GetGroupBy() string { +func (o *CreateSloRequest) GetGroupBy() SloResponseGroupBy { if o == nil || IsNil(o.GroupBy) { - var ret string + var ret SloResponseGroupBy return ret } return *o.GroupBy @@ -278,7 +277,7 @@ func (o *CreateSloRequest) GetGroupBy() string { // GetGroupByOk returns a tuple with the GroupBy field value if set, nil otherwise // and a boolean to check if the value has been set. -func (o *CreateSloRequest) GetGroupByOk() (*string, bool) { +func (o *CreateSloRequest) GetGroupByOk() (*SloResponseGroupBy, bool) { if o == nil || IsNil(o.GroupBy) { return nil, false } @@ -294,8 +293,8 @@ func (o *CreateSloRequest) HasGroupBy() bool { return false } -// SetGroupBy gets a reference to the given string and assigns it to the GroupBy field. -func (o *CreateSloRequest) SetGroupBy(v string) { +// SetGroupBy gets a reference to the given SloResponseGroupBy and assigns it to the GroupBy field. +func (o *CreateSloRequest) SetGroupBy(v SloResponseGroupBy) { o.GroupBy = &v } diff --git a/generated/slo/model_slo_response.go b/generated/slo/model_slo_response.go index 91044e65b..6bb82ce1e 100644 --- a/generated/slo/model_slo_response.go +++ b/generated/slo/model_slo_response.go @@ -34,9 +34,8 @@ type SloResponse struct { Revision float64 `json:"revision"` Summary Summary `json:"summary"` // Indicate if the SLO is enabled - Enabled bool `json:"enabled"` - // optional group by field to use to generate an SLO per distinct value - GroupBy string `json:"groupBy"` + Enabled bool `json:"enabled"` + GroupBy SloResponseGroupBy `json:"groupBy"` // the value derived from the groupBy field, if present, otherwise '*' InstanceId string `json:"instanceId"` // List of tags @@ -51,7 +50,7 @@ type SloResponse struct { // This constructor will assign default values to properties that have it defined, // and makes sure properties required by API are set, but the set of arguments // will change when the set of required properties is changed -func NewSloResponse(id string, name string, description string, indicator SloResponseIndicator, timeWindow TimeWindow, budgetingMethod BudgetingMethod, objective Objective, settings Settings, revision float64, summary Summary, enabled bool, groupBy string, instanceId string, tags []string, createdAt string, updatedAt string) *SloResponse { +func NewSloResponse(id string, name string, description string, indicator SloResponseIndicator, timeWindow TimeWindow, budgetingMethod BudgetingMethod, objective Objective, settings Settings, revision float64, summary Summary, enabled bool, groupBy SloResponseGroupBy, instanceId string, tags []string, createdAt string, updatedAt string) *SloResponse { this := SloResponse{} this.Id = id this.Name = name @@ -345,9 +344,9 @@ func (o *SloResponse) SetEnabled(v bool) { } // GetGroupBy returns the GroupBy field value -func (o *SloResponse) GetGroupBy() string { +func (o *SloResponse) GetGroupBy() SloResponseGroupBy { if o == nil { - var ret string + var ret SloResponseGroupBy return ret } @@ -356,7 +355,7 @@ func (o *SloResponse) GetGroupBy() string { // GetGroupByOk returns a tuple with the GroupBy field value // and a boolean to check if the value has been set. -func (o *SloResponse) GetGroupByOk() (*string, bool) { +func (o *SloResponse) GetGroupByOk() (*SloResponseGroupBy, bool) { if o == nil { return nil, false } @@ -364,7 +363,7 @@ func (o *SloResponse) GetGroupByOk() (*string, bool) { } // SetGroupBy sets field value -func (o *SloResponse) SetGroupBy(v string) { +func (o *SloResponse) SetGroupBy(v SloResponseGroupBy) { o.GroupBy = v } diff --git a/generated/slo/model_slo_response_group_by.go b/generated/slo/model_slo_response_group_by.go new file mode 100644 index 000000000..6e8723595 --- /dev/null +++ b/generated/slo/model_slo_response_group_by.go @@ -0,0 +1,145 @@ +/* +SLOs + +OpenAPI schema for SLOs endpoints + +API version: 1.0 +*/ + +// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT. + +package slo + +import ( + "encoding/json" + "fmt" +) + +// SloResponseGroupBy - optional group by field to use to generate an SLO per distinct value +type SloResponseGroupBy struct { + ArrayOfString *[]string + String *string +} + +// []stringAsSloResponseGroupBy is a convenience function that returns []string wrapped in SloResponseGroupBy +func ArrayOfStringAsSloResponseGroupBy(v *[]string) SloResponseGroupBy { + return SloResponseGroupBy{ + ArrayOfString: v, + } +} + +// stringAsSloResponseGroupBy is a convenience function that returns string wrapped in SloResponseGroupBy +func StringAsSloResponseGroupBy(v *string) SloResponseGroupBy { + return SloResponseGroupBy{ + String: v, + } +} + +// Unmarshal JSON data into one of the pointers in the struct +func (dst *SloResponseGroupBy) UnmarshalJSON(data []byte) error { + var err error + match := 0 + // try to unmarshal data into ArrayOfString + err = json.Unmarshal(data, &dst.ArrayOfString) + if err == nil { + jsonstring, _ := json.Marshal(dst.ArrayOfString) + if string(jsonstring) == "{}" { // empty struct + dst.ArrayOfString = nil + } else { + match++ + } + } else { + dst.ArrayOfString = nil + } + + // try to unmarshal data into String + err = json.Unmarshal(data, &dst.String) + if err == nil { + jsonstring, _ := json.Marshal(dst.String) + if string(jsonstring) == "{}" { // empty struct + dst.String = nil + } else { + match++ + } + } else { + dst.String = nil + } + + if match > 1 { // more than 1 match + // reset to nil + dst.ArrayOfString = nil + dst.String = nil + + return fmt.Errorf("data matches more than one schema in oneOf(SloResponseGroupBy)") + } else if match == 1 { + return nil // exactly one match + } else { // no match + return fmt.Errorf("data failed to match schemas in oneOf(SloResponseGroupBy)") + } +} + +// Marshal data from the first non-nil pointers in the struct to JSON +func (src SloResponseGroupBy) MarshalJSON() ([]byte, error) { + if src.ArrayOfString != nil { + return json.Marshal(&src.ArrayOfString) + } + + if src.String != nil { + return json.Marshal(&src.String) + } + + return nil, nil // no data in oneOf schemas +} + +// Get the actual instance +func (obj *SloResponseGroupBy) GetActualInstance() interface{} { + if obj == nil { + return nil + } + if obj.ArrayOfString != nil { + return obj.ArrayOfString + } + + if obj.String != nil { + return obj.String + } + + // all schemas are nil + return nil +} + +type NullableSloResponseGroupBy struct { + value *SloResponseGroupBy + isSet bool +} + +func (v NullableSloResponseGroupBy) Get() *SloResponseGroupBy { + return v.value +} + +func (v *NullableSloResponseGroupBy) Set(val *SloResponseGroupBy) { + v.value = val + v.isSet = true +} + +func (v NullableSloResponseGroupBy) IsSet() bool { + return v.isSet +} + +func (v *NullableSloResponseGroupBy) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableSloResponseGroupBy(val *SloResponseGroupBy) *NullableSloResponseGroupBy { + return &NullableSloResponseGroupBy{value: val, isSet: true} +} + +func (v NullableSloResponseGroupBy) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableSloResponseGroupBy) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} diff --git a/internal/clients/kibana/slo.go b/internal/clients/kibana/slo.go index 4edc8434d..259eebe3f 100644 --- a/internal/clients/kibana/slo.go +++ b/internal/clients/kibana/slo.go @@ -109,7 +109,7 @@ func CreateSlo(ctx context.Context, apiClient *clients.ApiClient, s models.Slo) BudgetingMethod: slo.BudgetingMethod(s.BudgetingMethod), Objective: s.Objective, Settings: s.Settings, - GroupBy: s.GroupBy, + GroupBy: &slo.SloResponseGroupBy{String: s.GroupBy}, Tags: s.Tags, } From 382dd97025b73039119af26036b5d28aabee126e Mon Sep 17 00:00:00 2001 From: Dominik Giger Date: Wed, 7 Aug 2024 17:43:57 +0200 Subject: [PATCH 2/3] Add changelog entry. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9201c9ce3..6150798fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Fix setting `id` for Fleet outputs and servers ([#666](https://github.com/elastic/terraform-provider-elasticstack/pull/666)) - Fix `elasticstack_fleet_enrollment_tokens` returning empty tokens in some case ([#683](https://github.com/elastic/terraform-provider-elasticstack/pull/683)) - Add support for Kibana synthetics private locations ([#696](https://github.com/elastic/terraform-provider-elasticstack/pull/696)) +- Fix type of `group_by` attribute in the `kibana_slo` resource to be compatible with versions 8.14+ ([#701](https://github.com/elastic/terraform-provider-elasticstack/pull/701)) ## [0.11.4] - 2024-06-13 From 9d2db599dd20e6c3e9bf297d95997f2ab2bff3f9 Mon Sep 17 00:00:00 2001 From: Dominik Giger Date: Thu, 8 Aug 2024 12:58:01 +0200 Subject: [PATCH 3/3] Fix nil group-by. --- internal/clients/kibana/slo.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/internal/clients/kibana/slo.go b/internal/clients/kibana/slo.go index 259eebe3f..1c0c519c6 100644 --- a/internal/clients/kibana/slo.go +++ b/internal/clients/kibana/slo.go @@ -109,7 +109,7 @@ func CreateSlo(ctx context.Context, apiClient *clients.ApiClient, s models.Slo) BudgetingMethod: slo.BudgetingMethod(s.BudgetingMethod), Objective: s.Objective, Settings: s.Settings, - GroupBy: &slo.SloResponseGroupBy{String: s.GroupBy}, + GroupBy: transformGroupBy(s.GroupBy), Tags: s.Tags, } @@ -180,3 +180,11 @@ func sloResponseToModel(spaceID string, res *slo.SloResponse) *models.Slo { Tags: res.Tags, } } + +func transformGroupBy(groupBy *string) *slo.SloResponseGroupBy { + if groupBy == nil { + return nil + } + + return &slo.SloResponseGroupBy{String: groupBy} +}