Skip to content

Commit 0e40d16

Browse files
authored
Merge pull request #94 from jvshahid/notification-alert-reminders
2 parents 63ab8ad + c4ac519 commit 0e40d16

File tree

3 files changed

+120
-5
lines changed

3 files changed

+120
-5
lines changed

grafana/resource_alert_notification.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package grafana
22

33
import (
4+
"errors"
45
"fmt"
56
"log"
67
"strconv"
8+
"time"
79

810
"github.com/hashicorp/terraform/helper/schema"
911
gapi "github.com/nytm/go-grafana-api"
1012
)
1113

14+
var (
15+
ErrFrequencyMustBeSet = errors.New("frequency must be set when send_reminder is set to 'true'")
16+
)
17+
1218
func ResourceAlertNotification() *schema.Resource {
1319
return &schema.Resource{
1420
Create: CreateAlertNotification,
@@ -33,6 +39,18 @@ func ResourceAlertNotification() *schema.Resource {
3339
Default: false,
3440
},
3541

42+
"send_reminder": {
43+
Type: schema.TypeBool,
44+
Optional: true,
45+
Default: false,
46+
},
47+
48+
"frequency": {
49+
Type: schema.TypeString,
50+
Optional: true,
51+
Default: "",
52+
},
53+
3654
"settings": {
3755
Type: schema.TypeMap,
3856
Optional: true,
@@ -143,11 +161,26 @@ func makeAlertNotification(d *schema.ResourceData) (*gapi.AlertNotification, err
143161
}
144162
}
145163

164+
sendReminder := d.Get("send_reminder").(bool)
165+
frequency := d.Get("frequency").(string)
166+
167+
if sendReminder {
168+
if frequency == "" {
169+
return nil, ErrFrequencyMustBeSet
170+
}
171+
172+
if _, err := time.ParseDuration(frequency); err != nil {
173+
return nil, err
174+
}
175+
}
176+
146177
return &gapi.AlertNotification{
147-
Id: id,
148-
Name: d.Get("name").(string),
149-
Type: d.Get("type").(string),
150-
IsDefault: d.Get("is_default").(bool),
151-
Settings: settings,
178+
Id: id,
179+
Name: d.Get("name").(string),
180+
Type: d.Get("type").(string),
181+
IsDefault: d.Get("is_default").(bool),
182+
SendReminder: sendReminder,
183+
Frequency: frequency,
184+
Settings: settings,
152185
}, err
153186
}

grafana/resource_alert_notification_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,19 @@ func TestAccAlertNotification_basic(t *testing.T) {
2424
Config: testAccAlertNotificationConfig_basic,
2525
Check: resource.ComposeTestCheckFunc(
2626
testAccAlertNotificationCheckExists("grafana_alert_notification.test", &alertNotification),
27+
testAccAlertNotificationDefinition(&alertNotification),
2728
resource.TestCheckResourceAttr(
2829
"grafana_alert_notification.test", "type", "email",
2930
),
3031
resource.TestMatchResourceAttr(
3132
"grafana_alert_notification.test", "id", regexp.MustCompile(`\d+`),
3233
),
34+
resource.TestCheckResourceAttr(
35+
"grafana_alert_notification.test", "send_reminder", "true",
36+
),
37+
resource.TestCheckResourceAttr(
38+
"grafana_alert_notification.test", "frequency", "12h",
39+
),
3340
resource.TestCheckResourceAttr(
3441
"grafana_alert_notification.test", "settings.addresses", "[email protected]",
3542
),
@@ -39,6 +46,38 @@ func TestAccAlertNotification_basic(t *testing.T) {
3946
})
4047
}
4148

49+
func TestAccAlertNotification_invalid_frequence(t *testing.T) {
50+
var alertNotification gapi.AlertNotification
51+
52+
resource.Test(t, resource.TestCase{
53+
PreCheck: func() { testAccPreCheck(t) },
54+
Providers: testAccProviders,
55+
CheckDestroy: testAccAlertNotificationCheckDestroy(&alertNotification),
56+
Steps: []resource.TestStep{
57+
{
58+
ExpectError: regexp.MustCompile("invalid duration hi"),
59+
Config: testAccAlertNotificationConfig_invalid_frequency,
60+
},
61+
},
62+
})
63+
}
64+
65+
func TestAccAlertNotification_reminder_no_frequence(t *testing.T) {
66+
var alertNotification gapi.AlertNotification
67+
68+
resource.Test(t, resource.TestCase{
69+
PreCheck: func() { testAccPreCheck(t) },
70+
Providers: testAccProviders,
71+
CheckDestroy: testAccAlertNotificationCheckDestroy(&alertNotification),
72+
Steps: []resource.TestStep{
73+
{
74+
ExpectError: regexp.MustCompile("frequency must be set when send_reminder is set to 'true'"),
75+
Config: testAccAlertNotificationConfig_reminder_no_frequency,
76+
},
77+
},
78+
})
79+
}
80+
4281
func testAccAlertNotificationCheckExists(rn string, a *gapi.AlertNotification) resource.TestCheckFunc {
4382
return func(s *terraform.State) error {
4483
rs, ok := s.RootModule().Resources[rn]
@@ -67,6 +106,16 @@ func testAccAlertNotificationCheckExists(rn string, a *gapi.AlertNotification) r
67106
}
68107
}
69108

109+
func testAccAlertNotificationDefinition(a *gapi.AlertNotification) resource.TestCheckFunc {
110+
return func(s *terraform.State) error {
111+
if !a.SendReminder {
112+
return fmt.Errorf("send_reminder is not set properly")
113+
}
114+
115+
return nil
116+
}
117+
}
118+
70119
func testAccAlertNotificationCheckDestroy(a *gapi.AlertNotification) resource.TestCheckFunc {
71120
return func(s *terraform.State) error {
72121
client := testAccProvider.Meta().(*gapi.Client)
@@ -82,6 +131,35 @@ const testAccAlertNotificationConfig_basic = `
82131
resource "grafana_alert_notification" "test" {
83132
type = "email"
84133
name = "terraform-acc-test"
134+
send_reminder = true
135+
frequency = "12h"
136+
settings = {
137+
"addresses" = "[email protected]"
138+
"uploadImage" = "false"
139+
"autoResolve" = "true"
140+
}
141+
}
142+
`
143+
144+
const testAccAlertNotificationConfig_invalid_frequency = `
145+
resource "grafana_alert_notification" "test" {
146+
type = "email"
147+
name = "terraform-acc-test"
148+
send_reminder = true
149+
frequency = "hi"
150+
settings = {
151+
"addresses" = "[email protected]"
152+
"uploadImage" = "false"
153+
"autoResolve" = "true"
154+
}
155+
}
156+
`
157+
158+
const testAccAlertNotificationConfig_reminder_no_frequency = `
159+
resource "grafana_alert_notification" "test" {
160+
type = "email"
161+
name = "terraform-acc-test"
162+
send_reminder = true
85163
settings = {
86164
"addresses" = "[email protected]"
87165
"uploadImage" = "false"

website/docs/r/alert_notification.html.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ resource "grafana_alert_notification" "email_someteam" {
1717
name = "Email that team"
1818
type = "email"
1919
is_default = false
20+
send_reminder = true
21+
frequency = "24h"
2022
2123
settings {
2224
@@ -32,6 +34,8 @@ The following arguments are supported:
3234
* `name` - (Required) The name of the alert notification channel.
3335
* `type` - (Required) The type of the alert notification channel.
3436
* `is_default` - (Optional) Is this the default channel for all your alerts.
37+
* `send_reminder` - (Optional) Whether to send reminders for triggered alerts.
38+
* `frequency` - (Optional) Frequency of alert reminders. Frequency must be set if reminders are enabled.
3539
* `settings` - (Optional) Additional settings, for full reference lookup [Grafana HTTP API documentation](http://docs.grafana.org/http_api/alerting).
3640

3741
**Note:** In `settings` the strings `"true"` and `"false"` are mapped to boolean `true` and `false` when sent to Grafana.

0 commit comments

Comments
 (0)