Add HLRC support for delete policy api (#45833)

This PR also adds HLRC docs.

Relates to #32789
This commit is contained in:
Martijn van Groningen 2019-08-26 09:54:25 +02:00
parent a82d24b3ce
commit b170b76670
No known key found for this signature in database
GPG Key ID: AB236F4FCF2AF12A
8 changed files with 228 additions and 11 deletions

View File

@ -20,6 +20,7 @@ package org.elasticsearch.client;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.client.core.AcknowledgedResponse;
import org.elasticsearch.client.enrich.DeletePolicyRequest;
import org.elasticsearch.client.enrich.PutPolicyRequest;
import java.io.IOException;
@ -43,7 +44,7 @@ public final class EnrichClient {
/**
* Executes the put policy api, which stores an enrich policy.
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-processor.html#put-policy-api">
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-policy-apis.html#put-policy-api">
* the docs</a> for more.
*
* @param request the {@link PutPolicyRequest}
@ -64,7 +65,7 @@ public final class EnrichClient {
/**
* Asynchronously executes the put policy api, which stores an enrich policy.
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-processor.html#put-policy-api">
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/enrich-policy-apis.html#put-policy-api">
* the docs</a> for more.
*
* @param request the {@link PutPolicyRequest}
@ -83,4 +84,48 @@ public final class EnrichClient {
Collections.emptySet()
);
}
/**
* Executes the delete policy api, which deletes an enrich policy.
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-policy-apis.html#delete-policy-api">
* the docs</a> for more.
*
* @param request the {@link DeletePolicyRequest}
* @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 deletePolicy(DeletePolicyRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(
request,
EnrichRequestConverters::deletePolicy,
options,
AcknowledgedResponse::fromXContent,
Collections.emptySet()
);
}
/**
* Asynchronously executes the delete policy api, which deletes an enrich policy.
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-policy-apis.html#delete-policy-api">
* the docs</a> for more.
*
* @param request the {@link DeletePolicyRequest}
* @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 deletePolicyAsync(DeletePolicyRequest request,
RequestOptions options,
ActionListener<AcknowledgedResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(
request,
EnrichRequestConverters::deletePolicy,
options,
AcknowledgedResponse::fromXContent,
listener,
Collections.emptySet()
);
}
}

View File

@ -18,7 +18,9 @@
*/
package org.elasticsearch.client;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPut;
import org.elasticsearch.client.enrich.DeletePolicyRequest;
import org.elasticsearch.client.enrich.PutPolicyRequest;
import java.io.IOException;
@ -38,4 +40,12 @@ final class EnrichRequestConverters {
return request;
}
static Request deletePolicy(DeletePolicyRequest deletePolicyRequest) {
String endpoint = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_enrich", "policy")
.addPathPart(deletePolicyRequest.getName())
.build();
return new Request(HttpDelete.METHOD_NAME, endpoint);
}
}

View File

@ -0,0 +1,38 @@
/*
* 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.enrich;
import org.elasticsearch.client.Validatable;
import org.elasticsearch.common.Strings;
public class DeletePolicyRequest implements Validatable {
private final String name;
public DeletePolicyRequest(String name) {
if (Strings.hasLength(name) == false) {
throw new IllegalArgumentException("name must be a non-null and non-empty string");
}
this.name = name;
}
public String getName() {
return name;
}
}

View File

@ -20,6 +20,7 @@ package org.elasticsearch.client;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.core.AcknowledgedResponse;
import org.elasticsearch.client.enrich.DeletePolicyRequest;
import org.elasticsearch.client.enrich.PutPolicyRequest;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.json.JsonXContent;
@ -47,15 +48,26 @@ public class EnrichIT extends ESRestHighLevelClientTestCase {
Response getPolicyResponse = highLevelClient().getLowLevelClient().performRequest(getPolicyRequest);
assertThat(getPolicyResponse.getHttpResponse().getStatusLine().getStatusCode(), equalTo(200));
Map<String, Object> responseBody = toMap(getPolicyResponse);
@SuppressWarnings("unchecked")
List<Map<String, Object>> responsePolicies = (List<Map<String, Object>>) responseBody.get("policies");
List<?> responsePolicies = (List<?>) responseBody.get("policies");
assertThat(responsePolicies.size(), equalTo(1));
assertThat(XContentMapValues.extractValue("exact_match.indices", responsePolicies.get(0)),
equalTo(putPolicyRequest.getIndices()));
assertThat(XContentMapValues.extractValue("exact_match.match_field", responsePolicies.get(0)),
equalTo(putPolicyRequest.getMatchField()));
assertThat(XContentMapValues.extractValue("exact_match.enrich_fields", responsePolicies.get(0)),
Map<?, ?> responsePolicy = (Map<?, ?>) responsePolicies.get(0);
assertThat(XContentMapValues.extractValue("exact_match.indices", responsePolicy), equalTo(putPolicyRequest.getIndices()));
assertThat(XContentMapValues.extractValue("exact_match.match_field", responsePolicy), equalTo(putPolicyRequest.getMatchField()));
assertThat(XContentMapValues.extractValue("exact_match.enrich_fields", responsePolicy),
equalTo(putPolicyRequest.getEnrichFields()));
DeletePolicyRequest deletePolicyRequest = new DeletePolicyRequest("my-policy");
AcknowledgedResponse deletePolicyResponse =
execute(deletePolicyRequest, enrichClient::deletePolicy, enrichClient::deletePolicyAsync);
assertThat(deletePolicyResponse.isAcknowledged(), is(true));
// TODO: Replace with get policy hlrc code:
getPolicyRequest = new Request("get", "/_enrich/policy");
getPolicyResponse = highLevelClient().getLowLevelClient().performRequest(getPolicyRequest);
assertThat(getPolicyResponse.getHttpResponse().getStatusLine().getStatusCode(), equalTo(200));
responseBody = toMap(getPolicyResponse);
responsePolicies = (List<?>) responseBody.get("policies");
assertThat(responsePolicies.size(), equalTo(0));
}
private static Map<String, Object> toMap(Response response) throws IOException {

View File

@ -18,12 +18,15 @@
*/
package org.elasticsearch.client;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPut;
import org.elasticsearch.client.enrich.DeletePolicyRequest;
import org.elasticsearch.client.enrich.PutPolicyRequest;
import org.elasticsearch.client.enrich.PutPolicyRequestTests;
import org.elasticsearch.test.ESTestCase;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
public class EnrichRequestConvertersTests extends ESTestCase {
@ -33,7 +36,18 @@ public class EnrichRequestConvertersTests extends ESTestCase {
assertThat(result.getMethod(), equalTo(HttpPut.METHOD_NAME));
assertThat(result.getEndpoint(), equalTo("/_enrich/policy/" + request.getName()));
assertThat(result.getParameters().size(), equalTo(0));
RequestConvertersTests.assertToXContentBody(request, result.getEntity());
}
public void testDeletePolicy() throws Exception {
DeletePolicyRequest request = new DeletePolicyRequest(randomAlphaOfLength(4));
Request result = EnrichRequestConverters.deletePolicy(request);
assertThat(result.getMethod(), equalTo(HttpDelete.METHOD_NAME));
assertThat(result.getEndpoint(), equalTo("/_enrich/policy/" + request.getName()));
assertThat(result.getParameters().size(), equalTo(0));
assertThat(result.getEntity(), nullValue());
}
}

View File

@ -24,7 +24,9 @@ import org.elasticsearch.client.ESRestHighLevelClientTestCase;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.AcknowledgedResponse;
import org.elasticsearch.client.enrich.DeletePolicyRequest;
import org.elasticsearch.client.enrich.PutPolicyRequest;
import org.junit.After;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
@ -32,6 +34,17 @@ import java.util.concurrent.TimeUnit;
public class EnrichDocumentationIT extends ESRestHighLevelClientTestCase {
@After
public void cleanup() {
RestHighLevelClient client = highLevelClient();
DeletePolicyRequest deletePolicyRequest = new DeletePolicyRequest("users-policy");
try {
client.enrich().deletePolicy(deletePolicyRequest, RequestOptions.DEFAULT);
} catch (Exception e) {
// ignore... it is ok if policy has already been removed
}
}
public void testPutPolicy() throws Exception {
RestHighLevelClient client = highLevelClient();
// tag::enrich-put-policy-request
@ -55,8 +68,7 @@ public class EnrichDocumentationIT extends ESRestHighLevelClientTestCase {
new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(AcknowledgedResponse response) { // <1>
boolean isAcknowledged =
putPolicyResponse.isAcknowledged();
boolean isAcknowledged = response.isAcknowledged();
}
@Override
@ -78,4 +90,57 @@ public class EnrichDocumentationIT extends ESRestHighLevelClientTestCase {
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
public void testDeletePolicy() throws Exception {
RestHighLevelClient client = highLevelClient();
{
// Add a policy, so that it can be deleted:
PutPolicyRequest putPolicyRequest = new PutPolicyRequest(
"users-policy", "exact_match", Arrays.asList("users"),
"email", Arrays.asList("address", "zip", "city", "state"));
client.enrich().putPolicy(putPolicyRequest, RequestOptions.DEFAULT);
}
// tag::enrich-delete-policy-request
DeletePolicyRequest deletePolicyRequest =
new DeletePolicyRequest("users-policy");
// end::enrich-delete-policy-request
// tag::enrich-delete-policy-execute
AcknowledgedResponse deletePolicyResponse = client.enrich()
.deletePolicy(deletePolicyRequest, RequestOptions.DEFAULT);
// end::enrich-delete-policy-execute
// tag::enrich-delete-policy-response
boolean isAcknowledged =
deletePolicyResponse.isAcknowledged(); // <1>
// end::enrich-delete-policy-response
// tag::enrich-delete-policy-execute-listener
ActionListener<AcknowledgedResponse> listener =
new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(AcknowledgedResponse response) { // <1>
boolean isAcknowledged = response.isAcknowledged();
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::enrich-delete-policy-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::enrich-delete-policy-execute-async
client.enrich().deletePolicyAsync(deletePolicyRequest,
RequestOptions.DEFAULT, listener); // <1>
// end::enrich-delete-policy-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}

View File

@ -0,0 +1,31 @@
--
:api: enrich-delete-policy
:request: DeletePolicyRequest
:response: AcknowledgedResponse
--
[id="{upid}-{api}"]
=== Delete Policy API
[id="{upid}-{api}-request"]
==== Request
The Delete Policy API deletes an enrich policy from Elasticsearch.
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-request]
--------------------------------------------------
[id="{upid}-{api}-response"]
==== Response
The returned +{response}+ indicates if the delete policy request was acknowledged.
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-response]
--------------------------------------------------
<1> Whether delete policy request was acknowledged.
include::../execution.asciidoc[]

View File

@ -603,5 +603,7 @@ include::dataframe/stop_data_frame.asciidoc[]
The Java High Level REST Client supports the following Enrich APIs:
* <<{upid}-enrich-put-policy>>
* <<{upid}-enrich-delete-policy>>
include::enrich/put_policy.asciidoc[]
include::enrich/delete_policy.asciidoc[]