-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Watcher: migrate PagerDuty v1 events API to v2 API #32285
Changes from 15 commits
9705705
155b036
426c81d
003896a
d3e5921
1166ec0
942e62d
fdab5e3
4012f03
6644f2b
71629ae
c67d742
fa1fc07
dc7edb5
a0e68bb
f4ee457
6a23d31
ade99d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,8 @@ | |
public class IncidentEvent implements ToXContentObject { | ||
|
||
static final String HOST = "events.pagerduty.com"; | ||
static final String PATH = "/generic/2010-04-15/create_event.json"; | ||
static final String PATH = "/v2/enqueue"; | ||
static final String ACCEPT_HEADER = "application/vnd.pagerduty+json;version=2"; | ||
|
||
final String description; | ||
@Nullable final HttpProxy proxy; | ||
|
@@ -93,46 +94,96 @@ public int hashCode() { | |
return result; | ||
} | ||
|
||
public HttpRequest createRequest(final String serviceKey, final Payload payload) throws IOException { | ||
public HttpRequest createRequest(final String serviceKey, final Payload payload, final String watchId) throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. packabe visibility? |
||
return HttpRequest.builder(HOST, -1) | ||
.method(HttpMethod.POST) | ||
.scheme(Scheme.HTTPS) | ||
.path(PATH) | ||
.proxy(proxy) | ||
.jsonBody(new ToXContent() { | ||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.field(Fields.SERVICE_KEY.getPreferredName(), serviceKey); | ||
builder.field(Fields.EVENT_TYPE.getPreferredName(), eventType); | ||
builder.field(Fields.DESCRIPTION.getPreferredName(), description); | ||
if (incidentKey != null) { | ||
builder.field(Fields.INCIDENT_KEY.getPreferredName(), incidentKey); | ||
} | ||
if (client != null) { | ||
builder.field(Fields.CLIENT.getPreferredName(), client); | ||
} | ||
if (clientUrl != null) { | ||
builder.field(Fields.CLIENT_URL.getPreferredName(), clientUrl); | ||
} | ||
if (attachPayload) { | ||
builder.startObject(Fields.DETAILS.getPreferredName()); | ||
builder.field(Fields.PAYLOAD.getPreferredName()); | ||
payload.toXContent(builder, params); | ||
builder.endObject(); | ||
} | ||
if (contexts != null && contexts.length > 0) { | ||
builder.startArray(Fields.CONTEXTS.getPreferredName()); | ||
for (IncidentEventContext context : contexts) { | ||
context.toXContent(builder, params); | ||
} | ||
builder.endArray(); | ||
} | ||
return builder; | ||
} | ||
}) | ||
.setHeader("Accept", ACCEPT_HEADER) | ||
.jsonBody((b, p) -> buildAPIXContent(b, p, serviceKey, payload, watchId)) | ||
.build(); | ||
} | ||
|
||
public XContentBuilder buildAPIXContent(XContentBuilder builder, Params params, String serviceKey, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. package visibility? |
||
Payload payload, String watchId) throws IOException { | ||
builder.field(Fields.ROUTING_KEY.getPreferredName(), serviceKey); | ||
builder.field(Fields.EVENT_ACTION.getPreferredName(), eventType); | ||
if (incidentKey != null) { | ||
builder.field(Fields.DEDUP_KEY.getPreferredName(), incidentKey); | ||
} | ||
|
||
builder.startObject(Fields.PAYLOAD.getPreferredName()); | ||
{ | ||
builder.field(Fields.SUMMARY.getPreferredName(), description); | ||
|
||
if (attachPayload && payload != null) { | ||
builder.startObject(Fields.CUSTOM_DETAILS.getPreferredName()); | ||
{ | ||
builder.field(Fields.PAYLOAD.getPreferredName(), payload, params); | ||
} | ||
builder.endObject(); | ||
} | ||
|
||
if (watchId != null) { | ||
builder.field(Fields.SOURCE.getPreferredName(), watchId); | ||
} else { | ||
builder.field(Fields.SOURCE.getPreferredName(), "watcher"); | ||
} | ||
// TODO externalize this into something user editable | ||
builder.field(Fields.SEVERITY.getPreferredName(), "critical"); | ||
} | ||
builder.endObject(); | ||
|
||
if (client != null) { | ||
builder.field(Fields.CLIENT.getPreferredName(), client); | ||
} | ||
if (clientUrl != null) { | ||
builder.field(Fields.CLIENT_URL.getPreferredName(), clientUrl); | ||
} | ||
|
||
if (contexts != null && contexts.length > 0) { | ||
toXContentV2Contexts(builder, params, contexts); | ||
} | ||
|
||
return builder; | ||
} | ||
|
||
/** | ||
* Turns the V1 API contexts into 2 distinct lists, images and links. The V2 API has separated these out into 2 top level fields. | ||
*/ | ||
private void toXContentV2Contexts(XContentBuilder builder, ToXContent.Params params, | ||
IncidentEventContext[] contexts) throws IOException { | ||
// contexts can be either links or images, and the v2 api needs them separate | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about making this more readable (at least to me :-)
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dang i like those streams.. I still am a streaming noob. ty. |
||
List<IncidentEventContext> links = new ArrayList<>(); | ||
List<IncidentEventContext> images = new ArrayList<>(); | ||
for (IncidentEventContext context: contexts) { | ||
if (context.type == IncidentEventContext.Type.LINK) { | ||
links.add(context); | ||
} else if (context.type == IncidentEventContext.Type.IMAGE) { | ||
images.add(context); | ||
} | ||
} | ||
if (links.isEmpty() == false) { | ||
builder.startArray(Fields.LINKS.getPreferredName()); | ||
{ | ||
for (IncidentEventContext link: links) { | ||
link.toXContent(builder, params); | ||
} | ||
} | ||
builder.endArray(); | ||
} | ||
if (images.isEmpty() == false) { | ||
builder.startArray(Fields.IMAGES.getPreferredName()); | ||
{ | ||
for (IncidentEventContext image: images) { | ||
image.toXContent(builder, params); | ||
} | ||
} | ||
builder.endArray(); | ||
} | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
builder.startObject(); | ||
|
@@ -445,8 +496,15 @@ interface Fields { | |
// we need to keep this for BWC | ||
ParseField CONTEXT_DEPRECATED = new ParseField("context"); | ||
|
||
ParseField SERVICE_KEY = new ParseField("service_key"); | ||
ParseField PAYLOAD = new ParseField("payload"); | ||
ParseField DETAILS = new ParseField("details"); | ||
ParseField ROUTING_KEY = new ParseField("routing_key"); | ||
ParseField EVENT_ACTION = new ParseField("event_action"); | ||
ParseField DEDUP_KEY = new ParseField("dedup_key"); | ||
ParseField SUMMARY = new ParseField("summary"); | ||
ParseField SOURCE = new ParseField("source"); | ||
ParseField SEVERITY = new ParseField("severity"); | ||
ParseField LINKS = new ParseField("links"); | ||
ParseField IMAGES = new ParseField("images"); | ||
ParseField CUSTOM_DETAILS = new ParseField("custom_details"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,85 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |
return builder.endObject(); | ||
} | ||
|
||
public static IncidentEventContext parse(XContentParser parser) throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are straight copys of the Template's parse method, but without the TextTemplate mess. These will be used in the future when i finally finish removing the Templates :) |
||
Type type = null; | ||
String href = null; | ||
String text = null; | ||
String src = null; | ||
String alt = null; | ||
|
||
String currentFieldName = null; | ||
XContentParser.Token token; | ||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { | ||
if (token == XContentParser.Token.FIELD_NAME) { | ||
currentFieldName = parser.currentName(); | ||
} else if (Strings.hasLength(currentFieldName)) { | ||
if (XField.TYPE.match(currentFieldName, parser.getDeprecationHandler())) { | ||
try { | ||
type = Type.valueOf(parser.text().toUpperCase(Locale.ROOT)); | ||
} catch (IllegalArgumentException e) { | ||
String msg = "could not parse trigger incident event context. unknown context type [{}]"; | ||
throw new ElasticsearchParseException(msg, parser.text()); | ||
} | ||
} else { | ||
if (XField.HREF.match(currentFieldName, parser.getDeprecationHandler())) { | ||
href = parser.text(); | ||
} else if (XField.TEXT.match(currentFieldName, parser.getDeprecationHandler())) { | ||
text = parser.text(); | ||
} else if (XField.SRC.match(currentFieldName, parser.getDeprecationHandler())) { | ||
src = parser.text(); | ||
} else if (XField.ALT.match(currentFieldName, parser.getDeprecationHandler())) { | ||
alt = parser.text(); | ||
} else { | ||
String msg = "could not parse trigger incident event context. unknown field [{}]"; | ||
throw new ElasticsearchParseException(msg, currentFieldName); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return createAndValidateTemplate(type, href, src, alt, text); | ||
} | ||
|
||
private static IncidentEventContext createAndValidateTemplate(Type type, String href, String src, String alt, | ||
String text) { | ||
if (type == null) { | ||
throw new ElasticsearchParseException("could not parse trigger incident event context. missing required field [{}]", | ||
XField.TYPE.getPreferredName()); | ||
} | ||
|
||
switch (type) { | ||
case LINK: | ||
if (href == null) { | ||
throw new ElasticsearchParseException("could not parse trigger incident event context. missing required field " + | ||
"[{}] for [{}] context", XField.HREF.getPreferredName(), Type.LINK.name().toLowerCase(Locale.ROOT)); | ||
} | ||
if (src != null) { | ||
throw new ElasticsearchParseException("could not parse trigger incident event context. unexpected field [{}] for " + | ||
"[{}] context", XField.SRC.getPreferredName(), Type.LINK.name().toLowerCase(Locale.ROOT)); | ||
} | ||
if (alt != null) { | ||
throw new ElasticsearchParseException("could not parse trigger incident event context. unexpected field [{}] for " + | ||
"[{}] context", XField.ALT.getPreferredName(), Type.LINK.name().toLowerCase(Locale.ROOT)); | ||
} | ||
return link(href, text); | ||
case IMAGE: | ||
if (src == null) { | ||
throw new ElasticsearchParseException("could not parse trigger incident event context. missing required field " + | ||
"[{}] for [{}] context", XField.SRC.getPreferredName(), Type.IMAGE.name().toLowerCase(Locale.ROOT)); | ||
} | ||
if (text != null) { | ||
throw new ElasticsearchParseException("could not parse trigger incident event context. unexpected field [{}] for " + | ||
"[{}] context", XField.TEXT.getPreferredName(), Type.IMAGE.name().toLowerCase(Locale.ROOT)); | ||
} | ||
return image(src, href, alt); | ||
default: | ||
throw new ElasticsearchParseException("could not parse trigger incident event context. unknown context type [{}]", | ||
type); | ||
} | ||
} | ||
|
||
|
||
public static class Template implements ToXContentObject { | ||
|
||
final Type type; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe also fix the above links, as they probably link to the old documentation?