diff --git a/grafana/resource_alert_notification.go b/grafana/resource_alert_notification.go index efbabd89a..52e84dba1 100644 --- a/grafana/resource_alert_notification.go +++ b/grafana/resource_alert_notification.go @@ -1,14 +1,20 @@ package grafana import ( + "errors" "fmt" "log" "strconv" + "time" "github.com/hashicorp/terraform/helper/schema" gapi "github.com/nytm/go-grafana-api" ) +var ( + ErrFrequencyMustBeSet = errors.New("frequency must be set when send_reminder is set to 'true'") +) + func ResourceAlertNotification() *schema.Resource { return &schema.Resource{ Create: CreateAlertNotification, @@ -33,6 +39,18 @@ func ResourceAlertNotification() *schema.Resource { Default: false, }, + "send_reminder": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "frequency": { + Type: schema.TypeString, + Optional: true, + Default: "", + }, + "settings": { Type: schema.TypeMap, Optional: true, @@ -143,11 +161,26 @@ func makeAlertNotification(d *schema.ResourceData) (*gapi.AlertNotification, err } } + sendReminder := d.Get("send_reminder").(bool) + frequency := d.Get("frequency").(string) + + if sendReminder { + if frequency == "" { + return nil, ErrFrequencyMustBeSet + } + + if _, err := time.ParseDuration(frequency); err != nil { + return nil, err + } + } + return &gapi.AlertNotification{ - Id: id, - Name: d.Get("name").(string), - Type: d.Get("type").(string), - IsDefault: d.Get("is_default").(bool), - Settings: settings, + Id: id, + Name: d.Get("name").(string), + Type: d.Get("type").(string), + IsDefault: d.Get("is_default").(bool), + SendReminder: sendReminder, + Frequency: frequency, + Settings: settings, }, err } diff --git a/grafana/resource_alert_notification_test.go b/grafana/resource_alert_notification_test.go index 8af373a4b..9e34caa63 100644 --- a/grafana/resource_alert_notification_test.go +++ b/grafana/resource_alert_notification_test.go @@ -24,12 +24,19 @@ func TestAccAlertNotification_basic(t *testing.T) { Config: testAccAlertNotificationConfig_basic, Check: resource.ComposeTestCheckFunc( testAccAlertNotificationCheckExists("grafana_alert_notification.test", &alertNotification), + testAccAlertNotificationDefinition(&alertNotification), resource.TestCheckResourceAttr( "grafana_alert_notification.test", "type", "email", ), resource.TestMatchResourceAttr( "grafana_alert_notification.test", "id", regexp.MustCompile(`\d+`), ), + resource.TestCheckResourceAttr( + "grafana_alert_notification.test", "send_reminder", "true", + ), + resource.TestCheckResourceAttr( + "grafana_alert_notification.test", "frequency", "12h", + ), resource.TestCheckResourceAttr( "grafana_alert_notification.test", "settings.addresses", "foo@bar.test", ), @@ -39,6 +46,38 @@ func TestAccAlertNotification_basic(t *testing.T) { }) } +func TestAccAlertNotification_invalid_frequence(t *testing.T) { + var alertNotification gapi.AlertNotification + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccAlertNotificationCheckDestroy(&alertNotification), + Steps: []resource.TestStep{ + { + ExpectError: regexp.MustCompile("invalid duration hi"), + Config: testAccAlertNotificationConfig_invalid_frequency, + }, + }, + }) +} + +func TestAccAlertNotification_reminder_no_frequence(t *testing.T) { + var alertNotification gapi.AlertNotification + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccAlertNotificationCheckDestroy(&alertNotification), + Steps: []resource.TestStep{ + { + ExpectError: regexp.MustCompile("frequency must be set when send_reminder is set to 'true'"), + Config: testAccAlertNotificationConfig_reminder_no_frequency, + }, + }, + }) +} + func testAccAlertNotificationCheckExists(rn string, a *gapi.AlertNotification) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[rn] @@ -67,6 +106,16 @@ func testAccAlertNotificationCheckExists(rn string, a *gapi.AlertNotification) r } } +func testAccAlertNotificationDefinition(a *gapi.AlertNotification) resource.TestCheckFunc { + return func(s *terraform.State) error { + if !a.SendReminder { + return fmt.Errorf("send_reminder is not set properly") + } + + return nil + } +} + func testAccAlertNotificationCheckDestroy(a *gapi.AlertNotification) resource.TestCheckFunc { return func(s *terraform.State) error { client := testAccProvider.Meta().(*gapi.Client) @@ -82,6 +131,35 @@ const testAccAlertNotificationConfig_basic = ` resource "grafana_alert_notification" "test" { type = "email" name = "terraform-acc-test" + send_reminder = true + frequency = "12h" + settings = { + "addresses" = "foo@bar.test" + "uploadImage" = "false" + "autoResolve" = "true" + } +} +` + +const testAccAlertNotificationConfig_invalid_frequency = ` +resource "grafana_alert_notification" "test" { + type = "email" + name = "terraform-acc-test" + send_reminder = true + frequency = "hi" + settings = { + "addresses" = "foo@bar.test" + "uploadImage" = "false" + "autoResolve" = "true" + } +} +` + +const testAccAlertNotificationConfig_reminder_no_frequency = ` +resource "grafana_alert_notification" "test" { + type = "email" + name = "terraform-acc-test" + send_reminder = true settings = { "addresses" = "foo@bar.test" "uploadImage" = "false" diff --git a/website/docs/r/alert_notification.html.md b/website/docs/r/alert_notification.html.md index 44dda2725..1d37eac24 100644 --- a/website/docs/r/alert_notification.html.md +++ b/website/docs/r/alert_notification.html.md @@ -17,6 +17,8 @@ resource "grafana_alert_notification" "email_someteam" { name = "Email that team" type = "email" is_default = false + send_reminder = true + frequency = "24h" settings { addresses = "foo@example.net;bar@example.net" @@ -32,6 +34,8 @@ The following arguments are supported: * `name` - (Required) The name of the alert notification channel. * `type` - (Required) The type of the alert notification channel. * `is_default` - (Optional) Is this the default channel for all your alerts. +* `send_reminder` - (Optional) Whether to send reminders for triggered alerts. +* `frequency` - (Optional) Frequency of alert reminders. Frequency must be set if reminders are enabled. * `settings` - (Optional) Additional settings, for full reference lookup [Grafana HTTP API documentation](http://docs.grafana.org/http_api/alerting). **Note:** In `settings` the strings `"true"` and `"false"` are mapped to boolean `true` and `false` when sent to Grafana.