forked from AcalephStorage/consul-alerts
/
opsgenie_alert_client.go
859 lines (827 loc) · 31 KB
/
opsgenie_alert_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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
// Copyright 2015 OpsGenie. All rights reserved.
// Use of this source code is governed by an Apache Software
// license that can be found in the LICENSE file.
package client
import (
"bytes"
"errors"
"fmt"
goreq "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/franela/goreq"
goquery "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/google/go-querystring/query"
alerts "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/opsgenie/opsgenie-go-sdk/alerts"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"time"
)
const (
CREATE_ALERT_URL = ENDPOINT_URL + "/v1/json/alert"
CLOSE_ALERT_URL = ENDPOINT_URL + "/v1/json/alert/close"
DELETE_ALERT_URL = ENDPOINT_URL + "/v1/json/alert"
GET_ALERT_URL = ENDPOINT_URL + "/v1/json/alert"
LIST_ALERTS_URL = ENDPOINT_URL + "/v1/json/alert"
LIST_ALERT_NOTES_URL = ENDPOINT_URL + "/v1/json/alert/note"
LIST_ALERT_LOGS_URL = ENDPOINT_URL + "/v1/json/alert/log"
LIST_ALERT_RECIPIENTS_URL = ENDPOINT_URL + "/v1/json/alert/recipient"
ACKNOWLEDGE_ALERT_URL = ENDPOINT_URL + "/v1/json/alert/acknowledge"
RENOTIFY_ALERT_URL = ENDPOINT_URL + "/v1/json/alert/renotify"
TAKE_OWNERSHIP_ALERT_URL = ENDPOINT_URL + "/v1/json/alert/takeOwnership"
ASSIGN_OWNERSHIP_ALERT_URL = ENDPOINT_URL + "/v1/json/alert/assign"
ADD_TEAM_ALERT_URL = ENDPOINT_URL + "/v1/json/alert/team"
ADD_RECIPIENT_ALERT_URL = ENDPOINT_URL + "/v1/json/alert/recipient"
ADD_NOTE_ALERT_URL = ENDPOINT_URL + "/v1/json/alert/note"
EXECUTE_ACTION_ALERT_URL = ENDPOINT_URL + "/v1/json/alert/executeAction"
ATTACH_FILE_ALERT_URL = ENDPOINT_URL + "/v1/json/alert/attach"
)
type OpsGenieAlertClient struct {
apiKey string
proxy string
retries int
}
func (cli *OpsGenieAlertClient) buildRequest(method string, uri string, body interface{}) goreq.Request {
req := goreq.Request{}
req.Method = method
req.Uri = uri
if body != nil {
req.Body = body
}
if cli.proxy != "" {
req.Proxy = cli.proxy
}
req.UserAgent = userAgentParam.ToString()
return req
}
func (cli *OpsGenieAlertClient) SetConnectionTimeout(timeoutInSeconds time.Duration) {
goreq.SetConnectTimeout(timeoutInSeconds * time.Second)
}
func (cli *OpsGenieAlertClient) SetMaxRetryAttempts(retries int) {
cli.retries = retries
}
func (cli *OpsGenieAlertClient) Create(req alerts.CreateAlertRequest) (*alerts.CreateAlertResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameters: apiKey, message
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
if req.Message == "" {
return nil, errors.New("Message is a mandatory field and can not be empty.")
}
// send the request
var resp *goreq.Response
var err error
for i := 0; i < cli.retries; i++ {
resp, err = cli.buildRequest("POST", CREATE_ALERT_URL, req).Do()
if err == nil {
break
}
// sleep for a second
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
if err != nil {
return nil, errors.New("Could not create the alert: a problem occured while sending the request.")
}
// check the returning HTTP status code
httpStatusCode := resp.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned.", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned.", httpStatusCode))
}
var createAlertResp alerts.CreateAlertResponse
// check if the response can be unmarshalled
if err = resp.Body.FromJsonTo(&createAlertResp); err != nil {
return nil, errors.New("Server response can not be parsed")
}
return &createAlertResp, nil
}
func (cli *OpsGenieAlertClient) Close(req alerts.CloseAlertRequest) (*alerts.CloseAlertResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameters: apiKey, alertId/alias
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
if req.AlertId == "" && req.Alias == "" {
return nil, errors.New("At least either Alert Id or Alias should be set in the request.")
}
if req.AlertId != "" && req.Alias != "" {
return nil, errors.New("Either Alert Id or Alias should be set in the request not both.")
}
var resp *goreq.Response
var err error
for i := 0; i < cli.retries; i++ {
resp, err = cli.buildRequest("POST", CLOSE_ALERT_URL, req).Do()
if err == nil {
break
}
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
// resp, err := goreq.Request{ Method: "POST", Uri: CLOSE_ALERT_URL, Body: req, }.Do()
if err != nil {
return nil, errors.New("Could not close the alert: a problem occured while sending the request.")
}
// check the returning HTTP status code
httpStatusCode := resp.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
}
var closeAlertResp alerts.CloseAlertResponse
// check if the response can be unmarshalled
if err = resp.Body.FromJsonTo(&closeAlertResp); err != nil {
return nil, errors.New("Server response can not be parsed")
}
return &closeAlertResp, nil
}
func (cli *OpsGenieAlertClient) Delete(req alerts.DeleteAlertRequest) (*alerts.DeleteAlertResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameters: apiKey, alertId/alias
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
if req.AlertId == "" && req.Alias == "" {
return nil, errors.New("Either Alert Id or Alias at least should be set in the request.")
}
if req.AlertId != "" && req.Alias != "" {
return nil, errors.New("Either Alert Id or Alias should be set in the request not both.")
}
v, _ := goquery.Values(req)
var resp *goreq.Response
var err error
for i := 0; i < cli.retries; i++ {
resp, err = cli.buildRequest("DELETE", DELETE_ALERT_URL+"?"+v.Encode(), nil).Do()
if err == nil {
break
}
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
if err != nil {
return nil, errors.New("Could not delete the alert: a problem occured while sending the request.")
}
// check the returning HTTP status code
httpStatusCode := resp.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
}
var deleteAlertResp alerts.DeleteAlertResponse
// check if the response can be unmarshalled
if err = resp.Body.FromJsonTo(&deleteAlertResp); err != nil {
return nil, errors.New("Server response can not be parsed.")
}
return &deleteAlertResp, nil
}
func (cli *OpsGenieAlertClient) Get(req alerts.GetAlertRequest) (*alerts.GetAlertResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameters: apiKey, id/alias/tinyId
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
if req.Id == "" && req.Alias == "" && req.TinyId == "" {
return nil, errors.New("At least one of the parameters of id, alias and tiny id should be set.")
}
if (req.Id != "" && req.Alias != "") || (req.Id != "" && req.TinyId != "") || (req.Alias != "" && req.TinyId != "") {
return nil, errors.New("Only one of the parameters of id, alias and tiny id should be set.")
}
v, _ := goquery.Values(req)
var resp *goreq.Response
var err error
for i := 0; i < cli.retries; i++ {
resp, err = cli.buildRequest("GET", GET_ALERT_URL+"?"+v.Encode(), nil).Do()
if err == nil {
break
}
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
if err != nil {
return nil, errors.New("Could not retrieve the alert: a problem occured while sending the request")
}
// check the returning HTTP status code
httpStatusCode := resp.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
}
var getAlertResp alerts.GetAlertResponse
// check if the response can be unmarshalled
if err = resp.Body.FromJsonTo(&getAlertResp); err != nil {
return nil, errors.New("Server response can not be parsed.")
}
return &getAlertResp, nil
}
func (cli *OpsGenieAlertClient) List(req alerts.ListAlertsRequest) (*alerts.ListAlertsResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameter: apiKey
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
v, _ := goquery.Values(req)
var resp *goreq.Response
var err error
for i := 0; i < cli.retries; i++ {
resp, err = cli.buildRequest("GET", LIST_ALERTS_URL+"?"+v.Encode(), nil).Do()
if err == nil {
break
}
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
if err != nil {
return nil, errors.New("Could not retrieve the alert: a problem occured while sending the request")
}
// check the returning HTTP status code
httpStatusCode := resp.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
}
var listAlertsResp alerts.ListAlertsResponse
// check if the response can be unmarshalled
if err = resp.Body.FromJsonTo(&listAlertsResp); err != nil {
return nil, errors.New("Server response can not be parsed.")
}
return &listAlertsResp, nil
}
func (cli *OpsGenieAlertClient) ListNotes(req alerts.ListAlertNotesRequest) (*alerts.ListAlertNotesResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameters: apiKey, id/alias
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
if req.Id == "" && req.Alias == "" {
return nil, errors.New("At least either Id or Alias should be set in the request.")
}
if req.Id != "" && req.Alias != "" {
return nil, errors.New("Either Id or Alias should be set in the request not both.")
}
v, _ := goquery.Values(req)
var resp *goreq.Response
var err error
for i := 0; i < cli.retries; i++ {
resp, err = cli.buildRequest("GET", LIST_ALERT_NOTES_URL+"?"+v.Encode(), nil).Do()
if err == nil {
break
}
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
if err != nil {
return nil, errors.New("Could not send the request: a problem occured while sending the request")
}
// check the returning HTTP status code
httpStatusCode := resp.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
}
var listAlertNotesResp alerts.ListAlertNotesResponse
// check if the response can be unmarshalled
if err = resp.Body.FromJsonTo(&listAlertNotesResp); err != nil {
return nil, errors.New("Server response can not be parsed.")
}
return &listAlertNotesResp, nil
}
func (cli *OpsGenieAlertClient) ListLogs(req alerts.ListAlertLogsRequest) (*alerts.ListAlertLogsResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameters: apiKey, id/alias
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
if req.Id == "" && req.Alias == "" {
return nil, errors.New("At least either Id or Alias should be set in the request.")
}
if req.Id != "" && req.Alias != "" {
return nil, errors.New("Either Id or Alias should be set in the request not both.")
}
v, _ := goquery.Values(req)
var resp *goreq.Response
var err error
for i := 0; i < cli.retries; i++ {
resp, err = cli.buildRequest("GET", LIST_ALERT_LOGS_URL+"?"+v.Encode(), nil).Do()
if err == nil {
break
}
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
if err != nil {
return nil, errors.New("Could not retrieve the logs: a problem occured while sending the request")
}
// check the returning HTTP status code
httpStatusCode := resp.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
}
var listAlertLogsResp alerts.ListAlertLogsResponse
// check if the response can be unmarshalled
if err = resp.Body.FromJsonTo(&listAlertLogsResp); err != nil {
return nil, errors.New("Server response can not be parsed.")
}
return &listAlertLogsResp, nil
}
func (cli *OpsGenieAlertClient) ListRecipients(req alerts.ListAlertRecipientsRequest) (*alerts.ListAlertRecipientsResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameters: apiKey, id/alias
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
if req.Id == "" && req.Alias == "" {
return nil, errors.New("At least either Id or Alias should be set in the request.")
}
if req.Id != "" && req.Alias != "" {
return nil, errors.New("Either Id or Alias should be set in the request not both.")
}
v, _ := goquery.Values(req)
var resp *goreq.Response
var err error
for i := 0; i < cli.retries; i++ {
resp, err = cli.buildRequest("GET", LIST_ALERT_RECIPIENTS_URL+"?"+v.Encode(), nil).Do()
if err == nil {
break
}
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
if err != nil {
return nil, errors.New("Can not list the recipient list, unable to send the request")
}
// check the returning HTTP status code
httpStatusCode := resp.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
}
var listAlertRecipientsResp alerts.ListAlertRecipientsResponse
// check if the response can be unmarshalled
if err = resp.Body.FromJsonTo(&listAlertRecipientsResp); err != nil {
return nil, errors.New("Server response can not be parsed.")
}
return &listAlertRecipientsResp, nil
}
func (cli *OpsGenieAlertClient) Acknowledge(req alerts.AcknowledgeAlertRequest) (*alerts.AcknowledgeAlertResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameters: apiKey, alertId/alias
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
if req.AlertId == "" && req.Alias == "" {
return nil, errors.New("At least either Alert Id or Alias should be set in the request.")
}
if req.AlertId != "" && req.Alias != "" {
return nil, errors.New("Either Alert Id or Alias should be set in the request not both.")
}
var resp *goreq.Response
var err error
for i := 0; i < cli.retries; i++ {
resp, err = cli.buildRequest("POST", ACKNOWLEDGE_ALERT_URL, req).Do()
if err == nil {
break
}
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
if err != nil {
return nil, errors.New("Can not ack the alert, unable to send the request")
}
// check the returning HTTP status code
httpStatusCode := resp.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
}
var acknowledgeAlertResp alerts.AcknowledgeAlertResponse
// check if the response can be unmarshalled
if err = resp.Body.FromJsonTo(&acknowledgeAlertResp); err != nil {
return nil, errors.New("Server response can not be parsed.")
}
return &acknowledgeAlertResp, nil
}
func (cli *OpsGenieAlertClient) Renotify(req alerts.RenotifyAlertRequest) (*alerts.RenotifyAlertResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameters: apiKey, alertId/alias
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
if req.AlertId == "" && req.Alias == "" {
return nil, errors.New("At least either Alert Id or Alias should be set in the request.")
}
if req.AlertId != "" && req.Alias != "" {
return nil, errors.New("Either Alert Id or Alias should be set in the request not both.")
}
var resp *goreq.Response
var err error
for i := 0; i < cli.retries; i++ {
resp, err = cli.buildRequest("POST", RENOTIFY_ALERT_URL, req).Do()
if err == nil {
break
}
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
if err != nil {
return nil, errors.New("Can not renotify, unable to send the request")
}
// check the returning HTTP status code
httpStatusCode := resp.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
}
var renotifyAlertResp alerts.RenotifyAlertResponse
// check if the response can be unmarshalled
if err = resp.Body.FromJsonTo(&renotifyAlertResp); err != nil {
return nil, errors.New("Server response can not be parsed.")
}
return &renotifyAlertResp, nil
}
func (cli *OpsGenieAlertClient) TakeOwnership(req alerts.TakeOwnershipAlertRequest) (*alerts.TakeOwnershipAlertResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameters: apiKey, alertId/alias
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
if req.AlertId == "" && req.Alias == "" {
return nil, errors.New("At least either Alert Id or Alias should be set in the request.")
}
if req.AlertId != "" && req.Alias != "" {
return nil, errors.New("Either Alert Id or Alias should be set in the request not both.")
}
var resp *goreq.Response
var err error
for i := 0; i < cli.retries; i++ {
resp, err = cli.buildRequest("POST", TAKE_OWNERSHIP_ALERT_URL, req).Do()
if err == nil {
break
}
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
if err != nil {
return nil, errors.New("Can not change the ownership, unable to send the request")
}
// check the returning HTTP status code
httpStatusCode := resp.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
}
var takeOwnershipResp alerts.TakeOwnershipAlertResponse
// check if the response can be unmarshalled
if err = resp.Body.FromJsonTo(&takeOwnershipResp); err != nil {
return nil, errors.New("Server response can not be parsed.")
}
return &takeOwnershipResp, nil
}
func (cli *OpsGenieAlertClient) AssignOwner(req alerts.AssignOwnerAlertRequest) (*alerts.AssignOwnerAlertResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameters: apiKey, alertId/alias, owner
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
if req.Owner == "" {
return nil, errors.New("Owner is a mandatory field and can not be empty")
}
if req.AlertId == "" && req.Alias == "" {
return nil, errors.New("At least either Alert Id or Alias should be set in the request.")
}
if req.AlertId != "" && req.Alias != "" {
return nil, errors.New("Either Alert Id or Alias should be set in the request not both.")
}
var resp *goreq.Response
var err error
for i := 0; i < cli.retries; i++ {
resp, err = cli.buildRequest("POST", ASSIGN_OWNERSHIP_ALERT_URL, req).Do()
if err == nil {
break
}
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
if err != nil {
return nil, errors.New("Can not assign the owner, unable to send the request")
}
// check the returning HTTP status code
httpStatusCode := resp.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
}
var assignOwnerAlertResp alerts.AssignOwnerAlertResponse
// check if the response can be unmarshalled
if err = resp.Body.FromJsonTo(&assignOwnerAlertResp); err != nil {
return nil, errors.New("Server response can not be parsed.")
}
return &assignOwnerAlertResp, nil
}
func (cli *OpsGenieAlertClient) AddTeam(req alerts.AddTeamAlertRequest) (*alerts.AddTeamAlertResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameters: apiKey, alertId/alias, team
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
if req.Team == "" {
return nil, errors.New("Team is a mandatory field and can not be empty")
}
if req.AlertId == "" && req.Alias == "" {
return nil, errors.New("At least either Alert Id or Alias should be set in the request.")
}
if req.AlertId != "" && req.Alias != "" {
return nil, errors.New("Either Alert Id or Alias should be set in the request not both.")
}
var resp *goreq.Response
var err error
for i := 0; i < cli.retries; i++ {
resp, err = cli.buildRequest("POST", ADD_TEAM_ALERT_URL, req).Do()
if err == nil {
break
}
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
if err != nil {
return nil, errors.New("Team can not be added, unable to send the request")
}
// check the returning HTTP status code
httpStatusCode := resp.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
}
var addTeamAlertResp alerts.AddTeamAlertResponse
// check if the response can be unmarshalled
if err = resp.Body.FromJsonTo(&addTeamAlertResp); err != nil {
return nil, errors.New("Server response can not be parsed.")
}
return &addTeamAlertResp, nil
}
func (cli *OpsGenieAlertClient) AddRecipient(req alerts.AddRecipientAlertRequest) (*alerts.AddRecipientAlertResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameters: apiKey, alertId/alias, recipient
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
if req.Recipient == "" {
return nil, errors.New("Recipient is a mandatory field and can not be empty")
}
if req.AlertId == "" && req.Alias == "" {
return nil, errors.New("At least either Alert Id or Alias should be set in the request.")
}
if req.AlertId != "" && req.Alias != "" {
return nil, errors.New("Either Alert Id or Alias should be set in the request not both.")
}
var resp *goreq.Response
var err error
for i := 0; i < cli.retries; i++ {
resp, err = cli.buildRequest("POST", ADD_RECIPIENT_ALERT_URL, req).Do()
if err == nil {
break
}
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
if err != nil {
return nil, errors.New("Can not add recipient, unable to send the request")
}
// check the returning HTTP status code
httpStatusCode := resp.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
}
var addRecipientAlertResp alerts.AddRecipientAlertResponse
// check if the response can be unmarshalled
if err = resp.Body.FromJsonTo(&addRecipientAlertResp); err != nil {
return nil, errors.New("Server response can not be parsed.")
}
return &addRecipientAlertResp, nil
}
func (cli *OpsGenieAlertClient) AddNote(req alerts.AddNoteAlertRequest) (*alerts.AddNoteAlertResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameters: apiKey, alertId/alias, note
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
if req.Note == "" {
return nil, errors.New("Note is a mandatory field and can not be empty")
}
if req.AlertId == "" && req.Alias == "" {
return nil, errors.New("At least either Alert Id or Alias should be set in the request.")
}
if req.AlertId != "" && req.Alias != "" {
return nil, errors.New("Either Alert Id or Alias should be set in the request not both.")
}
var resp *goreq.Response
var err error
for i := 0; i < cli.retries; i++ {
resp, err = cli.buildRequest("POST", ADD_NOTE_ALERT_URL, req).Do()
if err == nil {
break
}
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
if err != nil {
return nil, errors.New("Can not add note, unable to send the request.")
}
// check the returning HTTP status code
httpStatusCode := resp.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
}
var addNoteAlertResp alerts.AddNoteAlertResponse
// check if the response can be unmarshalled
if err = resp.Body.FromJsonTo(&addNoteAlertResp); err != nil {
return nil, errors.New("Server response can not be parsed.")
}
return &addNoteAlertResp, nil
}
func (cli *OpsGenieAlertClient) ExecuteAction(req alerts.ExecuteActionAlertRequest) (*alerts.ExecuteActionAlertResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameters: apiKey, alertId/alias, action
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
if req.Action == "" {
return nil, errors.New("Action is a mandatory field and can not be empty.")
}
if req.AlertId == "" && req.Alias == "" {
return nil, errors.New("At least either Alert Id or Alias should be set in the request.")
}
if req.AlertId != "" && req.Alias != "" {
return nil, errors.New("Either Alert Id or Alias should be set in the request not both.")
}
var resp *goreq.Response
var err error
for i := 0; i < cli.retries; i++ {
resp, err = cli.buildRequest("POST", EXECUTE_ACTION_ALERT_URL, req).Do()
if err == nil {
break
}
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
if err != nil {
return nil, errors.New("Can not execute the action, unable to send the request.")
}
// check the returning HTTP status code
httpStatusCode := resp.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
}
var executeActionAlertResp alerts.ExecuteActionAlertResponse
// check if the response can be unmarshalled
if err = resp.Body.FromJsonTo(&executeActionAlertResp); err != nil {
return nil, errors.New("Server response can not be parsed.")
}
return &executeActionAlertResp, nil
}
func (cli *OpsGenieAlertClient) AttachFile(req alerts.AttachFileAlertRequest) (*alerts.AttachFileAlertResponse, error) {
req.ApiKey = cli.apiKey
// validate the mandatory parameters: apiKey, alertId/alias, attachment
if req.ApiKey == "" {
return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
}
if req.Attachment == "" {
return nil, errors.New("Attachment is a mandatory field and can not be empty.")
}
if req.AlertId == "" && req.Alias == "" {
return nil, errors.New("At least either Alert Id or Alias should be set in the request.")
}
if req.AlertId != "" && req.Alias != "" {
return nil, errors.New("Either Alert Id or Alias should be set in the request not both.")
}
var b bytes.Buffer
w := multipart.NewWriter(&b)
fileName := req.Attachment
file, err := os.Open(fileName)
if err != nil {
return nil, errors.New("Attachment can not be opened for reading.")
}
// add the attachment
fw, err := w.CreateFormFile("attachment", fileName)
if err != nil {
return nil, errors.New("Can not build the request with the field attachment.")
}
if _, err := io.Copy(fw, file); err != nil {
return nil, errors.New("Can not copy the attachment into the request.")
}
defer file.Close()
// Add the other fields
// empty fields should not be placed into the request
// otherwise it yields an incomplete boundary exception
if req.ApiKey != "" {
if fw, err = w.CreateFormField("apiKey"); err != nil {
return nil, errors.New("Can not build the request with the field apiKey.")
}
if _, err = fw.Write([]byte(req.ApiKey)); err != nil {
return nil, errors.New("Can not write the ApiKey value into the request.")
}
}
if req.AlertId != "" {
if fw, err = w.CreateFormField("alertId"); err != nil {
return nil, errors.New("Can not build the request with the field alertId.")
}
if _, err = fw.Write([]byte(req.AlertId)); err != nil {
return nil, errors.New("Can not write the AlertId value into the request.")
}
}
if req.Alias != "" {
if fw, err = w.CreateFormField("alias"); err != nil {
return nil, errors.New("Can not build the request with the field alias.")
}
if _, err = fw.Write([]byte(req.Alias)); err != nil {
return nil, errors.New("Can not write the Alias value into the request.")
}
}
if req.User != "" {
if fw, err = w.CreateFormField("user"); err != nil {
return nil, errors.New("Can not build the request with the field user.")
}
if _, err = fw.Write([]byte(req.User)); err != nil {
return nil, errors.New("Can not write the User value into the request.")
}
}
if req.Source != "" {
if fw, err = w.CreateFormField("source"); err != nil {
return nil, errors.New("Can not build the request with the field source.")
}
if _, err = fw.Write([]byte(req.Source)); err != nil {
return nil, errors.New("Can not write the Source value into the request.")
}
}
if req.IndexFile != "" {
if fw, err = w.CreateFormField("indexFile"); err != nil {
return nil, errors.New("Can not build the request with the field indexFile.")
}
if _, err = fw.Write([]byte(req.IndexFile)); err != nil {
return nil, errors.New("Can not write the IndexFile value into the request.")
}
}
if req.Note != "" {
if fw, err = w.CreateFormField("note"); err != nil {
return nil, errors.New("Can not build the request with the field note.")
}
if _, err = fw.Write([]byte(req.Note)); err != nil {
return nil, errors.New("Can not write the Note value into the request.")
}
}
w.Close()
httpReq, err := http.NewRequest("POST", ATTACH_FILE_ALERT_URL, &b)
if err != nil {
return nil, errors.New("Can not create the multipart/form-data request.")
}
httpReq.Header.Set("Content-Type", w.FormDataContentType())
client := &http.Client{}
// proxy settings
if cli.proxy != "" {
proxyUrl, proxyErr := url.Parse(cli.proxy)
if proxyErr != nil {
return nil, errors.New("Can not set the proxy configuration")
}
client.Transport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}
}
var res *http.Response
for i := 0; i < cli.retries; i++ {
res, err = client.Do(httpReq)
if err == nil {
break
}
time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
}
if err != nil {
return nil, errors.New("Can not attach the file, unable to send the request.")
}
// check the returning HTTP status code
httpStatusCode := res.StatusCode
if httpStatusCode >= 400 && httpStatusCode < 500 {
return nil, errors.New(fmt.Sprintf("Client error %d returned", httpStatusCode))
}
if httpStatusCode >= 500 {
return nil, errors.New(fmt.Sprintf("Server error %d returned", httpStatusCode))
}
attachFileAlertResp := alerts.AttachFileAlertResponse{Status: res.Status, Code: res.StatusCode}
return &attachFileAlertResp, nil
}