This repository was archived by the owner on Dec 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathalerts.go
More file actions
209 lines (170 loc) · 5.42 KB
/
alerts.go
File metadata and controls
209 lines (170 loc) · 5.42 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package appoptics
import (
"fmt"
)
// Alert defines a policy for sending alarms to services when conditions are met
type Alert struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Active *bool `json:"active,omitempty"`
RearmSeconds int `json:"rearm_seconds,omitempty"`
Conditions []*AlertCondition `json:"conditions,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
Services []*Service `json:"services,omitempty"`
CreatedAt int `json:"created_at,omitempty"`
UpdatedAt int `json:"updated_at,omitempty"`
}
// AlertRequest is identical to Alert except for the fact that Services is a []int in AlertRequest
type AlertRequest struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Active *bool `json:"active,omitempty"`
RearmSeconds int `json:"rearm_seconds,omitempty"`
Conditions []*AlertCondition `json:"conditions,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
Services []int `json:"services,omitempty"` // correspond to IDs of Service objects
CreatedAt int `json:"created_at,omitempty"`
UpdatedAt int `json:"updated_at,omitempty"`
}
type AlertCondition struct {
ID int `json:"id,omitempty"`
Type string `json:"type,omitempty"`
MetricName string `json:"metric_name,omitempty"`
Threshold float64 `json:"threshold"`
SummaryFunction string `json:"summary_function,omitempty"`
Duration int `json:"duration,omitempty"`
DetectReset bool `json:"detect_reset,omitempty"`
Tags []*Tag `json:"tags,omitempty"`
}
type AlertStatus struct {
Alert Alert `json:"alert,omitempty"`
Status string `json:"status,omitempty"`
}
type AlertsListResponse struct {
Query QueryInfo `json:"query,omitempty"`
Alerts []*Alert `json:"alerts,omitempty"`
}
type AlertsService struct {
client *Client
}
type AlertsCommunicator interface {
List() (*AlertsListResponse, error)
Retrieve(int) (*Alert, error)
Create(*AlertRequest) (*Alert, error)
Update(*AlertRequest) error
AssociateToService(int, int) error
DisassociateFromService(alertId, serviceId int) error
Delete(int) error
Status(int) (*AlertStatus, error)
}
func NewAlertsService(c *Client) *AlertsService {
return &AlertsService{c}
}
// List retrieves all Alerts
func (as *AlertsService) List() (*AlertsListResponse, error) {
req, err := as.client.NewRequest("GET", "alerts", nil)
if err != nil {
return nil, err
}
alertsResponse := &AlertsListResponse{}
_, err = as.client.Do(req, &alertsResponse)
if err != nil {
return nil, err
}
return alertsResponse, nil
}
// Retrieve returns the Alert identified by the parameter
func (as *AlertsService) Retrieve(id int) (*Alert, error) {
alert := &Alert{}
path := fmt.Sprintf("alerts/%d", id)
req, err := as.client.NewRequest("GET", path, nil)
if err != nil {
return nil, err
}
_, err = as.client.Do(req, alert)
if err != nil {
return nil, err
}
return alert, nil
}
// Create creates the Alert
func (as *AlertsService) Create(a *AlertRequest) (*Alert, error) {
req, err := as.client.NewRequest("POST", "alerts", a)
if err != nil {
return nil, err
}
createdAlert := &Alert{}
_, err = as.client.Do(req, createdAlert)
if err != nil {
return nil, err
}
return createdAlert, nil
}
// Update updates the Alert
func (as *AlertsService) Update(a *AlertRequest) error {
path := fmt.Sprintf("alerts/%d", a.ID)
req, err := as.client.NewRequest("PUT", path, a)
if err != nil {
return err
}
_, err = as.client.Do(req, nil)
if err != nil {
return err
}
return nil
}
// AssociateToService updates the Alert to allow assign it to the Service identified
func (as *AlertsService) AssociateToService(alertId, serviceId int) error {
path := fmt.Sprintf("alerts/%d/services", alertId)
bodyStruct := struct {
ID int `json:"service"`
}{serviceId}
req, err := as.client.NewRequest("POST", path, bodyStruct)
if err != nil {
return err
}
_, err = as.client.Do(req, nil)
if err != nil {
return err
}
return nil
}
// DisassociateFromService updates the Alert to remove the Service identified
func (as *AlertsService) DisassociateFromService(alertId, serviceId int) error {
path := fmt.Sprintf("alerts/%d/services/%d", alertId, serviceId)
req, err := as.client.NewRequest("DELETE", path, nil)
if err != nil {
return err
}
_, err = as.client.Do(req, nil)
if err != nil {
return err
}
return nil
}
// Delete deletes the Alert
func (as *AlertsService) Delete(id int) error {
path := fmt.Sprintf("alerts/%d", id)
req, err := as.client.NewRequest("DELETE", path, nil)
if err != nil {
return err
}
_, err = as.client.Do(req, nil)
return err
}
// Status returns the Alert's status
func (as *AlertsService) Status(id int) (*AlertStatus, error) {
path := fmt.Sprintf("alerts/%d/status", id)
req, err := as.client.NewRequest("GET", path, nil)
if err != nil {
return nil, err
}
alertStatus := &AlertStatus{}
_, err = as.client.Do(req, alertStatus)
if err != nil {
return nil, err
}
return alertStatus, nil
}