File size: 4,480 Bytes
1e92f2d |
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
package webhook
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/target/goalert/config"
"github.com/target/goalert/notification"
"github.com/target/goalert/notification/nfydest"
)
type Sender struct{}
// POSTDataAlert represents fields in outgoing alert notification.
type POSTDataAlert struct {
AppName string
Type string
AlertID int
Summary string
Details string
ServiceID string
ServiceName string
Meta map[string]string
}
// POSTDataAlertBundle represents fields in outgoing alert bundle notification.
type POSTDataAlertBundle struct {
AppName string
Type string
ServiceID string
ServiceName string
Count int
}
// POSTDataAlertStatus represents fields in outgoing alert status notification.
type POSTDataAlertStatus struct {
AppName string
Type string
AlertID int
LogEntry string
}
// POSTDataAlertStatusBundle represents fields in outgoing alert status bundle notification.
type POSTDataAlertStatusBundle struct {
AppName string
Type string
AlertID int
LogEntry string
Count int
}
// POSTDataVerification represents fields in outgoing verification notification.
type POSTDataVerification struct {
AppName string
Type string
Code string
}
// POSTDataOnCallUser represents User fields in outgoing on call notification.
type POSTDataOnCallUser struct {
ID string
Name string
URL string
}
// POSTDataOnCallNotification represents fields in outgoing on call notification.
type POSTDataOnCallNotification struct {
AppName string
Type string
Users []POSTDataOnCallUser
ScheduleID string
ScheduleName string
ScheduleURL string
}
// POSTDataTest represents fields in outgoing test notification.
type POSTDataTest struct {
AppName string
Type string
}
func NewSender(ctx context.Context) *Sender {
return &Sender{}
}
var _ nfydest.MessageSender = &Sender{}
// Send will send an alert for the provided message type
func (s *Sender) SendMessage(ctx context.Context, msg notification.Message) (*notification.SentMessage, error) {
cfg := config.FromContext(ctx)
var payload interface{}
switch m := msg.(type) {
case notification.Test:
payload = POSTDataTest{
AppName: cfg.ApplicationName(),
Type: "Test",
}
case notification.Verification:
payload = POSTDataVerification{
AppName: cfg.ApplicationName(),
Type: "Verification",
Code: m.Code,
}
case notification.Alert:
payload = POSTDataAlert{
AppName: cfg.ApplicationName(),
Type: "Alert",
Details: m.Details,
AlertID: m.AlertID,
Summary: m.Summary,
ServiceID: m.ServiceID,
ServiceName: m.ServiceName,
Meta: m.Meta,
}
case notification.AlertBundle:
payload = POSTDataAlertBundle{
AppName: cfg.ApplicationName(),
Type: "AlertBundle",
ServiceID: m.ServiceID,
ServiceName: m.ServiceName,
Count: m.Count,
}
case notification.AlertStatus:
payload = POSTDataAlertStatus{
AppName: cfg.ApplicationName(),
Type: "AlertStatus",
AlertID: m.AlertID,
LogEntry: m.LogEntry,
}
case notification.ScheduleOnCallUsers:
// We use types defined in this package to insulate against unintended API
// changes.
users := make([]POSTDataOnCallUser, len(m.Users))
for i, u := range m.Users {
users[i] = POSTDataOnCallUser(u)
}
payload = POSTDataOnCallNotification{
AppName: cfg.ApplicationName(),
Type: "ScheduleOnCallUsers",
Users: users,
ScheduleID: m.ScheduleID,
ScheduleName: m.ScheduleName,
ScheduleURL: m.ScheduleURL,
}
default:
return nil, fmt.Errorf("message type '%T' not supported", m)
}
data, err := json.Marshal(payload)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(ctx, time.Second*3)
defer cancel()
webURL := msg.DestArg(FieldWebhookURL)
if !cfg.ValidWebhookURL(webURL) {
// fail permanently if the URL is not currently valid/allowed
return ¬ification.SentMessage{
State: notification.StateFailedPerm,
StateDetails: "invalid or not allowed URL",
}, nil
}
req, err := http.NewRequestWithContext(ctx, "POST", webURL, bytes.NewReader(data))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
_, err = http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
return ¬ification.SentMessage{State: notification.StateSent}, nil
}
|