From d7309fdd2cb41f0f1748d1e166c18dd561fcd7ec Mon Sep 17 00:00:00 2001 From: adcoelho Date: Thu, 5 Sep 2024 12:53:47 +0200 Subject: [PATCH 1/9] Initial commit --- internal/clients/kibana/alerting.go | 35 ++++++++-- internal/clients/kibana/alerting_test.go | 10 +++ internal/kibana/alerting.go | 85 +++++++++++++++++++++--- internal/models/alert_rule.go | 17 +++-- 4 files changed, 129 insertions(+), 18 deletions(-) diff --git a/internal/clients/kibana/alerting.go b/internal/clients/kibana/alerting.go index 2228a9278..ad0d09de0 100644 --- a/internal/clients/kibana/alerting.go +++ b/internal/clients/kibana/alerting.go @@ -20,11 +20,26 @@ func ruleResponseToModel(spaceID string, res *alerting.RuleResponseProperties) * actions := []models.AlertingRuleAction{} for _, action := range res.Actions { - actions = append(actions, models.AlertingRuleAction{ + + a := models.AlertingRuleAction{ Group: action.Group, ID: action.Id, Params: action.Params, - }) + } + + if alerting.IsNil(action.Frequency) { + a.Frequency = nil + } else { + frequency := unwrapOptionalField(action.Frequency) + + a.Frequency = &models.AlertingRuleActionFrequency{ + Summary: frequency.Summary, + NotifyWhen: (string)(frequency.NotifyWhen), + Throttle: *frequency.Throttle.Get(), + } + } + + actions = append(actions, a) } var alertDelay *float32 @@ -68,11 +83,23 @@ func ruleActionsToActionsInner(ruleActions []models.AlertingRuleAction) []alerti actions := []alerting.ActionsInner{} for index := range ruleActions { action := ruleActions[index] - actions = append(actions, alerting.ActionsInner{ + actionToAppend := alerting.ActionsInner{ Group: action.Group, Id: action.ID, Params: action.Params, - }) + } + + if alerting.IsNil(action.Frequency) { + actionToAppend.Frequency = nil + } else { + actionToAppend.Frequency = &alerting.ActionsInnerFrequency{ + Summary: action.Frequency.Summary, + NotifyWhen: (alerting.NotifyWhen)(action.Frequency.NotifyWhen), + Throttle: *alerting.NewNullableString(&action.Frequency.Throttle), + } + } + + actions = append(actions, actionToAppend) } return actions } diff --git a/internal/clients/kibana/alerting_test.go b/internal/clients/kibana/alerting_test.go index 90ce90454..3ea35c7a4 100644 --- a/internal/clients/kibana/alerting_test.go +++ b/internal/clients/kibana/alerting_test.go @@ -71,6 +71,11 @@ func Test_ruleResponseToModel(t *testing.T) { Group: "group-1", Id: "id", Params: map[string]interface{}{}, + Frequency: makePtr(alerting.ActionsInnerFrequency{ + Summary: true, + NotifyWhen: "onThrottleInterval", + Throttle: *alerting.NewNullableString(makePtr("10s")), + }), }, { Group: "group-2", @@ -113,6 +118,11 @@ func Test_ruleResponseToModel(t *testing.T) { Group: "group-1", ID: "id", Params: map[string]interface{}{}, + Frequency: &models.AlertingRuleActionFrequency{ + Summary: true, + NotifyWhen: "onThrottleInterval", + Throttle: "10s", + }, }, { Group: "group-2", diff --git a/internal/kibana/alerting.go b/internal/kibana/alerting.go index a0db6c607..06153ea45 100644 --- a/internal/kibana/alerting.go +++ b/internal/kibana/alerting.go @@ -3,6 +3,7 @@ package kibana import ( "context" "encoding/json" + "fmt" "strings" "github.com/elastic/terraform-provider-elasticstack/internal/clients" @@ -47,7 +48,7 @@ func ResourceAlertingRule() *schema.Resource { "notify_when": { Description: "Defines how often alerts generate actions. Valid values include: `onActionGroupChange`: Actions run when the alert status changes; `onActiveAlert`: Actions run when the alert becomes active and at each check interval while the rule conditions are met; `onThrottleInterval`: Actions run when the alert becomes active and at the interval specified in the throttle property while the rule conditions are met. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `notify_when` values.", Type: schema.TypeString, - Required: true, + Optional: true, ValidateFunc: validation.StringInSlice([]string{"onActionGroupChange", "onActiveAlert", "onThrottleInterval"}, false), }, "params": { @@ -93,6 +94,34 @@ func ResourceAlertingRule() *schema.Resource { ValidateFunc: validation.StringIsJSON, DiffSuppressFunc: utils.DiffJsonSuppress, }, + "frequency": { + Description: "The parameters for the action, which are sent to the connector.", + Type: schema.TypeList, + MinItems: 0, + MaxItems: 1, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "summary": { + Description: "Indicates whether the action is a summary.", + Type: schema.TypeBool, + Required: true, + }, + "notify_when": { + Description: "Defines how often alerts generate actions. Valid values include: `onActionGroupChange`: Actions run when the alert status changes; `onActiveAlert`: Actions run when the alert becomes active and at each check interval while the rule conditions are met; `onThrottleInterval`: Actions run when the alert becomes active and at the interval specified in the throttle property while the rule conditions are met. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `notify_when` values.", + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{"onActionGroupChange", "onActiveAlert", "onThrottleInterval"}, false), + }, + "throttle": { + Description: "Defines how often an alert generates repeated actions. This custom action interval must be specified in seconds, minutes, hours, or days. For example, 10m or 1h. This property is applicable only if `notify_when` is `onThrottleInterval`. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `throttle` values.", + Type: schema.TypeString, + Optional: true, + ValidateFunc: utils.StringIsDuration, + }, + }, + }, + }, }, }, }, @@ -115,7 +144,6 @@ func ResourceAlertingRule() *schema.Resource { Optional: true, ValidateFunc: utils.StringIsDuration, }, - "scheduled_task_id": { Description: "ID of the scheduled task that will execute the alert.", Type: schema.TypeString, @@ -207,7 +235,7 @@ func getAlertingRuleFromResourceData(d *schema.ResourceData, serverVersion *vers rule.AlertDelay = utils.Pointer(float32(v.(float64))) } - actions, diags := getActionsFromResourceData(d) + actions, diags := getActionsFromResourceData(d, serverVersion) if diags.HasError() { return models.AlertingRule{}, diags } @@ -222,11 +250,11 @@ func getAlertingRuleFromResourceData(d *schema.ResourceData, serverVersion *vers return rule, diags } -func getActionsFromResourceData(d *schema.ResourceData) ([]models.AlertingRuleAction, diag.Diagnostics) { +func getActionsFromResourceData(d *schema.ResourceData, serverVersion *version.Version) ([]models.AlertingRuleAction, diag.Diagnostics) { actions := []models.AlertingRuleAction{} if v, ok := d.GetOk("actions"); ok { resourceActions := v.([]interface{}) - for _, a := range resourceActions { + for i, a := range resourceActions { action := a.(map[string]interface{}) paramsStr := action["params"].(string) var params map[string]interface{} @@ -235,11 +263,33 @@ func getActionsFromResourceData(d *schema.ResourceData) ([]models.AlertingRuleAc return []models.AlertingRuleAction{}, diag.FromErr(err) } - actions = append(actions, models.AlertingRuleAction{ + a := models.AlertingRuleAction{ Group: action["group"].(string), ID: action["id"].(string), Params: params, - }) + } + + currentAction := fmt.Sprintf("actions.%d", i) + + if _, ok := d.GetOk(currentAction + ".frequency"); ok { + if serverVersion.LessThan(alertDelayMinSupportedVersion) { + return []models.AlertingRuleAction{}, diag.Diagnostics{ + diag.Diagnostic{ + Severity: diag.Error, + Summary: "actions.frequency is only supported for Elasticsearch v8.13 or higher", + Detail: "actions.frequency is only supported for Elasticsearch v8.13 or higher", + }, + } + } + + a.Frequency = &models.AlertingRuleActionFrequency{ + Summary: d.Get(currentAction + ".frequency.0.summary").(bool), + NotifyWhen: d.Get(currentAction + ".frequency.0.notify_when").(string), + Throttle: d.Get(currentAction + ".frequency.0.throttle").(string), + } + } + + actions = append(actions, a) } } @@ -380,12 +430,27 @@ func resourceRuleRead(ctx context.Context, d *schema.ResourceData, meta interfac if err != nil { return diag.FromErr(err) } + + frequency := []interface{}{} + + if action.Frequency != nil { + frequency = append(frequency, map[string]interface{}{ + "summary": action.Frequency.Summary, + "notify_when": action.Frequency.NotifyWhen, + "throttle": action.Frequency.Throttle, + }) + } else { + frequency = nil + } + actions = append(actions, map[string]interface{}{ - "group": action.Group, - "id": action.ID, - "params": string(params), + "group": action.Group, + "id": action.ID, + "params": string(params), + "frequency": frequency, }) } + if err := d.Set("actions", actions); err != nil { return diag.FromErr(err) } diff --git a/internal/models/alert_rule.go b/internal/models/alert_rule.go index 1bbae824a..7351c872a 100644 --- a/internal/models/alert_rule.go +++ b/internal/models/alert_rule.go @@ -1,6 +1,8 @@ package models -import "time" +import ( + "time" +) type AlertingRule struct { RuleID string @@ -26,12 +28,19 @@ type AlertingRuleSchedule struct { } type AlertingRuleAction struct { - Group string - ID string - Params map[string]interface{} + Group string + ID string + Params map[string]interface{} + Frequency *AlertingRuleActionFrequency } type AlertingRuleExecutionStatus struct { LastExecutionDate *time.Time Status *string } + +type AlertingRuleActionFrequency struct { + Summary bool + NotifyWhen string + Throttle string +} From 234415d427f0dd90c943c5bde670390999dd476d Mon Sep 17 00:00:00 2001 From: adcoelho Date: Thu, 5 Sep 2024 12:54:58 +0200 Subject: [PATCH 2/9] make docs-generate --- docs/resources/kibana_alerting_rule.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/resources/kibana_alerting_rule.md b/docs/resources/kibana_alerting_rule.md index 496ad077d..d10232ffd 100644 --- a/docs/resources/kibana_alerting_rule.md +++ b/docs/resources/kibana_alerting_rule.md @@ -55,7 +55,6 @@ Could not create API key - Unsupported scheme "ApiKey" for granting API Key - `consumer` (String) The name of the application or feature that owns the rule. - `interval` (String) The check interval, which specifies how frequently the rule conditions are checked. The interval must be specified in seconds, minutes, hours or days. - `name` (String) The name of the rule. While this name does not have to be unique, a distinctive name can help you identify a rule. -- `notify_when` (String) Defines how often alerts generate actions. Valid values include: `onActionGroupChange`: Actions run when the alert status changes; `onActiveAlert`: Actions run when the alert becomes active and at each check interval while the rule conditions are met; `onThrottleInterval`: Actions run when the alert becomes active and at the interval specified in the throttle property while the rule conditions are met. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `notify_when` values. - `params` (String) The rule parameters, which differ for each rule type. - `rule_type_id` (String) The ID of the rule type that you want to call when the rule is scheduled to run. For more information about the valid values, list the rule types using [Get rule types API](https://www.elastic.co/guide/en/kibana/master/list-rule-types-api.html) or refer to the [Rule types documentation](https://www.elastic.co/guide/en/kibana/master/rule-types.html). @@ -64,6 +63,7 @@ Could not create API key - Unsupported scheme "ApiKey" for granting API Key - `actions` (Block List) An action that runs under defined conditions. (see [below for nested schema](#nestedblock--actions)) - `alert_delay` (Number) A number that indicates how many consecutive runs need to meet the rule conditions for an alert to occur. - `enabled` (Boolean) Indicates if you want to run the rule on an interval basis. +- `notify_when` (String) Defines how often alerts generate actions. Valid values include: `onActionGroupChange`: Actions run when the alert status changes; `onActiveAlert`: Actions run when the alert becomes active and at each check interval while the rule conditions are met; `onThrottleInterval`: Actions run when the alert becomes active and at the interval specified in the throttle property while the rule conditions are met. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `notify_when` values. - `rule_id` (String) A UUID v1 or v4 to use instead of a randomly generated ID. - `space_id` (String) An identifier for the space. If space_id is not provided, the default space is used. - `tags` (List of String) A list of tag names that are applied to the rule. @@ -86,8 +86,21 @@ Required: Optional: +- `frequency` (Block List, Max: 1) The parameters for the action, which are sent to the connector. (see [below for nested schema](#nestedblock--actions--frequency)) - `group` (String) The group name, which affects when the action runs (for example, when the threshold is met or when the alert is recovered). Each rule type has a list of valid action group names. + +### Nested Schema for `actions.frequency` + +Required: + +- `notify_when` (String) Defines how often alerts generate actions. Valid values include: `onActionGroupChange`: Actions run when the alert status changes; `onActiveAlert`: Actions run when the alert becomes active and at each check interval while the rule conditions are met; `onThrottleInterval`: Actions run when the alert becomes active and at the interval specified in the throttle property while the rule conditions are met. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `notify_when` values. +- `summary` (Boolean) Indicates whether the action is a summary. + +Optional: + +- `throttle` (String) Defines how often an alert generates repeated actions. This custom action interval must be specified in seconds, minutes, hours, or days. For example, 10m or 1h. This property is applicable only if `notify_when` is `onThrottleInterval`. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `throttle` values. + ## Import Import is supported using the following syntax: From 5925649744a4de6718642323a00ccdaeda7533de Mon Sep 17 00:00:00 2001 From: adcoelho Date: Thu, 5 Sep 2024 14:51:17 +0200 Subject: [PATCH 3/9] Add Frequency. --- .../resource_rule_action_frequency.tf | 38 +++++++++++++++++++ internal/clients/kibana/alerting.go | 11 ++++-- internal/clients/kibana/alerting_test.go | 20 +++++++++- internal/kibana/alerting.go | 9 ++++- internal/kibana/alerting_test.go | 2 +- internal/models/alert_rule.go | 2 +- 6 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 examples/resources/elasticstack_kibana_alerting_rule/resource_rule_action_frequency.tf diff --git a/examples/resources/elasticstack_kibana_alerting_rule/resource_rule_action_frequency.tf b/examples/resources/elasticstack_kibana_alerting_rule/resource_rule_action_frequency.tf new file mode 100644 index 000000000..8bf1a59a5 --- /dev/null +++ b/examples/resources/elasticstack_kibana_alerting_rule/resource_rule_action_frequency.tf @@ -0,0 +1,38 @@ +provider "elasticstack" { + kibana {} +} + +resource "elasticstack_kibana_alerting_rule" "example_with_action_frequency" { + name = "%s" + consumer = "alerts" + params = jsonencode({ + aggType = "avg" + groupBy = "top" + termSize = 10 + timeWindowSize = 10 + timeWindowUnit = "s" + threshold = [10] + thresholdComparator = ">" + index = ["test-index"] + timeField = "@timestamp" + aggField = "version" + termField = "name" + }) + rule_type_id = ".index-threshold" + interval = "1m" + enabled = true + + actions { + # Should be the id of a MS Teams connector + id = elasticstack_kibana_action_connector.index_example.connector_id + group = "threshold met" + params = jsonencode({ + "message" : "foobar" + }) + + frequency { + summary = false + notify_when = "onActionGroupChange" + } + } +} diff --git a/internal/clients/kibana/alerting.go b/internal/clients/kibana/alerting.go index ad0d09de0..0bfbdcc39 100644 --- a/internal/clients/kibana/alerting.go +++ b/internal/clients/kibana/alerting.go @@ -35,7 +35,7 @@ func ruleResponseToModel(spaceID string, res *alerting.RuleResponseProperties) * a.Frequency = &models.AlertingRuleActionFrequency{ Summary: frequency.Summary, NotifyWhen: (string)(frequency.NotifyWhen), - Throttle: *frequency.Throttle.Get(), + Throttle: frequency.Throttle.Get(), } } @@ -92,11 +92,16 @@ func ruleActionsToActionsInner(ruleActions []models.AlertingRuleAction) []alerti if alerting.IsNil(action.Frequency) { actionToAppend.Frequency = nil } else { - actionToAppend.Frequency = &alerting.ActionsInnerFrequency{ + frequency := alerting.ActionsInnerFrequency{ Summary: action.Frequency.Summary, NotifyWhen: (alerting.NotifyWhen)(action.Frequency.NotifyWhen), - Throttle: *alerting.NewNullableString(&action.Frequency.Throttle), } + + if action.Frequency.Throttle != nil { + frequency.Throttle = *alerting.NewNullableString(action.Frequency.Throttle) + } + + actionToAppend.Frequency = &frequency } actions = append(actions, actionToAppend) diff --git a/internal/clients/kibana/alerting_test.go b/internal/clients/kibana/alerting_test.go index 3ea35c7a4..be275d29f 100644 --- a/internal/clients/kibana/alerting_test.go +++ b/internal/clients/kibana/alerting_test.go @@ -81,6 +81,15 @@ func Test_ruleResponseToModel(t *testing.T) { Group: "group-2", Id: "id", Params: map[string]interface{}{}, + Frequency: makePtr(alerting.ActionsInnerFrequency{ + Summary: true, + NotifyWhen: "onActionGroupChange", + }), + }, + { + Group: "group-3", + Id: "id", + Params: map[string]interface{}{}, }, }, ExecutionStatus: alerting.RuleResponsePropertiesExecutionStatus{ @@ -121,13 +130,22 @@ func Test_ruleResponseToModel(t *testing.T) { Frequency: &models.AlertingRuleActionFrequency{ Summary: true, NotifyWhen: "onThrottleInterval", - Throttle: "10s", + Throttle: makePtr("10s"), }, }, { Group: "group-2", ID: "id", Params: map[string]interface{}{}, + Frequency: &models.AlertingRuleActionFrequency{ + Summary: true, + NotifyWhen: "onActionGroupChange", + }, + }, + { + Group: "group-3", + ID: "id", + Params: map[string]interface{}{}, }, }, AlertDelay: makePtr(float32(4)), diff --git a/internal/kibana/alerting.go b/internal/kibana/alerting.go index 06153ea45..4d69ee35e 100644 --- a/internal/kibana/alerting.go +++ b/internal/kibana/alerting.go @@ -282,11 +282,16 @@ func getActionsFromResourceData(d *schema.ResourceData, serverVersion *version.V } } - a.Frequency = &models.AlertingRuleActionFrequency{ + frequency := models.AlertingRuleActionFrequency{ Summary: d.Get(currentAction + ".frequency.0.summary").(bool), NotifyWhen: d.Get(currentAction + ".frequency.0.notify_when").(string), - Throttle: d.Get(currentAction + ".frequency.0.throttle").(string), } + + if throttle := getOrNilString(currentAction+".frequency.0.throttle", d); throttle != nil && *throttle != "" { + frequency.Throttle = throttle + } + + a.Frequency = &frequency } actions = append(actions, a) diff --git a/internal/kibana/alerting_test.go b/internal/kibana/alerting_test.go index 385b273c1..f435b8646 100644 --- a/internal/kibana/alerting_test.go +++ b/internal/kibana/alerting_test.go @@ -63,7 +63,7 @@ func TestAccResourceAlertingRule(t *testing.T) { 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", "notify_when", ""), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "rule_type_id", ".index-threshold"), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "interval", "1m"), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "enabled", "true"), diff --git a/internal/models/alert_rule.go b/internal/models/alert_rule.go index 7351c872a..31378c8c8 100644 --- a/internal/models/alert_rule.go +++ b/internal/models/alert_rule.go @@ -42,5 +42,5 @@ type AlertingRuleExecutionStatus struct { type AlertingRuleActionFrequency struct { Summary bool NotifyWhen string - Throttle string + Throttle *string } From ccbe9f3877623a73d72b793f97dd69885db32447 Mon Sep 17 00:00:00 2001 From: adcoelho Date: Thu, 5 Sep 2024 15:05:30 +0200 Subject: [PATCH 4/9] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c075572b5..144b1852b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Fix a provider panic when `elasticstack_kibana_action_connector` reads a non-existant connector ([#729](https://github.com/elastic/terraform-provider-elasticstack/pull/729)) - Add support for `remote_indicies` to `elasticstack_elasticsearch_security_role` & `elasticstack_kibana_security_role` (#723)[https://github.com/elastic/terraform-provider-elasticstack/pull/723] - Fix error handling in `elasticstack_kibana_import_saved_objects` ([#738](https://github.com/elastic/terraform-provider-elasticstack/pull/738)) +- Add the `Frequency` field to the Create Rule API ([#753](https://github.com/elastic/terraform-provider-elasticstack/pull/753)) ## [0.11.6] - 2024-08-20 From 1e4c058ab528378e88ad43fc92227fab5374acf3 Mon Sep 17 00:00:00 2001 From: adcoelho Date: Thu, 5 Sep 2024 15:08:10 +0200 Subject: [PATCH 5/9] fix test --- internal/kibana/alerting_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/kibana/alerting_test.go b/internal/kibana/alerting_test.go index f435b8646..385b273c1 100644 --- a/internal/kibana/alerting_test.go +++ b/internal/kibana/alerting_test.go @@ -63,7 +63,7 @@ func TestAccResourceAlertingRule(t *testing.T) { 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", ""), + resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "notify_when", "onActiveAlert"), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "rule_type_id", ".index-threshold"), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "interval", "1m"), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "enabled", "true"), From d2f6165d87ccd2d592bfda768da6c5fc8cb4fbd0 Mon Sep 17 00:00:00 2001 From: adcoelho Date: Mon, 9 Sep 2024 16:31:30 +0900 Subject: [PATCH 6/9] Updated docs. --- docs/resources/kibana_alerting_rule.md | 4 ++-- internal/kibana/alerting.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/resources/kibana_alerting_rule.md b/docs/resources/kibana_alerting_rule.md index d10232ffd..2940932a9 100644 --- a/docs/resources/kibana_alerting_rule.md +++ b/docs/resources/kibana_alerting_rule.md @@ -63,7 +63,7 @@ Could not create API key - Unsupported scheme "ApiKey" for granting API Key - `actions` (Block List) An action that runs under defined conditions. (see [below for nested schema](#nestedblock--actions)) - `alert_delay` (Number) A number that indicates how many consecutive runs need to meet the rule conditions for an alert to occur. - `enabled` (Boolean) Indicates if you want to run the rule on an interval basis. -- `notify_when` (String) Defines how often alerts generate actions. Valid values include: `onActionGroupChange`: Actions run when the alert status changes; `onActiveAlert`: Actions run when the alert becomes active and at each check interval while the rule conditions are met; `onThrottleInterval`: Actions run when the alert becomes active and at the interval specified in the throttle property while the rule conditions are met. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `notify_when` values. +- `notify_when` (String) Deprecated in 8.13.0. Use the `notify_when` property in the action `frequency` object instead. Defines how often alerts generate actions. Valid values include: `onActionGroupChange`: Actions run when the alert status changes; `onActiveAlert`: Actions run when the alert becomes active and at each check interval while the rule conditions are met; `onThrottleInterval`: Actions run when the alert becomes active and at the interval specified in the throttle property while the rule conditions are met. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `notify_when` values. - `rule_id` (String) A UUID v1 or v4 to use instead of a randomly generated ID. - `space_id` (String) An identifier for the space. If space_id is not provided, the default space is used. - `tags` (List of String) A list of tag names that are applied to the rule. @@ -86,7 +86,7 @@ Required: Optional: -- `frequency` (Block List, Max: 1) The parameters for the action, which are sent to the connector. (see [below for nested schema](#nestedblock--actions--frequency)) +- `frequency` (Block List, Max: 1) The properties that affect how often actions are generated. If the rule type supports setting summary to true, the action can be a summary of alerts at the specified notification interval. Otherwise, an action runs for each alert at the specified notification interval. NOTE: You cannot specify these parameters when `notify_when` or `throttle` are defined at the rule level. (see [below for nested schema](#nestedblock--actions--frequency)) - `group` (String) The group name, which affects when the action runs (for example, when the threshold is met or when the alert is recovered). Each rule type has a list of valid action group names. diff --git a/internal/kibana/alerting.go b/internal/kibana/alerting.go index 4d69ee35e..b241e6039 100644 --- a/internal/kibana/alerting.go +++ b/internal/kibana/alerting.go @@ -46,7 +46,7 @@ func ResourceAlertingRule() *schema.Resource { ForceNew: true, }, "notify_when": { - Description: "Defines how often alerts generate actions. Valid values include: `onActionGroupChange`: Actions run when the alert status changes; `onActiveAlert`: Actions run when the alert becomes active and at each check interval while the rule conditions are met; `onThrottleInterval`: Actions run when the alert becomes active and at the interval specified in the throttle property while the rule conditions are met. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `notify_when` values.", + Description: "Deprecated in 8.13.0. Use the `notify_when` property in the action `frequency` object instead. Defines how often alerts generate actions. Valid values include: `onActionGroupChange`: Actions run when the alert status changes; `onActiveAlert`: Actions run when the alert becomes active and at each check interval while the rule conditions are met; `onThrottleInterval`: Actions run when the alert becomes active and at the interval specified in the throttle property while the rule conditions are met. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `notify_when` values.", Type: schema.TypeString, Optional: true, ValidateFunc: validation.StringInSlice([]string{"onActionGroupChange", "onActiveAlert", "onThrottleInterval"}, false), @@ -95,7 +95,7 @@ func ResourceAlertingRule() *schema.Resource { DiffSuppressFunc: utils.DiffJsonSuppress, }, "frequency": { - Description: "The parameters for the action, which are sent to the connector.", + Description: "The properties that affect how often actions are generated. If the rule type supports setting summary to true, the action can be a summary of alerts at the specified notification interval. Otherwise, an action runs for each alert at the specified notification interval. NOTE: You cannot specify these parameters when `notify_when` or `throttle` are defined at the rule level.", Type: schema.TypeList, MinItems: 0, MaxItems: 1, From 91b361d5be38bd4aa0e550c7b771423d831a82ed Mon Sep 17 00:00:00 2001 From: adcoelho Date: Mon, 9 Sep 2024 16:41:01 +0900 Subject: [PATCH 7/9] Addressing PR comments. --- internal/clients/kibana/alerting.go | 8 ++------ internal/kibana/alerting.go | 11 +++-------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/internal/clients/kibana/alerting.go b/internal/clients/kibana/alerting.go index 0bfbdcc39..61db39f35 100644 --- a/internal/clients/kibana/alerting.go +++ b/internal/clients/kibana/alerting.go @@ -27,9 +27,7 @@ func ruleResponseToModel(spaceID string, res *alerting.RuleResponseProperties) * Params: action.Params, } - if alerting.IsNil(action.Frequency) { - a.Frequency = nil - } else { + if !alerting.IsNil(action.Frequency) { frequency := unwrapOptionalField(action.Frequency) a.Frequency = &models.AlertingRuleActionFrequency{ @@ -89,9 +87,7 @@ func ruleActionsToActionsInner(ruleActions []models.AlertingRuleAction) []alerti Params: action.Params, } - if alerting.IsNil(action.Frequency) { - actionToAppend.Frequency = nil - } else { + if !alerting.IsNil(action.Frequency) { frequency := alerting.ActionsInnerFrequency{ Summary: action.Frequency.Summary, NotifyWhen: (alerting.NotifyWhen)(action.Frequency.NotifyWhen), diff --git a/internal/kibana/alerting.go b/internal/kibana/alerting.go index b241e6039..f5c624ece 100644 --- a/internal/kibana/alerting.go +++ b/internal/kibana/alerting.go @@ -17,6 +17,7 @@ import ( ) var alertDelayMinSupportedVersion = version.Must(version.NewVersion("8.13.0")) +var frequencyMinSupportedVersion = version.Must(version.NewVersion("8.6.0")) func ResourceAlertingRule() *schema.Resource { apikeySchema := map[string]*schema.Schema{ @@ -272,14 +273,8 @@ func getActionsFromResourceData(d *schema.ResourceData, serverVersion *version.V currentAction := fmt.Sprintf("actions.%d", i) if _, ok := d.GetOk(currentAction + ".frequency"); ok { - if serverVersion.LessThan(alertDelayMinSupportedVersion) { - return []models.AlertingRuleAction{}, diag.Diagnostics{ - diag.Diagnostic{ - Severity: diag.Error, - Summary: "actions.frequency is only supported for Elasticsearch v8.13 or higher", - Detail: "actions.frequency is only supported for Elasticsearch v8.13 or higher", - }, - } + if serverVersion.LessThan(frequencyMinSupportedVersion) { + return []models.AlertingRuleAction{}, diag.Errorf("actions.frequency is only supported for Elasticsearch v8.6 or higher") } frequency := models.AlertingRuleActionFrequency{ From 0ef4627e7ab8a38ee4eba55301eda5de2ba50783 Mon Sep 17 00:00:00 2001 From: adcoelho Date: Mon, 9 Sep 2024 21:31:45 +0900 Subject: [PATCH 8/9] Addressing the final PR comments. --- docs/resources/kibana_alerting_rule.md | 4 ++-- internal/kibana/alerting.go | 27 +++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/docs/resources/kibana_alerting_rule.md b/docs/resources/kibana_alerting_rule.md index 2940932a9..4dcab5651 100644 --- a/docs/resources/kibana_alerting_rule.md +++ b/docs/resources/kibana_alerting_rule.md @@ -63,11 +63,11 @@ Could not create API key - Unsupported scheme "ApiKey" for granting API Key - `actions` (Block List) An action that runs under defined conditions. (see [below for nested schema](#nestedblock--actions)) - `alert_delay` (Number) A number that indicates how many consecutive runs need to meet the rule conditions for an alert to occur. - `enabled` (Boolean) Indicates if you want to run the rule on an interval basis. -- `notify_when` (String) Deprecated in 8.13.0. Use the `notify_when` property in the action `frequency` object instead. Defines how often alerts generate actions. Valid values include: `onActionGroupChange`: Actions run when the alert status changes; `onActiveAlert`: Actions run when the alert becomes active and at each check interval while the rule conditions are met; `onThrottleInterval`: Actions run when the alert becomes active and at the interval specified in the throttle property while the rule conditions are met. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `notify_when` values. +- `notify_when` (String) Required until v8.6.0. Deprecated in v8.13.0. Use the `notify_when` property in the action `frequency` object instead. Defines how often alerts generate actions. Valid values include: `onActionGroupChange`: Actions run when the alert status changes; `onActiveAlert`: Actions run when the alert becomes active and at each check interval while the rule conditions are met; `onThrottleInterval`: Actions run when the alert becomes active and at the interval specified in the throttle property while the rule conditions are met. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `notify_when` values. - `rule_id` (String) A UUID v1 or v4 to use instead of a randomly generated ID. - `space_id` (String) An identifier for the space. If space_id is not provided, the default space is used. - `tags` (List of String) A list of tag names that are applied to the rule. -- `throttle` (String) Defines how often an alert generates repeated actions. This custom action interval must be specified in seconds, minutes, hours, or days. For example, 10m or 1h. This property is applicable only if `notify_when` is `onThrottleInterval`. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `throttle` values. +- `throttle` (String) Required until v8.6.0. Deprecated in 8.13.0. Defines how often an alert generates repeated actions. This custom action interval must be specified in seconds, minutes, hours, or days. For example, 10m or 1h. This property is applicable only if `notify_when` is `onThrottleInterval`. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `throttle` values. ### Read-Only diff --git a/internal/kibana/alerting.go b/internal/kibana/alerting.go index f5c624ece..4c91720fe 100644 --- a/internal/kibana/alerting.go +++ b/internal/kibana/alerting.go @@ -17,6 +17,8 @@ import ( ) var alertDelayMinSupportedVersion = version.Must(version.NewVersion("8.13.0")) + +// when notify_when and throttle became optional var frequencyMinSupportedVersion = version.Must(version.NewVersion("8.6.0")) func ResourceAlertingRule() *schema.Resource { @@ -47,7 +49,7 @@ func ResourceAlertingRule() *schema.Resource { ForceNew: true, }, "notify_when": { - Description: "Deprecated in 8.13.0. Use the `notify_when` property in the action `frequency` object instead. Defines how often alerts generate actions. Valid values include: `onActionGroupChange`: Actions run when the alert status changes; `onActiveAlert`: Actions run when the alert becomes active and at each check interval while the rule conditions are met; `onThrottleInterval`: Actions run when the alert becomes active and at the interval specified in the throttle property while the rule conditions are met. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `notify_when` values.", + Description: "Required until v8.6.0. Deprecated in v8.13.0. Use the `notify_when` property in the action `frequency` object instead. Defines how often alerts generate actions. Valid values include: `onActionGroupChange`: Actions run when the alert status changes; `onActiveAlert`: Actions run when the alert becomes active and at each check interval while the rule conditions are met; `onThrottleInterval`: Actions run when the alert becomes active and at the interval specified in the throttle property while the rule conditions are met. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `notify_when` values.", Type: schema.TypeString, Optional: true, ValidateFunc: validation.StringInSlice([]string{"onActionGroupChange", "onActiveAlert", "onThrottleInterval"}, false), @@ -140,7 +142,7 @@ func ResourceAlertingRule() *schema.Resource { }, }, "throttle": { - Description: "Defines how often an alert generates repeated actions. This custom action interval must be specified in seconds, minutes, hours, or days. For example, 10m or 1h. This property is applicable only if `notify_when` is `onThrottleInterval`. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `throttle` values.", + Description: "Required until v8.6.0. Deprecated in 8.13.0. Defines how often an alert generates repeated actions. This custom action interval must be specified in seconds, minutes, hours, or days. For example, 10m or 1h. This property is applicable only if `notify_when` is `onThrottleInterval`. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `throttle` values.", Type: schema.TypeString, Optional: true, ValidateFunc: utils.StringIsDuration, @@ -215,10 +217,30 @@ func getAlertingRuleFromResourceData(d *schema.ResourceData, serverVersion *vers if v, ok := d.GetOk("throttle"); ok { t := v.(string) rule.Throttle = &t + } else { + if serverVersion.LessThan(frequencyMinSupportedVersion) { + return models.AlertingRule{}, diag.Diagnostics{ + diag.Diagnostic{ + Severity: diag.Error, + Summary: "throttle is required until v8.6", + Detail: "throttle is required until v8.6", + }, + } + } } if v, ok := d.GetOk("notify_when"); ok { rule.NotifyWhen = utils.Pointer(v.(string)) + } else { + if serverVersion.LessThan(frequencyMinSupportedVersion) { + return models.AlertingRule{}, diag.Diagnostics{ + diag.Diagnostic{ + Severity: diag.Error, + Summary: "notify_when is required until v8.6", + Detail: "notify_when is required until v8.6", + }, + } + } } if v, ok := d.GetOk("alert_delay"); ok { @@ -230,7 +252,6 @@ func getAlertingRuleFromResourceData(d *schema.ResourceData, serverVersion *vers Detail: "alert_delay is only supported for Elasticsearch v8.13 or higher", }, } - } rule.AlertDelay = utils.Pointer(float32(v.(float64))) From 4ce7988604b77cc09b25561d626e9cce7adc6ad2 Mon Sep 17 00:00:00 2001 From: adcoelho Date: Mon, 9 Sep 2024 22:08:29 +0900 Subject: [PATCH 9/9] Throttle shouldnt have a version check. --- docs/resources/kibana_alerting_rule.md | 2 +- internal/kibana/alerting.go | 12 +----------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/docs/resources/kibana_alerting_rule.md b/docs/resources/kibana_alerting_rule.md index 4dcab5651..ee7deb0b7 100644 --- a/docs/resources/kibana_alerting_rule.md +++ b/docs/resources/kibana_alerting_rule.md @@ -67,7 +67,7 @@ Could not create API key - Unsupported scheme "ApiKey" for granting API Key - `rule_id` (String) A UUID v1 or v4 to use instead of a randomly generated ID. - `space_id` (String) An identifier for the space. If space_id is not provided, the default space is used. - `tags` (List of String) A list of tag names that are applied to the rule. -- `throttle` (String) Required until v8.6.0. Deprecated in 8.13.0. Defines how often an alert generates repeated actions. This custom action interval must be specified in seconds, minutes, hours, or days. For example, 10m or 1h. This property is applicable only if `notify_when` is `onThrottleInterval`. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `throttle` values. +- `throttle` (String) Deprecated in 8.13.0. Defines how often an alert generates repeated actions. This custom action interval must be specified in seconds, minutes, hours, or days. For example, 10m or 1h. This property is applicable only if `notify_when` is `onThrottleInterval`. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `throttle` values. ### Read-Only diff --git a/internal/kibana/alerting.go b/internal/kibana/alerting.go index 4c91720fe..b8dfdc3d3 100644 --- a/internal/kibana/alerting.go +++ b/internal/kibana/alerting.go @@ -142,7 +142,7 @@ func ResourceAlertingRule() *schema.Resource { }, }, "throttle": { - Description: "Required until v8.6.0. Deprecated in 8.13.0. Defines how often an alert generates repeated actions. This custom action interval must be specified in seconds, minutes, hours, or days. For example, 10m or 1h. This property is applicable only if `notify_when` is `onThrottleInterval`. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `throttle` values.", + Description: "Deprecated in 8.13.0. Defines how often an alert generates repeated actions. This custom action interval must be specified in seconds, minutes, hours, or days. For example, 10m or 1h. This property is applicable only if `notify_when` is `onThrottleInterval`. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `throttle` values.", Type: schema.TypeString, Optional: true, ValidateFunc: utils.StringIsDuration, @@ -217,16 +217,6 @@ func getAlertingRuleFromResourceData(d *schema.ResourceData, serverVersion *vers if v, ok := d.GetOk("throttle"); ok { t := v.(string) rule.Throttle = &t - } else { - if serverVersion.LessThan(frequencyMinSupportedVersion) { - return models.AlertingRule{}, diag.Diagnostics{ - diag.Diagnostic{ - Severity: diag.Error, - Summary: "throttle is required until v8.6", - Detail: "throttle is required until v8.6", - }, - } - } } if v, ok := d.GetOk("notify_when"); ok {