Skip to content

Commit

Permalink
[HLRC] Added support for CCR Delete Auto Follow Pattern API
Browse files Browse the repository at this point in the history
This change also adds documentation for the Delete Auto Follow Pattern API.

Relates to elastic#33824
  • Loading branch information
martijnvg committed Nov 28, 2018
1 parent fc7e7e7 commit 3bfe79d
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 12 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -262,4 +268,49 @@ public void putAutoFollowPatternAsync(PutAutoFollowPatternRequest request,
Collections.emptySet());
}

/**
* Deletes an auto follow pattern.
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-delete-auto-follow-pattern.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 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 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-delete-auto-follow-pattern.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 deleteAutoFollowPatternAsync(DeleteAutoFollowPatternRequest request,
RequestOptions options,
ActionListener<AcknowledgedResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(
request,
CcrRequestConverters::deleteAutoFollowPattern,
options,
AcknowledgedResponse::fromXContent,
listener,
Collections.emptySet()
);
}

}
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}
@@ -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;
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<AcknowledgedResponse> listener =
new ActionListener<AcknowledgedResponse>() {
@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<String, Object> toMap(Response response) throws IOException {
return XContentHelper.convertToMap(JsonXContent.jsonXContent, EntityUtils.toString(response.getEntity()), false);
}
Expand Down
32 changes: 32 additions & 0 deletions 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[]
2 changes: 2 additions & 0 deletions docs/java-rest/high-level/supported-apis.asciidoc
Expand Up @@ -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

Expand Down

0 comments on commit 3bfe79d

Please sign in to comment.