From 31fbbcfc9a1807ff9b03522163bc16b8c0cb53b6 Mon Sep 17 00:00:00 2001 From: Brian Murphy Date: Mon, 6 Apr 2015 11:29:36 -0400 Subject: [PATCH] Add support for testing all REST endpoints. This change adds support for testing all watcher REST endpoints. It also updates the api docs to be current with the latest code. Change GetWatchResponse to only have the information in needs GetWatchResponse used to contain a GetResponse this is not needed. Now it just contains the needed fields. Closes elastic/elasticsearch#35 Original commit: elastic/x-pack-elasticsearch@905c5da318d97893aea5bd74ce9ed55292297411 --- rest-api-spec/api/ack_watch.json | 25 +++++++ rest-api-spec/api/cluster.health.json | 55 ++++++++++++++++ rest-api-spec/api/delete_watch.json | 25 +++++++ rest-api-spec/api/get_watch.json | 25 +++++++ rest-api-spec/api/info.json | 15 +++++ rest-api-spec/api/put_watch.json | 28 ++++++++ rest-api-spec/api/restart_watcher.json | 15 +++++ rest-api-spec/api/start_watcher.json | 15 +++++ rest-api-spec/api/stop_watcher.json | 15 +++++ rest-api-spec/api/watcher_info.json | 15 +++++ .../api/{stats.json => watcher_stats.json} | 6 +- rest-api-spec/test/ack_watch/10_basic.yaml | 53 +++++++++++++++ rest-api-spec/test/delete_watch/10_basic.yaml | 44 +++++++++++++ rest-api-spec/test/get_watch/10_basic.yaml | 46 +++++++++++++ rest-api-spec/test/put_watch/10_basic.yaml | 39 +++++++++++ .../test/restart_watcher/10_basic.yaml | 8 +++ .../test/start_watcher/10_basic.yaml | 8 +++ rest-api-spec/test/stop_watcher/10_basic.yaml | 11 ++++ rest-api-spec/test/watch_info/10_basic.yaml | 6 ++ .../rest/action/RestAckWatchAction.java | 3 +- .../rest/action/RestGetWatchAction.java | 12 ++-- .../rest/action/RestWatchServiceAction.java | 39 ++++++----- .../rest/action/RestWatcherInfoAction.java | 3 +- .../actions/get/GetWatchResponse.java | 66 ++++++++++++++----- .../actions/get/TransportGetWatchAction.java | 33 ++++++---- .../watcher/watch/WatchService.java | 9 ++- .../test/integration/BasicWatcherTests.java | 4 +- .../test/integration/WatchThrottleTests.java | 6 +- .../watcher/test/rest/WatcherRestTests.java | 6 +- 29 files changed, 570 insertions(+), 65 deletions(-) create mode 100644 rest-api-spec/api/ack_watch.json create mode 100644 rest-api-spec/api/cluster.health.json create mode 100644 rest-api-spec/api/delete_watch.json create mode 100644 rest-api-spec/api/get_watch.json create mode 100644 rest-api-spec/api/info.json create mode 100644 rest-api-spec/api/put_watch.json create mode 100644 rest-api-spec/api/restart_watcher.json create mode 100644 rest-api-spec/api/start_watcher.json create mode 100644 rest-api-spec/api/stop_watcher.json create mode 100644 rest-api-spec/api/watcher_info.json rename rest-api-spec/api/{stats.json => watcher_stats.json} (64%) create mode 100644 rest-api-spec/test/ack_watch/10_basic.yaml create mode 100644 rest-api-spec/test/delete_watch/10_basic.yaml create mode 100644 rest-api-spec/test/get_watch/10_basic.yaml create mode 100644 rest-api-spec/test/put_watch/10_basic.yaml create mode 100644 rest-api-spec/test/restart_watcher/10_basic.yaml create mode 100644 rest-api-spec/test/start_watcher/10_basic.yaml create mode 100644 rest-api-spec/test/stop_watcher/10_basic.yaml create mode 100644 rest-api-spec/test/watch_info/10_basic.yaml diff --git a/rest-api-spec/api/ack_watch.json b/rest-api-spec/api/ack_watch.json new file mode 100644 index 00000000000..1db905d7db0 --- /dev/null +++ b/rest-api-spec/api/ack_watch.json @@ -0,0 +1,25 @@ +{ + "ack_watch": { + "documentation": "http://www.elastic.co/guide/en/watcher/current/appendix-api-ack-watch.html", + "methods": [ "PUT", "POST" ], + "url": { + "path": "/_watcher/watch/{id}/_ack", + "paths": [ "/_watcher/watch/{id}/_ack" ], + "parts": { + "id": { + "type" : "string", + "description" : "Watch ID", + "required" : true + } + }, + "params": { + "pretty": { + "type": "boolean", + "description": "Pretty the output", + "default": false + } + } + }, + "body": null + } +} diff --git a/rest-api-spec/api/cluster.health.json b/rest-api-spec/api/cluster.health.json new file mode 100644 index 00000000000..9d679d673fc --- /dev/null +++ b/rest-api-spec/api/cluster.health.json @@ -0,0 +1,55 @@ +{ + "cluster.health": { + "documentation": "http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.x/cluster-health.html", + "methods": ["GET"], + "url": { + "path": "/_cluster/health", + "paths": ["/_cluster/health", "/_cluster/health/{index}"], + "parts": { + "index": { + "type" : "string", + "description" : "Limit the information returned to a specific index" + } + }, + "params": { + "level": { + "type" : "enum", + "options" : ["cluster","indices","shards"], + "default" : "cluster", + "description" : "Specify the level of detail for returned information" + }, + "local": { + "type" : "boolean", + "description" : "Return local information, do not retrieve the state from master node (default: false)" + }, + "master_timeout": { + "type" : "time", + "description" : "Explicit operation timeout for connection to master node" + }, + "timeout": { + "type" : "time", + "description" : "Explicit operation timeout" + }, + "wait_for_active_shards": { + "type" : "number", + "description" : "Wait until the specified number of shards is active" + }, + "wait_for_nodes": { + "type" : "string", + "description" : "Wait until the specified number of nodes is available" + }, + "wait_for_relocating_shards": { + "type" : "number", + "description" : "Wait until the specified number of relocating shards is finished" + }, + "wait_for_status": { + "type" : "enum", + "options" : ["green","yellow","red"], + "default" : null, + "description" : "Wait until cluster is in a specific state" + } + } + }, + "body": null + } +} diff --git a/rest-api-spec/api/delete_watch.json b/rest-api-spec/api/delete_watch.json new file mode 100644 index 00000000000..6ea5d8e7619 --- /dev/null +++ b/rest-api-spec/api/delete_watch.json @@ -0,0 +1,25 @@ +{ + "delete_watch": { + "documentation": "http://www.elastic.co/guide/en/watcher/current/appendix-api-delete-watch.html", + "methods": [ "DELETE" ], + "url": { + "path": "/_watcher/watch/{id}", + "paths": [ "/_watcher/watch/{id}" ], + "parts": { + "id": { + "type" : "string", + "description" : "Watch ID", + "required" : true + } + }, + "params": { + "pretty": { + "type": "boolean", + "description": "Pretty the output", + "default": false + } + } + }, + "body": null + } +} diff --git a/rest-api-spec/api/get_watch.json b/rest-api-spec/api/get_watch.json new file mode 100644 index 00000000000..e4d1ca31a01 --- /dev/null +++ b/rest-api-spec/api/get_watch.json @@ -0,0 +1,25 @@ +{ + "get_watch": { + "documentation": "http://www.elastic.co/guide/en/watcher/current/appendix-api-get-watch.html", + "methods": [ "GET" ], + "url": { + "path": "/_watcher/watch/{id}", + "paths": [ "/_watcher/watch/{id}" ], + "parts": { + "id": { + "type" : "string", + "description" : "Watch ID", + "required" : true + } + }, + "params": { + "pretty": { + "type": "boolean", + "description": "Pretty the output", + "default": false + } + } + }, + "body": null + } +} diff --git a/rest-api-spec/api/info.json b/rest-api-spec/api/info.json new file mode 100644 index 00000000000..b83b666a655 --- /dev/null +++ b/rest-api-spec/api/info.json @@ -0,0 +1,15 @@ +{ + "info": { + "documentation": "http://www.elasticsearch.org/guide/", + "methods": ["GET"], + "url": { + "path": "/", + "paths": ["/"], + "parts": { + }, + "params": { + } + }, + "body": null + } +} diff --git a/rest-api-spec/api/put_watch.json b/rest-api-spec/api/put_watch.json new file mode 100644 index 00000000000..ee7316161d0 --- /dev/null +++ b/rest-api-spec/api/put_watch.json @@ -0,0 +1,28 @@ +{ + "put_watch": { + "documentation": "http://www.elastic.co/guide/en/watcher/current/appendix-api-put-watch.html", + "methods": [ "PUT", "POST" ], + "url": { + "path": "/_watcher/watch/{id}", + "paths": [ "/_watcher/watch/{id}" ], + "parts": { + "id": { + "type" : "string", + "description" : "Watch ID", + "required" : true + } + }, + "params": { + "pretty": { + "type": "boolean", + "description": "Pretty the output", + "default": false + } + } + }, + "body": { + "description" : "The watch", + "required" : true + } + } +} diff --git a/rest-api-spec/api/restart_watcher.json b/rest-api-spec/api/restart_watcher.json new file mode 100644 index 00000000000..3951d68c470 --- /dev/null +++ b/rest-api-spec/api/restart_watcher.json @@ -0,0 +1,15 @@ +{ + "restart_watcher": { + "documentation": "http://www.elastic.co/guide/en/watcher/current/appendix-api-service.html", + "methods": [ "PUT" ], + "url": { + "path": "/_watcher/_restart", + "paths": [ "/_watcher/_restart" ], + "parts": { + }, + "params": { + } + }, + "body": null + } +} diff --git a/rest-api-spec/api/start_watcher.json b/rest-api-spec/api/start_watcher.json new file mode 100644 index 00000000000..a8fc27ca73a --- /dev/null +++ b/rest-api-spec/api/start_watcher.json @@ -0,0 +1,15 @@ +{ + "start_watcher": { + "documentation": "http://www.elastic.co/guide/en/watcher/current/appendix-api-service.html", + "methods": [ "PUT" ], + "url": { + "path": "/_watcher/_start", + "paths": [ "/_watcher/_start" ], + "parts": { + }, + "params": { + } + }, + "body": null + } +} diff --git a/rest-api-spec/api/stop_watcher.json b/rest-api-spec/api/stop_watcher.json new file mode 100644 index 00000000000..f62f5db7b38 --- /dev/null +++ b/rest-api-spec/api/stop_watcher.json @@ -0,0 +1,15 @@ +{ + "stop_watcher": { + "documentation": "http://www.elastic.co/guide/en/watcher/current/appendix-api-service.html", + "methods": [ "PUT" ], + "url": { + "path": "/_watcher/_stop", + "paths": [ "/_watcher/_stop" ], + "parts": { + }, + "params": { + } + }, + "body": null + } +} diff --git a/rest-api-spec/api/watcher_info.json b/rest-api-spec/api/watcher_info.json new file mode 100644 index 00000000000..74b37a8f34d --- /dev/null +++ b/rest-api-spec/api/watcher_info.json @@ -0,0 +1,15 @@ +{ + "watcher_info": { + "documentation": "http://www.elastic.co/guide/en/watcher/current/appendix-api-info.html", + "methods": [ "GET" ], + "url": { + "path": "/_watcher/", + "paths": [ "/_watcher/" ], + "parts": { + }, + "params": { + } + }, + "body": null + } +} diff --git a/rest-api-spec/api/stats.json b/rest-api-spec/api/watcher_stats.json similarity index 64% rename from rest-api-spec/api/stats.json rename to rest-api-spec/api/watcher_stats.json index 5d17ae24851..4f60345c79c 100644 --- a/rest-api-spec/api/stats.json +++ b/rest-api-spec/api/watcher_stats.json @@ -1,10 +1,10 @@ { "watcher_stats": { - "documentation": "http://elastic.co/alertsdocs", - "methods": ["GET"], + "documentation": "http://www.elastic.co/guide/en/watcher/current/appendix-api-stats.html", + "methods": [ "GET" ], "url": { "path": "/_watcher/stats", - "paths": ["/_watcher/stats"], + "paths": [ "/_watcher/stats" ], "parts": { }, "params": { diff --git a/rest-api-spec/test/ack_watch/10_basic.yaml b/rest-api-spec/test/ack_watch/10_basic.yaml new file mode 100644 index 00000000000..af4f3254c83 --- /dev/null +++ b/rest-api-spec/test/ack_watch/10_basic.yaml @@ -0,0 +1,53 @@ +--- +"Test ack watch api": + - do: + cluster.health: + wait_for_status: green + + - do: + put_watch: + id: "my_watch" + body: > + { + "trigger": { + "schedule": { + "hourly": { + "minute": [ 0, 5 ] + } + } + }, + "input": { + "simple": { + "payload": { + "send": "yes" + } + } + }, + "condition": { + "always_true": {} + }, + "actions": [ + { + "test_index": { + "index": { + "index": "test", + "type": "test2" + } + } + } ] + } + - match: { _id: "my_watch" } + + - do: + cluster.health: + wait_for_status: green + + - do: + ack_watch: + id: "my_watch" + - match: { "status.ack.state" : "awaits_execution" } + + - do: + delete_watch: + id: "my_watch" + - match: { found: true } diff --git a/rest-api-spec/test/delete_watch/10_basic.yaml b/rest-api-spec/test/delete_watch/10_basic.yaml new file mode 100644 index 00000000000..05fda3116b6 --- /dev/null +++ b/rest-api-spec/test/delete_watch/10_basic.yaml @@ -0,0 +1,44 @@ +--- +"Test delete watch api": + - do: + cluster.health: + wait_for_status: green + + - do: + put_watch: + id: "my_watch" + body: > + { + "trigger": { + "schedule": { + "hourly": { + "minute": [ 0, 5 ] + } + } + }, + "input": { + "simple": { + "payload": { + "send": "yes" + } + } + }, + "condition": { + "always_true": {} + }, + "actions": [ + { + "test_index": { + "index": { + "index": "test", + "type": "test2" + } + } + } ] + } + - match: { _id: "my_watch" } + + - do: + delete_watch: + id: "my_watch" + - match: { found: true } diff --git a/rest-api-spec/test/get_watch/10_basic.yaml b/rest-api-spec/test/get_watch/10_basic.yaml new file mode 100644 index 00000000000..b507b8dc38a --- /dev/null +++ b/rest-api-spec/test/get_watch/10_basic.yaml @@ -0,0 +1,46 @@ +--- +"Test get watch api": + - do: + cluster.health: + wait_for_status: green + + - do: + put_watch: + id: "my_watch" + body: > + { + "trigger": { + "schedule": { + "hourly": { + "minute": [ 0, 5 ] + } + } + }, + "input": { + "simple": { + "payload": { + "send": "yes" + } + } + }, + "condition": { + "always_true": {} + }, + "actions": [ + { + "test_index": { + "index": { + "index": "test", + "type": "test2" + } + } + } ] + } + - match: { _id: "my_watch" } + - match: { created: true } + + - do: + get_watch: + id: "my_watch" + - match: { found : true} + - match: { _id: "my_watch" } diff --git a/rest-api-spec/test/put_watch/10_basic.yaml b/rest-api-spec/test/put_watch/10_basic.yaml new file mode 100644 index 00000000000..efe7e0d9537 --- /dev/null +++ b/rest-api-spec/test/put_watch/10_basic.yaml @@ -0,0 +1,39 @@ +--- +"Test put watch api": + - do: + cluster.health: + wait_for_status: green + + - do: + put_watch: + id: "my_watch" + body: > + { + "trigger": { + "schedule": { + "hourly": { + "minute": [ 0, 5 ] + } + } + }, + "input": { + "simple": { + "payload": { + "send": "yes" + } + } + }, + "condition": { + "always_true": {} + }, + "actions": [ + { + "test_index": { + "index": { + "index": "test", + "type": "test2" + } + } + } ] + } + - match: { _id: "my_watch" } diff --git a/rest-api-spec/test/restart_watcher/10_basic.yaml b/rest-api-spec/test/restart_watcher/10_basic.yaml new file mode 100644 index 00000000000..55159a28181 --- /dev/null +++ b/rest-api-spec/test/restart_watcher/10_basic.yaml @@ -0,0 +1,8 @@ +--- +"Test restart watcher api": + - do: + cluster.health: + wait_for_status: green + + - do: {restart_watcher: {}} + - match: { acknowledged: true } diff --git a/rest-api-spec/test/start_watcher/10_basic.yaml b/rest-api-spec/test/start_watcher/10_basic.yaml new file mode 100644 index 00000000000..b7bf9307cf7 --- /dev/null +++ b/rest-api-spec/test/start_watcher/10_basic.yaml @@ -0,0 +1,8 @@ +--- +"Test start watcher api": + - do: + cluster.health: + wait_for_status: green + + - do: {start_watcher: {}} + - match: { acknowledged: true } diff --git a/rest-api-spec/test/stop_watcher/10_basic.yaml b/rest-api-spec/test/stop_watcher/10_basic.yaml new file mode 100644 index 00000000000..526319ab25d --- /dev/null +++ b/rest-api-spec/test/stop_watcher/10_basic.yaml @@ -0,0 +1,11 @@ +--- +"Test stop watcher api": + - do: + cluster.health: + wait_for_status: green + + - do: {stop_watcher: {}} + - match: { acknowledged: true } + + - do: {start_watcher: {}} + - match: { acknowledged: true } diff --git a/rest-api-spec/test/watch_info/10_basic.yaml b/rest-api-spec/test/watch_info/10_basic.yaml new file mode 100644 index 00000000000..df64f385c9f --- /dev/null +++ b/rest-api-spec/test/watch_info/10_basic.yaml @@ -0,0 +1,6 @@ +--- +"Test watcher info api": + - do: {watcher_info: {}} + - is_true: version.build_hash + - is_true: version.build_timestamp + - is_true: version.build_snapshot diff --git a/src/main/java/org/elasticsearch/watcher/rest/action/RestAckWatchAction.java b/src/main/java/org/elasticsearch/watcher/rest/action/RestAckWatchAction.java index a73aeea028f..8d61ccc0dc2 100644 --- a/src/main/java/org/elasticsearch/watcher/rest/action/RestAckWatchAction.java +++ b/src/main/java/org/elasticsearch/watcher/rest/action/RestAckWatchAction.java @@ -26,6 +26,7 @@ public class RestAckWatchAction extends WatcherRestHandler { protected RestAckWatchAction(Settings settings, RestController controller, Client client) { super(settings, controller, client); controller.registerHandler(RestRequest.Method.PUT, URI_BASE + "/watch/{name}/_ack", this); + controller.registerHandler(RestRequest.Method.POST, URI_BASE + "/watch/{name}/_ack", this); } @Override @@ -35,7 +36,7 @@ public class RestAckWatchAction extends WatcherRestHandler { @Override public RestResponse buildResponse(AckWatchResponse response, XContentBuilder builder) throws Exception { return new BytesRestResponse(RestStatus.OK, builder.startObject() - .field(Watch.Parser.STATUS_FIELD.getPreferredName(), response.getStatus().toString()) + .field(Watch.Parser.STATUS_FIELD.getPreferredName(), response.getStatus()) .endObject()); } diff --git a/src/main/java/org/elasticsearch/watcher/rest/action/RestGetWatchAction.java b/src/main/java/org/elasticsearch/watcher/rest/action/RestGetWatchAction.java index 95621d5bbd6..b79c10c4e88 100644 --- a/src/main/java/org/elasticsearch/watcher/rest/action/RestGetWatchAction.java +++ b/src/main/java/org/elasticsearch/watcher/rest/action/RestGetWatchAction.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.watcher.rest.action; -import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; @@ -35,15 +34,14 @@ public class RestGetWatchAction extends WatcherRestHandler { client.getWatch(getWatchRequest, new RestBuilderListener(channel) { @Override public RestResponse buildResponse(GetWatchResponse response, XContentBuilder builder) throws Exception { - GetResponse getResponse = response.getResponse(); builder.startObject() - .field("found", getResponse.isExists()) - .field("_id", getResponse.getId()) - .field("_version", getResponse.getVersion()) - .field("watch", getResponse.getSource()) + .field("found", response.exists()) + .field("_id", response.id()) + .field("_version", response.version()) + .field("watch", response.sourceAsMap()) .endObject(); - RestStatus status = getResponse.isExists() ? OK : NOT_FOUND; + RestStatus status = response.exists() ? OK : NOT_FOUND; return new BytesRestResponse(status, builder); } }); diff --git a/src/main/java/org/elasticsearch/watcher/rest/action/RestWatchServiceAction.java b/src/main/java/org/elasticsearch/watcher/rest/action/RestWatchServiceAction.java index 947d4e721ac..7a889d3cc8e 100644 --- a/src/main/java/org/elasticsearch/watcher/rest/action/RestWatchServiceAction.java +++ b/src/main/java/org/elasticsearch/watcher/rest/action/RestWatchServiceAction.java @@ -5,10 +5,6 @@ */ package org.elasticsearch.watcher.rest.action; -import org.elasticsearch.watcher.watch.WatchStore; -import org.elasticsearch.watcher.client.WatcherClient; -import org.elasticsearch.watcher.transport.actions.service.WatcherServiceRequest; -import org.elasticsearch.watcher.transport.actions.service.WatcherServiceResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; @@ -17,45 +13,58 @@ import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.support.AcknowledgedRestListener; +import org.elasticsearch.watcher.client.WatcherClient; +import org.elasticsearch.watcher.rest.WatcherRestHandler; +import org.elasticsearch.watcher.transport.actions.service.WatcherServiceRequest; +import org.elasticsearch.watcher.transport.actions.service.WatcherServiceResponse; /** */ -public class RestWatchServiceAction extends BaseRestHandler { +public class RestWatchServiceAction extends WatcherRestHandler { + + private final WatcherClient watcherClient; @Inject - protected RestWatchServiceAction(Settings settings, RestController controller, Client client) { + protected RestWatchServiceAction(Settings settings, RestController controller, Client client, WatcherClient watcherClient) { super(settings, controller, client); - controller.registerHandler(RestRequest.Method.PUT, WatchStore.INDEX + "/_restart", this); - controller.registerHandler(RestRequest.Method.PUT, WatchStore.INDEX + "/_start", new StartRestHandler(settings, controller, client)); - controller.registerHandler(RestRequest.Method.PUT, WatchStore.INDEX + "/_stop", new StopRestHandler(settings, controller, client)); + controller.registerHandler(RestRequest.Method.PUT, URI_BASE + "/_restart", this); + controller.registerHandler(RestRequest.Method.PUT, URI_BASE + "/_start", new StartRestHandler(settings, controller, client, watcherClient)); + controller.registerHandler(RestRequest.Method.PUT, URI_BASE + "/_stop", new StopRestHandler(settings, controller, client, watcherClient)); + this.watcherClient = watcherClient; } @Override - protected void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception { - new WatcherClient(client).watcherService(new WatcherServiceRequest().restart(), new AcknowledgedRestListener(channel)); + protected void handleRequest(RestRequest request, RestChannel channel, WatcherClient client) throws Exception { + watcherClient.watcherService(new WatcherServiceRequest().restart(), new AcknowledgedRestListener(channel)); } static class StartRestHandler extends BaseRestHandler { - public StartRestHandler(Settings settings, RestController controller, Client client) { + private final WatcherClient watcherClient; + + public StartRestHandler(Settings settings, RestController controller, Client client, WatcherClient watcherClient) { super(settings, controller, client); + this.watcherClient = watcherClient; } @Override public void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception { - new WatcherClient(client).watcherService(new WatcherServiceRequest().start(), new AcknowledgedRestListener(channel)); + watcherClient.watcherService(new WatcherServiceRequest().start(), new AcknowledgedRestListener(channel)); } } static class StopRestHandler extends BaseRestHandler { - public StopRestHandler(Settings settings, RestController controller, Client client) { + private final WatcherClient watcherClient; + + public StopRestHandler(Settings settings, RestController controller, Client client, WatcherClient watcherClient) { super(settings, controller, client); + this.watcherClient = watcherClient; } @Override public void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception { - new WatcherClient(client).watcherService(new WatcherServiceRequest().stop(), new AcknowledgedRestListener(channel)); + watcherClient.watcherService(new WatcherServiceRequest().stop(), new AcknowledgedRestListener(channel)); } } } diff --git a/src/main/java/org/elasticsearch/watcher/rest/action/RestWatcherInfoAction.java b/src/main/java/org/elasticsearch/watcher/rest/action/RestWatcherInfoAction.java index 8e39548c012..5add5f52817 100644 --- a/src/main/java/org/elasticsearch/watcher/rest/action/RestWatcherInfoAction.java +++ b/src/main/java/org/elasticsearch/watcher/rest/action/RestWatcherInfoAction.java @@ -35,12 +35,13 @@ public class RestWatcherInfoAction extends WatcherRestHandler { watcherClient.watcherStats(new WatcherStatsRequest(), new RestBuilderListener(restChannel) { @Override public RestResponse buildResponse(WatcherStatsResponse watcherStatsResponse, XContentBuilder builder) throws Exception { + builder.startObject(); builder.startObject("version") .field("number", watcherStatsResponse.getVersion().number()) .field("build_hash", watcherStatsResponse.getBuild().hash()) .field("build_timestamp", watcherStatsResponse.getBuild().timestamp()) .field("build_snapshot", watcherStatsResponse.getVersion().snapshot) - .endObject(); + .endObject().endObject(); return new BytesRestResponse(OK, builder); diff --git a/src/main/java/org/elasticsearch/watcher/transport/actions/get/GetWatchResponse.java b/src/main/java/org/elasticsearch/watcher/transport/actions/get/GetWatchResponse.java index 1ead4b315f2..54fc5c2f7a7 100644 --- a/src/main/java/org/elasticsearch/watcher/transport/actions/get/GetWatchResponse.java +++ b/src/main/java/org/elasticsearch/watcher/transport/actions/get/GetWatchResponse.java @@ -5,49 +5,85 @@ */ package org.elasticsearch.watcher.transport.actions.get; +import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.get.GetResponse; +import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.search.lookup.SourceLookup; import java.io.IOException; +import java.util.Map; public class GetWatchResponse extends ActionResponse { - private GetResponse getResponse; + private boolean exists = false; + private String id; + private long version; + private BytesReference source; + + private Map sourceAsMap; + + public GetWatchResponse(boolean found, String id, long version, BytesReference source) { + this.exists = found; + this.id = id; + this.version = version; + this.source = source; + GetResponse foo; + } public GetWatchResponse() { } - public GetWatchResponse(GetResponse getResponse) { - this.getResponse = getResponse; + public BytesReference source() { + return source; } - /** - * Sets the GetResponse containing the watch source - */ - public void getResponse(GetResponse getResponse) { - this.getResponse = getResponse; + public Map sourceAsMap() throws ElasticsearchParseException { + if (source == null) { + return null; + } + if (sourceAsMap != null) { + return sourceAsMap; + } + + sourceAsMap = SourceLookup.sourceAsMap(source); + return sourceAsMap; } - public GetResponse getResponse() { - return this.getResponse; + + public boolean exists() { + return exists; + } + + public String id() { + return id; + } + + public long version() { + return version; } @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); - if (in.readBoolean()) { - getResponse = GetResponse.readGetResponse(in); + exists = in.readBoolean(); + if (exists) { + id = in.readString(); + version = in.readLong(); + source = in.readBytesReference(); } } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); - out.writeBoolean(getResponse != null); - if (getResponse != null) { - getResponse.writeTo(out); + out.writeBoolean(exists); + if (exists) { + out.writeString(id); + out.writeLong(version); + out.writeBytesReference(source); } } } diff --git a/src/main/java/org/elasticsearch/watcher/transport/actions/get/TransportGetWatchAction.java b/src/main/java/org/elasticsearch/watcher/transport/actions/get/TransportGetWatchAction.java index 647466d1cc5..0c12a670c20 100644 --- a/src/main/java/org/elasticsearch/watcher/transport/actions/get/TransportGetWatchAction.java +++ b/src/main/java/org/elasticsearch/watcher/transport/actions/get/TransportGetWatchAction.java @@ -7,7 +7,6 @@ package org.elasticsearch.watcher.transport.actions.get; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterState; @@ -60,21 +59,27 @@ public class TransportGetWatchAction extends WatcherTransportAction listener) throws ElasticsearchException { - Watch watch = watchService.getWatch(request.watchName()); - GetResult getResult; - if (watch != null) { - BytesReference watchSource = null; - try (XContentBuilder builder = JsonXContent.contentBuilder()) { - builder.value(watch); - watchSource = builder.bytes(); - } catch (IOException e) { - listener.onFailure(e); + try { + Watch watch = watchService.getWatch(request.watchName()); + GetResult getResult; + if (watch != null) { + BytesReference watchSource = null; + try (XContentBuilder builder = JsonXContent.contentBuilder()) { + builder.value(watch); + watchSource = builder.bytes(); + } catch (IOException e) { + listener.onFailure(e); + return; + } + getResult = new GetResult(WatchStore.INDEX, WatchStore.DOC_TYPE, watch.name(), watch.status().version(), true, watchSource, null); + } else { + getResult = new GetResult(WatchStore.INDEX, WatchStore.DOC_TYPE, request.watchName(), -1, false, null, null); } - getResult = new GetResult(WatchStore.INDEX, WatchStore.DOC_TYPE, watch.name(), watch.status().version(), true, watchSource, null); - } else { - getResult = new GetResult(WatchStore.INDEX, WatchStore.DOC_TYPE, request.watchName(), -1, false, null, null); + listener.onResponse(new GetWatchResponse(getResult.isExists(), getResult.getId(), getResult.getVersion(), getResult.sourceRef())); + } catch (Throwable t) { + logger.error("failed to get watch [{}]", t, request.watchName()); + throw t; } - listener.onResponse(new GetWatchResponse(new GetResponse(getResult))); } @Override diff --git a/src/main/java/org/elasticsearch/watcher/watch/WatchService.java b/src/main/java/org/elasticsearch/watcher/watch/WatchService.java index d0b74bee88e..6e4f70ef433 100644 --- a/src/main/java/org/elasticsearch/watcher/watch/WatchService.java +++ b/src/main/java/org/elasticsearch/watcher/watch/WatchService.java @@ -8,14 +8,14 @@ package org.elasticsearch.watcher.watch; import org.elasticsearch.ElasticsearchIllegalStateException; import org.elasticsearch.action.index.IndexResponse; -import org.elasticsearch.watcher.WatcherException; -import org.elasticsearch.watcher.history.HistoryService; -import org.elasticsearch.watcher.support.Callback; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.watcher.WatcherException; +import org.elasticsearch.watcher.history.HistoryService; +import org.elasticsearch.watcher.support.Callback; import org.elasticsearch.watcher.trigger.TriggerService; import java.io.IOException; @@ -109,6 +109,9 @@ public class WatchService extends AbstractComponent { triggerService.add(result.current()); } return result.indexResponse(); + } catch (Exception e) { + logger.warn("failed to put watch [{}]", e, name); + throw new WatcherException("failed to put [" + watchSource.toUtf8() + "]", e); } finally { lock.release(); } diff --git a/src/test/java/org/elasticsearch/watcher/test/integration/BasicWatcherTests.java b/src/test/java/org/elasticsearch/watcher/test/integration/BasicWatcherTests.java index 555de7c739f..a627e0d3a3a 100644 --- a/src/test/java/org/elasticsearch/watcher/test/integration/BasicWatcherTests.java +++ b/src/test/java/org/elasticsearch/watcher/test/integration/BasicWatcherTests.java @@ -77,8 +77,8 @@ public class BasicWatcherTests extends AbstractWatcherIntegrationTests { assertWatchWithMinimumPerformedActionsCount("_name", 1); GetWatchResponse getWatchResponse = watcherClient().prepareGetWatch().setWatchName("_name").get(); - assertThat(getWatchResponse.getResponse().isExists(), is(true)); - assertThat(getWatchResponse.getResponse().isSourceEmpty(), is(false)); + assertThat(getWatchResponse.exists(), is(true)); + assertThat(getWatchResponse.source().length(), greaterThan(0)); } @Test diff --git a/src/test/java/org/elasticsearch/watcher/test/integration/WatchThrottleTests.java b/src/test/java/org/elasticsearch/watcher/test/integration/WatchThrottleTests.java index 81104032f4e..15b1e2ce127 100644 --- a/src/test/java/org/elasticsearch/watcher/test/integration/WatchThrottleTests.java +++ b/src/test/java/org/elasticsearch/watcher/test/integration/WatchThrottleTests.java @@ -100,10 +100,10 @@ public class WatchThrottleTests extends AbstractWatcherIntegrationTests { } GetWatchResponse getWatchResponse = watcherClient.prepareGetWatch("_name").get(); - assertThat(getWatchResponse.getResponse().isExists(), is(true)); + assertThat(getWatchResponse.exists(), is(true)); - Watch parsedWatch = watchParser().parse(getWatchResponse.getResponse().getId(), true, - getWatchResponse.getResponse().getSourceAsBytesRef()); + Watch parsedWatch = watchParser().parse(getWatchResponse.id(), true, + getWatchResponse.source()); assertThat(parsedWatch.status().ackStatus().state(), is(Watch.Status.AckStatus.State.AWAITS_EXECUTION)); long throttledCount = docCount(HistoryStore.INDEX_PREFIX + "*", null, diff --git a/src/test/java/org/elasticsearch/watcher/test/rest/WatcherRestTests.java b/src/test/java/org/elasticsearch/watcher/test/rest/WatcherRestTests.java index 217947f035d..6d1455342a0 100644 --- a/src/test/java/org/elasticsearch/watcher/test/rest/WatcherRestTests.java +++ b/src/test/java/org/elasticsearch/watcher/test/rest/WatcherRestTests.java @@ -73,6 +73,8 @@ public class WatcherRestTests extends ElasticsearchRestTests { return ImmutableSettings.builder() .put("plugin.types", WatcherPlugin.class.getName()) .put(InternalNode.HTTP_ENABLED, true) + .put("plugin.types", WatcherPlugin.class.getName() + "," + + "," + LicensePlugin.class.getName()) .build(); } @@ -93,6 +95,8 @@ public class WatcherRestTests extends ElasticsearchRestTests { return ImmutableSettings.builder() .put("plugin.types", WatcherPlugin.class.getName()) .put(InternalNode.HTTP_ENABLED, true) + .put("plugin.types", WatcherPlugin.class.getName() + "," + + "," + LicensePlugin.class.getName()) .build(); } @@ -139,7 +143,7 @@ public class WatcherRestTests extends ElasticsearchRestTests { .put("shield.user", "test:changeme") .put("shield.authc.realms.esusers.type", ESUsersRealm.TYPE) .put("shield.authc.anonymous.username","anonymous_user") - .put("shield.authc.anonymous.roles", "monitor") + .put("shield.authc.anonymous.roles", "admin") .put("shield.authc.realms.esusers.order", 0) .put("shield.authc.realms.esusers.files.users", writeFile(folder, "users", USERS)) .put("shield.authc.realms.esusers.files.users_roles", writeFile(folder, "users_roles", USER_ROLES))