|
| 1 | +package services |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + texttemplate "text/template" |
| 8 | + |
| 9 | + "github.com/PagerDuty/go-pagerduty" |
| 10 | + log "github.com/sirupsen/logrus" |
| 11 | +) |
| 12 | + |
| 13 | +type PagerDutyV2Notification struct { |
| 14 | + Summary string `json:"summary"` |
| 15 | + Severity string `json:"severity"` |
| 16 | + Source string `json:"source"` |
| 17 | + Component string `json:"component,omitempty"` |
| 18 | + Group string `json:"group,omitempty"` |
| 19 | + Class string `json:"class,omitempty"` |
| 20 | + URL string `json:"url"` |
| 21 | +} |
| 22 | + |
| 23 | +type PagerdutyV2Options struct { |
| 24 | + ServiceKeys map[string]string `json:"serviceKeys"` |
| 25 | +} |
| 26 | + |
| 27 | +func (p *PagerDutyV2Notification) GetTemplater(name string, f texttemplate.FuncMap) (Templater, error) { |
| 28 | + summary, err := texttemplate.New(name).Funcs(f).Parse(p.Summary) |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + severity, err := texttemplate.New(name).Funcs(f).Parse(p.Severity) |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + source, err := texttemplate.New(name).Funcs(f).Parse(p.Source) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + component, err := texttemplate.New(name).Funcs(f).Parse(p.Component) |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + group, err := texttemplate.New(name).Funcs(f).Parse(p.Group) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + class, err := texttemplate.New(name).Funcs(f).Parse(p.Class) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + url, err := texttemplate.New(name).Funcs(f).Parse(p.URL) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + return func(notification *Notification, vars map[string]interface{}) error { |
| 58 | + if notification.PagerdutyV2 == nil { |
| 59 | + notification.PagerdutyV2 = &PagerDutyV2Notification{} |
| 60 | + } |
| 61 | + var summaryData bytes.Buffer |
| 62 | + if err := summary.Execute(&summaryData, vars); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + notification.PagerdutyV2.Summary = summaryData.String() |
| 66 | + |
| 67 | + var severityData bytes.Buffer |
| 68 | + if err := severity.Execute(&severityData, vars); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + notification.PagerdutyV2.Severity = severityData.String() |
| 72 | + |
| 73 | + var sourceData bytes.Buffer |
| 74 | + if err := source.Execute(&sourceData, vars); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + notification.PagerdutyV2.Source = sourceData.String() |
| 78 | + |
| 79 | + var componentData bytes.Buffer |
| 80 | + if err := component.Execute(&componentData, vars); err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + notification.PagerdutyV2.Component = componentData.String() |
| 84 | + |
| 85 | + var groupData bytes.Buffer |
| 86 | + if err := group.Execute(&groupData, vars); err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + notification.PagerdutyV2.Group = groupData.String() |
| 90 | + |
| 91 | + var classData bytes.Buffer |
| 92 | + if err := class.Execute(&classData, vars); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + notification.PagerdutyV2.Class = classData.String() |
| 96 | + |
| 97 | + var urlData bytes.Buffer |
| 98 | + if err := url.Execute(&urlData, vars); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + notification.PagerdutyV2.URL = urlData.String() |
| 102 | + |
| 103 | + return nil |
| 104 | + }, nil |
| 105 | +} |
| 106 | + |
| 107 | +func NewPagerdutyV2Service(opts PagerdutyV2Options) NotificationService { |
| 108 | + return &pagerdutyV2Service{opts: opts} |
| 109 | +} |
| 110 | + |
| 111 | +type pagerdutyV2Service struct { |
| 112 | + opts PagerdutyV2Options |
| 113 | +} |
| 114 | + |
| 115 | +func (p pagerdutyV2Service) Send(notification Notification, dest Destination) error { |
| 116 | + routingKey, ok := p.opts.ServiceKeys[dest.Recipient] |
| 117 | + if !ok { |
| 118 | + return fmt.Errorf("no API key configured for recipient %s", dest.Recipient) |
| 119 | + } |
| 120 | + |
| 121 | + if notification.PagerdutyV2 == nil { |
| 122 | + return fmt.Errorf("no config found for pagerdutyv2") |
| 123 | + } |
| 124 | + |
| 125 | + event := buildEvent(routingKey, notification) |
| 126 | + |
| 127 | + response, err := pagerduty.ManageEventWithContext(context.TODO(), event) |
| 128 | + if err != nil { |
| 129 | + log.Errorf("Error: %v", err) |
| 130 | + return err |
| 131 | + } |
| 132 | + log.Debugf("PagerDuty event triggered succesfully. Status: %v, Message: %v", response.Status, response.Message) |
| 133 | + return nil |
| 134 | +} |
| 135 | + |
| 136 | +func buildEvent(routingKey string, notification Notification) pagerduty.V2Event { |
| 137 | + payload := pagerduty.V2Payload{ |
| 138 | + Summary: notification.PagerdutyV2.Summary, |
| 139 | + Severity: notification.PagerdutyV2.Severity, |
| 140 | + Source: notification.PagerdutyV2.Source, |
| 141 | + } |
| 142 | + |
| 143 | + if len(notification.PagerdutyV2.Component) > 0 { |
| 144 | + payload.Component = notification.PagerdutyV2.Component |
| 145 | + } |
| 146 | + if len(notification.PagerdutyV2.Group) > 0 { |
| 147 | + payload.Group = notification.PagerdutyV2.Group |
| 148 | + } |
| 149 | + if len(notification.PagerdutyV2.Class) > 0 { |
| 150 | + payload.Class = notification.PagerdutyV2.Class |
| 151 | + } |
| 152 | + |
| 153 | + event := pagerduty.V2Event{ |
| 154 | + RoutingKey: routingKey, |
| 155 | + Action: "trigger", |
| 156 | + Payload: &payload, |
| 157 | + Client: "ArgoCD", |
| 158 | + } |
| 159 | + |
| 160 | + if len(notification.PagerdutyV2.URL) > 0 { |
| 161 | + event.ClientURL = notification.PagerdutyV2.URL |
| 162 | + } |
| 163 | + |
| 164 | + return event |
| 165 | +} |
0 commit comments