Skip to content

Commit 1ab0c98

Browse files
committed
HLRC: Add delete watch action (#32337)
Adds the "delete watch" API to the High-Level Rest Client. Relates #29827
1 parent 3ae8bd8 commit 1ab0c98

File tree

22 files changed

+394
-83
lines changed

22 files changed

+394
-83
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java

+13
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
import org.elasticsearch.index.VersionType;
108108
import org.elasticsearch.index.rankeval.RankEvalRequest;
109109
import org.elasticsearch.protocol.xpack.XPackInfoRequest;
110+
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
110111
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest;
111112
import org.elasticsearch.protocol.xpack.XPackUsageRequest;
112113
import org.elasticsearch.protocol.xpack.license.PutLicenseRequest;
@@ -1144,6 +1145,18 @@ static Request xPackWatcherPutWatch(PutWatchRequest putWatchRequest) {
11441145
return request;
11451146
}
11461147

1148+
static Request xPackWatcherDeleteWatch(DeleteWatchRequest deleteWatchRequest) {
1149+
String endpoint = new EndpointBuilder()
1150+
.addPathPartAsIs("_xpack")
1151+
.addPathPartAsIs("watcher")
1152+
.addPathPartAsIs("watch")
1153+
.addPathPart(deleteWatchRequest.getId())
1154+
.build();
1155+
1156+
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
1157+
return request;
1158+
}
1159+
11471160
static Request xpackUsage(XPackUsageRequest usageRequest) {
11481161
Request request = new Request(HttpGet.METHOD_NAME, "/_xpack/usage");
11491162
Params parameters = new Params(request);

client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java

+30
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919
package org.elasticsearch.client;
2020

2121
import org.elasticsearch.action.ActionListener;
22+
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
23+
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse;
2224
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest;
2325
import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse;
2426

2527
import java.io.IOException;
2628

2729
import static java.util.Collections.emptySet;
30+
import static java.util.Collections.singleton;
2831

2932
public final class WatcherClient {
3033

@@ -61,4 +64,31 @@ public void putWatchAsync(PutWatchRequest request, RequestOptions options,
6164
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::xPackWatcherPutWatch, options,
6265
PutWatchResponse::fromXContent, listener, emptySet());
6366
}
67+
68+
/**
69+
* Deletes a watch from the cluster
70+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-delete-watch.html">
71+
* the docs</a> for more.
72+
* @param request the request
73+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
74+
* @return the response
75+
* @throws IOException in case there is a problem sending the request or parsing back the response
76+
*/
77+
public DeleteWatchResponse deleteWatch(DeleteWatchRequest request, RequestOptions options) throws IOException {
78+
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::xPackWatcherDeleteWatch, options,
79+
DeleteWatchResponse::fromXContent, singleton(404));
80+
}
81+
82+
/**
83+
* Asynchronously deletes a watch from the cluster
84+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-delete-watch.html">
85+
* the docs</a> for more.
86+
* @param request the request
87+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
88+
* @param listener the listener to be notified upon request completion
89+
*/
90+
public void deleteWatchAsync(DeleteWatchRequest request, RequestOptions options, ActionListener<DeleteWatchResponse> listener) {
91+
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::xPackWatcherDeleteWatch, options,
92+
DeleteWatchResponse::fromXContent, listener, singleton(404));
93+
}
6494
}

client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
import org.elasticsearch.index.rankeval.RatedRequest;
128128
import org.elasticsearch.index.rankeval.RestRankEvalAction;
129129
import org.elasticsearch.protocol.xpack.XPackInfoRequest;
130+
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
130131
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest;
131132
import org.elasticsearch.repositories.fs.FsRepository;
132133
import org.elasticsearch.rest.action.search.RestSearchAction;
@@ -2614,6 +2615,17 @@ public void testXPackPutWatch() throws Exception {
26142615
assertThat(bos.toString("UTF-8"), is(body));
26152616
}
26162617

