forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alertnotification.go
81 lines (67 loc) · 2.26 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
package gapi
import (
"bytes"
"encoding/json"
"fmt"
)
// AlertNotification represents a Grafana alert notification.
type AlertNotification struct {
Id int64 `json:"id,omitempty"`
Uid string `json:"uid"`
Name string `json:"name"`
Type string `json:"type"`
IsDefault bool `json:"isDefault"`
DisableResolveMessage bool `json:"disableResolveMessage"`
SendReminder bool `json:"sendReminder"`
Frequency string `json:"frequency"`
Settings interface{} `json:"settings"`
}
// AlertNotifications fetches and returns Grafana alert notifications.
func (c *Client) AlertNotifications() ([]AlertNotification, error) {
alertnotifications := make([]AlertNotification, 0)
err := c.request("GET", "/api/alert-notifications/", nil, nil, &alertnotifications)
if err != nil {
return nil, err
}
return alertnotifications, err
}
// AlertNotification fetches and returns a Grafana alert notification.
func (c *Client) AlertNotification(id int64) (*AlertNotification, error) {
path := fmt.Sprintf("/api/alert-notifications/%d", id)
result := &AlertNotification{}
err := c.request("GET", path, nil, nil, result)
if err != nil {
return nil, err
}
return result, err
}
// NewAlertNotification creates a new Grafana alert notification.
func (c *Client) NewAlertNotification(a *AlertNotification) (int64, error) {
data, err := json.Marshal(a)
if err != nil {
return 0, err
}
result := struct {
Id int64 `json:"id"`
}{}
err = c.request("POST", "/api/alert-notifications", nil, bytes.NewBuffer(data), &result)
if err != nil {
return 0, err
}
return result.Id, err
}
// UpdateAlertNotification updates a Grafana alert notification.
func (c *Client) UpdateAlertNotification(a *AlertNotification) error {
path := fmt.Sprintf("/api/alert-notifications/%d", a.Id)
data, err := json.Marshal(a)
if err != nil {
return err
}
err = c.request("PUT", path, nil, bytes.NewBuffer(data), nil)
return err
}
// DeleteAlertNotification deletes a Grafana alert notification.
func (c *Client) DeleteAlertNotification(id int64) error {
path := fmt.Sprintf("/api/alert-notifications/%d", id)
return c.request("DELETE", path, nil, nil, nil)
}