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

Allow alert notification reminder to be turned on #94

Merged
merged 3 commits into from
Jun 2, 2020
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
43 changes: 38 additions & 5 deletions grafana/resource_alert_notification.go
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
78 changes: 78 additions & 0 deletions grafana/resource_alert_notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected]",
),
Expand All @@ -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]
Expand Down Expand Up @@ -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)
Expand All @@ -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" = "[email protected]"
"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" = "[email protected]"
"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" = "[email protected]"
"uploadImage" = "false"
Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/alert_notification.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected];[email protected]"
Expand All @@ -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.
Expand Down