Skip to content

Commit 6a7415e

Browse files
Add support for active timings to notification policies (#2249)
* add support for active timings * update schema * add exclusion to TestAccNotificationPolicy_inheritContactPoint * run notificaiton policies only for 12.1 * fix one more test * Update internal/resources/grafana/resource_alerting_notification_policy.go Co-authored-by: Alexander Akhmetov <[email protected]> --------- Co-authored-by: Alexander Akhmetov <[email protected]>
1 parent 2e16120 commit 6a7415e

File tree

7 files changed

+88
-17
lines changed

7 files changed

+88
-17
lines changed

docs/resources/notification_policy.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ resource "grafana_mute_timing" "a_mute_timing" {
4040
}
4141
}
4242
43+
resource "grafana_mute_timing" "working_hours" {
44+
name = "Working Hours"
45+
intervals {
46+
times {
47+
start = "09:00"
48+
end = "18:00"
49+
}
50+
}
51+
}
52+
4353
4454
resource "grafana_notification_policy" "my_notification_policy" {
4555
group_by = ["..."]
@@ -65,9 +75,10 @@ resource "grafana_notification_policy" "my_notification_policy" {
6575
match = "=~"
6676
value = "host.*|host-b.*"
6777
}
68-
contact_point = grafana_contact_point.a_contact_point.name // This can be omitted to inherit from the parent
69-
continue = true
70-
mute_timings = [grafana_mute_timing.a_mute_timing.name]
78+
contact_point = grafana_contact_point.a_contact_point.name // This can be omitted to inherit from the parent
79+
continue = true
80+
mute_timings = [grafana_mute_timing.a_mute_timing.name]
81+
active_timings = [grafana_mute_timing.working_hours.name]
7182
7283
group_wait = "45s"
7384
group_interval = "6m"
@@ -122,13 +133,14 @@ resource "grafana_notification_policy" "my_notification_policy" {
122133

123134
Optional:
124135

136+
- `active_timings` (List of String) A list of time interval names to apply to alerts that match this policy to suppress them unless they are sent at the specified time. Supported in Grafana 12.1.0 and later
125137
- `contact_point` (String) The contact point to route notifications that match this rule to.
126138
- `continue` (Boolean) Whether to continue matching subsequent rules if an alert matches the current rule. Otherwise, the rule will be 'consumed' by the first policy to match it.
127139
- `group_by` (List of String) A list of alert labels to group alerts into notifications by. Use the special label `...` to group alerts by all labels, effectively disabling grouping. Required for root policy only. If empty, the parent grouping is used.
128140
- `group_interval` (String) Minimum time interval between two notifications for the same group. Default is 5 minutes.
129141
- `group_wait` (String) Time to wait to buffer alerts of the same group before sending a notification. Default is 30 seconds.
130142
- `matcher` (Block Set) Describes which labels this rule should match. When multiple matchers are supplied, an alert must match ALL matchers to be accepted by this policy. When no matchers are supplied, the rule will match all alert instances. (see [below for nested schema](#nestedblock--policy--matcher))
131-
- `mute_timings` (List of String) A list of mute timing names to apply to alerts that match this policy.
143+
- `mute_timings` (List of String) A list of time intervals to apply to alerts that match this policy to mute them for the specified time.
132144
- `policy` (Block List) Routing rules for specific label sets. (see [below for nested schema](#nestedblock--policy--policy))
133145
- `repeat_interval` (String) Minimum time interval for re-sending a notification if an alert is still firing. Default is 4 hours.
134146

@@ -147,13 +159,14 @@ Required:
147159

148160
Optional:
149161

162+
- `active_timings` (List of String) A list of time interval names to apply to alerts that match this policy to suppress them unless they are sent at the specified time. Supported in Grafana 12.1.0 and later
150163
- `contact_point` (String) The contact point to route notifications that match this rule to.
151164
- `continue` (Boolean) Whether to continue matching subsequent rules if an alert matches the current rule. Otherwise, the rule will be 'consumed' by the first policy to match it.
152165
- `group_by` (List of String) A list of alert labels to group alerts into notifications by. Use the special label `...` to group alerts by all labels, effectively disabling grouping. Required for root policy only. If empty, the parent grouping is used.
153166
- `group_interval` (String) Minimum time interval between two notifications for the same group. Default is 5 minutes.
154167
- `group_wait` (String) Time to wait to buffer alerts of the same group before sending a notification. Default is 30 seconds.
155168
- `matcher` (Block Set) Describes which labels this rule should match. When multiple matchers are supplied, an alert must match ALL matchers to be accepted by this policy. When no matchers are supplied, the rule will match all alert instances. (see [below for nested schema](#nestedblock--policy--policy--matcher))
156-
- `mute_timings` (List of String) A list of mute timing names to apply to alerts that match this policy.
169+
- `mute_timings` (List of String) A list of time intervals to apply to alerts that match this policy to mute them for the specified time.
157170
- `policy` (Block List) Routing rules for specific label sets. (see [below for nested schema](#nestedblock--policy--policy--policy))
158171
- `repeat_interval` (String) Minimum time interval for re-sending a notification if an alert is still firing. Default is 4 hours.
159172

@@ -172,13 +185,14 @@ Required:
172185

173186
Optional:
174187

188+
- `active_timings` (List of String) A list of time interval names to apply to alerts that match this policy to suppress them unless they are sent at the specified time. Supported in Grafana 12.1.0 and later
175189
- `contact_point` (String) The contact point to route notifications that match this rule to.
176190
- `continue` (Boolean) Whether to continue matching subsequent rules if an alert matches the current rule. Otherwise, the rule will be 'consumed' by the first policy to match it.
177191
- `group_by` (List of String) A list of alert labels to group alerts into notifications by. Use the special label `...` to group alerts by all labels, effectively disabling grouping. Required for root policy only. If empty, the parent grouping is used.
178192
- `group_interval` (String) Minimum time interval between two notifications for the same group. Default is 5 minutes.
179193
- `group_wait` (String) Time to wait to buffer alerts of the same group before sending a notification. Default is 30 seconds.
180194
- `matcher` (Block Set) Describes which labels this rule should match. When multiple matchers are supplied, an alert must match ALL matchers to be accepted by this policy. When no matchers are supplied, the rule will match all alert instances. (see [below for nested schema](#nestedblock--policy--policy--policy--matcher))
181-
- `mute_timings` (List of String) A list of mute timing names to apply to alerts that match this policy.
195+
- `mute_timings` (List of String) A list of time intervals to apply to alerts that match this policy to mute them for the specified time.
182196
- `policy` (Block List) Routing rules for specific label sets. (see [below for nested schema](#nestedblock--policy--policy--policy--policy))
183197
- `repeat_interval` (String) Minimum time interval for re-sending a notification if an alert is still firing. Default is 4 hours.
184198

@@ -201,12 +215,13 @@ Required:
201215

202216
Optional:
203217

218+
- `active_timings` (List of String) A list of time interval names to apply to alerts that match this policy to suppress them unless they are sent at the specified time. Supported in Grafana 12.1.0 and later
204219
- `contact_point` (String) The contact point to route notifications that match this rule to.
205220
- `continue` (Boolean) Whether to continue matching subsequent rules if an alert matches the current rule. Otherwise, the rule will be 'consumed' by the first policy to match it.
206221
- `group_interval` (String) Minimum time interval between two notifications for the same group. Default is 5 minutes.
207222
- `group_wait` (String) Time to wait to buffer alerts of the same group before sending a notification. Default is 30 seconds.
208223
- `matcher` (Block Set) Describes which labels this rule should match. When multiple matchers are supplied, an alert must match ALL matchers to be accepted by this policy. When no matchers are supplied, the rule will match all alert instances. (see [below for nested schema](#nestedblock--policy--policy--policy--policy--matcher))
209-
- `mute_timings` (List of String) A list of mute timing names to apply to alerts that match this policy.
224+
- `mute_timings` (List of String) A list of time intervals to apply to alerts that match this policy to mute them for the specified time.
210225
- `repeat_interval` (String) Minimum time interval for re-sending a notification if an alert is still firing. Default is 4 hours.
211226

212227
<a id="nestedblock--policy--policy--policy--policy--matcher"></a>

examples/resources/grafana_notification_policy/resource.tf

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ resource "grafana_mute_timing" "a_mute_timing" {
1515
}
1616
}
1717

18+
resource "grafana_mute_timing" "working_hours" {
19+
name = "Working Hours"
20+
intervals {
21+
times {
22+
start = "09:00"
23+
end = "18:00"
24+
}
25+
}
26+
}
27+
1828

1929
resource "grafana_notification_policy" "my_notification_policy" {
2030
group_by = ["..."]
@@ -40,9 +50,10 @@ resource "grafana_notification_policy" "my_notification_policy" {
4050
match = "=~"
4151
value = "host.*|host-b.*"
4252
}
43-
contact_point = grafana_contact_point.a_contact_point.name // This can be omitted to inherit from the parent
44-
continue = true
45-
mute_timings = [grafana_mute_timing.a_mute_timing.name]
53+
contact_point = grafana_contact_point.a_contact_point.name // This can be omitted to inherit from the parent
54+
continue = true
55+
mute_timings = [grafana_mute_timing.a_mute_timing.name]
56+
active_timings = [grafana_mute_timing.working_hours.name]
4657

4758
group_wait = "45s"
4859
group_interval = "6m"

internal/resources/examples_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ func TestAccExamples(t *testing.T) {
2929
{
3030
category: "Alerting",
3131
testCheck: func(t *testing.T, filename string) {
32-
testutils.CheckOSSTestsEnabled(t, ">=11.0.0") // Only run on latest OSS version. The examples should be updated to reflect their latest working config.
32+
if strings.Contains(filename, "grafana_notification_policy") {
33+
testutils.CheckOSSTestsEnabled(t, ">=12.1.0") // Only run on latest OSS version. The examples should be updated to reflect their latest working config.
34+
} else {
35+
testutils.CheckOSSTestsEnabled(t, ">=11.0.0") // Only run on latest OSS version. The examples should be updated to reflect their latest working config.
36+
}
3337
},
3438
},
3539
{

internal/resources/grafana/resource_alerting_notification_policy.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,15 @@ func policySchema(depth uint) *schema.Resource {
151151
"mute_timings": {
152152
Type: schema.TypeList,
153153
Optional: true,
154-
Description: "A list of mute timing names to apply to alerts that match this policy.",
154+
Description: "A list of time intervals to apply to alerts that match this policy to mute them for the specified time.",
155+
Elem: &schema.Schema{
156+
Type: schema.TypeString,
157+
},
158+
},
159+
"active_timings": {
160+
Type: schema.TypeList,
161+
Optional: true,
162+
Description: "A list of time interval names to apply to alerts that match this policy to suppress them unless they are sent at the specified time. Supported in Grafana 12.1.0 and later",
155163
Elem: &schema.Schema{
156164
Type: schema.TypeString,
157165
},
@@ -308,6 +316,9 @@ func packSpecificPolicy(p *models.Route, depth uint) interface{} {
308316
if len(p.MuteTimeIntervals) > 0 {
309317
result["mute_timings"] = p.MuteTimeIntervals
310318
}
319+
if len(p.ActiveTimeIntervals) > 0 {
320+
result["active_timings"] = p.ActiveTimeIntervals
321+
}
311322
if p.GroupWait != "" {
312323
result["group_wait"] = p.GroupWait
313324
}
@@ -390,6 +401,9 @@ func unpackSpecificPolicy(p interface{}) (*models.Route, error) {
390401
if v, ok := json["mute_timings"]; ok && v != nil {
391402
policy.MuteTimeIntervals = common.ListToStringSlice(v.([]interface{}))
392403
}
404+
if v, ok := json["active_timings"]; ok && v != nil {
405+
policy.ActiveTimeIntervals = common.ListToStringSlice(v.([]interface{}))
406+
}
393407
if v, ok := json["continue"]; ok && v != nil {
394408
policy.Continue = v.(bool)
395409
}

internal/resources/grafana/resource_alerting_notification_policy_test.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1010

1111
"github.com/grafana/grafana-openapi-client-go/models"
12+
1213
"github.com/grafana/terraform-provider-grafana/v3/internal/testutils"
1314
)
1415

@@ -24,7 +25,9 @@ func TestAccNotificationPolicy_basic(t *testing.T) {
2425
Steps: []resource.TestStep{
2526
// Test creation.
2627
{
27-
Config: testutils.TestAccExample(t, "resources/grafana_notification_policy/resource.tf"),
28+
Config: testutils.TestAccExampleWithReplace(t, "resources/grafana_notification_policy/resource.tf", map[string]string{
29+
"active_timings = [grafana_mute_timing.working_hours.name]": "", // old versions of Grafana do not support this field
30+
}),
2831
Check: resource.ComposeTestCheckFunc(
2932
alertingNotificationPolicyCheckExists.exists("grafana_notification_policy.my_notification_policy", &policy),
3033
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "contact_point", "A Contact Point"),
@@ -78,6 +81,7 @@ func TestAccNotificationPolicy_basic(t *testing.T) {
7881
{
7982
Config: testutils.TestAccExampleWithReplace(t, "resources/grafana_notification_policy/resource.tf", map[string]string{
8083
"...": "alertname",
84+
"active_timings = [grafana_mute_timing.working_hours.name]": "", // old versions of Grafana do not support this field
8185
}),
8286
Check: resource.ComposeTestCheckFunc(
8387
alertingNotificationPolicyCheckExists.exists("grafana_notification_policy.my_notification_policy", &policy),
@@ -90,9 +94,30 @@ func TestAccNotificationPolicy_basic(t *testing.T) {
9094
})
9195
}
9296

93-
func TestAccNotificationPolicy_inheritContactPoint(t *testing.T) {
94-
testutils.CheckCloudInstanceTestsEnabled(t) // Replace this when v11 is released
97+
func TestAccNotificationPolicy_activeTimings(t *testing.T) {
98+
testutils.CheckOSSTestsEnabled(t, ">=12.1.0")
99+
100+
var policy models.Route
101+
102+
resource.Test(t, resource.TestCase{
103+
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
104+
// Implicitly tests deletion.
105+
CheckDestroy: alertingNotificationPolicyCheckExists.destroyed(&policy, nil),
106+
Steps: []resource.TestStep{
107+
// Test creation.
108+
{
109+
Config: testutils.TestAccExample(t, "resources/grafana_notification_policy/resource.tf"),
110+
Check: resource.ComposeTestCheckFunc(
111+
alertingNotificationPolicyCheckExists.exists("grafana_notification_policy.my_notification_policy", &policy),
112+
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.active_timings.0", "Working Hours"),
113+
),
114+
},
115+
},
116+
})
117+
}
95118

119+
func TestAccNotificationPolicy_inheritContactPoint(t *testing.T) {
120+
testutils.CheckOSSTestsEnabled(t, ">=11.0.0")
96121
var policy models.Route
97122

98123
resource.Test(t, resource.TestCase{
@@ -103,8 +128,9 @@ func TestAccNotificationPolicy_inheritContactPoint(t *testing.T) {
103128
// Test creation.
104129
{
105130
Config: testutils.TestAccExampleWithReplace(t, "resources/grafana_notification_policy/resource.tf", map[string]string{
106-
"contact_point = grafana_contact_point.a_contact_point.name // This can be omitted to inherit from the parent": "",
131+
"contact_point = grafana_contact_point.a_contact_point.name // This can be omitted to inherit from the parent": "",
107132
"contact_point = grafana_contact_point.a_contact_point.name // This can also be omitted to inherit from the parent's parent": "",
133+
"active_timings = [grafana_mute_timing.working_hours.name]": "",
108134
}),
109135
Check: resource.ComposeTestCheckFunc(
110136
alertingNotificationPolicyCheckExists.exists("grafana_notification_policy.my_notification_policy", &policy),

pkg/generate/postprocessing/replace_references.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ var knownReferences = []string{
8484
"grafana_machine_learning_job.datasource_uid=grafana_data_source.uid",
8585
"grafana_message_template.org_id=grafana_organization.id",
8686
"grafana_mute_timing.org_id=grafana_organization.id",
87+
"grafana_notification_policy.active_timings=grafana_mute_timing.name",
8788
"grafana_notification_policy.contact_point=grafana_contact_point.name",
8889
"grafana_notification_policy.mute_timings=grafana_mute_timing.name",
8990
"grafana_notification_policy.org_id=grafana_organization.id",

provider_schema.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)