Skip to content

Implemented alert_notification management #3

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

Merged
merged 1 commit into from
Aug 16, 2017
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
5 changes: 3 additions & 2 deletions grafana/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ func Provider() terraform.ResourceProvider {
},

ResourcesMap: map[string]*schema.Resource{
"grafana_dashboard": ResourceDashboard(),
"grafana_data_source": ResourceDataSource(),
"grafana_alert_notification": ResourceAlertNotification(),
"grafana_dashboard": ResourceDashboard(),
"grafana_data_source": ResourceDataSource(),
},

ConfigureFunc: providerConfigure,
Expand Down
127 changes: 127 additions & 0 deletions grafana/resource_alert_notification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package grafana

import (
"fmt"
"strconv"

gapi "github.com/apparentlymart/go-grafana-api"
"github.com/hashicorp/terraform/helper/schema"
)

func ResourceAlertNotification() *schema.Resource {
return &schema.Resource{
Create: CreateAlertNotification,
Update: UpdateAlertNotification,
Delete: DeleteAlertNotification,
Read: ReadAlertNotification,

Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},

"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},

"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},

"is_default": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"settings": {
Type: schema.TypeMap,
Optional: true,
},
},
}
}

func CreateAlertNotification(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)

alertNotification, err := makeAlertNotification(d)
if err != nil {
return err
}

id, err := client.NewAlertNotification(alertNotification)
if err != nil {
return err
}

d.SetId(strconv.FormatInt(id, 10))

return ReadAlertNotification(d, meta)
}

func UpdateAlertNotification(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)

alertNotification, err := makeAlertNotification(d)
if err != nil {
return err
}

return client.UpdateAlertNotification(alertNotification)
}

func ReadAlertNotification(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)

idStr := d.Id()
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
return fmt.Errorf("Invalid id: %#v", idStr)
}

alertNotification, err := client.AlertNotification(id)
if err != nil {
return err
}

d.Set("id", alertNotification.Id)
d.Set("is_default", alertNotification.IsDefault)
d.Set("name", alertNotification.Name)
d.Set("type", alertNotification.Type)
d.Set("settings", alertNotification.Settings)

return nil
}

func DeleteAlertNotification(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)

idStr := d.Id()
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
return fmt.Errorf("Invalid id: %#v", idStr)
}

return client.DeleteAlertNotification(id)
}

func makeAlertNotification(d *schema.ResourceData) (*gapi.AlertNotification, error) {
idStr := d.Id()
var id int64
var err error
if idStr != "" {
id, err = strconv.ParseInt(idStr, 10, 64)
}

return &gapi.AlertNotification{
Id: id,
Name: d.Get("name").(string),
Type: d.Get("type").(string),
IsDefault: d.Get("is_default").(bool),
Settings: d.Get("settings").(interface{}),
}, err
}
87 changes: 87 additions & 0 deletions grafana/resource_alert_notification_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package grafana

import (
"fmt"
"regexp"
"strconv"
"testing"

gapi "github.com/apparentlymart/go-grafana-api"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAlertNotification_basic(t *testing.T) {
var alertNotification gapi.AlertNotification

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccAlertNotificationCheckDestroy(&alertNotification),
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAlertNotificationConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccAlertNotificationCheckExists("grafana_alert_notification.test", &alertNotification),
resource.TestCheckResourceAttr(
"grafana_alert_notification.test", "type", "email",
),
resource.TestMatchResourceAttr(
"grafana_alert_notification.test", "id", regexp.MustCompile(`\d+`),
),
),
},
},
})
}

func testAccAlertNotificationCheckExists(rn string, a *gapi.AlertNotification) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[rn]
if !ok {
return fmt.Errorf("resource not found: %s", rn)
}

if rs.Primary.ID == "" {
return fmt.Errorf("resource id not set")
}

id, err := strconv.ParseInt(rs.Primary.ID, 10, 64)
if err != nil {
return fmt.Errorf("resource id is malformed")
}

client := testAccProvider.Meta().(*gapi.Client)
gotAlertNotification, err := client.AlertNotification(id)
if err != nil {
return fmt.Errorf("error getting data source: %s", err)
}

*a = *gotAlertNotification

return nil
}
}

func testAccAlertNotificationCheckDestroy(a *gapi.AlertNotification) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*gapi.Client)
_, err := client.AlertNotification(a.Id)
if err == nil {
return fmt.Errorf("alert-notification still exists")
}
return nil
}
}

const testAccAlertNotificationConfig_basic = `
resource "grafana_alert_notification" "test" {
type = "email"
name = "terraform-acc-test"
settings {
"addresses" = "[email protected]"
"uploadImage" = "false"
}
}
`

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"revisionTime": "2017-04-18T07:21:50Z"
},
{
"checksumSHA1": "yicZ9OtLcy3iCgraWO015yeoO5E=",
"checksumSHA1": "7pePgJHRok7HkNUzZqY9/wY72QQ=",
"path": "github.com/apparentlymart/go-grafana-api",
"revision": "d49f95c81c580a4e7a15244b9b12dce8f60750f4",
"revisionTime": "2015-10-12T02:03:32Z"
"revision": "086eb9b364680893a53f41fe6173a909ebd22e1d",
"revisionTime": "2017-08-15T16:18:14Z"
},
{
"checksumSHA1": "+2yCNqbcf7VcavAptooQReTGiHY=",
Expand Down
21 changes: 21 additions & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,25 @@ provider "grafana" {
resource "grafana_dashboard" "metrics" {
config_json = "${file("grafana-dashboard.json")}"
}

resource "grafana_data_source" "influxdb" {
type = "influxdb"
name = "test_influxdb"
url = "http://influxdb.example.net:8086/"
username = "foo"
password = "bar"
database_name = "mydb"
}

resource "grafana_alert_notification" "slack" {
name = "My Slack"
type = "slack"

settings {
"slack" = "https://myteam.slack.com/hoook"
"recipient" = "@someguy"
"uploadImage" = "false"
}
}

```
Loading