-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPagerDutyEvent.cfc
More file actions
117 lines (99 loc) · 3.37 KB
/
Copy pathPagerDutyEvent.cfc
File metadata and controls
117 lines (99 loc) · 3.37 KB
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
component accessors = "true" {
property name = "component" type = "string";
property name = "customDetails" type = "struct";
property name = "eventAction" type = "string" setter = "false";
property name = "severity" type = "string" setter = "false";
property name = "summary" type = "string" default = "info";
property name = "timestamp" type = "date";
property name = "type" type = "string";
PagerDutyEvent function init(required IPagerDutyClient pagerDutyClient, required string eventKey) {
variables.pagerDutyClient = arguments.pagerDutyClient;
variables.eventKey = arguments.eventKey;
variables.customDetails = {};
variables.images = [];
variables.links = [];
variables.timestamp = now();
return this;
}
struct function acknowledge() {
return postToPagerDuty("acknowledge");
}
PagerDutyEvent function addImage(required string src, string href, string alt) {
arrayAppend(
variables.images,
{
"src": arguments.src,
"href": structKeyExists(arguments, "href") ? arguments.href : javaCast("null", ""),
"alt": structKeyExists(arguments, "alt") ? arguments.alt : javaCast("null", "")
}
);
return this;
}
PagerDutyEvent function addLink(required string href, string text) {
arrayAppend(
variables.links,
{
"href": arguments.href,
"text": structKeyExists(arguments, "text") ? arguments.text : javaCast("null", "")
}
);
return this;
}
struct function resolve() {
return postToPagerDuty("resolve");
}
PagerDutyEvent function setSeverity(required string severity) {
if(arrayFindNoCase([ "critical", "error", "info", "warning" ], arguments.severity)) {
variables.severity = arguments.severity;
} else {
throw(type = "PagerDutyEvent.InvalidSeverity", message = "The severity furnished is not valid; please use one of the following: critical, error, info, or warning");
}
return this;
}
struct function trigger() {
return postToPagerDuty("trigger");
}
private struct function postToPagerDuty(required string eventAction) {
if(!structKeyExists(variables, "severity") || !structKeyExists(variables, "summary")) {
throw(type = "PagerDutyEvent.MissingParameter", message = "Severity and summary must be set");
} else if(structKeyExists(variables, "httpResult")) {
return {
statusCode: 500,
statusText: "This event has already been processed"
};
}
local.payload = serializeJSON({
"dedup_key": left(variables.eventKey, 255),
"event_action": arguments.eventAction,
"images": variables.images,
"links": variables.links,
"payload": {
"class": getType(),
"custom_details": getCustomDetails(),
"group": variables.pagerDutyClient.getApplicationName(),
"severity": getSeverity(),
"source": variables.pagerDutyClient.getApplicationURL(),
"summary": left(getSummary(), 1024),
"timestamp": dateTimeFormat(getTimestamp(), "yyyy-mm-dd'T'HH:nn:ss.lZ")
},
"routing_key": variables.pagerDutyClient.getPagerDutyKey()
});
try {
cfhttp(
method = "POST",
result = "variables.httpResult",
timeout = "10",
url = "https://events.pagerduty.com/v2/enqueue"
) {
cfhttpparam(type = "header", name = "Content-type", value = "application/json");
cfhttpparam(type = "body", value = local.payload);
};
} catch(Any e) {
variables.httpResult = {
statusCode: 500,
statusText: e.message & ":" & e.details
};
}
return variables.httpResult;
}
}