Skip to content

Commit ba8d4eb

Browse files
authored
HLRC: Add Lifecycle Policy delete to the HLRC (#33142)
Adds support for Lifecycle Policy deletion to the Java High-Level Rest Client. Relates to #33100
1 parent 454ce99 commit ba8d4eb

File tree

7 files changed

+250
-2
lines changed

7 files changed

+250
-2
lines changed

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

+30
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.elasticsearch.action.ActionListener;
2323
import org.elasticsearch.action.support.master.AcknowledgedResponse;
24+
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
2425
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest;
2526
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleResponse;
2627
import org.elasticsearch.protocol.xpack.indexlifecycle.SetIndexLifecyclePolicyRequest;
@@ -39,6 +40,35 @@ public class IndexLifecycleClient {
3940
this.restHighLevelClient = restHighLevelClient;
4041
}
4142

43+
/**
44+
* Delete a lifecycle definition
45+
* See <a href="https://fix-me-when-we-have-docs.com">
46+
* the docs</a> for more.
47+
* @param request the request
48+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
49+
* @return the response
50+
* @throws IOException in case there is a problem sending the request or parsing back the response
51+
*/
52+
public AcknowledgedResponse deleteLifecyclePolicy(DeleteLifecyclePolicyRequest request,
53+
RequestOptions options) throws IOException {
54+
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::deleteLifecyclePolicy, options,
55+
AcknowledgedResponse::fromXContent, emptySet());
56+
}
57+
58+
/**
59+
* Asynchronously delete a lifecycle definition
60+
* See <a href="https://fix-me-when-we-have-docs.com">
61+
* the docs</a> for more.
62+
* @param request the request
63+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
64+
* @param listener the listener to be notified upon request completion
65+
*/
66+
public void deleteLifecyclePolicyAsync(DeleteLifecyclePolicyRequest request, RequestOptions options,
67+
ActionListener<AcknowledgedResponse> listener) {
68+
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::deleteLifecyclePolicy, options,
69+
AcknowledgedResponse::fromXContent, listener, emptySet());
70+
}
71+
4272
/**
4373
* Set the index lifecycle policy for an index
4474
* See <a href="https://fix-me-when-we-have-docs.com">

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

+13
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
import org.elasticsearch.action.support.IndicesOptions;
8989
import org.elasticsearch.action.support.WriteRequest;
9090
import org.elasticsearch.action.update.UpdateRequest;
91+
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
9192
import org.elasticsearch.cluster.health.ClusterHealthStatus;
9293
import org.elasticsearch.common.Nullable;
9394
import org.elasticsearch.common.Priority;
@@ -1190,6 +1191,18 @@ static Request xpackUsage(XPackUsageRequest usageRequest) {
11901191
return request;
11911192
}
11921193

1194+
static Request deleteLifecyclePolicy(DeleteLifecyclePolicyRequest deleteLifecyclePolicyRequest) {
1195+
Request request = new Request(HttpDelete.METHOD_NAME,
1196+
new EndpointBuilder()
1197+
.addPathPartAsIs("_ilm")
1198+
.addPathPartAsIs(deleteLifecyclePolicyRequest.getLifecyclePolicy())
1199+
.build());
1200+
Params params = new Params(request);
1201+
params.withMasterTimeout(deleteLifecyclePolicyRequest.masterNodeTimeout());
1202+
params.withTimeout(deleteLifecyclePolicyRequest.timeout());
1203+
return request;
1204+
}
1205+
11931206
static Request setIndexLifecyclePolicy(SetIndexLifecyclePolicyRequest setPolicyRequest) {
11941207
String[] indices = setPolicyRequest.indices() == null ? Strings.EMPTY_ARRAY : setPolicyRequest.indices();
11951208
Request request = new Request(HttpPut.METHOD_NAME,

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@
2828
*/
2929
public class TimedRequest implements Validatable {
3030

31-
private TimeValue timeout;
32-
private TimeValue masterTimeout;
31+
public static final TimeValue DEFAULT_TIMEOUT = TimeValue.timeValueSeconds(30);
32+
public static final TimeValue DEFAULT_MASTER_TIMEOUT = TimeValue.timeValueSeconds(30);
33+
34+
private TimeValue timeout = DEFAULT_TIMEOUT;
35+
private TimeValue masterTimeout = DEFAULT_MASTER_TIMEOUT;
3336

3437
public void setTimeout(TimeValue timeout) {
3538
this.timeout = timeout;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
20+
package org.elasticsearch.client.indexlifecycle;
21+
22+
import org.elasticsearch.client.TimedRequest;
23+
import org.elasticsearch.common.Strings;
24+
25+
import java.util.Objects;
26+
27+
public class DeleteLifecyclePolicyRequest extends TimedRequest {
28+
29+
private final String lifecyclePolicy;
30+
31+
public DeleteLifecyclePolicyRequest(String lifecyclePolicy) {
32+
if (Strings.isNullOrEmpty(lifecyclePolicy)) {
33+
throw new IllegalArgumentException("lifecycle name must be present");
34+
}
35+
this.lifecyclePolicy = lifecyclePolicy;
36+
}
37+
38+
public String getLifecyclePolicy() {
39+
return lifecyclePolicy;
40+
}
41+
42+
43+
@Override
44+
public boolean equals(Object o) {
45+
if (this == o) {
46+
return true;
47+
}
48+
if (o == null || getClass() != o.getClass()) {
49+
return false;
50+
}
51+
DeleteLifecyclePolicyRequest that = (DeleteLifecyclePolicyRequest) o;
52+
return Objects.equals(getLifecyclePolicy(), that.getLifecyclePolicy());
53+
}
54+
55+
@Override
56+
public int hashCode() {
57+
return Objects.hash(getLifecyclePolicy());
58+
}
59+
}

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

+69
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
2727
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
2828
import org.elasticsearch.action.support.master.AcknowledgedResponse;
29+
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
2930
import org.elasticsearch.common.settings.Settings;
3031
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest;
3132
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleResponse;
@@ -36,8 +37,10 @@
3637
import org.elasticsearch.protocol.xpack.indexlifecycle.StopILMRequest;
3738
import org.hamcrest.Matchers;
3839

40+
import java.io.IOException;
3941
import java.util.Map;
4042

43+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
4144
import static org.hamcrest.Matchers.equalTo;
4245
import static org.hamcrest.Matchers.is;
4346

@@ -286,4 +289,70 @@ public void testExplainLifecycle() throws Exception {
286289
assertFalse(squashResponse.managedByILM());
287290
assertEquals("squash", squashResponse.getIndex());
288291
}
292+
293+
public void testDeleteLifecycle() throws IOException {
294+
String policy = randomAlphaOfLength(10);
295+
296+
// TODO: NORELEASE convert this to using the high level client once there are APIs for it
297+
String jsonString = "{\n" +
298+
" \"policy\": {\n" +
299+
" \"phases\": {\n" +
300+
" \"hot\": {\n" +
301+
" \"actions\": {\n" +
302+
" \"rollover\": {\n" +
303+
" \"max_age\": \"50d\"\n" +
304+
" } \n" +
305+
" }\n" +
306+
" },\n" +
307+
" \"warm\": {\n" +
308+
" \"after\": \"1000s\",\n" +
309+
" \"actions\": {\n" +
310+
" \"allocate\": {\n" +
311+
" \"require\": { \"_name\": \"node-1\" },\n" +
312+
" \"include\": {},\n" +
313+
" \"exclude\": {}\n" +
314+
" },\n" +
315+
" \"shrink\": {\n" +
316+
" \"number_of_shards\": 1\n" +
317+
" },\n" +
318+
" \"forcemerge\": {\n" +
319+
" \"max_num_segments\": 1000\n" +
320+
" }\n" +
321+
" }\n" +
322+
" },\n" +
323+
" \"cold\": {\n" +
324+
" \"after\": \"2000s\",\n" +
325+
" \"actions\": {\n" +
326+
" \"allocate\": {\n" +
327+
" \"number_of_replicas\": 0\n" +
328+
" }\n" +
329+
" }\n" +
330+
" },\n" +
331+
" \"delete\": {\n" +
332+
" \"after\": \"3000s\",\n" +
333+
" \"actions\": {\n" +
334+
" \"delete\": {}\n" +
335+
" }\n" +
336+
" }\n" +
337+
" }\n" +
338+
" }\n" +
339+
"}";
340+
HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);
341+
Request request = new Request("PUT", "/_ilm/" + policy);
342+
request.setEntity(entity);
343+
client().performRequest(request);
344+
345+
DeleteLifecyclePolicyRequest deleteRequest = new DeleteLifecyclePolicyRequest(policy);
346+
assertAcked(execute(deleteRequest, highLevelClient().indexLifecycle()::deleteLifecyclePolicy,
347+
highLevelClient().indexLifecycle()::deleteLifecyclePolicyAsync));
348+
349+
// TODO: NORELEASE convert this to using the high level client once there are APIs for it
350+
Request getLifecycle = new Request("GET", "/_ilm/" + policy);
351+
try {
352+
client().performRequest(getLifecycle);
353+
fail("index should not exist after being deleted");
354+
} catch (ResponseException ex) {
355+
assertEquals(404, ex.getResponse().getStatusLine().getStatusCode());
356+
}
357+
}
289358
}

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

