Skip to content

Commit 820cfb6

Browse files
committed
Implemented alert_notification management
1 parent 2c1b68b commit 820cfb6

File tree

10 files changed

+402
-9
lines changed

10 files changed

+402
-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,
+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package grafana
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
7+
"github.com/hashicorp/terraform/helper/schema"
8+
9+
gapi "github.com/apparentlymart/go-grafana-api"
10+
)
11+
12+
func ResourceAlertNotification() *schema.Resource {
13+
return &schema.Resource{
14+
Create: CreateAlertNotification,
15+
Update: UpdateAlertNotification,
16+
Delete: DeleteAlertNotification,
17+
Read: ReadAlertNotification,
18+
19+
Schema: map[string]*schema.Schema{
20+
"id": &schema.Schema{
21+
Type: schema.TypeString,
22+
Computed: true,
23+
},
24+
25+
"type": &schema.Schema{
26+
Type: schema.TypeString,
27+
Required: true,
28+
},
29+
30+
"name": &schema.Schema{
31+
Type: schema.TypeString,
32+
Required: true,
33+
},
34+
35+
"is_default": &schema.Schema{
36+
Type: schema.TypeBool,
37+
Optional: true,
38+
Default: false,
39+
},
40+
41+
"settings": {
42+
Type: schema.TypeMap,
43+
Optional: true,
44+
},
45+
},
46+
}
47+
}
48+
49+
func CreateAlertNotification(a *schema.ResourceData, meta interface{}) error {
50+
client := meta.(*gapi.Client)
51+
52+
alertNotification, err := makeAlertNotification(a)
53+
if err != nil {
54+
return err
55+
}
56+
57+
id, err := client.NewAlertNotification(alertNotification)
58+
if err != nil {
59+
return err
60+
}
61+
62+
a.SetId(strconv.FormatInt(id, 10))
63+
64+
return ReadAlertNotification(a, meta)
65+
}
66+
67+
func UpdateAlertNotification(a *schema.ResourceData, meta interface{}) error {
68+
client := meta.(*gapi.Client)
69+
70+
alertNotification, err := makeAlertNotification(a)
71+
if err != nil {
72+
return err
73+
}
74+
75+
return client.UpdateAlertNotification(alertNotification)
76+
}
77+
78+
func ReadAlertNotification(a *schema.ResourceData, meta interface{}) error {
79+
client := meta.(*gapi.Client)
80+
81+
idStr := a.Id()
82+
id, err := strconv.ParseInt(idStr, 10, 64)
83+
if err != nil {
84+
return fmt.Errorf("Invalid id: %#v", idStr)
85+
}
86+
87+
alertNotification, err := client.AlertNotification(id)
88+
if err != nil {
89+
return err
90+
}
91+
92+
a.Set("id", alertNotification.Id)
93+
a.Set("is_default", alertNotification.IsDefault)
94+
a.Set("name", alertNotification.Name)
95+
a.Set("type", alertNotification.Type)
96+
a.Set("settings", alertNotification.Settings)
97+
98+
return nil
99+
}
100+
101+
func DeleteAlertNotification(a *schema.ResourceData, meta interface{}) error {
102+
client := meta.(*gapi.Client)
103+
104+
idStr := a.Id()
105+
id, err := strconv.ParseInt(idStr, 10, 64)
106+
if err != nil {
107+
return fmt.Errorf("Invalid id: %#v", idStr)
108+
}
109+
110+
return client.DeleteAlertNotification(id)
111+
}
112+
113+
func makeAlertNotification(a *schema.ResourceData) (*gapi.AlertNotification, error) {
114+
idStr := a.Id()
115+
var id int64
116+
var err error
117+
if idStr != "" {
118+
id, err = strconv.ParseInt(idStr, 10, 64)
119+
}
120+
121+
return &gapi.AlertNotification{
122+
Id: id,
123+
Name: a.Get("name").(string),
124+
Type: a.Get("type").(string),
125+
IsDefault: a.Get("is_default").(bool),
126+
Settings: a.Get("settings").(interface{}),
127+
}, err
128+
}
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)