Skip to content

Commit 5925649

Browse files
committed
Add Frequency.
1 parent 234415d commit 5925649

File tree

6 files changed

+74
-8
lines changed

6 files changed

+74
-8
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
provider "elasticstack" {
2+
kibana {}
3+
}
4+
5+
resource "elasticstack_kibana_alerting_rule" "example_with_action_frequency" {
6+
name = "%s"
7+
consumer = "alerts"
8+
params = jsonencode({
9+
aggType = "avg"
10+
groupBy = "top"
11+
termSize = 10
12+
timeWindowSize = 10
13+
timeWindowUnit = "s"
14+
threshold = [10]
15+
thresholdComparator = ">"
16+
index = ["test-index"]
17+
timeField = "@timestamp"
18+
aggField = "version"
19+
termField = "name"
20+
})
21+
rule_type_id = ".index-threshold"
22+
interval = "1m"
23+
enabled = true
24+
25+
actions {
26+
# Should be the id of a MS Teams connector
27+
id = elasticstack_kibana_action_connector.index_example.connector_id
28+
group = "threshold met"
29+
params = jsonencode({
30+
"message" : "foobar"
31+
})
32+
33+
frequency {
34+
summary = false
35+
notify_when = "onActionGroupChange"
36+
}
37+
}
38+
}

internal/clients/kibana/alerting.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func ruleResponseToModel(spaceID string, res *alerting.RuleResponseProperties) *
3535
a.Frequency = &models.AlertingRuleActionFrequency{
3636
Summary: frequency.Summary,
3737
NotifyWhen: (string)(frequency.NotifyWhen),
38-
Throttle: *frequency.Throttle.Get(),
38+
Throttle: frequency.Throttle.Get(),
3939
}
4040
}
4141

@@ -92,11 +92,16 @@ func ruleActionsToActionsInner(ruleActions []models.AlertingRuleAction) []alerti
9292
if alerting.IsNil(action.Frequency) {
9393
actionToAppend.Frequency = nil
9494
} else {
95-
actionToAppend.Frequency = &alerting.ActionsInnerFrequency{
95+
frequency := alerting.ActionsInnerFrequency{
9696
Summary: action.Frequency.Summary,
9797
NotifyWhen: (alerting.NotifyWhen)(action.Frequency.NotifyWhen),
98-
Throttle: *alerting.NewNullableString(&action.Frequency.Throttle),
9998
}
99+
100+
if action.Frequency.Throttle != nil {
101+
frequency.Throttle = *alerting.NewNullableString(action.Frequency.Throttle)
102+
}
103+
104+
actionToAppend.Frequency = &frequency
100105
}
101106

102107
actions = append(actions, actionToAppend)

internal/clients/kibana/alerting_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ func Test_ruleResponseToModel(t *testing.T) {
8181
Group: "group-2",
8282
Id: "id",
8383
Params: map[string]interface{}{},
84+
Frequency: makePtr(alerting.ActionsInnerFrequency{
85+
Summary: true,
86+
NotifyWhen: "onActionGroupChange",
87+
}),
88+
},
89+
{
90+
Group: "group-3",
91+
Id: "id",
92+
Params: map[string]interface{}{},
8493
},
8594
},
8695
ExecutionStatus: alerting.RuleResponsePropertiesExecutionStatus{
@@ -121,13 +130,22 @@ func Test_ruleResponseToModel(t *testing.T) {
121130
Frequency: &models.AlertingRuleActionFrequency{
122131
Summary: true,
123132
NotifyWhen: "onThrottleInterval",
124-
Throttle: "10s",
133+
Throttle: makePtr("10s"),
125134
},
126135
},
127136
{
128137
Group: "group-2",
129138
ID: "id",
130139
Params: map[string]interface{}{},
140+
Frequency: &models.AlertingRuleActionFrequency{
141+
Summary: true,
142+
NotifyWhen: "onActionGroupChange",
143+
},
144+
},
145+
{
146+
Group: "group-3",
147+
ID: "id",
148+
Params: map[string]interface{}{},
131149
},
132150
},
133151
AlertDelay: makePtr(float32(4)),

internal/kibana/alerting.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,16 @@ func getActionsFromResourceData(d *schema.ResourceData, serverVersion *version.V
282282
}
283283
}
284284

285-
a.Frequency = &models.AlertingRuleActionFrequency{
285+
frequency := models.AlertingRuleActionFrequency{
286286
Summary: d.Get(currentAction + ".frequency.0.summary").(bool),
287287
NotifyWhen: d.Get(currentAction + ".frequency.0.notify_when").(string),
288-
Throttle: d.Get(currentAction + ".frequency.0.throttle").(string),
289288
}
289+
290+
if throttle := getOrNilString(currentAction+".frequency.0.throttle", d); throttle != nil && *throttle != "" {
291+
frequency.Throttle = throttle
292+
}
293+
294+
a.Frequency = &frequency
290295
}
291296

292297
actions = append(actions, a)

internal/kibana/alerting_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestAccResourceAlertingRule(t *testing.T) {
6363
resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "name", ruleName),
6464
resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "rule_id", "af22bd1c-8fb3-4020-9249-a4ac5471624b"),
6565
resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "consumer", "alerts"),
66-
resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "notify_when", "onActiveAlert"),
66+
resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "notify_when", ""),
6767
resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "rule_type_id", ".index-threshold"),
6868
resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "interval", "1m"),
6969
resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "enabled", "true"),

internal/models/alert_rule.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ type AlertingRuleExecutionStatus struct {
4242
type AlertingRuleActionFrequency struct {
4343
Summary bool
4444
NotifyWhen string
45-
Throttle string
45+
Throttle *string
4646
}

0 commit comments

Comments
 (0)