Skip to content
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

Merged
merged 18 commits into from
Aug 14, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
public class IncidentEvent implements ToXContentObject {
Copy link
Contributor

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?


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;
Expand Down Expand Up @@ -99,40 +100,85 @@ public HttpRequest createRequest(final String serviceKey, final Payload payload)
.scheme(Scheme.HTTPS)
.path(PATH)
.proxy(proxy)
.setHeader("Accept", ACCEPT_HEADER)
.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);
builder.field("routing_key", serviceKey);
builder.field("event_action", eventType);
if (incidentKey != null) {
builder.field(Fields.INCIDENT_KEY.getPreferredName(), incidentKey);
builder.field("dedup_key", incidentKey);
}

builder.startObject("payload");
{
builder.field("summary", description);

if (attachPayload) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make sure: This gets parsed properly on the other side if you leave it out (and dont have the custom_details structure)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

custom_details is not mandatory, so it will def leave it out. Ill add a null check for payload as well in this statement

builder.startObject("custom_details");
builder.field(Fields.PAYLOAD.getPreferredName());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace two lines with

builder.field(Fields.PAYLOAD.getPreferredName(), payload, params);

payload.toXContent(builder, params);
builder.endObject();
}

// TODO externalize this into something user editable
builder.field("source", "watcher");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe have the watch id in here (if possible) to easily pinpoint the source?

builder.field("severity", "critical");
}
builder.endObject();

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();
toXContentV2Contexts(builder, params, contexts);
}

return builder;
}
})
.build();
}

/**
* 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about making this more readable (at least to me :-)

        Map<IncidentEventContext.Type, List<IncidentEventContext>> groupByType =
            Arrays.stream(contexts).collect(Collectors.groupingBy(ctx -> ctx.type));

        List<IncidentEventContext> linkContexts = groupByType.get(IncidentEventContext.Type.LINK);
        if (linkContexts != null && linkContexts.isEmpty() == false) {
            builder.startArray(Fields.LINKS.getPreferredName());
            for (IncidentEventContext context : linkContexts) {
                context.toXContent(builder, params);
            }
            builder.endArray();
        }
        List<IncidentEventContext> imagesContexts = groupByType.get(IncidentEventContext.Type.IMAGE);
        if (imagesContexts != null && imagesContexts.isEmpty() == false) {
            builder.startArray(Fields.IMAGES.getPreferredName());
            for (IncidentEventContext context : imagesContexts) {
                context.toXContent(builder, params);
            }
            builder.endArray();
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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("links");
{
for (IncidentEventContext link: links) {
link.toXContent(builder, params);
}
}
builder.endArray();
}
if (images.isEmpty() == false) {
builder.startArray("images");
{
for (IncidentEventContext image: images) {
image.toXContent(builder, params);
}
}
builder.endArray();
}
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject();
Expand Down