Skip to content

Commit b170b76

Browse files
committed
Add HLRC support for delete policy api (#45833)
This PR also adds HLRC docs. Relates to #32789
1 parent a82d24b commit b170b76

File tree

8 files changed

+228
-11
lines changed

8 files changed

+228
-11
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/EnrichClient.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.elasticsearch.action.ActionListener;
2222
import org.elasticsearch.client.core.AcknowledgedResponse;
23+
import org.elasticsearch.client.enrich.DeletePolicyRequest;
2324
import org.elasticsearch.client.enrich.PutPolicyRequest;
2425

2526
import java.io.IOException;
@@ -43,7 +44,7 @@ public final class EnrichClient {
4344
/**
4445
* Executes the put policy api, which stores an enrich policy.
4546
*
46-
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-processor.html#put-policy-api">
47+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-policy-apis.html#put-policy-api">
4748
* the docs</a> for more.
4849
*
4950
* @param request the {@link PutPolicyRequest}
@@ -64,7 +65,7 @@ public AcknowledgedResponse putPolicy(PutPolicyRequest request, RequestOptions o
6465
/**
6566
* Asynchronously executes the put policy api, which stores an enrich policy.
6667
*
67-
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-processor.html#put-policy-api">
68+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/enrich-policy-apis.html#put-policy-api">
6869
* the docs</a> for more.
6970
*
7071
* @param request the {@link PutPolicyRequest}
@@ -83,4 +84,48 @@ public void putPolicyAsync(PutPolicyRequest request,
8384
Collections.emptySet()
8485
);
8586
}
87+
88+
/**
89+
* Executes the delete policy api, which deletes an enrich policy.
90+
*
91+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-policy-apis.html#delete-policy-api">
92+
* the docs</a> for more.
93+
*
94+
* @param request the {@link DeletePolicyRequest}
95+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
96+
* @return the response
97+
* @throws IOException in case there is a problem sending the request or parsing back the response
98+
*/
99+
public AcknowledgedResponse deletePolicy(DeletePolicyRequest request, RequestOptions options) throws IOException {
100+
return restHighLevelClient.performRequestAndParseEntity(
101+
request,
102+
EnrichRequestConverters::deletePolicy,
103+
options,
104+
AcknowledgedResponse::fromXContent,
105+
Collections.emptySet()
106+
);
107+
}
108+
109+
/**
110+
* Asynchronously executes the delete policy api, which deletes an enrich policy.
111+
*
112+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/enrich-policy-apis.html#delete-policy-api">
113+
* the docs</a> for more.
114+
*
115+
* @param request the {@link DeletePolicyRequest}
116+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
117+
* @param listener the listener to be notified upon request completion
118+
*/
119+
public void deletePolicyAsync(DeletePolicyRequest request,
120+
RequestOptions options,
121+
ActionListener<AcknowledgedResponse> listener) {
122+
restHighLevelClient.performRequestAsyncAndParseEntity(
123+
request,
124+
EnrichRequestConverters::deletePolicy,
125+
options,
126+
AcknowledgedResponse::fromXContent,
127+
listener,
128+
Collections.emptySet()
129+
);
130+
}
86131
}

client/rest-high-level/src/main/java/org/elasticsearch/client/EnrichRequestConverters.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
*/
1919
package org.elasticsearch.client;
2020

21+
import org.apache.http.client.methods.HttpDelete;
2122
import org.apache.http.client.methods.HttpPut;
23+
import org.elasticsearch.client.enrich.DeletePolicyRequest;
2224
import org.elasticsearch.client.enrich.PutPolicyRequest;
2325

2426
import java.io.IOException;
@@ -38,4 +40,12 @@ static Request putPolicy(PutPolicyRequest putPolicyRequest) throws IOException {
3840
return request;
3941
}
4042

43+
static Request deletePolicy(DeletePolicyRequest deletePolicyRequest) {
44+
String endpoint = new RequestConverters.EndpointBuilder()
45+
.addPathPartAsIs("_enrich", "policy")
46+
.addPathPart(deletePolicyRequest.getName())
47+
.build();
48+
return new Request(HttpDelete.METHOD_NAME, endpoint);
49+
}
50+
4151
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.client.enrich;
20+
21+
import org.elasticsearch.client.Validatable;
22+
import org.elasticsearch.common.Strings;
23+
24+
public class DeletePolicyRequest implements Validatable {
25+
26+
private final String name;
27+
28+
public DeletePolicyRequest(String name) {
29+
if (Strings.hasLength(name) == false) {
30+
throw new IllegalArgumentException("name must be a non-null and non-empty string");
31+
}
32+
this.name = name;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
}

client/rest-high-level/src/test/java/org/elasticsearch/client/EnrichIT.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.apache.http.util.EntityUtils;
2222
import org.elasticsearch.client.core.AcknowledgedResponse;
23+
import org.elasticsearch.client.enrich.DeletePolicyRequest;
2324
import org.elasticsearch.client.enrich.PutPolicyRequest;
2425
import org.elasticsearch.common.xcontent.XContentHelper;
2526
import org.elasticsearch.common.xcontent.json.JsonXContent;
@@ -47,15 +48,26 @@ public void testCRUD() throws Exception {
4748
Response getPolicyResponse = highLevelClient().getLowLevelClient().performRequest(getPolicyRequest);
4849
assertThat(getPolicyResponse.getHttpResponse().getStatusLine().getStatusCode(), equalTo(200));
4950
Map<String, Object> responseBody = toMap(getPolicyResponse);
50-
@SuppressWarnings("unchecked")
51-
List<Map<String, Object>> responsePolicies = (List<Map<String, Object>>) responseBody.get("policies");
51+
List<?> responsePolicies = (List<?>) responseBody.get("policies");
5252
assertThat(responsePolicies.size(), equalTo(1));
53-
assertThat(XContentMapValues.extractValue("exact_match.indices", responsePolicies.get(0)),
54-
equalTo(putPolicyRequest.getIndices()));
55-
assertThat(XContentMapValues.extractValue("exact_match.match_field", responsePolicies.get(0)),
56-
equalTo(putPolicyRequest.getMatchField()));
57-
assertThat(XContentMapValues.extractValue("exact_match.enrich_fields", responsePolicies.get(0)),
53+
Map<?, ?> responsePolicy = (Map<?, ?>) responsePolicies.get(0);
54+
assertThat(XContentMapValues.extractValue("exact_match.indices", responsePolicy), equalTo(putPolicyRequest.getIndices()));
55+
assertThat(XContentMapValues.extractValue("exact_match.match_field", responsePolicy), equalTo(putPolicyRequest.getMatchField()));
56+
assertThat(XContentMapValues.extractValue("exact_match.enrich_fields", responsePolicy),
5857
equalTo(putPolicyRequest.getEnrichFields()));
58+
59+
DeletePolicyRequest deletePolicyRequest = new DeletePolicyRequest("my-policy");
60+
AcknowledgedResponse deletePolicyResponse =
61+
execute(deletePolicyRequest, enrichClient::deletePolicy, enrichClient::deletePolicyAsync);
62+
assertThat(deletePolicyResponse.isAcknowledged(), is(true));
63+
64+
// TODO: Replace with get policy hlrc code:
65+
getPolicyRequest = new Request("get", "/_enrich/policy");
66+
getPolicyResponse = highLevelClient().getLowLevelClient().performRequest(getPolicyRequest);
67+
assertThat(getPolicyResponse.getHttpResponse().getStatusLine().getStatusCode(), equalTo(200));
68+
responseBody = toMap(getPolicyResponse);
69+
responsePolicies = (List<?>) responseBody.get("policies");
70+
assertThat(responsePolicies.size(), equalTo(0));
5971
}
6072

6173
private static Map<String, Object> toMap(Response response) throws IOException {

client/rest-high-level/src/test/java/org/elasticsearch/client/EnrichRequestConvertersTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
*/
1919
package org.elasticsearch.client;
2020

21+
import org.apache.http.client.methods.HttpDelete;
2122
import org.apache.http.client.methods.HttpPut;
23+
import org.elasticsearch.client.enrich.DeletePolicyRequest;
2224
import org.elasticsearch.client.enrich.PutPolicyRequest;
2325
import org.elasticsearch.client.enrich.PutPolicyRequestTests;
2426
import org.elasticsearch.test.ESTestCase;
2527

2628
import static org.hamcrest.Matchers.equalTo;
29+
import static org.hamcrest.Matchers.nullValue;
2730

2831
public class EnrichRequestConvertersTests extends ESTestCase {
2932

@@ -33,7 +36,18 @@ public void testPutPolicy() throws Exception {
3336

3437
assertThat(result.getMethod(), equalTo(HttpPut.METHOD_NAME));
3538
assertThat(result.getEndpoint(), equalTo("/_enrich/policy/" + request.getName()));
39+
assertThat(result.getParameters().size(), equalTo(0));
3640
RequestConvertersTests.assertToXContentBody(request, result.getEntity());
3741
}
3842

43+
public void testDeletePolicy() throws Exception {
44+
DeletePolicyRequest request = new DeletePolicyRequest(randomAlphaOfLength(4));
45+
Request result = EnrichRequestConverters.deletePolicy(request);
46+
47+
assertThat(result.getMethod(), equalTo(HttpDelete.METHOD_NAME));
48+
assertThat(result.getEndpoint(), equalTo("/_enrich/policy/" + request.getName()));
49+
assertThat(result.getParameters().size(), equalTo(0));
50+
assertThat(result.getEntity(), nullValue());
51+
}
52+
3953
}

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/EnrichDocumentationIT.java

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,27 @@
2424
import org.elasticsearch.client.RequestOptions;
2525
import org.elasticsearch.client.RestHighLevelClient;
2626
import org.elasticsearch.client.core.AcknowledgedResponse;
27+
import org.elasticsearch.client.enrich.DeletePolicyRequest;
2728
import org.elasticsearch.client.enrich.PutPolicyRequest;
29+
import org.junit.After;
2830

2931
import java.util.Arrays;
3032
import java.util.concurrent.CountDownLatch;
3133
import java.util.concurrent.TimeUnit;
3234

3335
public class EnrichDocumentationIT extends ESRestHighLevelClientTestCase {
3436

37+
@After
38+
public void cleanup() {
39+
RestHighLevelClient client = highLevelClient();
40+
DeletePolicyRequest deletePolicyRequest = new DeletePolicyRequest("users-policy");
41+
try {
42+
client.enrich().deletePolicy(deletePolicyRequest, RequestOptions.DEFAULT);
43+
} catch (Exception e) {
44+
// ignore... it is ok if policy has already been removed
45+
}
46+
}
47+
3548
public void testPutPolicy() throws Exception {
3649
RestHighLevelClient client = highLevelClient();
3750
// tag::enrich-put-policy-request
@@ -55,8 +68,7 @@ public void testPutPolicy() throws Exception {
5568
new ActionListener<AcknowledgedResponse>() {
5669
@Override
5770
public void onResponse(AcknowledgedResponse response) { // <1>
58-
boolean isAcknowledged =
59-
putPolicyResponse.isAcknowledged();
71+
boolean isAcknowledged = response.isAcknowledged();
6072
}
6173

6274
@Override
@@ -78,4 +90,57 @@ public void onFailure(Exception e) {
7890
assertTrue(latch.await(30L, TimeUnit.SECONDS));
7991
}
8092

93+
public void testDeletePolicy() throws Exception {
94+
RestHighLevelClient client = highLevelClient();
95+
96+
{
97+
// Add a policy, so that it can be deleted:
98+
PutPolicyRequest putPolicyRequest = new PutPolicyRequest(
99+
"users-policy", "exact_match", Arrays.asList("users"),
100+
"email", Arrays.asList("address", "zip", "city", "state"));
101+
client.enrich().putPolicy(putPolicyRequest, RequestOptions.DEFAULT);
102+
}
103+
104+
// tag::enrich-delete-policy-request
105+
DeletePolicyRequest deletePolicyRequest =
106+
new DeletePolicyRequest("users-policy");
107+
// end::enrich-delete-policy-request
108+
109+
// tag::enrich-delete-policy-execute
110+
AcknowledgedResponse deletePolicyResponse = client.enrich()
111+
.deletePolicy(deletePolicyRequest, RequestOptions.DEFAULT);
112+
// end::enrich-delete-policy-execute
113+
114+
// tag::enrich-delete-policy-response
115+
boolean isAcknowledged =
116+
deletePolicyResponse.isAcknowledged(); // <1>
117+
// end::enrich-delete-policy-response
118+
119+
// tag::enrich-delete-policy-execute-listener
120+
ActionListener<AcknowledgedResponse> listener =
121+
new ActionListener<AcknowledgedResponse>() {
122+
@Override
123+
public void onResponse(AcknowledgedResponse response) { // <1>
124+
boolean isAcknowledged = response.isAcknowledged();
125+
}
126+
127+
@Override
128+
public void onFailure(Exception e) {
129+
// <2>
130+
}
131+
};
132+
// end::enrich-delete-policy-execute-listener
133+
134+
// Replace the empty listener by a blocking listener in test
135+
final CountDownLatch latch = new CountDownLatch(1);
136+
listener = new LatchedActionListener<>(listener, latch);
137+
138+
// tag::enrich-delete-policy-execute-async
139+
client.enrich().deletePolicyAsync(deletePolicyRequest,
140+
RequestOptions.DEFAULT, listener); // <1>
141+
// end::enrich-delete-policy-execute-async
142+
143+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
144+
}
145+
81146
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--
2+
:api: enrich-delete-policy
3+
:request: DeletePolicyRequest
4+
:response: AcknowledgedResponse
5+
--
6+
7+
[id="{upid}-{api}"]
8+
=== Delete Policy API
9+
10+
[id="{upid}-{api}-request"]
11+
==== Request
12+
13+
The Delete Policy API deletes an enrich policy from Elasticsearch.
14+
15+
["source","java",subs="attributes,callouts,macros"]
16+
--------------------------------------------------
17+
include-tagged::{doc-tests-file}[{api}-request]
18+
--------------------------------------------------
19+
20+
[id="{upid}-{api}-response"]
21+
==== Response
22+
23+
The returned +{response}+ indicates if the delete policy request was acknowledged.
24+
25+
["source","java",subs="attributes,callouts,macros"]
26+
--------------------------------------------------
27+
include-tagged::{doc-tests-file}[{api}-response]
28+
--------------------------------------------------
29+
<1> Whether delete policy request was acknowledged.
30+
31+
include::../execution.asciidoc[]

docs/java-rest/high-level/supported-apis.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,5 +603,7 @@ include::dataframe/stop_data_frame.asciidoc[]
603603
The Java High Level REST Client supports the following Enrich APIs:
604604

605605
* <<{upid}-enrich-put-policy>>
606+
* <<{upid}-enrich-delete-policy>>
606607

607608
include::enrich/put_policy.asciidoc[]
609+
include::enrich/delete_policy.asciidoc[]

0 commit comments

Comments
 (0)