diff --git a/CHANGELOG.md b/CHANGELOG.md index 89e55a6d8..ccec2224a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Prevent `elasticstack_kibana_space` to attempt the space recreation if `initials` and `color` are not provided. ([#606](https://github.com/elastic/terraform-provider-elasticstack/pull/606)) - Prevent a provider panic in `elasticstack_kibana_data_view` when a `field_format` does not include a `pattern`. ([#619](https://github.com/elastic/terraform-provider-elasticstack/pull/619/files)) - Fixed a bug where the `id` attribute for `elasticstack_kibana_slo` resources was ignored by renaming the attribute to `slo_id`. ([#622](https://github.com/elastic/terraform-provider-elasticstack/pull/622)) +- Fixed a bug where the `rule_id` attribute for `elasticstack_kibana_alerting_rule` was ignored. ([#626](https://github.com/elastic/terraform-provider-elasticstack/pull/626)) ### Added diff --git a/generated/alerting/.openapi-generator/VERSION b/generated/alerting/.openapi-generator/VERSION index 40e36364a..73a86b197 100644 --- a/generated/alerting/.openapi-generator/VERSION +++ b/generated/alerting/.openapi-generator/VERSION @@ -1 +1 @@ -7.1.0-SNAPSHOT \ No newline at end of file +7.0.1 \ No newline at end of file diff --git a/generated/alerting/README.md b/generated/alerting/README.md index 9bbae6df9..1ee6d4317 100644 --- a/generated/alerting/README.md +++ b/generated/alerting/README.md @@ -36,7 +36,7 @@ Default configuration comes with `Servers` field that contains server objects as ### Select Server Configuration -For using other server than the one defined on index 0 set context value `alerting.ContextServerIndex` of type `int`. +For using other server than the one defined on index 0 set context value `sw.ContextServerIndex` of type `int`. ```golang ctx := context.WithValue(context.Background(), alerting.ContextServerIndex, 1) @@ -44,7 +44,7 @@ ctx := context.WithValue(context.Background(), alerting.ContextServerIndex, 1) ### Templated Server URL -Templated server URL is formatted using default variables from configuration or from context value `alerting.ContextServerVariables` of type `map[string]string`. +Templated server URL is formatted using default variables from configuration or from context value `sw.ContextServerVariables` of type `map[string]string`. ```golang ctx := context.WithValue(context.Background(), alerting.ContextServerVariables, map[string]string{ @@ -58,7 +58,7 @@ Note, enum values are always validated and all unused variables are silently ign Each operation can use different server URL defined using `OperationServers` map in the `Configuration`. An operation is uniquely identified by `"{classname}Service.{nickname}"` string. -Similar rules for overriding default operation server index and variables applies by using `alerting.ContextOperationServerIndices` and `alerting.ContextOperationServerVariables` context maps. +Similar rules for overriding default operation server index and variables applies by using `sw.ContextOperationServerIndices` and `sw.ContextOperationServerVariables` context maps. ```golang ctx := context.WithValue(context.Background(), alerting.ContextOperationServerIndices, map[string]int{ @@ -168,7 +168,7 @@ Authentication schemes defined for the API: Example ```golang -auth := context.WithValue(context.Background(), alerting.ContextBasicAuth, alerting.BasicAuth{ +auth := context.WithValue(context.Background(), sw.ContextBasicAuth, sw.BasicAuth{ UserName: "username", Password: "password", }) @@ -188,8 +188,8 @@ Example ```golang auth := context.WithValue( context.Background(), - alerting.ContextAPIKeys, - map[string]alerting.APIKey{ + sw.ContextAPIKeys, + map[string]sw.APIKey{ "ApiKey": {Key: "API_KEY_STRING"}, }, ) diff --git a/generated/alerting/api/openapi.yaml b/generated/alerting/api/openapi.yaml index 3f8256e9f..bbaff7138 100644 --- a/generated/alerting/api/openapi.yaml +++ b/generated/alerting/api/openapi.yaml @@ -3619,5 +3619,5 @@ components: type: http apiKeyAuth: in: header - name: Authorization + name: ApiKey type: apiKey diff --git a/generated/alerting/api_alerting.go b/generated/alerting/api_alerting.go index 965a0d993..8e443481e 100644 --- a/generated/alerting/api_alerting.go +++ b/generated/alerting/api_alerting.go @@ -583,7 +583,7 @@ func (a *AlertingAPIService) CreateRuleExecute(r ApiCreateRuleRequest) (*RuleRes } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -734,7 +734,7 @@ func (a *AlertingAPIService) DeleteRuleExecute(r ApiDeleteRuleRequest) (*http.Re } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -876,7 +876,7 @@ func (a *AlertingAPIService) DisableRuleExecute(r ApiDisableRuleRequest) (*http. } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -1018,7 +1018,7 @@ func (a *AlertingAPIService) EnableRuleExecute(r ApiEnableRuleRequest) (*http.Re } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -1268,7 +1268,7 @@ func (a *AlertingAPIService) FindRulesExecute(r ApiFindRulesRequest) (*FindRules } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -1396,7 +1396,7 @@ func (a *AlertingAPIService) GetAlertingHealthExecute(r ApiGetAlertingHealthRequ } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -1528,7 +1528,7 @@ func (a *AlertingAPIService) GetRuleExecute(r ApiGetRuleRequest) (*RuleResponseP } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -1667,7 +1667,7 @@ func (a *AlertingAPIService) GetRuleTypesExecute(r ApiGetRuleTypesRequest) ([]Ge } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -1825,7 +1825,7 @@ func (a *AlertingAPIService) LegacyCreateAlertExecute(r ApiLegacyCreateAlertRequ } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -1968,7 +1968,7 @@ func (a *AlertingAPIService) LegacyDisableAlertExecute(r ApiLegacyDisableAlertRe } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -2102,7 +2102,7 @@ func (a *AlertingAPIService) LegacyEnableAlertExecute(r ApiLegacyEnableAlertRequ } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -2345,7 +2345,7 @@ func (a *AlertingAPIService) LegacyFindAlertsExecute(r ApiLegacyFindAlertsReques } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -2481,7 +2481,7 @@ func (a *AlertingAPIService) LegacyGetAlertExecute(r ApiLegacyGetAlertRequest) ( } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -2613,7 +2613,7 @@ func (a *AlertingAPIService) LegacyGetAlertTypesExecute(r ApiLegacyGetAlertTypes } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -2745,7 +2745,7 @@ func (a *AlertingAPIService) LegacyGetAlertingHealthExecute(r ApiLegacyGetAlerti } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -2892,7 +2892,7 @@ func (a *AlertingAPIService) LegacyMuteAlertInstanceExecute(r ApiLegacyMuteAlert } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -3026,7 +3026,7 @@ func (a *AlertingAPIService) LegacyMuteAllAlertInstancesExecute(r ApiLegacyMuteA } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -3164,7 +3164,7 @@ func (a *AlertingAPIService) LegacyUnmuteAlertInstanceExecute(r ApiLegacyUnmuteA } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -3298,7 +3298,7 @@ func (a *AlertingAPIService) LegacyUnmuteAllAlertInstancesExecute(r ApiLegacyUnm } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -3447,7 +3447,7 @@ func (a *AlertingAPIService) LegacyUpdateAlertExecute(r ApiLegacyUpdateAlertRequ } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -3590,7 +3590,7 @@ func (a *AlertingAPIService) LegaryDeleteAlertExecute(r ApiLegaryDeleteAlertRequ } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -3725,7 +3725,7 @@ func (a *AlertingAPIService) MuteAlertExecute(r ApiMuteAlertRequest) (*http.Resp } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -3856,7 +3856,7 @@ func (a *AlertingAPIService) MuteAllAlertsExecute(r ApiMuteAllAlertsRequest) (*h } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -3991,7 +3991,7 @@ func (a *AlertingAPIService) UnmuteAlertExecute(r ApiUnmuteAlertRequest) (*http. } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -4122,7 +4122,7 @@ func (a *AlertingAPIService) UnmuteAllAlertsExecute(r ApiUnmuteAllAlertsRequest) } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } @@ -4267,7 +4267,7 @@ func (a *AlertingAPIService) UpdateRuleExecute(r ApiUpdateRuleRequest) (*RuleRes } else { key = apiKey.Key } - localVarHeaderParams["Authorization"] = key + localVarHeaderParams["ApiKey"] = key } } } diff --git a/generated/alerting/client.go b/generated/alerting/client.go index 82cdfee61..57ce60a5d 100644 --- a/generated/alerting/client.go +++ b/generated/alerting/client.go @@ -34,8 +34,8 @@ import ( ) var ( - JsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:[^;]+\+)?json)`) - XmlCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:[^;]+\+)?xml)`) + jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) + xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) queryParamSplit = regexp.MustCompile(`(^|&)([^&]+)`) queryDescape = strings.NewReplacer("%5B", "[", "%5D", "]") ) @@ -414,11 +414,6 @@ func (c *APIClient) prepareRequest( localVarRequest.SetBasicAuth(auth.UserName, auth.Password) } - // APIKey Authentication - if auth, ok := ctx.Value(ContextAPIKeys).(APIKey); ok { - localVarRequest.Header.Add("Authorization", "ApiKey "+auth.Key) - } - } for header, value := range c.cfg.DefaultHeader { @@ -445,6 +440,7 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return } _, err = f.Seek(0, io.SeekStart) + err = os.Remove(f.Name()) return } if f, ok := v.(**os.File); ok { @@ -457,15 +453,16 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return } _, err = (*f).Seek(0, io.SeekStart) + err = os.Remove((*f).Name()) return } - if XmlCheck.MatchString(contentType) { + if xmlCheck.MatchString(contentType) { if err = xml.Unmarshal(b, v); err != nil { return err } return nil } - if JsonCheck.MatchString(contentType) { + if jsonCheck.MatchString(contentType) { if actualObj, ok := v.(interface{ GetActualInstance() interface{} }); ok { // oneOf, anyOf schemas if unmarshalObj, ok := actualObj.(interface{ UnmarshalJSON([]byte) error }); ok { // make sure it has UnmarshalJSON defined if err = unmarshalObj.UnmarshalJSON(b); err != nil { @@ -530,9 +527,9 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e _, err = bodyBuf.WriteString(s) } else if s, ok := body.(*string); ok { _, err = bodyBuf.WriteString(*s) - } else if JsonCheck.MatchString(contentType) { + } else if jsonCheck.MatchString(contentType) { err = json.NewEncoder(bodyBuf).Encode(body) - } else if XmlCheck.MatchString(contentType) { + } else if xmlCheck.MatchString(contentType) { var bs []byte bs, err = xml.Marshal(body) if err == nil { diff --git a/generated/alerting/model_actions_inner_frequency.go b/generated/alerting/model_actions_inner_frequency.go index 32872db4e..b33da8787 100644 --- a/generated/alerting/model_actions_inner_frequency.go +++ b/generated/alerting/model_actions_inner_frequency.go @@ -12,7 +12,6 @@ package alerting import ( "encoding/json" - "fmt" ) // checks if the ActionsInnerFrequency type satisfies the MappedNullable interface at compile time @@ -27,8 +26,6 @@ type ActionsInnerFrequency struct { Throttle NullableString `json:"throttle,omitempty"` } -type _ActionsInnerFrequency ActionsInnerFrequency - // NewActionsInnerFrequency instantiates a new ActionsInnerFrequency 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 @@ -157,42 +154,6 @@ func (o ActionsInnerFrequency) ToMap() (map[string]interface{}, error) { return toSerialize, nil } -func (o *ActionsInnerFrequency) UnmarshalJSON(bytes []byte) (err error) { - // This validates that all required properties are included in the JSON object - // by unmarshalling the object into a generic map with string keys and checking - // that every required field exists as a key in the generic map. - requiredProperties := []string{ - "notify_when", - "summary", - } - - allProperties := make(map[string]interface{}) - - err = json.Unmarshal(bytes, &allProperties) - - if err != nil { - return err - } - - for _, requiredProperty := range requiredProperties { - if _, exists := allProperties[requiredProperty]; !exists { - return fmt.Errorf("no value given for required property %v", requiredProperty) - } - } - - varActionsInnerFrequency := _ActionsInnerFrequency{} - - err = json.Unmarshal(bytes, &varActionsInnerFrequency) - - if err != nil { - return err - } - - *o = ActionsInnerFrequency(varActionsInnerFrequency) - - return err -} - type NullableActionsInnerFrequency struct { value *ActionsInnerFrequency isSet bool diff --git a/generated/alerting/model_create_rule_request.go b/generated/alerting/model_create_rule_request.go index 7c088ce40..3b03004fa 100644 --- a/generated/alerting/model_create_rule_request.go +++ b/generated/alerting/model_create_rule_request.go @@ -12,7 +12,6 @@ package alerting import ( "encoding/json" - "fmt" ) // checks if the CreateRuleRequest type satisfies the MappedNullable interface at compile time @@ -39,8 +38,6 @@ type CreateRuleRequest struct { Throttle NullableString `json:"throttle,omitempty"` } -type _CreateRuleRequest CreateRuleRequest - // NewCreateRuleRequest instantiates a new CreateRuleRequest 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 @@ -388,45 +385,6 @@ func (o CreateRuleRequest) ToMap() (map[string]interface{}, error) { return toSerialize, nil } -func (o *CreateRuleRequest) UnmarshalJSON(bytes []byte) (err error) { - // This validates that all required properties are included in the JSON object - // by unmarshalling the object into a generic map with string keys and checking - // that every required field exists as a key in the generic map. - requiredProperties := []string{ - "consumer", - "name", - "params", - "rule_type_id", - "schedule", - } - - allProperties := make(map[string]interface{}) - - err = json.Unmarshal(bytes, &allProperties) - - if err != nil { - return err - } - - for _, requiredProperty := range requiredProperties { - if _, exists := allProperties[requiredProperty]; !exists { - return fmt.Errorf("no value given for required property %v", requiredProperty) - } - } - - varCreateRuleRequest := _CreateRuleRequest{} - - err = json.Unmarshal(bytes, &varCreateRuleRequest) - - if err != nil { - return err - } - - *o = CreateRuleRequest(varCreateRuleRequest) - - return err -} - type NullableCreateRuleRequest struct { value *CreateRuleRequest isSet bool diff --git a/generated/alerting/model_legacy_create_alert_request_properties.go b/generated/alerting/model_legacy_create_alert_request_properties.go index eb256af91..df02db423 100644 --- a/generated/alerting/model_legacy_create_alert_request_properties.go +++ b/generated/alerting/model_legacy_create_alert_request_properties.go @@ -12,7 +12,6 @@ package alerting import ( "encoding/json" - "fmt" ) // checks if the LegacyCreateAlertRequestProperties type satisfies the MappedNullable interface at compile time @@ -40,8 +39,6 @@ type LegacyCreateAlertRequestProperties struct { Throttle *string `json:"throttle,omitempty"` } -type _LegacyCreateAlertRequestProperties LegacyCreateAlertRequestProperties - // NewLegacyCreateAlertRequestProperties instantiates a new LegacyCreateAlertRequestProperties 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 @@ -368,46 +365,6 @@ func (o LegacyCreateAlertRequestProperties) ToMap() (map[string]interface{}, err return toSerialize, nil } -func (o *LegacyCreateAlertRequestProperties) UnmarshalJSON(bytes []byte) (err error) { - // This validates that all required properties are included in the JSON object - // by unmarshalling the object into a generic map with string keys and checking - // that every required field exists as a key in the generic map. - requiredProperties := []string{ - "alertTypeId", - "consumer", - "name", - "notifyWhen", - "params", - "schedule", - } - - allProperties := make(map[string]interface{}) - - err = json.Unmarshal(bytes, &allProperties) - - if err != nil { - return err - } - - for _, requiredProperty := range requiredProperties { - if _, exists := allProperties[requiredProperty]; !exists { - return fmt.Errorf("no value given for required property %v", requiredProperty) - } - } - - varLegacyCreateAlertRequestProperties := _LegacyCreateAlertRequestProperties{} - - err = json.Unmarshal(bytes, &varLegacyCreateAlertRequestProperties) - - if err != nil { - return err - } - - *o = LegacyCreateAlertRequestProperties(varLegacyCreateAlertRequestProperties) - - return err -} - type NullableLegacyCreateAlertRequestProperties struct { value *LegacyCreateAlertRequestProperties isSet bool diff --git a/generated/alerting/model_legacy_update_alert_request_properties.go b/generated/alerting/model_legacy_update_alert_request_properties.go index ba1b5c8d2..52c0cc513 100644 --- a/generated/alerting/model_legacy_update_alert_request_properties.go +++ b/generated/alerting/model_legacy_update_alert_request_properties.go @@ -12,7 +12,6 @@ package alerting import ( "encoding/json" - "fmt" ) // checks if the LegacyUpdateAlertRequestProperties type satisfies the MappedNullable interface at compile time @@ -34,8 +33,6 @@ type LegacyUpdateAlertRequestProperties struct { Throttle *string `json:"throttle,omitempty"` } -type _LegacyUpdateAlertRequestProperties LegacyUpdateAlertRequestProperties - // NewLegacyUpdateAlertRequestProperties instantiates a new LegacyUpdateAlertRequestProperties 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 @@ -275,44 +272,6 @@ func (o LegacyUpdateAlertRequestProperties) ToMap() (map[string]interface{}, err return toSerialize, nil } -func (o *LegacyUpdateAlertRequestProperties) UnmarshalJSON(bytes []byte) (err error) { - // This validates that all required properties are included in the JSON object - // by unmarshalling the object into a generic map with string keys and checking - // that every required field exists as a key in the generic map. - requiredProperties := []string{ - "name", - "notifyWhen", - "params", - "schedule", - } - - allProperties := make(map[string]interface{}) - - err = json.Unmarshal(bytes, &allProperties) - - if err != nil { - return err - } - - for _, requiredProperty := range requiredProperties { - if _, exists := allProperties[requiredProperty]; !exists { - return fmt.Errorf("no value given for required property %v", requiredProperty) - } - } - - varLegacyUpdateAlertRequestProperties := _LegacyUpdateAlertRequestProperties{} - - err = json.Unmarshal(bytes, &varLegacyUpdateAlertRequestProperties) - - if err != nil { - return err - } - - *o = LegacyUpdateAlertRequestProperties(varLegacyUpdateAlertRequestProperties) - - return err -} - type NullableLegacyUpdateAlertRequestProperties struct { value *LegacyUpdateAlertRequestProperties isSet bool diff --git a/generated/alerting/model_legacy_update_alert_request_properties_actions_inner.go b/generated/alerting/model_legacy_update_alert_request_properties_actions_inner.go index 7121e5090..d7395b789 100644 --- a/generated/alerting/model_legacy_update_alert_request_properties_actions_inner.go +++ b/generated/alerting/model_legacy_update_alert_request_properties_actions_inner.go @@ -12,7 +12,6 @@ package alerting import ( "encoding/json" - "fmt" ) // checks if the LegacyUpdateAlertRequestPropertiesActionsInner type satisfies the MappedNullable interface at compile time @@ -30,8 +29,6 @@ type LegacyUpdateAlertRequestPropertiesActionsInner struct { Params map[string]interface{} `json:"params"` } -type _LegacyUpdateAlertRequestPropertiesActionsInner LegacyUpdateAlertRequestPropertiesActionsInner - // NewLegacyUpdateAlertRequestPropertiesActionsInner instantiates a new LegacyUpdateAlertRequestPropertiesActionsInner 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 @@ -166,44 +163,6 @@ func (o LegacyUpdateAlertRequestPropertiesActionsInner) ToMap() (map[string]inte return toSerialize, nil } -func (o *LegacyUpdateAlertRequestPropertiesActionsInner) UnmarshalJSON(bytes []byte) (err error) { - // This validates that all required properties are included in the JSON object - // by unmarshalling the object into a generic map with string keys and checking - // that every required field exists as a key in the generic map. - requiredProperties := []string{ - "actionTypeId", - "group", - "id", - "params", - } - - allProperties := make(map[string]interface{}) - - err = json.Unmarshal(bytes, &allProperties) - - if err != nil { - return err - } - - for _, requiredProperty := range requiredProperties { - if _, exists := allProperties[requiredProperty]; !exists { - return fmt.Errorf("no value given for required property %v", requiredProperty) - } - } - - varLegacyUpdateAlertRequestPropertiesActionsInner := _LegacyUpdateAlertRequestPropertiesActionsInner{} - - err = json.Unmarshal(bytes, &varLegacyUpdateAlertRequestPropertiesActionsInner) - - if err != nil { - return err - } - - *o = LegacyUpdateAlertRequestPropertiesActionsInner(varLegacyUpdateAlertRequestPropertiesActionsInner) - - return err -} - type NullableLegacyUpdateAlertRequestPropertiesActionsInner struct { value *LegacyUpdateAlertRequestPropertiesActionsInner isSet bool diff --git a/generated/alerting/model_rule_response_properties.go b/generated/alerting/model_rule_response_properties.go index 17b16447b..5c5a40603 100644 --- a/generated/alerting/model_rule_response_properties.go +++ b/generated/alerting/model_rule_response_properties.go @@ -12,7 +12,6 @@ package alerting import ( "encoding/json" - "fmt" "time" ) @@ -59,8 +58,6 @@ type RuleResponseProperties struct { UpdatedBy NullableString `json:"updated_by"` } -type _RuleResponseProperties RuleResponseProperties - // NewRuleResponseProperties instantiates a new RuleResponseProperties 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 @@ -761,58 +758,6 @@ func (o RuleResponseProperties) ToMap() (map[string]interface{}, error) { return toSerialize, nil } -func (o *RuleResponseProperties) UnmarshalJSON(bytes []byte) (err error) { - // This validates that all required properties are included in the JSON object - // by unmarshalling the object into a generic map with string keys and checking - // that every required field exists as a key in the generic map. - requiredProperties := []string{ - "actions", - "api_key_owner", - "consumer", - "created_at", - "created_by", - "enabled", - "execution_status", - "id", - "muted_alert_ids", - "mute_all", - "name", - "params", - "rule_type_id", - "schedule", - "tags", - "throttle", - "updated_at", - "updated_by", - } - - allProperties := make(map[string]interface{}) - - err = json.Unmarshal(bytes, &allProperties) - - if err != nil { - return err - } - - for _, requiredProperty := range requiredProperties { - if _, exists := allProperties[requiredProperty]; !exists { - return fmt.Errorf("no value given for required property %v", requiredProperty) - } - } - - varRuleResponseProperties := _RuleResponseProperties{} - - err = json.Unmarshal(bytes, &varRuleResponseProperties) - - if err != nil { - return err - } - - *o = RuleResponseProperties(varRuleResponseProperties) - - return err -} - type NullableRuleResponseProperties struct { value *RuleResponseProperties isSet bool diff --git a/generated/alerting/model_update_rule_request.go b/generated/alerting/model_update_rule_request.go index f64b4fcc7..e2239b119 100644 --- a/generated/alerting/model_update_rule_request.go +++ b/generated/alerting/model_update_rule_request.go @@ -12,7 +12,6 @@ package alerting import ( "encoding/json" - "fmt" ) // checks if the UpdateRuleRequest type satisfies the MappedNullable interface at compile time @@ -33,8 +32,6 @@ type UpdateRuleRequest struct { Throttle NullableString `json:"throttle,omitempty"` } -type _UpdateRuleRequest UpdateRuleRequest - // NewUpdateRuleRequest instantiates a new UpdateRuleRequest 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 @@ -295,43 +292,6 @@ func (o UpdateRuleRequest) ToMap() (map[string]interface{}, error) { return toSerialize, nil } -func (o *UpdateRuleRequest) UnmarshalJSON(bytes []byte) (err error) { - // This validates that all required properties are included in the JSON object - // by unmarshalling the object into a generic map with string keys and checking - // that every required field exists as a key in the generic map. - requiredProperties := []string{ - "name", - "params", - "schedule", - } - - allProperties := make(map[string]interface{}) - - err = json.Unmarshal(bytes, &allProperties) - - if err != nil { - return err - } - - for _, requiredProperty := range requiredProperties { - if _, exists := allProperties[requiredProperty]; !exists { - return fmt.Errorf("no value given for required property %v", requiredProperty) - } - } - - varUpdateRuleRequest := _UpdateRuleRequest{} - - err = json.Unmarshal(bytes, &varUpdateRuleRequest) - - if err != nil { - return err - } - - *o = UpdateRuleRequest(varUpdateRuleRequest) - - return err -} - type NullableUpdateRuleRequest struct { value *UpdateRuleRequest isSet bool diff --git a/internal/clients/kibana/alerting.go b/internal/clients/kibana/alerting.go index 3c8efb6af..9f4382b2f 100644 --- a/internal/clients/kibana/alerting.go +++ b/internal/clients/kibana/alerting.go @@ -26,7 +26,7 @@ func ruleResponseToModel(spaceID string, res *alerting.RuleResponseProperties) * } return &models.AlertingRule{ - ID: res.Id, + RuleID: res.Id, SpaceID: spaceID, Name: res.Name, Consumer: res.Consumer, @@ -84,12 +84,21 @@ func CreateAlertingRule(ctx context.Context, apiClient *clients.ApiClient, rule Tags: rule.Tags, Throttle: *alerting.NewNullableString(rule.Throttle), } - req := client.CreateRule(ctxWithAuth, rule.SpaceID, "").KbnXsrf("true").CreateRuleRequest(reqModel) + + req := client.CreateRule(ctxWithAuth, rule.SpaceID, rule.RuleID).KbnXsrf("true").CreateRuleRequest(reqModel) + ruleRes, res, err := req.Execute() if err != nil && res == nil { return nil, diag.FromErr(err) } + // TODO: Remove this manual check once OpenAPI spec is updated: https://github.com/elastic/kibana/issues/183223 + if res.StatusCode == http.StatusConflict { + return nil, diag.Errorf("Status code [%d], Saved object [%s/%s] conflict (Rule ID already exists in this Space)", res.StatusCode, rule.SpaceID, rule.RuleID) + } + + rule.RuleID = ruleRes.Id + defer res.Body.Close() return ruleResponseToModel(rule.SpaceID, ruleRes), utils.CheckHttpError(res, "Unabled to create alerting rule") } @@ -113,12 +122,12 @@ func UpdateAlertingRule(ctx context.Context, apiClient *clients.ApiClient, rule Tags: rule.Tags, Throttle: *alerting.NewNullableString(rule.Throttle), } - req := client.UpdateRule(ctxWithAuth, rule.ID, rule.SpaceID).KbnXsrf("true").UpdateRuleRequest(reqModel) + req := client.UpdateRule(ctxWithAuth, rule.RuleID, rule.SpaceID).KbnXsrf("true").UpdateRuleRequest(reqModel) ruleRes, res, err := req.Execute() if err != nil && res == nil { return nil, diag.FromErr(err) } - + rule.RuleID = ruleRes.Id defer res.Body.Close() if diags := utils.CheckHttpError(res, "Unable to update alerting rule"); diags.HasError() { return nil, diags @@ -126,7 +135,7 @@ func UpdateAlertingRule(ctx context.Context, apiClient *clients.ApiClient, rule shouldBeEnabled := rule.Enabled != nil && *rule.Enabled if shouldBeEnabled && !ruleRes.Enabled { - res, err := client.EnableRule(ctxWithAuth, rule.ID, rule.SpaceID).KbnXsrf("true").Execute() + res, err := client.EnableRule(ctxWithAuth, rule.RuleID, rule.SpaceID).KbnXsrf("true").Execute() if err != nil && res == nil { return nil, diag.FromErr(err) } @@ -137,7 +146,7 @@ func UpdateAlertingRule(ctx context.Context, apiClient *clients.ApiClient, rule } if !shouldBeEnabled && ruleRes.Enabled { - res, err := client.DisableRule(ctxWithAuth, rule.ID, rule.SpaceID).KbnXsrf("true").Execute() + res, err := client.DisableRule(ctxWithAuth, rule.RuleID, rule.SpaceID).KbnXsrf("true").Execute() if err != nil && res == nil { return nil, diag.FromErr(err) } diff --git a/internal/clients/kibana/alerting_test.go b/internal/clients/kibana/alerting_test.go index 27d85c4b3..c5bc8771a 100644 --- a/internal/clients/kibana/alerting_test.go +++ b/internal/clients/kibana/alerting_test.go @@ -36,7 +36,7 @@ func Test_ruleResponseToModel(t *testing.T) { Tags: []string{"hello"}, }, expectedModel: &models.AlertingRule{ - ID: "id", + RuleID: "id", SpaceID: "space-id", Name: "name", Consumer: "consumer", @@ -82,7 +82,7 @@ func Test_ruleResponseToModel(t *testing.T) { Throttle: *alerting.NewNullableString(makePtr("throttle")), }, expectedModel: &models.AlertingRule{ - ID: "id", + RuleID: "id", SpaceID: "space-id", Name: "name", Consumer: "consumer", diff --git a/internal/kibana/alerting.go b/internal/kibana/alerting.go index aff1108ed..efd9d24c1 100644 --- a/internal/kibana/alerting.go +++ b/internal/kibana/alerting.go @@ -159,6 +159,11 @@ func getAlertingRuleFromResourceData(d *schema.ResourceData) (models.AlertingRul }, } + // Explicitly set rule id if provided, otherwise we'll use the autogenerated ID from the Kibana API response + if ruleID := getOrNilString("rule_id", d); ruleID != nil && *ruleID != "" { + rule.RuleID = *ruleID + } + paramsStr := d.Get("params") params := map[string]interface{}{} if err := json.NewDecoder(strings.NewReader(paramsStr.(string))).Decode(¶ms); err != nil { @@ -232,8 +237,8 @@ func resourceRuleCreate(ctx context.Context, d *schema.ResourceData, meta interf return diags } - id := &clients.CompositeId{ClusterId: rule.SpaceID, ResourceId: res.ID} - d.SetId(id.String()) + compositeID := &clients.CompositeId{ClusterId: rule.SpaceID, ResourceId: res.RuleID} + d.SetId(compositeID.String()) return resourceRuleRead(ctx, d, meta) } @@ -249,20 +254,14 @@ func resourceRuleUpdate(ctx context.Context, d *schema.ResourceData, meta interf return diags } - compId, diags := clients.CompositeIdFromStr(d.Id()) - if diags.HasError() { - return diags - } - rule.ID = compId.ResourceId - res, diags := kibana.UpdateAlertingRule(ctx, client, rule) if diags.HasError() { return diags } - id := &clients.CompositeId{ClusterId: rule.SpaceID, ResourceId: res.ID} - d.SetId(id.String()) + compositeID := &clients.CompositeId{ClusterId: rule.SpaceID, ResourceId: res.RuleID} + d.SetId(compositeID.String()) return resourceRuleRead(ctx, d, meta) } @@ -289,7 +288,7 @@ func resourceRuleRead(ctx context.Context, d *schema.ResourceData, meta interfac } // set the fields - if err := d.Set("rule_id", rule.ID); err != nil { + if err := d.Set("rule_id", rule.RuleID); err != nil { return diag.FromErr(err) } if err := d.Set("space_id", rule.SpaceID); err != nil { diff --git a/internal/kibana/alerting_test.go b/internal/kibana/alerting_test.go index 65f4016af..4137c6cc5 100644 --- a/internal/kibana/alerting_test.go +++ b/internal/kibana/alerting_test.go @@ -32,6 +32,7 @@ func TestAccResourceAlertingRule(t *testing.T) { Config: testAccResourceAlertingRuleCreate(ruleName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "name", ruleName), + resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "rule_id", "af22bd1c-8fb3-4020-9249-a4ac5471624b"), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "consumer", "alerts"), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "notify_when", "onActiveAlert"), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "rule_type_id", ".index-threshold"), @@ -44,6 +45,7 @@ func TestAccResourceAlertingRule(t *testing.T) { Config: testAccResourceAlertingRuleUpdate(ruleName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "name", fmt.Sprintf("Updated %s", ruleName)), + resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "rule_id", "af22bd1c-8fb3-4020-9249-a4ac5471624b"), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "consumer", "alerts"), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "notify_when", "onActiveAlert"), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "rule_type_id", ".index-threshold"), @@ -66,6 +68,7 @@ provider "elasticstack" { resource "elasticstack_kibana_alerting_rule" "test_rule" { name = "%s" + rule_id = "af22bd1c-8fb3-4020-9249-a4ac5471624b" consumer = "alerts" notify_when = "onActiveAlert" params = jsonencode({ @@ -97,6 +100,7 @@ provider "elasticstack" { resource "elasticstack_kibana_alerting_rule" "test_rule" { name = "Updated %s" + rule_id = "af22bd1c-8fb3-4020-9249-a4ac5471624b" consumer = "alerts" notify_when = "onActiveAlert" params = jsonencode({ diff --git a/internal/models/alert_rule.go b/internal/models/alert_rule.go index 4474af460..9fdca956b 100644 --- a/internal/models/alert_rule.go +++ b/internal/models/alert_rule.go @@ -3,7 +3,7 @@ package models import "time" type AlertingRule struct { - ID string + RuleID string SpaceID string Name string Consumer string