From 3bfe79d04f62f1eb86414b682f33f44aa5464b34 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Wed, 28 Nov 2018 09:40:07 +0100 Subject: [PATCH] [HLRC] Added support for CCR Delete Auto Follow Pattern API This change also adds documentation for the Delete Auto Follow Pattern API. Relates to #33824 --- .../org/elasticsearch/client/CcrClient.java | 51 +++++++++++++ .../client/CcrRequestConverters.java | 10 +++ .../ccr/DeleteAutoFollowPatternRequest.java | 37 +++++++++ .../java/org/elasticsearch/client/CCRIT.java | 9 ++- .../documentation/CCRDocumentationIT.java | 75 +++++++++++++++++-- .../ccr/delete_auto_follow_pattern.asciidoc | 32 ++++++++ .../high-level/supported-apis.asciidoc | 2 + 7 files changed, 204 insertions(+), 12 deletions(-) create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/DeleteAutoFollowPatternRequest.java create mode 100644 docs/java-rest/high-level/ccr/delete_auto_follow_pattern.asciidoc diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/CcrClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/CcrClient.java index dfb516ae08395..86710ffdf8d04 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/CcrClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/CcrClient.java @@ -20,6 +20,7 @@ package org.elasticsearch.client; import org.elasticsearch.action.ActionListener; +import org.elasticsearch.client.ccr.DeleteAutoFollowPatternRequest; import org.elasticsearch.client.ccr.PauseFollowRequest; import org.elasticsearch.client.ccr.PutAutoFollowPatternRequest; import org.elasticsearch.client.ccr.PutFollowRequest; @@ -77,6 +78,7 @@ public PutFollowResponse putFollow(PutFollowRequest request, RequestOptions opti * * @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 putFollowAsync(PutFollowRequest request, RequestOptions options, @@ -120,6 +122,7 @@ public AcknowledgedResponse pauseFollow(PauseFollowRequest request, RequestOptio * * @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 pauseFollowAsync(PauseFollowRequest request, RequestOptions options, @@ -162,6 +165,7 @@ public AcknowledgedResponse resumeFollow(ResumeFollowRequest request, RequestOpt * * @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 resumeFollowAsync(ResumeFollowRequest request, RequestOptions options, @@ -206,6 +210,7 @@ public AcknowledgedResponse unfollow(UnfollowRequest request, RequestOptions opt * * @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 unfollowAsync(UnfollowRequest request, RequestOptions options, @@ -249,6 +254,7 @@ public AcknowledgedResponse putAutoFollowPattern(PutAutoFollowPatternRequest req * * @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 putAutoFollowPatternAsync(PutAutoFollowPatternRequest request, RequestOptions options, @@ -262,4 +268,49 @@ public void putAutoFollowPatternAsync(PutAutoFollowPatternRequest request, Collections.emptySet()); } + /** + * Deletes an auto follow pattern. + * + * See + * the docs 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 AcknowledgedResponse deleteAutoFollowPattern(DeleteAutoFollowPatternRequest request, + RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + CcrRequestConverters::deleteAutoFollowPattern, + options, + AcknowledgedResponse::fromXContent, + Collections.emptySet() + ); + } + + /** + * Deletes an auto follow pattern. + * + * See + * the docs 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 deleteAutoFollowPatternAsync(DeleteAutoFollowPatternRequest request, + RequestOptions options, + ActionListener listener) { + restHighLevelClient.performRequestAsyncAndParseEntity( + request, + CcrRequestConverters::deleteAutoFollowPattern, + options, + AcknowledgedResponse::fromXContent, + listener, + Collections.emptySet() + ); + } + } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/CcrRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/CcrRequestConverters.java index 47344ff6c329b..8963919bcd154 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/CcrRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/CcrRequestConverters.java @@ -19,8 +19,10 @@ package org.elasticsearch.client; +import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; +import org.elasticsearch.client.ccr.DeleteAutoFollowPatternRequest; import org.elasticsearch.client.ccr.PauseFollowRequest; import org.elasticsearch.client.ccr.PutAutoFollowPatternRequest; import org.elasticsearch.client.ccr.PutFollowRequest; @@ -80,4 +82,12 @@ static Request putAutoFollowPattern(PutAutoFollowPatternRequest putAutoFollowPat return request; } + static Request deleteAutoFollowPattern(DeleteAutoFollowPatternRequest deleteAutoFollowPatternRequest) { + String endpoint = new RequestConverters.EndpointBuilder() + .addPathPartAsIs("_ccr", "auto_follow") + .addPathPart(deleteAutoFollowPatternRequest.getName()) + .build(); + return new Request(HttpDelete.METHOD_NAME, endpoint); + } + } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/DeleteAutoFollowPatternRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/DeleteAutoFollowPatternRequest.java new file mode 100644 index 0000000000000..9293cea964741 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/DeleteAutoFollowPatternRequest.java @@ -0,0 +1,37 @@ +/* + * 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.ccr; + +import org.elasticsearch.client.Validatable; + +import java.util.Objects; + +public final class DeleteAutoFollowPatternRequest implements Validatable { + + private final String name; + + public DeleteAutoFollowPatternRequest(String name) { + this.name = Objects.requireNonNull(name); + } + + public String getName() { + return name; + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/CCRIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/CCRIT.java index 55ee556476f23..00b2d26abaf57 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/CCRIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/CCRIT.java @@ -29,6 +29,7 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.support.WriteRequest; +import org.elasticsearch.client.ccr.DeleteAutoFollowPatternRequest; import org.elasticsearch.client.ccr.PauseFollowRequest; import org.elasticsearch.client.ccr.PutAutoFollowPatternRequest; import org.elasticsearch.client.ccr.PutFollowRequest; @@ -148,10 +149,10 @@ public void testAutoFollowing() throws Exception { }); // Cleanup: - // TODO: replace with hlrc delete auto follow pattern when it is available: - final Request deleteAutoFollowPatternRequest = new Request("DELETE", "/_ccr/auto_follow/pattern1"); - Map deleteAutoFollowPatternResponse = toMap(client().performRequest(deleteAutoFollowPatternRequest)); - assertThat(deleteAutoFollowPatternResponse.get("acknowledged"), is(true)); + final DeleteAutoFollowPatternRequest deleteAutoFollowPatternRequest = new DeleteAutoFollowPatternRequest("pattern1"); + AcknowledgedResponse deleteAutoFollowPatternResponse = + execute(deleteAutoFollowPatternRequest, ccrClient::deleteAutoFollowPattern, ccrClient::deleteAutoFollowPatternAsync); + assertThat(deleteAutoFollowPatternResponse.isAcknowledged(), is(true)); PauseFollowRequest pauseFollowRequest = new PauseFollowRequest("copy-logs-20200101"); AcknowledgedResponse pauseFollowResponse = ccrClient.pauseFollow(pauseFollowRequest, RequestOptions.DEFAULT); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CCRDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CCRDocumentationIT.java index bc9375b6d6c9d..1d1aef514cab9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CCRDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CCRDocumentationIT.java @@ -33,6 +33,7 @@ import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.Response; import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.ccr.DeleteAutoFollowPatternRequest; import org.elasticsearch.client.ccr.PauseFollowRequest; import org.elasticsearch.client.ccr.PutAutoFollowPatternRequest; import org.elasticsearch.client.ccr.PutFollowRequest; @@ -401,10 +402,9 @@ public void testPutAutoFollowPattern() throws Exception { // Delete auto follow pattern, so that we can store it again: { - // TODO: replace with hlrc delete auto follow pattern when it is available: - final Request deleteRequest = new Request("DELETE", "/_ccr/auto_follow/my_pattern"); - Map deleteAutoFollowPatternResponse = toMap(client().performRequest(deleteRequest)); - assertThat(deleteAutoFollowPatternResponse.get("acknowledged"), is(true)); + final DeleteAutoFollowPatternRequest deleteRequest = new DeleteAutoFollowPatternRequest("my_pattern"); + AcknowledgedResponse deleteResponse = client.ccr().deleteAutoFollowPattern(deleteRequest, RequestOptions.DEFAULT); + assertThat(deleteResponse.isAcknowledged(), is(true)); } // tag::ccr-put-auto-follow-pattern-execute-listener @@ -435,13 +435,72 @@ public void onFailure(Exception e) { // Cleanup: { - // TODO: replace with hlrc delete auto follow pattern when it is available: - final Request deleteRequest = new Request("DELETE", "/_ccr/auto_follow/my_pattern"); - Map deleteAutoFollowPatternResponse = toMap(client().performRequest(deleteRequest)); - assertThat(deleteAutoFollowPatternResponse.get("acknowledged"), is(true)); + final DeleteAutoFollowPatternRequest deleteRequest = new DeleteAutoFollowPatternRequest("my_pattern"); + AcknowledgedResponse deleteResponse = client.ccr().deleteAutoFollowPattern(deleteRequest, RequestOptions.DEFAULT); + assertThat(deleteResponse.isAcknowledged(), is(true)); } } + public void testDeleteAutoFollowPattern() throws Exception { + RestHighLevelClient client = highLevelClient(); + + // Put auto follow pattern, so that we can delete it: + { + final PutAutoFollowPatternRequest putRequest = + new PutAutoFollowPatternRequest("my_pattern", "local", Collections.singletonList("logs-*")); + AcknowledgedResponse putResponse = client.ccr().putAutoFollowPattern(putRequest, RequestOptions.DEFAULT); + assertThat(putResponse.isAcknowledged(), is(true)); + } + + // tag::ccr-delete-auto-follow-pattern-request + DeleteAutoFollowPatternRequest request = + new DeleteAutoFollowPatternRequest("my_pattern"); // <1> + // end::ccr-delete-auto-follow-pattern-request + + // tag::ccr-delete-auto-follow-pattern-execute + AcknowledgedResponse response = client.ccr() + .deleteAutoFollowPattern(request, RequestOptions.DEFAULT); + // end::ccr-delete-auto-follow-pattern-execute + + // tag::ccr-delete-auto-follow-pattern-response + boolean acknowledged = response.isAcknowledged(); // <1> + // end::ccr-delete-auto-follow-pattern-response + + // Put auto follow pattern, so that we can delete it again: + { + final PutAutoFollowPatternRequest putRequest = + new PutAutoFollowPatternRequest("my_pattern", "local", Collections.singletonList("logs-*")); + AcknowledgedResponse putResponse = client.ccr().putAutoFollowPattern(putRequest, RequestOptions.DEFAULT); + assertThat(putResponse.isAcknowledged(), is(true)); + } + + // tag::ccr-delete-auto-follow-pattern-execute-listener + ActionListener listener = + new ActionListener() { + @Override + public void onResponse(AcknowledgedResponse response) { // <1> + boolean acknowledged = response.isAcknowledged(); + } + + @Override + public void onFailure(Exception e) { + // <2> + } + }; + // end::ccr-delete-auto-follow-pattern-execute-listener + + // Replace the empty listener by a blocking listener in test + final CountDownLatch latch = new CountDownLatch(1); + listener = new LatchedActionListener<>(listener, latch); + + // tag::ccr-delete-auto-follow-pattern-execute-async + client.ccr().deleteAutoFollowPatternAsync(request, + RequestOptions.DEFAULT, listener); // <1> + // end::ccr-delete-auto-follow-pattern-execute-async + + assertTrue(latch.await(30L, TimeUnit.SECONDS)); + } + static Map toMap(Response response) throws IOException { return XContentHelper.convertToMap(JsonXContent.jsonXContent, EntityUtils.toString(response.getEntity()), false); } diff --git a/docs/java-rest/high-level/ccr/delete_auto_follow_pattern.asciidoc b/docs/java-rest/high-level/ccr/delete_auto_follow_pattern.asciidoc new file mode 100644 index 0000000000000..f79dbd5d39de3 --- /dev/null +++ b/docs/java-rest/high-level/ccr/delete_auto_follow_pattern.asciidoc @@ -0,0 +1,32 @@ +-- +:api: ccr-delete-auto-follow-pattern +:request: DeleteAutoFollowPatternRequest +:response: AcknowledgedResponse +-- + +[id="{upid}-{api}"] +=== Delete Auto Follow Pattern API + +[id="{upid}-{api}-request"] +==== Request + +The Delete Auto Follow Pattern API allows you to delete an auto follow pattern. + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-request] +-------------------------------------------------- +<1> The name of the auto follow pattern to delete. + +[id="{upid}-{api}-response"] +==== Response + +The returned +{response}+ indicates if the delete auto follow pattern request was received. + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-response] +-------------------------------------------------- +<1> Whether or not the delete auto follow pattern request was acknowledged. + +include::../execution.asciidoc[] diff --git a/docs/java-rest/high-level/supported-apis.asciidoc b/docs/java-rest/high-level/supported-apis.asciidoc index ca9083db3f509..870702cc2fb3e 100644 --- a/docs/java-rest/high-level/supported-apis.asciidoc +++ b/docs/java-rest/high-level/supported-apis.asciidoc @@ -463,12 +463,14 @@ The Java High Level REST Client supports the following CCR APIs: * <<{upid}-ccr-resume-follow>> * <<{upid}-ccr-unfollow>> * <<{upid}-ccr-put-auto-follow-pattern>> +* <<{upid}-ccr-delete-auto-follow-pattern>> include::ccr/put_follow.asciidoc[] include::ccr/pause_follow.asciidoc[] include::ccr/resume_follow.asciidoc[] include::ccr/unfollow.asciidoc[] include::ccr/put_auto_follow_pattern.asciidoc[] +include::ccr/delete_auto_follow_pattern.asciidoc[] == Index Lifecycle Management APIs