1package pagerduty 2 3import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9) 10 11// Event includes the incident/alert details 12type V2Event struct { 13 RoutingKey string `json:"routing_key"` 14 Action string `json:"event_action"` 15 DedupKey string `json:"dedup_key,omitempty"` 16 Images []interface{} `json:"images,omitempty"` 17 Client string `json:"client,omitempty"` 18 ClientURL string `json:"client_url,omitempty"` 19 Payload *V2Payload `json:"payload,omitempty"` 20} 21 22// Payload represents the individual event details for an event 23type V2Payload struct { 24 Summary string `json:"summary"` 25 Source string `json:"source"` 26 Severity string `json:"severity"` 27 Timestamp string `json:"timestamp,omitempty"` 28 Component string `json:"component,omitempty"` 29 Group string `json:"group,omitempty"` 30 Class string `json:"class,omitempty"` 31 Details interface{} `json:"custom_details,omitempty"` 32} 33 34// Response is the json response body for an event 35type V2EventResponse struct { 36 RoutingKey string `json:"routing_key"` 37 DedupKey string `json:"dedup_key"` 38 EventAction string `json:"event_action"` 39} 40 41const v2eventEndPoint = "https://events.pagerduty.com/v2/enqueue" 42 43// ManageEvent handles the trigger, acknowledge, and resolve methods for an event 44func ManageEvent(e V2Event) (*V2EventResponse, error) { 45 data, err := json.Marshal(e) 46 if err != nil { 47 return nil, err 48 } 49 req, _ := http.NewRequest("POST", v2eventEndPoint, bytes.NewBuffer(data)) 50 req.Header.Set("Content-Type", "application/json") 51 resp, err := http.DefaultClient.Do(req) 52 if err != nil { 53 return nil, err 54 } 55 defer resp.Body.Close() 56 if resp.StatusCode != http.StatusAccepted { 57 bytes, err := ioutil.ReadAll(resp.Body) 58 if err != nil { 59 return nil, fmt.Errorf("HTTP Status Code: %d", resp.StatusCode) 60 } 61 return nil, fmt.Errorf("HTTP Status Code: %d, Message: %s", resp.StatusCode, string(bytes)) 62 } 63 var eventResponse V2EventResponse 64 if err := json.NewDecoder(resp.Body).Decode(&eventResponse); err != nil { 65 return nil, err 66 } 67 return &eventResponse, nil 68} 69