Skip to content

Commit 663fef1

Browse files
committed
Implemented alert_notification management
1 parent 2c1b68b commit 663fef1

8 files changed

+287
-6
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+
`

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
```
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
layout: "grafana"
3+
page_title: "Grafana: grafana_alert_notification"
4+
sidebar_current: "docs-grafana-alert-notification"
5+
description: |-
6+
The grafana_alert_notification resource allows a Grafana Alert Notification channel to be created.
7+
---
8+
9+
# grafana\_alert\_notification
10+
11+
The alert notification resource allows an alert notification channel to be created on a Grafana server.
12+
13+
## Example Usage
14+
15+
```hcl
16+
resource "grafana_alert_notification" "email_someteam" {
17+
name = "Email that team"
18+
type = "email"
19+
is_default = false
20+
21+
settings {
22+
23+
"uploadImage" = "false"
24+
}
25+
}
26+
```
27+
28+
## Argument Reference
29+
30+
The following arguments are supported:
31+
32+
* `name` - (Required) The name of the alert notification channel.
33+
* `type` - (Required) The type of the alert notification channel.
34+
* `is_default` - (Optional) Is this the default channel for all your alerts.
35+
* `settings` - (Optional) Additional settings, for full reference lookup [Grafana HTTP API documentation](http://docs.grafana.org/http_api/alerting).
36+
37+
## Attributes Reference
38+
39+
The resource exports the following attributes:
40+
41+
* `id` - The ID of the resource

website/docs/r/dashboard.html.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
layout: "grafana"
33
page_title: "Grafana: grafana_dashboard"
4-
sidebar_current: "docs-influxdb-resource-dashboard"
4+
sidebar_current: "docs-grafana-resource-dashboard"
55
description: |-
66
The grafana_dashboard resource allows a Grafana dashboard to be created.
77
---
88

99
# grafana\_dashboard
1010

11-
The dashboard resource allows a dashboard to created on a Grafana server.
11+
The dashboard resource allows a dashboard to be created on a Grafana server.
1212

1313
## Example Usage
1414

website/docs/r/data_source.html.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
layout: "grafana"
33
page_title: "Grafana: grafana_data_source"
4-
sidebar_current: "docs-influxdb-resource-data-source"
4+
sidebar_current: "docs-grafana-resource-data-source"
55
description: |-
66
The grafana_data_source resource allows a Grafana data source to be created.
77
---
88

99
# grafana\_data\_source
1010

11-
The data source resource allows a data source to created on a Grafana server.
11+
The data source resource allows a data source to be created on a Grafana server.
1212

1313
## Example Usage
1414

website/grafana.erb

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<li<%= sidebar_current("docs-grafana-resource") %>>
1414
<a href="#">Resources</a>
1515
<ul class="nav nav-visible">
16+
<li<%= sidebar_current("docs-grafana-alert-notification") %>>
17+
<a href="/docs/providers/grafana/r/alert_notification.html">grafana_alert_notification</a>
18+
</li>
1619
<li<%= sidebar_current("docs-grafana-resource-dashboard") %>>
1720
<a href="/docs/providers/grafana/r/dashboard.html">grafana_dashboard</a>
1821
</li>

0 commit comments

Comments
 (0)