Changed Execute Watch API response format

- the response now returns the id of the stored watch record
- update/fixed the docs

Closes elastic/elasticsearch#538

Original commit: elastic/x-pack-elasticsearch@5df7d166f4
This commit is contained in:
uboness 2015-05-27 21:07:58 +02:00
parent fb893e774a
commit 45d0fdf3de
11 changed files with 51 additions and 38 deletions

View File

@ -74,11 +74,11 @@
},
"record_execution" : true
}
- match: { "watch_id": "my_exe_watch" }
- match: { "state": "executed" }
- match: { "trigger_event.manual.schedule.scheduled_time": "2015-05-05T20:58:02.443Z" }
- match: { "execution_result.condition.always": {} }
- match: { "execution_result.input.simple.payload.foo": "bar" }
- match: { "execution_result.actions.0.id" : "email_admin" }
- match: { "execution_result.actions.0.email.status" : "simulated" }
- match: { "execution_result.actions.0.email.email.subject" : "404 recently encountered" }
- match: { "watch_record.watch_id": "my_exe_watch" }
- match: { "watch_record.state": "executed" }
- match: { "watch_record.trigger_event.manual.schedule.scheduled_time": "2015-05-05T20:58:02.443Z" }
- match: { "watch_record.execution_result.condition.always": {} }
- match: { "watch_record.execution_result.input.simple.payload.foo": "bar" }
- match: { "watch_record.execution_result.actions.0.id" : "email_admin" }
- match: { "watch_record.execution_result.actions.0.email.status" : "simulated" }
- match: { "watch_record.execution_result.actions.0.email.email.subject" : "404 recently encountered" }

View File

@ -43,9 +43,9 @@
}
}
- match: { "watch_id": "my_logging_watch" }
- match: { "execution_result.condition.script.met": true }
- match: { "state": "executed" }
- match: { "execution_result.actions.0.id" : "logging" }
- match: { "execution_result.actions.0.logging.status" : "success" }
- match: { "execution_result.actions.0.logging.logged_text" : "foobar" }
- match: { "watch_record.watch_id": "my_logging_watch" }
- match: { "watch_record.execution_result.condition.script.met": true }
- match: { "watch_record.state": "executed" }
- match: { "watch_record.execution_result.actions.0.id" : "logging" }
- match: { "watch_record.execution_result.actions.0.logging.status" : "success" }
- match: { "watch_record.execution_result.actions.0.logging.logged_text" : "foobar" }

View File