2618+
public void testXPackDeleteWatch() {
2619+
DeleteWatchRequest deleteWatchRequest = new DeleteWatchRequest();
2620+
String watchId = randomAlphaOfLength(10);
2621+
deleteWatchRequest.setId(watchId);
2622+
2623+
Request request = RequestConverters.xPackWatcherDeleteWatch(deleteWatchRequest);
2624+
assertEquals(HttpDelete.METHOD_NAME, request.getMethod());
2625+
assertEquals("/_xpack/watcher/watch/" + watchId, request.getEndpoint());
2626+
assertThat(request.getEntity(), nullValue());
2627+
}
2628+
26172629
/**
26182630
* Randomize the {@link FetchSourceContext} request parameters.
26192631
*/

client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java

+33-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.elasticsearch.common.bytes.BytesArray;
2222
import org.elasticsearch.common.bytes.BytesReference;
2323
import org.elasticsearch.common.xcontent.XContentType;
24+
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
25+
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse;
2426
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest;
2527
import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse;
2628

@@ -30,17 +32,44 @@ public class WatcherIT extends ESRestHighLevelClientTestCase {
3032

3133
public void testPutWatch() throws Exception {
3234
String watchId = randomAlphaOfLength(10);
35+
PutWatchResponse putWatchResponse = createWatch(watchId);
36+
assertThat(putWatchResponse.isCreated(), is(true));
37+
assertThat(putWatchResponse.getId(), is(watchId));
38+
assertThat(putWatchResponse.getVersion(), is(1L));
39+
}
40+
41+
private PutWatchResponse createWatch(String watchId) throws Exception {
3342
String json = "{ \n" +
3443
" \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" +
3544
" \"input\": { \"none\": {} },\n" +
3645
" \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" +
3746
"}";
3847
BytesReference bytesReference = new BytesArray(json);
3948
PutWatchRequest putWatchRequest = new PutWatchRequest(watchId, bytesReference, XContentType.JSON);
40-
PutWatchResponse putWatchResponse = highLevelClient().xpack().watcher().putWatch(putWatchRequest, RequestOptions.DEFAULT);
41-
assertThat(putWatchResponse.isCreated(), is(true));
42-
assertThat(putWatchResponse.getId(), is(watchId));
43-
assertThat(putWatchResponse.getVersion(), is(1L));
49+
return highLevelClient().xpack().watcher().putWatch(putWatchRequest, RequestOptions.DEFAULT);
50+
}
51+
52+
public void testDeleteWatch() throws Exception {
53+
// delete watch that exists
54+
{
55+
String watchId = randomAlphaOfLength(10);
56+
createWatch(watchId);
57+
DeleteWatchResponse deleteWatchResponse = highLevelClient().xpack().watcher().deleteWatch(new DeleteWatchRequest(watchId),
58+
RequestOptions.DEFAULT);
59+
assertThat(deleteWatchResponse.getId(), is(watchId));
60+
assertThat(deleteWatchResponse.getVersion(), is(2L));
61+
assertThat(deleteWatchResponse.isFound(), is(true));
62+
}
63+
64+
// delete watch that does not exist
65+
{
66+
String watchId = randomAlphaOfLength(10);
67+
DeleteWatchResponse deleteWatchResponse = highLevelClient().xpack().watcher().deleteWatch(new DeleteWatchRequest(watchId),
68+
RequestOptions.DEFAULT);
69+
assertThat(deleteWatchResponse.getId(), is(watchId));
70+
assertThat(deleteWatchResponse.getVersion(), is(1L));
71+
assertThat(deleteWatchResponse.isFound(), is(false));
72+
}
4473
}
4574

4675
}

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java

+44-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.elasticsearch.common.bytes.BytesArray;
2727
import org.elasticsearch.common.bytes.BytesReference;
2828
import org.elasticsearch.common.xcontent.XContentType;
29+
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
30+
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse;
2931
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest;
3032
import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse;
3133

@@ -34,7 +36,7 @@
3436

