Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
29 changes: 22 additions & 7 deletions docs/resources/notification_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ resource "grafana_mute_timing" "a_mute_timing" {
}
}

resource "grafana_mute_timing" "working_hours" {
name = "Working Hours"
intervals {
times {
start = "09:00"
end = "18:00"
}
}
}


resource "grafana_notification_policy" "my_notification_policy" {
group_by = ["..."]
Expand All @@ -65,9 +75,10 @@ resource "grafana_notification_policy" "my_notification_policy" {
match = "=~"
value = "host.*|host-b.*"
}
contact_point = grafana_contact_point.a_contact_point.name // This can be omitted to inherit from the parent
continue = true
mute_timings = [grafana_mute_timing.a_mute_timing.name]
contact_point = grafana_contact_point.a_contact_point.name // This can be omitted to inherit from the parent
continue = true
mute_timings = [grafana_mute_timing.a_mute_timing.name]
active_timings = [grafana_mute_timing.working_hours.name]

group_wait = "45s"
group_interval = "6m"
Expand Down Expand Up @@ -122,13 +133,14 @@ resource "grafana_notification_policy" "my_notification_policy" {

Optional:

- `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
- `contact_point` (String) The contact point to route notifications that match this rule to.
- `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_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 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.
- `mute_timings` (List of String) A list of time intervals to apply to apply to alerts that match this policy to mute them for the specified time.
- `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 All @@ -147,13 +159,14 @@ Required:

Optional:

- `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
- `contact_point` (String) The contact point to route notifications that match this rule to.
- `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_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 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.
- `mute_timings` (List of String) A list of time intervals to apply to apply to alerts that match this policy to mute them for the specified time.
- `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 All @@ -172,13 +185,14 @@ Required:

Optional:

- `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
- `contact_point` (String) The contact point to route notifications that match this rule to.
- `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_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 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.
- `mute_timings` (List of String) A list of time intervals to apply to apply to alerts that match this policy to mute them for the specified time.
- `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 All @@ -201,12 +215,13 @@ Required:

Optional:

- `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
- `contact_point` (String) The contact point to route notifications that match this rule to.
- `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 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.
- `mute_timings` (List of String) A list of time intervals to apply to apply to alerts that match this policy to mute them for the specified time.
- `repeat_interval` (String) Minimum time interval for re-sending a notification if an alert is still firing. Default is 4 hours.

<a id="nestedblock--policy--policy--policy--policy--matcher"></a>
Expand Down
17 changes: 14 additions & 3 deletions examples/resources/grafana_notification_policy/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ resource "grafana_mute_timing" "a_mute_timing" {
}
}

resource "grafana_mute_timing" "working_hours" {
name = "Working Hours"
intervals {
times {
start = "09:00"
end = "18:00"
}
}
}


resource "grafana_notification_policy" "my_notification_policy" {
group_by = ["..."]
Expand All @@ -40,9 +50,10 @@ resource "grafana_notification_policy" "my_notification_policy" {
match = "=~"
value = "host.*|host-b.*"
}
contact_point = grafana_contact_point.a_contact_point.name // This can be omitted to inherit from the parent
continue = true
mute_timings = [grafana_mute_timing.a_mute_timing.name]
contact_point = grafana_contact_point.a_contact_point.name // This can be omitted to inherit from the parent
continue = true
mute_timings = [grafana_mute_timing.a_mute_timing.name]
active_timings = [grafana_mute_timing.working_hours.name]

group_wait = "45s"
group_interval = "6m"
Expand Down
6 changes: 5 additions & 1 deletion internal/resources/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ func TestAccExamples(t *testing.T) {
{
category: "Alerting",
testCheck: func(t *testing.T, filename string) {
testutils.CheckOSSTestsEnabled(t, ">=11.0.0") // Only run on latest OSS version. The examples should be updated to reflect their latest working config.
if strings.Contains(filename, "grafana_notification_policy") {
testutils.CheckOSSTestsEnabled(t, ">=12.1.0") // Only run on latest OSS version. The examples should be updated to reflect their latest working config.
} else {
testutils.CheckOSSTestsEnabled(t, ">=11.0.0") // Only run on latest OSS version. The examples should be updated to reflect their latest working config.
}
},
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,15 @@ func policySchema(depth uint) *schema.Resource {
"mute_timings": {
Type: schema.TypeList,
Optional: true,
Description: "A list of mute timing names to apply to alerts that match this policy.",
Description: "A list of time intervals to apply to apply to alerts that match this policy to mute them for the specified time.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"active_timings": {
Type: schema.TypeList,
Optional: true,
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",
Elem: &schema.Schema{
Type: schema.TypeString,
},
Expand Down Expand Up @@ -308,6 +316,9 @@ func packSpecificPolicy(p *models.Route, depth uint) interface{} {
if len(p.MuteTimeIntervals) > 0 {
result["mute_timings"] = p.MuteTimeIntervals
}
if len(p.ActiveTimeIntervals) > 0 {
result["active_timings"] = p.ActiveTimeIntervals
}
if p.GroupWait != "" {
result["group_wait"] = p.GroupWait
}
Expand Down Expand Up @@ -390,6 +401,9 @@ func unpackSpecificPolicy(p interface{}) (*models.Route, error) {
if v, ok := json["mute_timings"]; ok && v != nil {
policy.MuteTimeIntervals = common.ListToStringSlice(v.([]interface{}))
}
if v, ok := json["active_timings"]; ok && v != nil {
policy.ActiveTimeIntervals = common.ListToStringSlice(v.([]interface{}))
}
if v, ok := json["continue"]; ok && v != nil {
policy.Continue = v.(bool)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/grafana/grafana-openapi-client-go/models"

"github.com/grafana/terraform-provider-grafana/v3/internal/testutils"
)

Expand All @@ -24,7 +25,9 @@ func TestAccNotificationPolicy_basic(t *testing.T) {
Steps: []resource.TestStep{
// Test creation.
{
Config: testutils.TestAccExample(t, "resources/grafana_notification_policy/resource.tf"),
Config: testutils.TestAccExampleWithReplace(t, "resources/grafana_notification_policy/resource.tf", map[string]string{
"active_timings = [grafana_mute_timing.working_hours.name]": "", // old versions of Grafana do not support this field
}),
Check: resource.ComposeTestCheckFunc(
alertingNotificationPolicyCheckExists.exists("grafana_notification_policy.my_notification_policy", &policy),
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "contact_point", "A Contact Point"),
Expand Down Expand Up @@ -78,6 +81,7 @@ func TestAccNotificationPolicy_basic(t *testing.T) {
{
Config: testutils.TestAccExampleWithReplace(t, "resources/grafana_notification_policy/resource.tf", map[string]string{
"...": "alertname",
"active_timings = [grafana_mute_timing.working_hours.name]": "", // old versions of Grafana do not support this field
}),
Check: resource.ComposeTestCheckFunc(
alertingNotificationPolicyCheckExists.exists("grafana_notification_policy.my_notification_policy", &policy),
Expand All @@ -90,9 +94,30 @@ func TestAccNotificationPolicy_basic(t *testing.T) {
})
}

func TestAccNotificationPolicy_inheritContactPoint(t *testing.T) {
testutils.CheckCloudInstanceTestsEnabled(t) // Replace this when v11 is released
func TestAccNotificationPolicy_activeTimings(t *testing.T) {
testutils.CheckOSSTestsEnabled(t, ">=12.1.0")

var policy models.Route

resource.Test(t, resource.TestCase{
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
// Implicitly tests deletion.
CheckDestroy: alertingNotificationPolicyCheckExists.destroyed(&policy, nil),
Steps: []resource.TestStep{
// Test creation.
{
Config: testutils.TestAccExample(t, "resources/grafana_notification_policy/resource.tf"),
Check: resource.ComposeTestCheckFunc(
alertingNotificationPolicyCheckExists.exists("grafana_notification_policy.my_notification_policy", &policy),
resource.TestCheckResourceAttr("grafana_notification_policy.my_notification_policy", "policy.0.active_timings.0", "Working Hours"),
),
},
},
})
}

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

resource.Test(t, resource.TestCase{
Expand All @@ -103,8 +128,9 @@ func TestAccNotificationPolicy_inheritContactPoint(t *testing.T) {
// Test creation.
{
Config: testutils.TestAccExampleWithReplace(t, "resources/grafana_notification_policy/resource.tf", map[string]string{
"contact_point = grafana_contact_point.a_contact_point.name // This can be omitted to inherit from the parent": "",
"contact_point = grafana_contact_point.a_contact_point.name // This can be omitted to inherit from the parent": "",
"contact_point = grafana_contact_point.a_contact_point.name // This can also be omitted to inherit from the parent's parent": "",
"active_timings = [grafana_mute_timing.working_hours.name]": "",
}),
Check: resource.ComposeTestCheckFunc(
alertingNotificationPolicyCheckExists.exists("grafana_notification_policy.my_notification_policy", &policy),
Expand Down
1 change: 1 addition & 0 deletions pkg/generate/postprocessing/replace_references.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ var knownReferences = []string{
"grafana_machine_learning_job.datasource_uid=grafana_data_source.uid",
"grafana_message_template.org_id=grafana_organization.id",
"grafana_mute_timing.org_id=grafana_organization.id",
"grafana_notification_policy.active_timings=grafana_mute_timing.name",
"grafana_notification_policy.contact_point=grafana_contact_point.name",
"grafana_notification_policy.mute_timings=grafana_mute_timing.name",
"grafana_notification_policy.org_id=grafana_organization.id",
Expand Down
2 changes: 1 addition & 1 deletion provider_schema.json

Large diffs are not rendered by default.

Loading