diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/get/TransportGetWatchAction.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/get/TransportGetWatchAction.java index ea86f13384c..4f577923889 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/get/TransportGetWatchAction.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/get/TransportGetWatchAction.java @@ -12,6 +12,7 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; @@ -64,6 +65,13 @@ public class TransportGetWatchAction extends WatcherTransportAction { + // special case. This API should not care if the index is missing or not, it should respond with the watch not being found + if (e instanceof IndexNotFoundException) { + listener.onResponse(new GetWatchResponse(request.getId())); + } else { + listener.onFailure(e); + } + })); } } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/watcher/transport/action/get/GetWatchTests.java b/plugin/src/test/java/org/elasticsearch/xpack/watcher/transport/action/get/GetWatchTests.java index a9afa3e49b2..c843e01ba3b 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/watcher/transport/action/get/GetWatchTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/watcher/transport/action/get/GetWatchTests.java @@ -22,7 +22,6 @@ import static org.elasticsearch.xpack.watcher.client.WatchSourceBuilders.watchBu import static org.elasticsearch.xpack.watcher.input.InputBuilders.simpleInput; import static org.elasticsearch.xpack.watcher.trigger.TriggerBuilders.schedule; import static org.elasticsearch.xpack.watcher.trigger.schedule.Schedules.interval; -import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; @@ -55,16 +54,14 @@ public class GetWatchTests extends AbstractWatcherIntegrationTestCase { assertThat(source, not(hasKey("status"))); } - public void testGetNotFoundOnNonExistingIndex() throws Exception { - // ensure index/alias is deleted, test infra might have created it automatically - try { - assertAcked(client().admin().indices().prepareDelete(Watch.INDEX)); - } catch (IndexNotFoundException e) {} - Exception e = expectThrows(Exception.class, () -> watcherClient().getWatch(new GetWatchRequest("_name")).get()); - assertThat(e.getMessage(), containsString("no such index")); - } - public void testGetNotFound() throws Exception { + // does not matter if the watch does not exist or the index does not exist, we expect the same response + if (randomBoolean()) { + try { + assertAcked(client().admin().indices().prepareDelete(Watch.INDEX)); + } catch (IndexNotFoundException e) {} + } + GetWatchResponse getResponse = watcherClient().getWatch(new GetWatchRequest("_name")).get(); assertThat(getResponse, notNullValue()); assertThat(getResponse.getId(), is("_name")); diff --git a/plugin/src/test/resources/rest-api-spec/test/watcher/get_watch/20_missing.yml b/plugin/src/test/resources/rest-api-spec/test/watcher/get_watch/20_missing.yml index 1a91bab5fcf..3083b8bbb92 100644 --- a/plugin/src/test/resources/rest-api-spec/test/watcher/get_watch/20_missing.yml +++ b/plugin/src/test/resources/rest-api-spec/test/watcher/get_watch/20_missing.yml @@ -1,5 +1,5 @@ --- -"Test get watch api with missing watch": +"Test get watch api with missing watch on existing index": - do: cluster.health: wait_for_status: yellow @@ -17,3 +17,21 @@ id: "missing_watch" - match: { found : false} - match: { _id: "missing_watch" } + +--- +"Test get watch api with missing watch on missing index": + - do: + cluster.health: + wait_for_status: yellow + + - do: + indices.delete: + index: .watches* + ignore: 404 + + - do: + catch: missing + xpack.watcher.get_watch: + id: "missing_watch" + - match: { found : false} + - match: { _id: "missing_watch" }