3537
public class WatcherDocumentationIT extends ESRestHighLevelClientTestCase {
3638

37-
public void testPutWatch() throws Exception {
39+
public void testWatcher() throws Exception {
3840
RestHighLevelClient client = highLevelClient();
3941

4042
{
@@ -88,5 +90,46 @@ public void onFailure(Exception e) {
8890

8991
assertTrue(latch.await(30L, TimeUnit.SECONDS));
9092
}
93+
94+
{
95+
//tag::x-pack-delete-watch-execute
96+
DeleteWatchRequest request = new DeleteWatchRequest("my_watch_id");
97+
DeleteWatchResponse response = client.xpack().watcher().deleteWatch(request, RequestOptions.DEFAULT);
98+
//end::x-pack-delete-watch-execute
99+
100+
//tag::x-pack-delete-watch-response
101+
String watchId = response.getId(); // <1>
102+
boolean found = response.isFound(); // <2>
103+
long version = response.getVersion(); // <3>
104+
//end::x-pack-delete-watch-response
105+
}
106+
107+
{
108+
DeleteWatchRequest request = new DeleteWatchRequest("my_other_watch_id");
109+
// tag::x-pack-delete-watch-execute-listener
110+
ActionListener<DeleteWatchResponse> listener = new ActionListener<DeleteWatchResponse>() {
111+
@Override
112+
public void onResponse(DeleteWatchResponse response) {
113+
// <1>
114+
}
115+
116+
@Override
117+
public void onFailure(Exception e) {
118+
// <2>
119+
}
120+
};
121+
// end::x-pack-delete-watch-execute-listener
122+
123+
// Replace the empty listener by a blocking listener in test
124+
final CountDownLatch latch = new CountDownLatch(1);
125+
listener = new LatchedActionListener<>(listener, latch);
126+
127+
// tag::x-pack-delete-watch-execute-async
128+
client.xpack().watcher().deleteWatchAsync(request, RequestOptions.DEFAULT, listener); // <1>
129+
// end::x-pack-delete-watch-execute-async
130+
131+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
132+
}
91133
}
134+
92135
}

docs/java-rest/high-level/supported-apis.asciidoc

+3
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,14 @@ The Java High Level REST Client supports the following Miscellaneous APIs:
5454
* <<java-rest-high-main>>
5555
* <<java-rest-high-ping>>
5656
* <<java-rest-high-x-pack-info>>
57+
* <<java-rest-high-x-pack-watcher-put-watch>>
58+
* <<java-rest-high-x-pack-watcher-delete-watch>>
5759

5860
include::miscellaneous/main.asciidoc[]
5961
include::miscellaneous/ping.asciidoc[]
6062
include::x-pack/x-pack-info.asciidoc[]
6163
include::x-pack/watcher/put-watch.asciidoc[]
64+
include::x-pack/watcher/delete-watch.asciidoc[]
6265

6366
== Indices APIs
6467

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
[[java-rest-high-x-pack-watcher-delete-watch]]
2+
=== X-Pack Delete Watch API
3+
4+
[[java-rest-high-x-pack-watcher-delete-watch-execution]]
5+
==== Execution
6+
7+
A watch can be deleted as follows:
8+
9+
["source","java",subs="attributes,callouts,macros"]
10+
--------------------------------------------------
11+
include-tagged::{doc-tests}/WatcherDocumentationIT.java[x-pack-delete-watch-execute]
12+
--------------------------------------------------
13+
14+
[[java-rest-high-x-pack-watcher-delete-watch-response]]
15+
==== Response
16+
17+
The returned `DeleteWatchResponse` contains `found`, `id`,
18+
and `version` information.
19+
20+
["source","java",subs="attributes,callouts,macros"]
21+
--------------------------------------------------
22+
include-tagged::{doc-tests}/WatcherDocumentationIT.java[x-pack-put-watch-response]
23+
--------------------------------------------------
24+
<1> `_id` contains id of the watch
25+
<2> `found` is a boolean indicating whether the watch was found
26+
<3> `_version` returns the version of the deleted watch
27+
28+
[[java-rest-high-x-pack-watcher-delete-watch-async]]
29+
==== Asynchronous Execution
30+
31+
This request can be executed asynchronously:
32+
33+
["source","java",subs="attributes,callouts,macros"]
34+
--------------------------------------------------
35+
include-tagged::{doc-tests}/WatcherDocumentationIT.java[x-pack-delete-watch-execute-async]
36+
--------------------------------------------------
37+
<1> The `DeleteWatchRequest` to execute and the `ActionListener` to use when
38+
the execution completes
39+
40+
The asynchronous method does not block and returns immediately. Once it is
41+
completed the `ActionListener` is called back using the `onResponse` method
42+
if the execution successfully completed or using the `onFailure` method if
43+
it failed.
44+
45+
A typical listener for `DeleteWatchResponse` looks like:
46+
47+
["source","java",subs="attributes,callouts,macros"]
48+
--------------------------------------------------
49+
include-tagged::{doc-tests}/WatcherDocumentationIT.java[x-pack-delete-watch-execute-listener]
50+
--------------------------------------------------
51+
<1> Called when the execution is successfully completed. The response is
52+
provided as an argument
53+
<2> Called in case of failure. The raised exception is provided as an argument

docs/java-rest/high-level/x-pack/watcher/put-watch.asciidoc

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[[java-rest-high-x-pack-watcher-put-watch]]
2-
=== X-Pack Info API
2+
=== X-Pack Put Watch API
33

44
[[java-rest-high-x-pack-watcher-put-watch-execution]]
55
==== Execution
@@ -16,7 +16,7 @@ include-tagged::{doc-tests}/WatcherDocumentationIT.java[x-pack-put-watch-execute
1616
[[java-rest-high-x-pack-watcher-put-watch-response]]
1717
==== Response
1818

19-
The returned `XPackPutWatchResponse` contain `created`, `id`,
19+
The returned `PutWatchResponse` contains `created`, `id`,
2020
and `version` information.
2121

2222
["source","java",subs="attributes,callouts,macros"]
@@ -36,15 +36,15 @@ This request can be executed asynchronously:
3636
--------------------------------------------------
3737
include-tagged::{doc-tests}/WatcherDocumentationIT.java[x-pack-put-watch-execute-async]
3838
--------------------------------------------------
39-
<1> The `XPackPutWatchRequest` to execute and the `ActionListener` to use when
39+
<1> The `PutWatchRequest` to execute and the `ActionListener` to use when
4040
the execution completes
4141

4242
The asynchronous method does not block and returns immediately. Once it is
4343
completed the `ActionListener` is called back using the `onResponse` method
4444
if the execution successfully completed or using the `onFailure` method if
4545
it failed.
4646

47-
A typical listener for `XPackPutWatchResponse` looks like:
47+
A typical listener for `PutWatchResponse` looks like:
4848

4949
["source","java",subs="attributes,callouts,macros"]
5050
--------------------------------------------------

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/client/WatcherClient.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import org.elasticsearch.xpack.core.watcher.transport.actions.activate.ActivateWatchRequestBuilder;
2020
import org.elasticsearch.xpack.core.watcher.transport.actions.activate.ActivateWatchResponse;
2121
import org.elasticsearch.xpack.core.watcher.transport.actions.delete.DeleteWatchAction;
22-
import org.elasticsearch.xpack.core.watcher.transport.actions.delete.DeleteWatchRequest;
22+
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
2323
import org.elasticsearch.xpack.core.watcher.transport.actions.delete.DeleteWatchRequestBuilder;
24-
import org.elasticsearch.xpack.core.watcher.transport.actions.delete.DeleteWatchResponse;
24+
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse;
2525
import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchAction;
2626
import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchRequest;
2727
import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchRequestBuilder;

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/delete/DeleteWatchAction.java

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import org.elasticsearch.action.Action;
99
import org.elasticsearch.client.ElasticsearchClient;
10+
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
11+
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse;
1012

1113
/**
1214
* This action deletes an watch from in memory, the scheduler and the index

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/delete/DeleteWatchRequestBuilder.java

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder;
99
import org.elasticsearch.client.ElasticsearchClient;
10+
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
11+
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse;
1012

1113
/**
1214
* A delete document action request builder.

0 commit comments

Comments
 (0)