Skip to content

Commit 99dfec4

Browse files
authored
Merge pull request #3 from mvisonneau/added_alert_notification
Implemented alert_notification management
2 parents 2c1b68b + d1f008e commit 99dfec4

File tree

10 files changed

+401
-9
lines changed

10 files changed

+401
-9
lines changed

grafana/provider.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ func Provider() terraform.ResourceProvider {
2525
},
2626

2727
ResourcesMap: map[string]*schema.Resource{
28-
"grafana_dashboard": ResourceDashboard(),
29-
"grafana_data_source": ResourceDataSource(),
28+
"grafana_alert_notification": ResourceAlertNotification(),
29+
"grafana_dashboard": ResourceDashboard(),
30+
"grafana_data_source": ResourceDataSource(),
3031
},
3132

3233
ConfigureFunc: providerConfigure,
+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package grafana
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
7+
gapi "github.com/apparentlymart/go-grafana-api"
8+
"github.com/hashicorp/terraform/helper/schema"
9+
)
10+
11+
func ResourceAlertNotification() *schema.Resource {
12+
return &schema.Resource{
13+
Create: CreateAlertNotification,
14+
Update: UpdateAlertNotification,
15+
Delete: DeleteAlertNotification,
16+
Read: ReadAlertNotification,
17+
18+
Schema: map[string]*schema.Schema{
19+
"id": &schema.Schema{
20+
Type: schema.TypeString,
21+
Computed: true,
22+
},
23+
24+
"type": &schema.Schema{
25+
Type: schema.TypeString,
26+
Required: true,
27+
},
28+
29+
"name": &schema.Schema{
30+
Type: schema.TypeString,
31+
Required: true,
32+
},
33+
34+
"is_default": &schema.Schema{
35+
Type: schema.TypeBool,
36+
Optional: true,
37+
Default: false,
38+
},
39+
40+
"settings": {
41+
Type: schema.TypeMap,
42+
Optional: true,
43+
},
44+
},
45+
}
46+
}
47+
48+
func CreateAlertNotification(d *schema.ResourceData, meta interface{}) error {
49+
client := meta.(*gapi.Client)
50+
51+
alertNotification, err := makeAlertNotification(d)
52+
if err != nil {
53+
return err
54+
}
55+
56+
id, err := client.NewAlertNotification(alertNotification)
57+
if err != nil {
58+
return err
59+
}
60+
61+
d.SetId(strconv.FormatInt(id, 10))
62+
63+
return ReadAlertNotification(d, meta)
64+
}
65+
66+
func UpdateAlertNotification(d *schema.ResourceData, meta interface{}) error {
67+
client := meta.(*gapi.Client)
68+
69+
alertNotification, err := makeAlertNotification(d)
70+
if err != nil {
71+
return err
72+
}
73+
74+
return client.UpdateAlertNotification(alertNotification)
75+
}
76+
77+
func ReadAlertNotification(d *schema.ResourceData, meta interface{}) error {
78+
client := meta.(*gapi.Client)
79+
80+
idStr := d.Id()
81+
id, err := strconv.ParseInt(idStr, 10, 64)
82+
if err != nil {
83+
return fmt.Errorf("Invalid id: %#v", idStr)
84+
}
85+
86+
alertNotification, err := client.AlertNotification(id)
87+
if err != nil {
88+
return err
89+
}
90+
91+
d.Set("id", alertNotification.Id)
92+
d.Set("is_default", alertNotification.IsDefault)
93+
d.Set("name", alertNotification.Name)
94+
d.Set("type", alertNotification.Type)
95+
d.Set("settings", alertNotification.Settings)
96+
97+
return nil
98+
}
99+
100+
func DeleteAlertNotification(d *schema.ResourceData, meta interface{}) error {
101+
client := meta.(*gapi.Client)
102+
103+
idStr := d.Id()
104+
id, err := strconv.ParseInt(idStr, 10, 64)
105+
if err != nil {
106+
return fmt.Errorf("Invalid id: %#v", idStr)
107+
}
108+
109+
return client.DeleteAlertNotification(id)
110+
}
111+
112+
func makeAlertNotification(d *schema.ResourceData) (*gapi.AlertNotification, error) {
113+
idStr := d.Id()
114+
var id int64
115+
var err error
116+
if idStr != "" {
117+
id, err = strconv.ParseInt(idStr, 10, 64)
118+
}
119+
120+
return &gapi.AlertNotification{
121+
Id: id,
122+
Name: d.Get("name").(string),
123+
Type: d.Get("type").(string),
124+
IsDefault: d.Get("is_default").(bool),
125+
Settings: d.Get("settings").(interface{}),
126+
}, err
127+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package grafana
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strconv"
7+
"testing"
8+
9+
gapi "github.com/apparentlymart/go-grafana-api"
10+
11+
"github.com/hashicorp/terraform/helper/resource"
12+
"github.com/hashicorp/terraform/terraform"
13+
)
14+
15+
func TestAccAlertNotification_basic(t *testing.T) {
16+
var alertNotification gapi.AlertNotification
17+
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() { testAccPreCheck(t) },
20+
Providers: testAccProviders,
21+
CheckDestroy: testAccAlertNotificationCheckDestroy(&alertNotification),
22+
Steps: []resource.TestStep{
23+
resource.TestStep{
24+
Config: testAccAlertNotificationConfig_basic,
25+
Check: resource.ComposeTestCheckFunc(
26+
testAccAlertNotificationCheckExists("grafana_alert_notification.test", &alertNotification),
27+
resource.TestCheckResourceAttr(
28+
"grafana_alert_notification.test", "type", "email",
29+
),
30+
resource.TestMatchResourceAttr(
31+
"grafana_alert_notification.test", "id", regexp.MustCompile(`\d+`),
32+
),
33+
),
34+
},
35+
},
36+
})
37+
}
38+
39+
func testAccAlertNotificationCheckExists(rn string, a *gapi.AlertNotification) resource.TestCheckFunc {
40+
return func(s *terraform.State) error {
41+
rs, ok := s.RootModule().Resources[rn]
42+
if !ok {
43+
return fmt.Errorf("resource not found: %s", rn)
44+
}
45+
46+
if rs.Primary.ID == "" {
47+
return fmt.Errorf("resource id not set")
48+
}
49+
50+
id, err := strconv.ParseInt(rs.Primary.ID, 10, 64)
51+
if err != nil {
52+
return fmt.Errorf("resource id is malformed")
53+
}
54+
55+
client := testAccProvider.Meta().(*gapi.Client)
56+
gotAlertNotification, err := client.AlertNotification(id)
57+
if err != nil {
58+
return fmt.Errorf("error getting data source: %s", err)
59+
}
60+
61+
*a = *gotAlertNotification
62+
63+
return nil
64+
}
65+
}
66+
67+
func testAccAlertNotificationCheckDestroy(a *gapi.AlertNotification) resource.TestCheckFunc {
68+
return func(s *terraform.State) error {
69+
client := testAccProvider.Meta().(*gapi.Client)
70+
_, err := client.AlertNotification(a.Id)
71+
if err == nil {
72+
return fmt.Errorf("alert-notification still exists")
73+
}
74+
return nil
75+
}
76+
}
77+
78+
const testAccAlertNotificationConfig_basic = `
79+
resource "grafana_alert_notification" "test" {
80+
type = "email"
81+
name = "terraform-acc-test"
82+
settings {
83+
"addresses" = "[email protected]"
84+
"uploadImage" = "false"
85+
}
86+
}
87+
`

