-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rest HL client: Add put watch action #32026
Changes from 2 commits
b23d5eb
0897230
ecbb75f
152c01e
7dc5088
b3f622e
b5391a6
f5e99bf
42d6189
88f173f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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()); | ||
} | ||
} |
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)); | ||
} | ||
|
||
} |
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 x-pack-transport 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" + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not pull this out of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this way the |
||
" \"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)); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since every one of the other xpack APIs will have this, maybe creating a method is worth it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disregard this comment. this is a feature of the watch being active, not the xpack plugin