@ -197,7 +197,7 @@ public class WatchRecord implements ToXContent {
try {
return valueOf(id.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException iae) {
throw new WatcherSettingsException("unknown watch record state [" + id + "]");
throw new WatcherSettingsException("unknown watch record state [{}]", id);
}
}

View File

@ -9,6 +9,7 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
@ -41,13 +42,16 @@ public class RestExecuteWatchAction extends WatcherRestHandler {
}
@Override
protected void handleRequest(RestRequest request, RestChannel channel, WatcherClient client) throws Exception {
protected void handleRequest(final RestRequest request, RestChannel channel, WatcherClient client) throws Exception {
ExecuteWatchRequest executeWatchRequest = parseRequest(request, client);
client.executeWatch(executeWatchRequest, new RestBuilderListener<ExecuteWatchResponse>(channel) {
@Override
public RestResponse buildResponse(ExecuteWatchResponse response, XContentBuilder builder) throws Exception {
builder.value(response.getSource());
builder.startObject();
builder.field(Field.ID.getPreferredName(), response.getRecordId());
builder.field(Field.WATCH_RECORD.getPreferredName(), response.getRecordSource(), ToXContent.EMPTY_PARAMS);
builder.endObject();
return new BytesRestResponse(RestStatus.OK, builder);
}
});
@ -127,6 +131,9 @@ public class RestExecuteWatchAction extends WatcherRestHandler {
}
interface Field {
ParseField ID = new ParseField("_id");
ParseField WATCH_RECORD = new ParseField("watch_record");
ParseField RECORD_EXECUTION = new ParseField("record_execution");
ParseField ACTION_MODES = new ParseField("action_modes");
ParseField ALTERNATIVE_INPUT = new ParseField("alternative_input");

View File

@ -6,7 +6,6 @@
package org.elasticsearch.watcher.transport.actions.execute;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
@ -19,34 +18,42 @@ import java.io.IOException;
*/
public class ExecuteWatchResponse extends ActionResponse {
private XContentSource source;
private String recordId;
private XContentSource recordSource;
public ExecuteWatchResponse() {
}
public ExecuteWatchResponse(@Nullable BytesReference source) {
this.source = source != null ? new XContentSource(source) : null;
public ExecuteWatchResponse(String recordId, BytesReference recordSource) {
this.recordId = recordId;
this.recordSource = new XContentSource(recordSource);
}
/**
* @return The id of the watch record holding the watch execution result.
*/
public String getRecordId() {
return recordId;
}
/**
* @return The watch record source
*/
public XContentSource getSource() {
return source;
public XContentSource getRecordSource() {
return recordSource;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
source = in.readBoolean() ? XContentSource.readFrom(in) : null;
recordId = in.readString();
recordSource = XContentSource.readFrom(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeBoolean(source != null);
if (source != null) {
XContentSource.writeTo(source, out);
}
out.writeString(recordId);
XContentSource.writeTo(recordSource, out);
}
}

View File

@ -81,7 +81,7 @@ public class TransportExecuteWatchAction extends WatcherTransportAction<ExecuteW
try {
Watch watch = watchStore.get(request.getId());
if (watch == null) {
throw new WatcherException("watch [" + request.getId() + "] does not exist");
throw new WatcherException("watch [{}] does not exist", request.getId());
}
TriggerEvent triggerEvent = triggerService.parseTriggerEvent(watch.id(), watch.id() + "_manual_execution", request.getTriggerType(), request.getTriggerSource());
@ -103,7 +103,7 @@ public class TransportExecuteWatchAction extends WatcherTransportAction<ExecuteW
WatchRecord record = executionService.execute(ctxBuilder.build());
XContentBuilder builder = XContentFactory.jsonBuilder();
record.toXContent(builder, WatcherParams.builder().hideSecrets(true).build());
ExecuteWatchResponse response = new ExecuteWatchResponse(builder.bytes());
ExecuteWatchResponse response = new ExecuteWatchResponse(record.id().value(), builder.bytes());
listener.onResponse(response);
} catch (Exception e) {
logger.error("failed to execute [{}]", e, request.getId());

View File

@ -49,7 +49,6 @@ public class PutWatchResponse extends ActionResponse {
out.writeBoolean(created);
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);

View File

@ -210,7 +210,7 @@ public class ManualExecutionTests extends AbstractWatcherIntegrationTests {
.setTriggerEvent(triggerEvent)
.get();
WatchRecord watchRecord = watchRecordParser.parse(wid.value(), 1, executeWatchResponse.getSource().getBytes());
WatchRecord watchRecord = watchRecordParser.parse(wid.value(), 1, executeWatchResponse.getRecordSource().getBytes());
assertThat(watchRecord.state(), equalTo(WatchRecord.State.EXECUTION_NOT_NEEDED));
assertThat(watchRecord.execution().inputResult().payload().data().get("foo").toString(), equalTo("bar"));
@ -225,7 +225,7 @@ public class ManualExecutionTests extends AbstractWatcherIntegrationTests {
executeWatchResponse = watcherClient().prepareExecuteWatch().setId("_id").setTriggerEvent(triggerEvent).setRecordExecution(true).get();
watchRecord = watchRecordParser.parse(wid.value(), 1, executeWatchResponse.getSource().getBytes());
watchRecord = watchRecordParser.parse(wid.value(), 1, executeWatchResponse.getRecordSource().getBytes());
assertThat(watchRecord.state(), equalTo(WatchRecord.State.EXECUTED));
@ -235,7 +235,7 @@ public class ManualExecutionTests extends AbstractWatcherIntegrationTests {
executeWatchResponse = watcherClient().prepareExecuteWatch().setId("_id").setTriggerEvent(triggerEvent).get();
watchRecord = watchRecordParser.parse(wid.value(), 1, executeWatchResponse.getSource().getBytes());
watchRecord = watchRecordParser.parse(wid.value(), 1, executeWatchResponse.getRecordSource().getBytes());
assertThat(watchRecord.state(), equalTo(WatchRecord.State.THROTTLED));
}

View File

@ -142,7 +142,7 @@ public class EmailSecretsIntegrationTests extends AbstractWatcherIntegrationTest
.setTriggerEvent(triggerEvent)
.get();
assertThat(executeResponse, notNullValue());
contentSource = executeResponse.getSource();
contentSource = executeResponse.getRecordSource();
value = contentSource.getValue("execution_result.actions.0.email.status");
assertThat((String) value, is("success"));

View File

@ -151,7 +151,7 @@ public class HttpSecretsIntegrationTests extends AbstractWatcherIntegrationTests
.setTriggerEvent(triggerEvent)
.get();
assertThat(executeResponse, notNullValue());
contentSource = executeResponse.getSource();
contentSource = executeResponse.getRecordSource();
value = contentSource.getValue("execution_result.input.http.status");
assertThat(value, notNullValue());
assertThat(value, is((Object) 200));
@ -223,7 +223,7 @@ public class HttpSecretsIntegrationTests extends AbstractWatcherIntegrationTests
.setTriggerEvent(triggerEvent)
.get();
assertThat(executeResponse, notNullValue());
contentSource = executeResponse.getSource();
contentSource = executeResponse.getRecordSource();
value = contentSource.getValue("execution_result.actions.0.webhook.response.status");
assertThat(value, notNullValue());

View File

@ -99,7 +99,7 @@ public class WatchMetadataTests extends AbstractWatcherIntegrationTests {
TriggerEvent triggerEvent = new ScheduleTriggerEvent(new DateTime(UTC), new DateTime(UTC));
ExecuteWatchResponse executeWatchResponse = watcherClient().prepareExecuteWatch("_name").setTriggerEvent(triggerEvent).setActionMode("_all", ActionExecutionMode.SIMULATE).get();
WatchRecord record = parser.parse("test_run", 1, executeWatchResponse.getSource().getBytes());
WatchRecord record = parser.parse("test_run", 1, executeWatchResponse.getRecordSource().getBytes());
assertThat(record.metadata().get("foo").toString(), equalTo("bar"));
assertThat(record.execution().actionsResults().get("testLogger").action(), instanceOf(LoggingAction.Result.Simulated.class));
LoggingAction.Result.Simulated simulatedResult = (LoggingAction.Result.Simulated) (record.execution().actionsResults().get("testLogger").action());