Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit 631a2ac

Browse files
authored
Merge pull request #86 from grafana/alexweav/alerting-contact-points
Alerting: Add support for Unified Alerting contact points, deprecate legacy notifiers
2 parents c08b640 + 79d5345 commit 631a2ac

File tree

3 files changed

+227
-0
lines changed

3 files changed

+227
-0
lines changed

alerting_contact_point.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package gapi
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
)
8+
9+
// ContactPoint represents a Grafana Alerting contact point.
10+
type ContactPoint struct {
11+
UID string `json:"uid"`
12+
Name string `json:"name"`
13+
Type string `json:"type"`
14+
Settings map[string]interface{} `json:"settings"`
15+
DisableResolveMessage bool `json:"disableResolveMessage"`
16+
Provenance string `json:"provenance"`
17+
}
18+
19+
// ContactPoints fetches all contact points.
20+
func (c *Client) ContactPoints() ([]ContactPoint, error) {
21+
ps := make([]ContactPoint, 0)
22+
err := c.request("GET", "/api/v1/provisioning/contact-points", nil, nil, &ps)
23+
if err != nil {
24+
return nil, err
25+
}
26+
return ps, nil
27+
}
28+
29+
// ContactPoint fetches a single contact point, identified by its UID.
30+
func (c *Client) ContactPoint(uid string) (ContactPoint, error) {
31+
ps, err := c.ContactPoints()
32+
if err != nil {
33+
return ContactPoint{}, err
34+
}
35+
36+
for _, p := range ps {
37+
if p.UID == uid {
38+
return p, nil
39+
}
40+
}
41+
return ContactPoint{}, fmt.Errorf("contact point with uid %s not found", uid)
42+
}
43+
44+
// NewContactPoint creates a new contact point.
45+
func (c *Client) NewContactPoint(p *ContactPoint) (string, error) {
46+
req, err := json.Marshal(p)
47+
if err != nil {
48+
return "", err
49+
}
50+
result := ContactPoint{}
51+
52+
err = c.request("POST", "/api/v1/provisioning/contact-points", nil, bytes.NewBuffer(req), &result)
53+
if err != nil {
54+
return "", err
55+
}
56+
return result.UID, nil
57+
}
58+
59+
// UpdateContactPoint replaces a contact point, identified by contact point's UID.
60+
func (c *Client) UpdateContactPoint(p *ContactPoint) error {
61+
uri := fmt.Sprintf("/api/v1/provisioning/contact-points/%s", p.UID)
62+
req, err := json.Marshal(p)
63+
if err != nil {
64+
return err
65+
}
66+
67+
return c.request("PUT", uri, nil, bytes.NewBuffer(req), nil)
68+
}
69+
70+
// DeleteContactPoint deletes a contact point.
71+
func (c *Client) DeleteContactPoint(uid string) error {
72+
uri := fmt.Sprintf("/api/v1/provisioning/contact-points/%s", uid)
73+
return c.request("DELETE", uri, nil, nil, nil)
74+
}

