-
Notifications
You must be signed in to change notification settings - Fork 156
/
alerts.go
82 lines (71 loc) · 2.37 KB
/
alerts.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
/*
* Datadog API for Go
*
* Please see the included LICENSE file for licensing information.
*
* Copyright 2013 by authors and contributors.
*/
package datadog
import (
"fmt"
)
// Alert represents the data of an alert: a query that can fire and send a
// message to the users.
type Alert struct {
Id *int `json:"id,omitempty"`
Creator *int `json:"creator,omitempty"`
Query *string `json:"query,omitempty"`
Name *string `json:"name,omitempty"`
Message *string `json:"message,omitempty"`
Silenced *bool `json:"silenced,omitempty"`
NotifyNoData *bool `json:"notify_no_data,omitempty"`
State *string `json:"state,omitempty"`
}
// reqAlerts receives a slice of all alerts.
type reqAlerts struct {
Alerts []Alert `json:"alerts,omitempty"`
}
// CreateAlert adds a new alert to the system. This returns a pointer to an
// Alert so you can pass that to UpdateAlert later if needed.
func (client *Client) CreateAlert(alert *Alert) (*Alert, error) {
var out Alert
if err := client.doJsonRequest("POST", "/v1/alert", alert, &out); err != nil {
return nil, err
}
return &out, nil
}
// UpdateAlert takes an alert that was previously retrieved through some method
// and sends it back to the server.
func (client *Client) UpdateAlert(alert *Alert) error {
return client.doJsonRequest("PUT", fmt.Sprintf("/v1/alert/%d", alert.Id),
alert, nil)
}
// GetAlert retrieves an alert by identifier.
func (client *Client) GetAlert(id int) (*Alert, error) {
var out Alert
if err := client.doJsonRequest("GET", fmt.Sprintf("/v1/alert/%d", id), nil, &out); err != nil {
return nil, err
}
return &out, nil
}
// DeleteAlert removes an alert from the system.
func (client *Client) DeleteAlert(id int) error {
return client.doJsonRequest("DELETE", fmt.Sprintf("/v1/alert/%d", id),
nil, nil)
}
// GetAlerts returns a slice of all alerts.
func (client *Client) GetAlerts() ([]Alert, error) {
var out reqAlerts
if err := client.doJsonRequest("GET", "/v1/alert", nil, &out); err != nil {
return nil, err
}
return out.Alerts, nil
}
// MuteAlerts turns off alerting notifications.
func (client *Client) MuteAlerts() error {
return client.doJsonRequest("POST", "/v1/mute_alerts", nil, nil)
}
// UnmuteAlerts turns on alerting notifications.
func (client *Client) UnmuteAlerts() error {
return client.doJsonRequest("POST", "/v1/unmute_alerts", nil, nil)
}