Watcher: Put response code in payload in http input (elastic/elasticsearch#2888)

The response status code was stored in the result of an http input,
but inaccessible in the payload itself and could not be used in
scripts.

This puts the status code in the payload under the name '_status_code',
similar to the '_headers' variable, which already stores the headers.

Original commit: elastic/x-pack-elasticsearch@dff2a39535
This commit is contained in:
Alexander Reelsen 2016-07-25 14:57:57 +02:00 committed by GitHub
parent f02a9cdc35
commit 0b2b50be94
3 changed files with 22 additions and 7 deletions

View File

@ -53,10 +53,14 @@ public class ExecutableHttpInput extends ExecutableInput<HttpInput, HttpInput.Re
HttpInput.Result doExecute(WatchExecutionContext ctx, HttpRequest request) throws Exception {
HttpResponse response = client.execute(request);
Map<String, List<String>> headers = response.headers();
Map<String, Object> payloadMap = new HashMap<>();
payloadMap.put("_status_code", response.status());
if (headers.isEmpty() == false) {
payloadMap.put("_headers", headers);
}
if (!response.hasContent()) {
Payload payload = headers.size() > 0 ? new Payload.Simple("_headers", headers) : Payload.EMPTY;
return new HttpInput.Result(request, -1, payload);
return new HttpInput.Result(request, response.status(), new Payload.Simple(payloadMap));
}
final XContentType contentType;
@ -72,7 +76,6 @@ public class ExecutableHttpInput extends ExecutableInput<HttpInput, HttpInput.Re
}
}
final Map<String, Object> payloadMap = new HashMap<>();
if (contentType != null) {
try (XContentParser parser = contentType.xContent().createParser(response.body())) {
if (input.getExtractKeys() != null) {
@ -88,9 +91,6 @@ public class ExecutableHttpInput extends ExecutableInput<HttpInput, HttpInput.Re
payloadMap.put("_value", response.body().utf8ToString());
}
if (headers.size() > 0) {
payloadMap.put("_headers", headers);
}
return new HttpInput.Result(request, response.status(), new Payload.Simple(payloadMap));
}
}

View File

@ -143,7 +143,7 @@ public class HttpInput implements Input {
public static class Result extends Input.Result {
@Nullable private final HttpRequest request;
private final int statusCode;
final int statusCode;
public Result(HttpRequest request, int statusCode, Payload payload) {
super(TYPE, payload);

View File

@ -289,6 +289,21 @@ public class HttpInputTests extends ESTestCase {
assertThat(result.payload().data(), not(hasKey("foo")));
}
public void testThatStatusCodeIsSetInResultAndPayload() throws Exception {
HttpResponse response = new HttpResponse(200);
when(httpClient.execute(any(HttpRequest.class))).thenReturn(response);
HttpRequestTemplate.Builder request = HttpRequestTemplate.builder("localhost", 8080);
HttpInput httpInput = InputBuilders.httpInput(request.build()).build();
ExecutableHttpInput input = new ExecutableHttpInput(httpInput, logger, httpClient, templateEngine);
WatchExecutionContext ctx = createWatchExecutionContext();
HttpInput.Result result = input.execute(ctx, new Payload.Simple());
assertThat(result.statusCode, is(200));
assertThat(result.payload().data(), hasKey("_status_code"));
assertThat(result.payload().data().get("_status_code"), is(200));
}
private WatchExecutionContext createWatchExecutionContext() {
Watch watch = new Watch("test-watch",
new ScheduleTrigger(new IntervalSchedule(new IntervalSchedule.Interval(1, IntervalSchedule.Interval.Unit.MINUTES))),