-
Notifications
You must be signed in to change notification settings - Fork 191
/
opsgenie_alertv2_client.go
318 lines (260 loc) · 9.76 KB
/
opsgenie_alertv2_client.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package client
import (
"errors"
"github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/franela/goreq"
"github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/opsgenie/opsgenie-go-sdk/alertsv2"
"github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/opsgenie/opsgenie-go-sdk/alertsv2/savedsearches"
)
// OpsGenieAlertClient is the data type to make Alert API requests.
type OpsGenieAlertV2Client struct {
RestClient
}
// SetOpsGenieClient sets the embedded OpsGenieClient type of the OpsGenieAlertV2Client.
func (cli *OpsGenieAlertV2Client) SetOpsGenieClient(ogCli OpsGenieClient) {
cli.OpsGenieClient = ogCli
}
// Returns AlertDetails
func (cli *OpsGenieAlertV2Client) Get(req alertsv2.GetAlertRequest) (*alertsv2.DetailedAlertResponse, error) {
var response alertsv2.DetailedAlertResponse
err := cli.sendGetRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// Retrieves the alerts from OpsGenie
func (cli *OpsGenieAlertV2Client) List(req alertsv2.ListAlertRequest) (*alertsv2.ListAlertResponse, error) {
var response alertsv2.ListAlertResponse
err := cli.sendGetRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// Retrieves the alert recipients from OpsGenie
func (cli *OpsGenieAlertV2Client) ListAlertRecipients(req alertsv2.ListAlertRecipientsRequest) (*alertsv2.ListAlertRecipientsResponse, error) {
var response alertsv2.ListAlertRecipientsResponse
err := cli.sendGetRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// Retrieves the alert logs from OpsGenie
func (cli *OpsGenieAlertV2Client) ListAlertLogs(req alertsv2.ListAlertLogsRequest) (*alertsv2.ListAlertLogsResponse, error) {
var response alertsv2.ListAlertLogsResponse
err := cli.sendGetRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// Retrieves the alert notes from OpsGenie
func (cli *OpsGenieAlertV2Client) ListAlertNotes(req alertsv2.ListAlertNotesRequest) (*alertsv2.ListAlertNotesResponse, error) {
var response alertsv2.ListAlertNotesResponse
err := cli.sendGetRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// Sends acknowledge request an alert
func (cli *OpsGenieAlertV2Client) Acknowledge(req alertsv2.AcknowledgeRequest) (*AsyncRequestResponse, error) {
return cli.sendAsyncPostRequest(&req)
}
// Sends close request for an alert
func (cli *OpsGenieAlertV2Client) Close(req alertsv2.CloseRequest) (*AsyncRequestResponse, error) {
return cli.sendAsyncPostRequest(&req)
}
// Sends unacknowledge request for an alert
func (cli *OpsGenieAlertV2Client) Unacknowledge(req alertsv2.UnacknowledgeRequest) (*AsyncRequestResponse, error) {
return cli.sendAsyncPostRequest(&req)
}
// Sends snooze request for an alert
func (cli *OpsGenieAlertV2Client) Snooze(req alertsv2.SnoozeRequest) (*AsyncRequestResponse, error) {
return cli.sendAsyncPostRequest(&req)
}
// Sends custom execution action for an alert
func (cli *OpsGenieAlertV2Client) ExecuteCustomAction(req alertsv2.ExecuteCustomActionRequest) (*AsyncRequestResponse, error) {
return cli.sendAsyncPostRequest(&req)
}
// Creates an alert
func (cli *OpsGenieAlertV2Client) Create(req alertsv2.CreateAlertRequest) (*AsyncRequestResponse, error) {
req.Init()
return cli.sendAsyncPostRequest(&req)
}
// Deletes an alert
func (cli *OpsGenieAlertV2Client) Delete(req alertsv2.DeleteAlertRequest) (*AsyncRequestResponse, error) {
var response AsyncRequestResponse
err := cli.sendDeleteRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// Adds a note to the alert
func (cli *OpsGenieAlertV2Client) AddNote(req alertsv2.AddNoteRequest) (*AsyncRequestResponse, error) {
return cli.sendAsyncPostRequest(&req)
}
// Sends escalation to next request
func (cli *OpsGenieAlertV2Client) EscalateToNext(req alertsv2.EscalateToNextRequest) (*AsyncRequestResponse, error) {
return cli.sendAsyncPostRequest(&req)
}
// Assigns an alert to a user
func (cli *OpsGenieAlertV2Client) Assign(req alertsv2.AssignAlertRequest) (*AsyncRequestResponse, error) {
return cli.sendAsyncPostRequest(&req)
}
// Adds a team to the alert
func (cli *OpsGenieAlertV2Client) AddTeamToAlert(req alertsv2.AddTeamToAlertRequest) (*AsyncRequestResponse, error) {
return cli.sendAsyncPostRequest(&req)
}
// Adds a tag to the alert
func (cli *OpsGenieAlertV2Client) AddTags(req alertsv2.AddTagsToAlertRequest) (*AsyncRequestResponse, error) {
return cli.sendAsyncPostRequest(&req)
}
// Removes a tag from the alert
func (cli *OpsGenieAlertV2Client) RemoveTags(req alertsv2.RemoveTagsRequest) (*AsyncRequestResponse, error) {
var response AsyncRequestResponse
err := cli.sendDeleteRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// Adds details to the alert
func (cli *OpsGenieAlertV2Client) AddDetails(req alertsv2.AddDetailsRequest) (*AsyncRequestResponse, error) {
return cli.sendAsyncPostRequest(&req)
}
// Removes the details from the alert
func (cli *OpsGenieAlertV2Client) RemoveDetails(req alertsv2.RemoveDetailsRequest) (*AsyncRequestResponse, error) {
var response AsyncRequestResponse
err := cli.sendDeleteRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// Creates a SavedSearch
func (cli *OpsGenieAlertV2Client) CreateSavedSearch(req savedsearches.CreateSavedSearchRequest) (*savedsearches.CreateSavedSearchResponse, error) {
var response savedsearches.CreateSavedSearchResponse
err := cli.sendPostRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// Retrieves the SavedSearch
func (cli *OpsGenieAlertV2Client) GetSavedSearch(req savedsearches.GetSavedSearchRequest) (*savedsearches.GetSavedSearchResponse, error) {
var response savedsearches.GetSavedSearchResponse
err := cli.sendGetRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// Updates the SavedSearch
func (cli *OpsGenieAlertV2Client) UpdateSavedSearch(req savedsearches.UpdateSavedSearchRequest) (*savedsearches.UpdateSavedSearchResponse, error) {
var response savedsearches.UpdateSavedSearchResponse
err := cli.sendPatchRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// Deletes the SavedSearch
func (cli *OpsGenieAlertV2Client) DeleteSavedSearch(req savedsearches.DeleteSavedSearchRequest) (*savedsearches.DeleteSavedSearchResponse, error) {
var response savedsearches.DeleteSavedSearchResponse
err := cli.sendDeleteRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// Retrieves list of saved searches
func (cli *OpsGenieAlertV2Client) ListSavedSearches(req alertsv2.LisSavedSearchRequest) (*savedsearches.ListSavedSearchResponse, error) {
var response savedsearches.ListSavedSearchResponse
err := cli.sendGetRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// Retrieves the status of the async request
func (cli *OpsGenieAlertV2Client) GetAsyncRequestStatus(req alertsv2.GetAsyncRequestStatusRequest) (*alertsv2.GetAsyncRequestStatusResponse, error) {
var response alertsv2.GetAsyncRequestStatusResponse
err := cli.sendGetRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// adds the attachment file to specified alert
func (cli *OpsGenieAlertV2Client) AttachFile(req alertsv2.AddAlertAttachmentRequest) (*alertsv2.AddAlertAttachmentResponse, error) {
var response alertsv2.AddAlertAttachmentResponse
if req.AttachmentFilePath == "" && req.AttachmentFileContent == nil {
return nil, errors.New("File path or content must be provided.")
}
if req.AttachmentFilePath == "" && req.AttachmentFileContent != nil && req.AttachmentFileName == "" {
return nil, errors.New("File name must be provided if only file content is given.")
}
err := cli.sendCreateAttachmentRequest(req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// retrieves the specified attachment with a download link
func (cli *OpsGenieAlertV2Client) GetAttachmentFile(req alertsv2.GetAlertAttachmentRequest) (*alertsv2.GetAlertAttachmentResponse, error) {
var response alertsv2.GetAlertAttachmentResponse
err := cli.sendGetRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// returns a list of attachment meta information for the specified alert
func (cli *OpsGenieAlertV2Client) ListAlertAttachments(req alertsv2.ListAlertAttachmentRequest) (*alertsv2.ListAlertAttachmentsResponse, error) {
var response alertsv2.ListAlertAttachmentsResponse
err := cli.sendGetRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// deletes the specified alert attachment
func (cli *OpsGenieAlertV2Client) DeleteAttachment(req alertsv2.DeleteAlertAttachmentRequest) (*alertsv2.DeleteAlertAttachmentResponse, error) {
var response alertsv2.DeleteAlertAttachmentResponse
err := cli.sendDeleteRequest(&req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
func (cli *RestClient) sendCreateAttachmentRequest(req alertsv2.AddAlertAttachmentRequest, response Response) error {
path, params, err := req.GenerateUrl()
if err != nil {
return err
}
path = cli.generateFullPathWithParams(path, params)
var httpRequest *goreq.Request
if req.AttachmentFilePath == "" {
httpRequest, err = cli.buildCreateAttachmentRequestWithBytes(path, req)
} else {
httpRequest, err = cli.buildCreateAttachmentRequest(path, req)
}
if err != nil {
return err
}
cli.setApiKey(httpRequest, req.GetApiKey())
httpResponse, err := cli.sendRequest(*httpRequest)
if err != nil {
return err
}
defer httpResponse.Body.Close()
err = cli.writeBody(httpResponse, &response)
if err != nil {
return err
}
cli.setResponseMeta(httpResponse, response)
return nil
}