This repository has been archived by the owner on May 5, 2022. It is now read-only.
forked from PagerDuty/go-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_v2.go
97 lines (85 loc) · 2.98 KB
/
event_v2.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
package pagerduty
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// V2Event includes the incident/alert details
type V2Event struct {
RoutingKey string `json:"routing_key"`
Action string `json:"event_action"`
DedupKey string `json:"dedup_key,omitempty"`
Images []interface{} `json:"images,omitempty"`
Links []interface{} `json:"links,omitempty"`
Client string `json:"client,omitempty"`
ClientURL string `json:"client_url,omitempty"`
Payload *V2Payload `json:"payload,omitempty"`
}
// V2Payload represents the individual event details for an event
type V2Payload struct {
Summary string `json:"summary"`
Source string `json:"source"`
Severity string `json:"severity"`
Timestamp string `json:"timestamp,omitempty"`
Component string `json:"component,omitempty"`
Group string `json:"group,omitempty"`
Class string `json:"class,omitempty"`
Details interface{} `json:"custom_details,omitempty"`
}
// V2EventResponse is the json response body for an event
type V2EventResponse struct {
Status string `json:"status,omitempty"`
DedupKey string `json:"dedup_key,omitempty"`
Message string `json:"message,omitempty"`
Errors []string `json:"errors,omitempty"`
}
const v2eventEndPoint = "https://events.pagerduty.com/v2/enqueue"
// ManageEvent handles the trigger, acknowledge, and resolve methods for an event
func ManageEvent(e V2Event) (*V2EventResponse, error) {
data, err := json.Marshal(e)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(context.TODO(), http.MethodPost, v2eventEndPoint, bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %w", err)
}
req.Header.Set("User-Agent", "go-pagerduty/"+Version)
req.Header.Set("Content-Type", "application/json")
// TODO(theckman): switch to a package-local default client
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted {
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("HTTP Status Code: %d", resp.StatusCode)
}
return nil, fmt.Errorf("HTTP Status Code: %d, Message: %s", resp.StatusCode, string(bytes))
}
var eventResponse V2EventResponse
if err := json.NewDecoder(resp.Body).Decode(&eventResponse); err != nil {
return nil, err
}
return &eventResponse, nil
}
// ManageEvent handles the trigger, acknowledge, and resolve methods for an event
func (c *Client) ManageEvent(e *V2Event) (*V2EventResponse, error) {
headers := make(map[string]string)
data, err := json.Marshal(e)
if err != nil {
return nil, err
}
resp, err := c.doWithEndpoint(context.TODO(), c.v2EventsAPIEndpoint, http.MethodPost, "/v2/enqueue", false, bytes.NewBuffer(data), headers)
if err != nil {
return nil, err
}
result := &V2EventResponse{}
err = json.NewDecoder(resp.Body).Decode(result)
return result, err
}