vendor/github.com/apparentlymart/go-grafana-api/alertnotification.go

+112
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/vendor.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
"revisionTime": "2017-04-18T07:21:50Z"
2727
},
2828
{
29-
"checksumSHA1": "yicZ9OtLcy3iCgraWO015yeoO5E=",
29+
"checksumSHA1": "7pePgJHRok7HkNUzZqY9/wY72QQ=",
3030
"path": "github.com/apparentlymart/go-grafana-api",
31-
"revision": "d49f95c81c580a4e7a15244b9b12dce8f60750f4",
32-
"revisionTime": "2015-10-12T02:03:32Z"
31+
"revision": "086eb9b364680893a53f41fe6173a909ebd22e1d",
32+
"revisionTime": "2017-08-15T16:18:14Z"
3333
},
3434
{
3535
"checksumSHA1": "+2yCNqbcf7VcavAptooQReTGiHY=",

website/docs/index.html.markdown

+21
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,25 @@ provider "grafana" {
3535
resource "grafana_dashboard" "metrics" {
3636
config_json = "${file("grafana-dashboard.json")}"
3737
}
38+
39+
resource "grafana_data_source" "influxdb" {
40+
type = "influxdb"
41+
name = "test_influxdb"
42+
url = "http://influxdb.example.net:8086/"
43+
username = "foo"
44+
password = "bar"
45+
database_name = "mydb"
46+
}
47+
48+
resource "grafana_alert_notification" "slack" {
49+
name = "My Slack"
50+
type = "slack"
51+
52+
settings {
53+
"slack" = "https://myteam.slack.com/hoook"
54+
"recipient" = "@someguy"
55+
"uploadImage" = "false"
56+
}
57+
}
58+
3859
```

0 commit comments

Comments
 (0)