forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rest HL client: Add put watch action (elastic#32026)
Relates elastic#29827 This implementation behaves like the current transport client, that you basically cannot configure a Watch POJO representation as an argument to the put watch API, but only a bytes reference. You can use the the `WatchSourceBuilder` from the `org.elasticsearch.plugin:x-pack-core` dependency to build watches. This commit also changes the license type to trial, so that watcher is available in high level rest client tests.
- Loading branch information
Showing
45 changed files
with
564 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; | ||
import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; | ||
|
||
import java.io.IOException; | ||
|
||
import static java.util.Collections.emptySet; | ||
|
||
public final class WatcherClient { | ||
|
||
private final RestHighLevelClient restHighLevelClient; | ||
|
||
WatcherClient(RestHighLevelClient restHighLevelClient) { | ||
this.restHighLevelClient = restHighLevelClient; | ||
} | ||
|
||
/** | ||
* Put a watch into the cluster | ||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-put-watch.html"> | ||
* the docs</a> for more. | ||
* @param request the request | ||
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized | ||
* @return the response | ||
* @throws IOException in case there is a problem sending the request or parsing back the response | ||
*/ | ||
public PutWatchResponse putWatch(PutWatchRequest request, RequestOptions options) throws IOException { | ||
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::xPackWatcherPutWatch, options, | ||
PutWatchResponse::fromXContent, emptySet()); | ||
} | ||
|
||
/** | ||
* Asynchronously put a watch into the cluster | ||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-put-watch.html"> | ||
* the docs</a> for more. | ||
* @param request the request | ||
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized | ||
* @param listener the listener to be notified upon request completion | ||
*/ | ||
public void putWatchAsync(PutWatchRequest request, RequestOptions options, | ||
ActionListener<PutWatchResponse> listener) { | ||
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::xPackWatcherPutWatch, options, | ||
PutWatchResponse::fromXContent, listener, emptySet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client; | ||
|
||
import org.elasticsearch.common.bytes.BytesArray; | ||
import org.elasticsearch.common.bytes.BytesReference; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; | ||
import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
|
||
public class WatcherIT extends ESRestHighLevelClientTestCase { | ||
|
||
public void testPutWatch() throws Exception { | ||
String watchId = randomAlphaOfLength(10); | ||
String json = "{ \n" + | ||
" \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + | ||
" \"input\": { \"none\": {} },\n" + | ||
" \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + | ||
"}"; | ||
BytesReference bytesReference = new BytesArray(json); | ||
PutWatchRequest putWatchRequest = new PutWatchRequest(watchId, bytesReference, XContentType.JSON); | ||
PutWatchResponse putWatchResponse = highLevelClient().xpack().watcher().putWatch(putWatchRequest, RequestOptions.DEFAULT); | ||
assertThat(putWatchResponse.isCreated(), is(true)); | ||
assertThat(putWatchResponse.getId(), is(watchId)); | ||
assertThat(putWatchResponse.getVersion(), is(1L)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...gh-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client.documentation; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.LatchedActionListener; | ||
import org.elasticsearch.client.ESRestHighLevelClientTestCase; | ||
import org.elasticsearch.client.RequestOptions; | ||
import org.elasticsearch.client.RestHighLevelClient; | ||
import org.elasticsearch.common.bytes.BytesArray; | ||
import org.elasticsearch.common.bytes.BytesReference; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; | ||
import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class WatcherDocumentationIT extends ESRestHighLevelClientTestCase { | ||
|
||
public void testPutWatch() throws Exception { | ||
RestHighLevelClient client = highLevelClient(); | ||
|
||
{ | ||
//tag::x-pack-put-watch-execute | ||
// you can also use the WatchSourceBuilder from org.elasticsearch.plugin:x-pack-core to create a watch programmatically | ||
BytesReference watch = new BytesArray("{ \n" + | ||
" \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + | ||
" \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" + | ||
" \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + | ||
"}"); | ||
PutWatchRequest request = new PutWatchRequest("my_watch_id", watch, XContentType.JSON); | ||
request.setActive(false); // <1> | ||
PutWatchResponse response = client.xpack().watcher().putWatch(request, RequestOptions.DEFAULT); | ||
//end::x-pack-put-watch-execute | ||
|
||
//tag::x-pack-put-watch-response | ||
String watchId = response.getId(); // <1> | ||
boolean isCreated = response.isCreated(); // <2> | ||
long version = response.getVersion(); // <3> | ||
//end::x-pack-put-watch-response | ||
} | ||
|
||
{ | ||
BytesReference watch = new BytesArray("{ \n" + | ||
" \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + | ||
" \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" + | ||
" \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + | ||
"}"); | ||
PutWatchRequest request = new PutWatchRequest("my_other_watch_id", watch, XContentType.JSON); | ||
// tag::x-pack-put-watch-execute-listener | ||
ActionListener<PutWatchResponse> listener = new ActionListener<PutWatchResponse>() { | ||
@Override | ||
public void onResponse(PutWatchResponse response) { | ||
// <1> | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
// <2> | ||
} | ||
}; | ||
// end::x-pack-put-watch-execute-listener | ||
|
||
// Replace the empty listener by a blocking listener in test | ||
final CountDownLatch latch = new CountDownLatch(1); | ||
listener = new LatchedActionListener<>(listener, latch); | ||
|
||
// tag::x-pack-put-watch-execute-async | ||
client.xpack().watcher().putWatchAsync(request, RequestOptions.DEFAULT, listener); // <1> | ||
// end::x-pack-put-watch-execute-async | ||
|
||
assertTrue(latch.await(30L, TimeUnit.SECONDS)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.