forked from nytm/go-grafana-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
alertnotification.go
89 lines (74 loc) · 2.17 KB
/
alertnotification.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package gapi
import (
"fmt"
)
// AlertNotification represents a Grafana alert notification
type AlertNotification struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name"`
Type string `json:"type"`
IsDefault bool `json:"isDefault"`
Settings interface{} `json:"settings"`
}
// AlertNotifications returns all notifications for the actual/current org
func (c *Client) AlertNotifications() ([]*AlertNotification, error) {
result := []*AlertNotification{}
res, err := c.doRequest("GET", "/api/alert-notifications", nil)
if err != nil {
return nil, err
}
if !res.OK() {
return result, res.Error()
}
err = res.BindJSON(&result)
return result, err
}
// AlertNotification gets the alert with the given ID from Grafana
func (c *Client) AlertNotification(id int64) (*AlertNotification, error) {
result := &AlertNotification{}
path := fmt.Sprintf("/api/alert-notifications/%d", id)
res, err := c.doRequest("GET", path, nil)
if err != nil {
return nil, err
}
if !res.OK() {
return result, res.Error()
}
err = res.BindJSON(&result)
return result, err
}
// NewAlertNotification creates the given alert notification object in Grafana
func (c *Client) NewAlertNotification(a *AlertNotification) (int64, error) {
res, err := c.doJSONRequest("POST", "/api/alert-notifications", a)
if err != nil {
return 0, err
}
if !res.OK() {
return 0, res.Error()
}
result := struct {
ID int64 `json:"id"`
}{}
err = res.BindJSON(&result)
return result.ID, err
}
// UpdateAlertNotification wll update the alert notification in Grafana that matches
// the ID from the given alert notification object
func (c *Client) UpdateAlertNotification(a *AlertNotification) error {
path := fmt.Sprintf("/api/alert-notifications/%d", a.ID)
res, err := c.doJSONRequest("PUT", path, a)
if err != nil {
return err
}
return res.Error()
}
// DeleteAlertNotification will delete the alert notification from Grafana
// matching the given ID
func (c *Client) DeleteAlertNotification(id int64) error {
path := fmt.Sprintf("/api/alert-notifications/%d", id)
res, err := c.doRequest("DELETE", path, nil)
if err != nil {
return err
}
return res.Error()
}