+35
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
import org.elasticsearch.action.support.replication.ReplicationRequest;
9898
import org.elasticsearch.action.update.UpdateRequest;
9999
import org.elasticsearch.client.RequestConverters.EndpointBuilder;
100+
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
100101
import org.elasticsearch.cluster.health.ClusterHealthStatus;
101102
import org.elasticsearch.common.CheckedBiConsumer;
102103
import org.elasticsearch.common.CheckedFunction;
@@ -2698,6 +2699,19 @@ public void testGraphExplore() throws Exception {
26982699
assertToXContentBody(graphExploreRequest, request.getEntity());
26992700
}
27002701

2702+
public void testDeleteLifecycle() {
2703+
String lifecycleName = randomAlphaOfLengthBetween(2,20);
2704+
DeleteLifecyclePolicyRequest req = new DeleteLifecyclePolicyRequest(lifecycleName);
2705+
Map<String, String> expectedParams = new HashMap<>();
2706+
setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_TIMEOUT, expectedParams);
2707+
setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_MASTER_TIMEOUT, expectedParams);
2708+
2709+
Request request = RequestConverters.deleteLifecyclePolicy(req);
2710+
assertEquals(request.getMethod(), HttpDelete.METHOD_NAME);
2711+
assertEquals(request.getEndpoint(), "/_ilm/" + lifecycleName);
2712+
assertEquals(request.getParameters(), expectedParams);
2713+
}
2714+
27012715
public void testSetIndexLifecyclePolicy() throws Exception {
27022716
SetIndexLifecyclePolicyRequest req = new SetIndexLifecyclePolicyRequest();
27032717
String policyName = randomAlphaOfLength(10);
@@ -2892,6 +2906,17 @@ private static void setRandomTimeout(Consumer<String> setter, TimeValue defaultT
28922906
}
28932907
}
28942908