alerting_contact_point_test.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package gapi
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gobs/pretty"
7+
)
8+
9+
func TestContactPoints(t *testing.T) {
10+
t.Run("get contact points succeeds", func(t *testing.T) {
11+
server, client := gapiTestTools(t, 200, getContactPointsJSON)
12+
defer server.Close()
13+
14+
ps, err := client.ContactPoints()
15+
16+
if err != nil {
17+
t.Error(err)
18+
}
19+
t.Log(pretty.PrettyFormat(ps))
20+
if len(ps) != 2 {
21+
t.Errorf("wrong number of contact points returned, got %#v", ps)
22+
}
23+
if ps[0].UID != "" {
24+
t.Errorf("incorrect UID - expected %s on element %d, got %#v", "", 0, ps)
25+
}
26+
if ps[1].UID != "rc5r0bjnz" {
27+
t.Errorf("incorrect UID - expected %s on element %d, got %#v", "rc5r0bjnz", 0, ps)
28+
}
29+
})
30+
31+
t.Run("get contact point succeeds", func(t *testing.T) {
32+
server, client := gapiTestTools(t, 200, getContactPointsJSON)
33+
defer server.Close()
34+
35+
p, err := client.ContactPoint("rc5r0bjnz")
36+
37+
if err != nil {
38+
t.Error(err)
39+
}
40+
t.Log(pretty.PrettyFormat(p))
41+
if p.UID != "rc5r0bjnz" {
42+
t.Errorf("incorrect UID - expected %s got %#v", "rc5r0bjnz", p)
43+
}
44+
})
45+
46+
t.Run("get non-existent contact point fails", func(t *testing.T) {
47+
server, client := gapiTestTools(t, 200, getContactPointsJSON)
48+
defer server.Close()
49+
50+
p, err := client.ContactPoint("does not exist")
51+
52+
if err == nil {
53+
t.Errorf("expected error but got nil")
54+
t.Log(pretty.PrettyFormat(p))
55+
}
56+
})
57+
58+
t.Run("create contact point succeeds", func(t *testing.T) {
59+
server, client := gapiTestTools(t, 201, writeContactPointJSON)
60+
defer server.Close()
61+
p := createContactPoint()
62+
63+
uid, err := client.NewContactPoint(&p)
64+
65+
if err != nil {
66+
t.Error(err)
67+
}
68+
if uid != "rc5r0bjnz" {
69+
t.Errorf("unexpected UID returned, got %s", uid)
70+
}
71+
})
72+
73+
t.Run("update contact point succeeds", func(t *testing.T) {
74+
server, client := gapiTestTools(t, 200, writeContactPointJSON)
75+
defer server.Close()
76+
p := createContactPoint()
77+
p.UID = "on7otbj7k"
78+
79+
err := client.UpdateContactPoint(&p)
80+
81+
if err != nil {
82+
t.Error(err)
83+
}
84+
})
85+
86+
t.Run("delete contact point succeeds", func(t *testing.T) {
87+
server, client := gapiTestTools(t, 204, "")
88+
defer server.Close()
89+
90+
err := client.DeleteContactPoint("rc5r0bjnz")
91+
92+
if err != nil {
93+
t.Error(err)
94+
}
95+
})
96+
}
97+
98+
func createContactPoint() ContactPoint {
99+
return ContactPoint{
100+
Name: "slack-receiver-123",
101+
Type: "slack",
102+
DisableResolveMessage: false,
103+
Settings: map[string]interface{}{
104+
"recipient": "@zxcv",
105+
"token": "test-token",
106+
"url": "https://test-url",
107+
},
108+
}
109+
}
110+
111+
const getContactPointsJSON = `
112+
[
113+
{
114+
"uid": "",
115+
"name": "default-email-receiver",
116+
"type": "email",
117+
"disableResolveMessage": false,
118+
"settings": {
119+
"addresses": "<example@email.com>"
120+
}
121+
},
122+
{
123+
"uid": "rc5r0bjnz",
124+
"name": "slack-receiver-1",
125+
"type": "slack",
126+
"disableResolveMessage": false,
127+
"settings": {
128+
"recipient": "@foo",
129+
"token": "[REDACTED]",
130+
"url": "[REDACTED]"
131+
}
132+
}
133+
]`
134+
135+
const writeContactPointJSON = `
136+
{
137+
"uid": "rc5r0bjnz",
138+
"name": "slack-receiver-1",
139+
"type": "slack",
140+
"disableResolveMessage": false,
141+
"settings": {
142+
"recipient": "@foo",
143+
"token": "[REDACTED]",
144+
"url": "[REDACTED]"
145+
}
146+
}
147+
`

alertnotification.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
)
88

99
// AlertNotification represents a Grafana alert notification.
10+
// Deprecated: Grafana Legacy Alerting is deprecated as of 9.0 and will be removed in the future. Use ContactPoint instead.
1011
type AlertNotification struct {
1112
ID int64 `json:"id,omitempty"`
1213
UID string `json:"uid"`
@@ -22,6 +23,7 @@ type AlertNotification struct {
2223
}
2324

2425
// AlertNotifications fetches and returns Grafana alert notifications.
26+
// Deprecated: Grafana Legacy Alerting is deprecated as of 9.0 and will be removed in the future. Use ContactPoints instead.
2527
func (c *Client) AlertNotifications() ([]AlertNotification, error) {
2628
alertnotifications := make([]AlertNotification, 0)
2729

@@ -34,6 +36,7 @@ func (c *Client) AlertNotifications() ([]AlertNotification, error) {
3436
}
3537

3638
// AlertNotification fetches and returns a Grafana alert notification.
39+
// Deprecated: Grafana Legacy Alerting is deprecated as of 9.0 and will be removed in the future. Use ContactPoint instead.
3740
func (c *Client) AlertNotification(id int64) (*AlertNotification, error) {
3841
path := fmt.Sprintf("/api/alert-notifications/%d", id)
3942
result := &AlertNotification{}
@@ -46,6 +49,7 @@ func (c *Client) AlertNotification(id int64) (*AlertNotification, error) {
4649
}
4750

4851
// NewAlertNotification creates a new Grafana alert notification.
52+
// Deprecated: Grafana Legacy Alerting is deprecated as of 9.0 and will be removed in the future. Use NewContactPoint instead.
4953
func (c *Client) NewAlertNotification(a *AlertNotification) (int64, error) {
5054
data, err := json.Marshal(a)
5155
if err != nil {
@@ -64,6 +68,7 @@ func (c *Client) NewAlertNotification(a *AlertNotification) (int64, error) {
6468
}
6569

6670
// UpdateAlertNotification updates a Grafana alert notification.
71+
// Deprecated: Grafana Legacy Alerting is deprecated as of 9.0 and will be removed in the future. Use UpdateContactPoint instead.
6772
func (c *Client) UpdateAlertNotification(a *AlertNotification) error {
6873
path := fmt.Sprintf("/api/alert-notifications/%d", a.ID)
6974
data, err := json.Marshal(a)
@@ -76,6 +81,7 @@ func (c *Client) UpdateAlertNotification(a *AlertNotification) error {
7681
}
7782

7883
// DeleteAlertNotification deletes a Grafana alert notification.
84+
// Deprecated: Grafana Legacy Alerting is deprecated as of 9.0 and will be removed in the future. Use DeleteContactPoint instead.
7985
func (c *Client) DeleteAlertNotification(id int64) error {
8086
path := fmt.Sprintf("/api/alert-notifications/%d", id)
8187

0 commit comments

Comments
 (0)