Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification Policy: Fix perma-drift #1168

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions docs/resources/notification_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ resource "grafana_notification_policy" "my_notification_policy" {
match = "="
value = "myvalue"
}
matcher {
label = "alertname"
match = "="
value = "CPU Usage"
}
matcher {
label = "Name"
match = "=~"
value = "host.*|host-b.*"
}
contact_point = grafana_contact_point.a_contact_point.name
continue = true
mute_timings = [grafana_mute_timing.a_mute_timing.name]
Expand Down Expand Up @@ -118,7 +128,7 @@ Optional:
- `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.
- `group_interval` (String) Minimum time interval between two notifications for the same group. Default is 5 minutes.
- `group_wait` (String) Time to wait to buffer alerts of the same group before sending a notification. Default is 30 seconds.
- `matcher` (Block List) 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))
- `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))
- `mute_timings` (List of String) A list of mute timing names to apply to alerts that match this policy.
- `policy` (Block List) Routing rules for specific label sets. (see [below for nested schema](#nestedblock--policy--policy))
- `repeat_interval` (String) Minimum time interval for re-sending a notification if an alert is still firing. Default is 4 hours.
Expand Down Expand Up @@ -146,7 +156,7 @@ Optional:
- `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.
- `group_interval` (String) Minimum time interval between two notifications for the same group. Default is 5 minutes.
- `group_wait` (String) Time to wait to buffer alerts of the same group before sending a notification. Default is 30 seconds.
- `matcher` (Block List) 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))
- `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))
- `mute_timings` (List of String) A list of mute timing names to apply to alerts that match this policy.
- `policy` (Block List) Routing rules for specific label sets. (see [below for nested schema](#nestedblock--policy--policy--policy))
- `repeat_interval` (String) Minimum time interval for re-sending a notification if an alert is still firing. Default is 4 hours.
Expand Down Expand Up @@ -174,7 +184,7 @@ Optional:
- `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.
- `group_interval` (String) Minimum time interval between two notifications for the same group. Default is 5 minutes.
- `group_wait` (String) Time to wait to buffer alerts of the same group before sending a notification. Default is 30 seconds.
- `matcher` (Block List) 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))
- `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))
- `mute_timings` (List of String) A list of mute timing names to apply to alerts that match this policy.
- `policy` (Block List) Routing rules for specific label sets. (see [below for nested schema](#nestedblock--policy--policy--policy--policy))
- `repeat_interval` (String) Minimum time interval for re-sending a notification if an alert is still firing. Default is 4 hours.
Expand Down Expand Up @@ -202,7 +212,7 @@ Optional:
- `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.
- `group_interval` (String) Minimum time interval between two notifications for the same group. Default is 5 minutes.
- `group_wait` (String) Time to wait to buffer alerts of the same group before sending a notification. Default is 30 seconds.
- `matcher` (Block List) 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))
- `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))
- `mute_timings` (List of String) A list of mute timing names to apply to alerts that match this policy.
- `repeat_interval` (String) Minimum time interval for re-sending a notification if an alert is still firing. Default is 4 hours.

Expand Down
10 changes: 10 additions & 0 deletions examples/resources/grafana_notification_policy/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ resource "grafana_notification_policy" "my_notification_policy" {
match = "="
value = "myvalue"
}
matcher {
label = "alertname"
match = "="
value = "CPU Usage"
}
matcher {
label = "Name"
match = "=~"
value = "host.*|host-b.*"
}
contact_point = grafana_contact_point.a_contact_point.name
continue = true
mute_timings = [grafana_mute_timing.a_mute_timing.name]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func policySchema(depth uint) *schema.Resource {
},
},
"matcher": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Description: "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.",
Elem: &schema.Resource{
Expand Down Expand Up @@ -341,7 +341,7 @@ func unpackSpecificPolicy(p interface{}) (gapi.SpecificPolicy, error) {
}

if v, ok := json["matcher"]; ok && v != nil {
ms := v.([]interface{})
ms := v.(*schema.Set).List()
matchers := make([]gapi.Matcher, 0, len(ms))
for _, m := range ms {
matcher, err := unpackPolicyMatcher(m)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ func TestAccNotificationPolicy_basic(t *testing.T) {
// nested
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.#", "2"),
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.contact_point", "A Contact Point"),
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.matcher.0.label", "mylabel"),
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.matcher.0.match", "="),
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.matcher.0.value", "myvalue"),
// Matchers are reordered by the API
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.matcher.0.label", "Name"),
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.matcher.0.match", "=~"),
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.matcher.0.value", "host.*|host-b.*"),
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.matcher.1.label", "alertname"),
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.matcher.1.match", "="),
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.matcher.1.value", "CPU Usage"),
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.matcher.2.label", "mylabel"),
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.matcher.2.match", "="),
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.matcher.2.value", "myvalue"),
resource.TestCheckNoResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.group_by"),
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.continue", "true"),
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.mute_timings.0", "Some Mute Timing"),
Expand Down