-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathpagerduty.go
105 lines (91 loc) · 3.01 KB
/
pagerduty.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
package services
import (
"bytes"
"context"
texttemplate "text/template"
"github.com/PagerDuty/go-pagerduty"
log "github.com/sirupsen/logrus"
)
type PagerDutyNotification struct {
Title string `json:"title"`
Body string `json:"body,omitempty"`
Urgency string `json:"urgency,omitempty"`
PriorityId string `json:"priorityId,omitempty"`
}
type PagerdutyOptions struct {
Token string `json:"token"`
From string `json:"from,omitempty"`
ServiceID string `json:"serviceId"`
}
func (p *PagerDutyNotification) GetTemplater(name string, f texttemplate.FuncMap) (Templater, error) {
title, err := texttemplate.New(name).Funcs(f).Parse(p.Title)
if err != nil {
return nil, err
}
body, err := texttemplate.New(name).Funcs(f).Parse(p.Body)
if err != nil {
return nil, err
}
urgency, err := texttemplate.New(name).Funcs(f).Parse(p.Urgency)
if err != nil {
return nil, err
}
priorityId, err := texttemplate.New(name).Funcs(f).Parse(p.PriorityId)
if err != nil {
return nil, err
}
return func(notification *Notification, vars map[string]interface{}) error {
if notification.Pagerduty == nil {
notification.Pagerduty = &PagerDutyNotification{}
}
var titleData bytes.Buffer
if err := title.Execute(&titleData, vars); err != nil {
return err
}
notification.Pagerduty.Title = titleData.String()
var pdBodyData bytes.Buffer
if err := body.Execute(&pdBodyData, vars); err != nil {
return err
}
notification.Pagerduty.Body = pdBodyData.String()
var pdUrgencyData bytes.Buffer
if err := urgency.Execute(&pdUrgencyData, vars); err != nil {
return err
}
notification.Pagerduty.Urgency = pdUrgencyData.String()
var pdPriorityIDData bytes.Buffer
if err := priorityId.Execute(&pdPriorityIDData, vars); err != nil {
return err
}
notification.Pagerduty.PriorityId = pdPriorityIDData.String()
return nil
}, nil
}
func NewPagerdutyService(opts PagerdutyOptions) NotificationService {
return &pagerdutyService{opts: opts}
}
type pagerdutyService struct {
opts PagerdutyOptions
}
func (p pagerdutyService) Send(notification Notification, dest Destination) error {
title := notification.Pagerduty.Title
body := notification.Pagerduty.Body
urgency := notification.Pagerduty.Urgency
priorityID := notification.Pagerduty.PriorityId
pagerDutyClient := pagerduty.NewClient(p.opts.Token)
input := &pagerduty.CreateIncidentOptions{
Type: "incident",
Service: &pagerduty.APIReference{ID: dest.Recipient, Type: "service_reference"},
Priority: &pagerduty.APIReference{ID: priorityID, Type: "priority"},
Title: title,
Urgency: urgency,
Body: &pagerduty.APIDetails{Type: "incident_details ", Details: body},
}
incident, err := pagerDutyClient.CreateIncidentWithContext(context.TODO(), p.opts.From, input)
if err != nil {
log.Errorf("Error: %v", err)
return err
}
log.Debugf("Incident created Succesfully. Incident Number: %v, IncidentKey:%v, incident.ID: %v, incident.Title: %v", incident.IncidentNumber, incident.IncidentKey, incident.ID, incident.Title)
return nil
}