2909+
private static void setRandomTimeoutTimeValue(Consumer<TimeValue> setter, TimeValue defaultTimeout,
2910+
Map<String, String> expectedParams) {
2911+
if (randomBoolean()) {
2912+
TimeValue timeout = TimeValue.parseTimeValue(randomTimeValue(), "random_timeout");
2913+
setter.accept(timeout);
2914+
expectedParams.put("timeout", timeout.getStringRep());
2915+
} else {
2916+
expectedParams.put("timeout", defaultTimeout.getStringRep());
2917+
}
2918+
}
2919+
28952920
private static void setRandomMasterTimeout(MasterNodeRequest<?> request, Map<String, String> expectedParams) {
28962921
if (randomBoolean()) {
28972922
String masterTimeout = randomTimeValue();
@@ -2902,6 +2927,16 @@ private static void setRandomMasterTimeout(MasterNodeRequest<?> request, Map<Str
29022927
}
29032928
}
29042929

2930+
private static void setRandomMasterTimeout(Consumer<TimeValue> setter, TimeValue defaultTimeout, Map<String, String> expectedParams) {
2931+
if (randomBoolean()) {
2932+
TimeValue masterTimeout = TimeValue.parseTimeValue(randomTimeValue(), "random_master_timeout");
2933+
setter.accept(masterTimeout);
2934+
expectedParams.put("master_timeout", masterTimeout.getStringRep());
2935+
} else {
2936+
expectedParams.put("master_timeout", defaultTimeout.getStringRep());
2937+
}
2938+
}
2939+
29052940
private static void setRandomWaitForActiveShards(Consumer<ActiveShardCount> setter, Map<String, String> expectedParams) {
29062941
setRandomWaitForActiveShards(setter, ActiveShardCount.DEFAULT, expectedParams);
29072942
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
20+
package org.elasticsearch.client.indexlifecycle;
21+
22+
import org.elasticsearch.test.ESTestCase;
23+
24+
public class DeleteLifecyclePolicyRequestTests extends ESTestCase {
25+
26+
private DeleteLifecyclePolicyRequest createTestInstance() {
27+
return new DeleteLifecyclePolicyRequest(randomAlphaOfLengthBetween(2, 20));
28+
}
29+
30+
public void testValidate() {
31+
DeleteLifecyclePolicyRequest req = createTestInstance();
32+
assertFalse(req.validate().isPresent());
33+
34+
}
35+
36+
public void testValidationFailure() {
37+
expectThrows(IllegalArgumentException.class, () -> new DeleteLifecyclePolicyRequest(randomFrom("", null)));
38+
}
39+
}

0 commit comments

Comments
 (0)