Skip to content

Commit d7309fd

Browse files
committed
Initial commit
1 parent b58a599 commit d7309fd

File tree

4 files changed

+129
-18
lines changed

4 files changed

+129
-18
lines changed

internal/clients/kibana/alerting.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,26 @@ func ruleResponseToModel(spaceID string, res *alerting.RuleResponseProperties) *
2020

2121
actions := []models.AlertingRuleAction{}
2222
for _, action := range res.Actions {
23-
actions = append(actions, models.AlertingRuleAction{
23+
24+
a := models.AlertingRuleAction{
2425
Group: action.Group,
2526
ID: action.Id,
2627
Params: action.Params,
27-
})
28+
}
29+
30+
if alerting.IsNil(action.Frequency) {
31+
a.Frequency = nil
32+
} else {
33+
frequency := unwrapOptionalField(action.Frequency)
34+
35+
a.Frequency = &models.AlertingRuleActionFrequency{
36+
Summary: frequency.Summary,
37+
NotifyWhen: (string)(frequency.NotifyWhen),
38+
Throttle: *frequency.Throttle.Get(),
39+
}
40+
}
41+
42+
actions = append(actions, a)
2843
}
2944

3045
var alertDelay *float32
@@ -68,11 +83,23 @@ func ruleActionsToActionsInner(ruleActions []models.AlertingRuleAction) []alerti
6883
actions := []alerting.ActionsInner{}
6984
for index := range ruleActions {
7085
action := ruleActions[index]
71-
actions = append(actions, alerting.ActionsInner{
86+
actionToAppend := alerting.ActionsInner{
7287
Group: action.Group,
7388
Id: action.ID,
7489
Params: action.Params,
75-
})
90+
}
91+
92+
if alerting.IsNil(action.Frequency) {
93+
actionToAppend.Frequency = nil
94+
} else {
95+
actionToAppend.Frequency = &alerting.ActionsInnerFrequency{
96+
Summary: action.Frequency.Summary,
97+
NotifyWhen: (alerting.NotifyWhen)(action.Frequency.NotifyWhen),
98+
Throttle: *alerting.NewNullableString(&action.Frequency.Throttle),
99+
}
100+
}
101+
102+
actions = append(actions, actionToAppend)
76103
}
77104
return actions
78105
}

