Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 204577e

Browse files
committed
add support for alerting API
This addresses issue #54. It should be noted that it builds on top of PR #51, which should probably be reviewed and, ideally, merged first, assuming PR #51 is approved.
1 parent 0f467ed commit 204577e

File tree

4 files changed

+265
-0
lines changed

4 files changed

+265
-0
lines changed

admin.go

+22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import (
66
"fmt"
77
)
88

9+
// PauseAllAlertsResponse represents the response body for a PauseAllAlerts request.
10+
type PauseAllAlertsResponse struct {
11+
AlertsAffected int64 `json:"alertsAffected,omitempty"`
12+
State string `json:"state,omitempty"`
13+
Message string `json:"message,omitempty"`
14+
}
15+
916
// CreateUser creates a Grafana user.
1017
func (c *Client) CreateUser(user User) (int64, error) {
1118
id := int64(0)
@@ -30,3 +37,18 @@ func (c *Client) CreateUser(user User) (int64, error) {
3037
func (c *Client) DeleteUser(id int64) error {
3138
return c.request("DELETE", fmt.Sprintf("/api/admin/users/%d", id), nil, nil, nil)
3239
}
40+
41+
// PauseAllAlerts pauses all Grafana alerts.
42+
func (c *Client) PauseAllAlerts() (PauseAllAlertsResponse, error) {
43+
result := PauseAllAlertsResponse{}
44+
data, err := json.Marshal(PauseAlertRequest{
45+
Paused: true,
46+
})
47+
if err != nil {
48+
return result, err
49+
}
50+
51+
err = c.request("POST", "/api/admin/pause-all-alerts", nil, bytes.NewBuffer(data), &result)
52+
53+
return result, err
54+
}

admin_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
package gapi
22

33
import (
4+
"strings"
45
"testing"
6+
7+
"github.com/gobs/pretty"
58
)
69

710
const (
811
createUserJSON = `{"id":1,"message":"User created"}`
912
deleteUserJSON = `{"message":"User deleted"}`
13+
14+
pauseAllAlertsJSON = `{
15+
"alertsAffected": 1,
16+
"state": "Paused",
17+
"message": "alert paused"
18+
}`
1019
)
1120

1221
func TestCreateUser(t *testing.T) {
@@ -37,3 +46,29 @@ func TestDeleteUser(t *testing.T) {
3746
t.Error(err)
3847
}
3948
}
49+
50+
func TestPauseAllAlerts(t *testing.T) {
51+
server, client := gapiTestTools(200, pauseAllAlertsJSON)
52+
defer server.Close()
53+
54+
res, err := client.PauseAllAlerts()
55+
if err != nil {
56+
t.Error(err)
57+
}
58+
59+
t.Log(pretty.PrettyFormat(res))
60+
61+
if res.State != "Paused" {
62+
t.Error("pause all alerts response should contain the correct response message")
63+
}
64+
}
65+
66+
func TestPauseAllAlerts_500(t *testing.T) {
67+
server, client := gapiTestTools(500, pauseAllAlertsJSON)
68+
defer server.Close()
69+
70+
_, err := client.PauseAllAlerts()
71+
if !strings.Contains(err.Error(), "status: 500") {
72+
t.Errorf("expected error to contain 'status: 500'; got: %s", err.Error())
73+
}
74+
}

alert.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package gapi
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"net/url"
8+
)
9+
10+
// Alert represents a Grafana API Alert
11+
type Alert struct {
12+
ID int64 `json:"id,omitempty"`
13+
DashboardID int64 `json:"dashboardId,omitempty"`
14+
DashboardUID string `json:"dashboardUid,omitempty"`
15+
DashboardSlug string `json:"dashboardSlug,omitempty"`
16+
PanelID int64 `json:"panelId,omitempty"`
17+
Name string `json:"name,omitempty"`
18+
State string `json:"state,omitempty"`
19+
NewStateDate string `json:"newStateDate,omitempty"`
20+
EvalDate string `json:"evalDate,omitempty"`
21+
ExecutionError string `json:"executionError,omitempty"`
22+
URL string `json:"url,omitempty"`
23+
}
24+
25+
// PauseAlertRequest represents the request payload for a PauseAlert request.
26+
type PauseAlertRequest struct {
27+
Paused bool `json:"paused"`
28+
}
29+
30+
// PauseAlertResponse represents the response body for a PauseAlert request.
31+
type PauseAlertResponse struct {
32+
AlertID int64 `json:"alertId,omitempty"`
33+
State string `json:"state,omitempty"`
34+
Message string `json:"message,omitempty"`
35+
}
36+
37+
// Alerts fetches the annotations queried with the params it's passed.
38+
func (c *Client) Alerts(params url.Values) ([]Alert, error) {
39+
result := []Alert{}
40+
err := c.request("GET", "/api/alerts", params, nil, &result)
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
return result, err
46+
}
47+
48+
// Alert fetches and returns an individual Grafana alert.
49+
func (c *Client) Alert(id int64) (Alert, error) {
50+
path := fmt.Sprintf("/api/alerts/%d", id)
51+
result := Alert{}
52+
err := c.request("GET", path, nil, nil, &result)
53+
if err != nil {
54+
return result, err
55+
}
56+
57+
return result, err
58+
}
59+
60+
// PauseAlert pauses the Grafana alert whose ID it's passed.
61+
func (c *Client) PauseAlert(id int64) (PauseAlertResponse, error) {
62+
path := fmt.Sprintf("/api/alerts/%d", id)
63+
result := PauseAlertResponse{}
64+
data, err := json.Marshal(PauseAlertRequest{
65+
Paused: true,
66+
})
67+
if err != nil {
68+
return result, err
69+
}
70+
71+
err = c.request("POST", path, nil, bytes.NewBuffer(data), &result)
72+
if err != nil {
73+
return result, err
74+
}
75+
76+
return result, err
77+
}

alert_test.go

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package gapi
2+
3+
import (
4+
"net/url"
5+
"strings"
6+
"testing"
7+
8+
"github.com/gobs/pretty"
9+
)
10+
11+
const (
12+
alertsJSON = `[{
13+
"id": 1,
14+
"dashboardId": 1,
15+
"dashboardUId": "ABcdEFghij",
16+
"dashboardSlug": "sensors",
17+
"panelId": 1,
18+
"name": "fire place sensor",
19+
"state": "alerting",
20+
"newStateDate": "2018-05-14T05:55:20+02:00",
21+
"evalDate": "0001-01-01T00:00:00Z",
22+
"evalData": null,
23+
"executionError": "",
24+
"url": "http://grafana.com/dashboard/db/sensors"
25+
}]`
26+
27+
alertJSON = `{
28+
"id": 1,
29+
"dashboardId": 1,
30+
"dashboardUId": "ABcdEFghij",
31+
"dashboardSlug": "sensors",
32+
"panelId": 1,
33+
"name": "fire place sensor",
34+
"state": "alerting",
35+
"message": "Someone is trying to break in through the fire place",
36+
"newStateDate": "2018-05-14T05:55:20+02:00",
37+
"evalDate": "0001-01-01T00:00:00Z",
38+
"executionError": "",
39+
"url": "http://grafana.com/dashboard/db/sensors"
40+
}`
41+
42+
pauseAlertJSON = `{
43+
"alertId": 1,
44+
"state": "Paused",
45+
"message": "alert paused"
46+
}`
47+
)
48+
49+
func TestAlerts(t *testing.T) {
50+
server, client := gapiTestTools(200, alertsJSON)
51+
defer server.Close()
52+
53+
params := url.Values{}
54+
params.Add("dashboardId", "123")
55+
56+
as, err := client.Alerts(params)
57+
if err != nil {
58+
t.Error(err)
59+
}
60+
61+
t.Log(pretty.PrettyFormat(as))
62+
63+
if as[0].ID != 1 {
64+
t.Error("alerts response should contain alerts with an ID")
65+
}
66+
}
67+
68+
func TestAlerts_500(t *testing.T) {
69+
server, client := gapiTestTools(500, alertsJSON)
70+
defer server.Close()
71+
72+
params := url.Values{}
73+
params.Add("dashboardId", "123")
74+
75+
_, err := client.Alerts(params)
76+
if !strings.Contains(err.Error(), "status: 500") {
77+
t.Errorf("expected error to contain 'status: 500'; got: %s", err.Error())
78+
}
79+
}
80+
81+
func TestAlert(t *testing.T) {
82+
server, client := gapiTestTools(200, alertJSON)
83+
defer server.Close()
84+
85+
res, err := client.Alert(1)
86+
if err != nil {
87+
t.Error(err)
88+
}
89+
90+
t.Log(pretty.PrettyFormat(res))
91+
92+
if res.ID != 1 {
93+
t.Error("alert response should contain the ID of the queried alert")
94+
}
95+
}
96+
97+
func TestAlert_500(t *testing.T) {
98+
server, client := gapiTestTools(500, alertJSON)
99+
defer server.Close()
100+
101+
_, err := client.Alert(1)
102+
if !strings.Contains(err.Error(), "status: 500") {
103+
t.Errorf("expected error to contain 'status: 500'; got: %s", err.Error())
104+
}
105+
}
106+
107+
func TestPauseAlert(t *testing.T) {
108+
server, client := gapiTestTools(200, pauseAlertJSON)
109+
defer server.Close()
110+
111+
res, err := client.PauseAlert(1)
112+
if err != nil {
113+
t.Error(err)
114+
}
115+
116+
t.Log(pretty.PrettyFormat(res))
117+
118+
if res.State != "Paused" {
119+
t.Error("pause alert response should contain the correct response message")
120+
}
121+
}
122+
123+
func TestPauseAlert_500(t *testing.T) {
124+
server, client := gapiTestTools(500, pauseAlertJSON)
125+
defer server.Close()
126+
127+
_, err := client.PauseAlert(1)
128+
if !strings.Contains(err.Error(), "status: 500") {
129+
t.Errorf("expected error to contain 'status: 500'; got: %s", err.Error())
130+
}
131+
}

0 commit comments

Comments
 (0)