forked from PagerDuty/go-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
46 lines (41 loc) · 1.18 KB
/
event.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
package pagerduty
import (
"bytes"
"encoding/json"
"fmt"
log "github.com/Sirupsen/logrus"
"net/http"
)
const EventEndPoint = "https://events.pagerduty.com/generic/2010-04-15/create_event.json"
type Event struct {
Type string `json:"event_type"`
ServiceKey string `json:"service_key"`
Description string `json:"description,omitempty"`
Client string `json:"client,omitempty"`
ClientURL string `json:"client_url,omitempty"`
Details interface{} `json:"details,omitempty"`
Contexts []interface{} `json:"contexts,omitempty"`
}
type EventResponse struct {
Status string
Message string
IncidentKey string
}
func CreateEvent(e Event) (*http.Response, error) {
log.Debugln("Endpoint:", EventEndPoint)
data, err := json.Marshal(e)
if err != nil {
return nil, err
}
log.Debugln(string(data))
req, _ := http.NewRequest("POST", EventEndPoint, bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return resp, fmt.Errorf("HTTP Status Code: %d", resp.StatusCode)
}
return resp, nil
}