internal/clients/kibana/alerting_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ func Test_ruleResponseToModel(t *testing.T) {
7171
Group: "group-1",
7272
Id: "id",
7373
Params: map[string]interface{}{},
74+
Frequency: makePtr(alerting.ActionsInnerFrequency{
75+
Summary: true,
76+
NotifyWhen: "onThrottleInterval",
77+
Throttle: *alerting.NewNullableString(makePtr("10s")),
78+
}),
7479
},
7580
{
7681
Group: "group-2",
@@ -113,6 +118,11 @@ func Test_ruleResponseToModel(t *testing.T) {
113118
Group: "group-1",
114119
ID: "id",
115120
Params: map[string]interface{}{},
121+
Frequency: &models.AlertingRuleActionFrequency{
122+
Summary: true,
123+
NotifyWhen: "onThrottleInterval",
124+
Throttle: "10s",
125+
},
116126
},
117127
{
118128
Group: "group-2",

internal/kibana/alerting.go

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package kibana
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"strings"
78

89
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
@@ -47,7 +48,7 @@ func ResourceAlertingRule() *schema.Resource {
4748
"notify_when": {
4849
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.",
4950
Type: schema.TypeString,
50-
Required: true,
51+
Optional: true,
5152
ValidateFunc: validation.StringInSlice([]string{"onActionGroupChange", "onActiveAlert", "onThrottleInterval"}, false),
5253
},
5354
"params": {
@@ -93,6 +94,34 @@ func ResourceAlertingRule() *schema.Resource {
9394
ValidateFunc: validation.StringIsJSON,
9495
DiffSuppressFunc: utils.DiffJsonSuppress,
9596
},
97+
"frequency": {
98+
Description: "The parameters for the action, which are sent to the connector.",
99+
Type: schema.TypeList,
100+
MinItems: 0,
101+
MaxItems: 1,
102+
Optional: true,
103+
Elem: &schema.Resource{
104+
Schema: map[string]*schema.Schema{
105+
"summary": {
106+
Description: "Indicates whether the action is a summary.",
107+
Type: schema.TypeBool,
108+
Required: true,
109+
},
110+
"notify_when": {
111+
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.",
112+
Type: schema.TypeString,
113+
Required: true,
114+
ValidateFunc: validation.StringInSlice([]string{"onActionGroupChange", "onActiveAlert", "onThrottleInterval"}, false),
115+
},
116+
"throttle": {
117+
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.",
118+
Type: schema.TypeString,
119+
Optional: true,
120+
ValidateFunc: utils.StringIsDuration,
121+
},
122+
},
123+
},
124+
},
96125
},
97126
},
98127
},
@@ -115,7 +144,6 @@ func ResourceAlertingRule() *schema.Resource {
115144
Optional: true,
116145
ValidateFunc: utils.StringIsDuration,
117146
},
118-
119147
"scheduled_task_id": {
120148
Description: "ID of the scheduled task that will execute the alert.",
121149
Type: schema.TypeString,
@@ -207,7 +235,7 @@ func getAlertingRuleFromResourceData(d *schema.ResourceData, serverVersion *vers
207235
rule.AlertDelay = utils.Pointer(float32(v.(float64)))
208236
}
209237

210-
actions, diags := getActionsFromResourceData(d)
238+
actions, diags := getActionsFromResourceData(d, serverVersion)
211239
if diags.HasError() {
212240
return models.AlertingRule{}, diags
213241
}
@@ -222,11 +250,11 @@ func getAlertingRuleFromResourceData(d *schema.ResourceData, serverVersion *vers
222250
return rule, diags
223251
}
224252

225-
func getActionsFromResourceData(d *schema.ResourceData) ([]models.AlertingRuleAction, diag.Diagnostics) {
253+
func getActionsFromResourceData(d *schema.ResourceData, serverVersion *version.Version) ([]models.AlertingRuleAction, diag.Diagnostics) {
226254
actions := []models.AlertingRuleAction{}
227255
if v, ok := d.GetOk("actions"); ok {
228256
resourceActions := v.([]interface{})
229-
for _, a := range resourceActions {
257+
for i, a := range resourceActions {
230258
action := a.(map[string]interface{})
231259
paramsStr := action["params"].(string)
232260
var params map[string]interface{}
@@ -235,11 +263,33 @@ func getActionsFromResourceData(d *schema.ResourceData) ([]models.AlertingRuleAc
235263
return []models.AlertingRuleAction{}, diag.FromErr(err)
236264
}
237265

238-
actions = append(actions, models.AlertingRuleAction{
266+
a := models.AlertingRuleAction{
239267
Group: action["group"].(string),
240268
ID: action["id"].(string),
241269
Params: params,
242-
})
270+
}
271+
272+
currentAction := fmt.Sprintf("actions.%d", i)
273+
274+
if _, ok := d.GetOk(currentAction + ".frequency"); ok {
275+
if serverVersion.LessThan(alertDelayMinSupportedVersion) {
276+
return []models.AlertingRuleAction{}, diag.Diagnostics{
277+
diag.Diagnostic{
278+
Severity: diag.Error,
279+
Summary: "actions.frequency is only supported for Elasticsearch v8.13 or higher",
280+
Detail: "actions.frequency is only supported for Elasticsearch v8.13 or higher",
281+
},
282+
}
283+
}
284+
285+
a.Frequency = &models.AlertingRuleActionFrequency{
286+
Summary: d.Get(currentAction + ".frequency.0.summary").(bool),
287+
NotifyWhen: d.Get(currentAction + ".frequency.0.notify_when").(string),
288+
Throttle: d.Get(currentAction + ".frequency.0.throttle").(string),
289+
}
290+
}
291+
292+
actions = append(actions, a)
243293
}
244294
}
245295

@@ -380,12 +430,27 @@ func resourceRuleRead(ctx context.Context, d *schema.ResourceData, meta interfac
380430
if err != nil {
381431
return diag.FromErr(err)
382432
}
433+
434+
frequency := []interface{}{}
435+
436+
if action.Frequency != nil {
437+
frequency = append(frequency, map[string]interface{}{
438+
"summary": action.Frequency.Summary,
439+
"notify_when": action.Frequency.NotifyWhen,
440+
"throttle": action.Frequency.Throttle,
441+
})
442+
} else {
443+
frequency = nil
444+
}
445+
383446
actions = append(actions, map[string]interface{}{
384-
"group": action.Group,
385-
"id": action.ID,
386-
"params": string(params),
447+
"group": action.Group,
448+
"id": action.ID,
449+
"params": string(params),
450+
"frequency": frequency,
387451
})
388452
}
453+
389454
if err := d.Set("actions", actions); err != nil {
390455
return diag.FromErr(err)
391456
}

internal/models/alert_rule.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package models
22

3-
import "time"
3+
import (
4+
"time"
5+
)
46

57
type AlertingRule struct {
68
RuleID string
@@ -26,12 +28,19 @@ type AlertingRuleSchedule struct {
2628
}
2729

2830
type AlertingRuleAction struct {
29-
Group string
30-
ID string
31-
Params map[string]interface{}
31+
Group string
32+
ID string
33+
Params map[string]interface{}
34+
Frequency *AlertingRuleActionFrequency
3235
}
3336

3437
type AlertingRuleExecutionStatus struct {
3538
LastExecutionDate *time.Time
3639
Status *string
3740
}
41+
42+
type AlertingRuleActionFrequency struct {
43+
Summary bool
44+
NotifyWhen string
45+
Throttle string
46+
}

0 commit comments

Comments
 (0)