Skip to content

Commit 2be39f4

Browse files
committed
HLRC: Add remove index lifecycle policy (#34204)
This change adds the command RemoveIndexLifecyclePolicy to the HLRC. This uses the new TimeRequest as a base class for RemoveIndexLifecyclePolicyRequest on the client side.
1 parent 8a3bae8 commit 2be39f4

File tree

17 files changed

+483
-38
lines changed

17 files changed

+483
-38
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
3030
import org.elasticsearch.client.indexlifecycle.ExplainLifecycleRequest;
3131
import org.elasticsearch.client.indexlifecycle.ExplainLifecycleResponse;
32+
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest;
33+
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyResponse;
3234
import org.elasticsearch.client.indexlifecycle.SetIndexLifecyclePolicyRequest;
3335
import org.elasticsearch.client.indexlifecycle.SetIndexLifecyclePolicyResponse;
3436
import org.elasticsearch.client.indexlifecycle.StartILMRequest;
@@ -161,6 +163,35 @@ public void setIndexLifecyclePolicyAsync(SetIndexLifecyclePolicyRequest request,
161163
SetIndexLifecyclePolicyResponse::fromXContent, listener, emptySet());
162164
}
163165

166+
/**
167+
* Remove the index lifecycle policy for an index
168+
* See <a href="https://fix-me-when-we-have-docs.com">
169+
* the docs</a> for more.
170+
* @param request the request
171+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
172+
* @return the response
173+
* @throws IOException in case there is a problem sending the request or parsing back the response
174+
*/
175+
public RemoveIndexLifecyclePolicyResponse removeIndexLifecyclePolicy(RemoveIndexLifecyclePolicyRequest request,
176+
RequestOptions options) throws IOException {
177+
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::removeIndexLifecyclePolicy, options,
178+
RemoveIndexLifecyclePolicyResponse::fromXContent, emptySet());
179+
}
180+
181+
/**
182+
* Asynchronously remove the index lifecycle policy for an index
183+
* See <a href="https://fix-me-when-we-have-docs.com">
184+
* the docs</a> for more.
185+
* @param request the request
186+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
187+
* @param listener the listener to be notified upon request completion
188+
*/
189+
public void removeIndexLifecyclePolicyAsync(RemoveIndexLifecyclePolicyRequest request, RequestOptions options,
190+
ActionListener<RemoveIndexLifecyclePolicyResponse> listener) {
191+
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::removeIndexLifecyclePolicy, options,
192+
RemoveIndexLifecyclePolicyResponse::fromXContent, listener, emptySet());
193+
}
194+
164195
/**
165196
* Start the Index Lifecycle Management feature.
166197
* See <a href="https://fix-me-when-we-have-docs.com">

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyRequest;
5656
import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest;
5757
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
58+
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest;
5859
import org.elasticsearch.client.indexlifecycle.SetIndexLifecyclePolicyRequest;
5960
import org.elasticsearch.client.indexlifecycle.StartILMRequest;
6061
import org.elasticsearch.client.indexlifecycle.StopILMRequest;
@@ -699,6 +700,20 @@ static Request setIndexLifecyclePolicy(SetIndexLifecyclePolicyRequest setPolicyR
699700
return request;
700701
}
701702

703+
static Request removeIndexLifecyclePolicy(RemoveIndexLifecyclePolicyRequest removePolicyRequest) {
704+
String[] indices = removePolicyRequest.indices() == null ?
705+
Strings.EMPTY_ARRAY : removePolicyRequest.indices().toArray(new String[] {});
706+
Request request = new Request(HttpDelete.METHOD_NAME,
707+
new EndpointBuilder()
708+
.addCommaSeparatedPathParts(indices)
709+
.addPathPartAsIs("_ilm")
710+
.build());
711+
Params params = new Params(request);
712+
params.withIndicesOptions(removePolicyRequest.indicesOptions());
713+
params.withMasterTimeout(removePolicyRequest.masterNodeTimeout());
714+
return request;
715+
}
716+
702717
static Request startILM(StartILMRequest startILMRequest) {
703718
Request request = new Request(HttpPost.METHOD_NAME,
704719
new EndpointBuilder()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.action.support.IndicesOptions;
23+
import org.elasticsearch.client.TimedRequest;
24+
25+
import java.util.Collections;
26+
import java.util.List;
27+
import java.util.Objects;
28+
29+
public class RemoveIndexLifecyclePolicyRequest extends TimedRequest {
30+
31+
private final List<String> indices;
32+
private final IndicesOptions indicesOptions;
33+
34+
public RemoveIndexLifecyclePolicyRequest(List<String> indices) {
35+
this(indices, IndicesOptions.strictExpandOpen());
36+
}
37+
38+
public RemoveIndexLifecyclePolicyRequest(List<String> indices, IndicesOptions indicesOptions) {
39+
this.indices = Collections.unmodifiableList(Objects.requireNonNull(indices));
40+
this.indicesOptions = Objects.requireNonNull(indicesOptions);
41+
}
42+
43+
public List<String> indices() {
44+
return indices;
45+
}
46+
47+
public IndicesOptions indicesOptions() {
48+
return indicesOptions;
49+
}
50+
51+
@Override
52+
public int hashCode() {
53+
return Objects.hash(indices, indicesOptions);
54+
}
55+
56+
@Override
57+
public boolean equals(Object obj) {
58+
if (obj == null) {
59+
return false;
60+
}
61+
if (getClass() != obj.getClass()) {
62+
return false;
63+
}
64+
RemoveIndexLifecyclePolicyRequest other = (RemoveIndexLifecyclePolicyRequest) obj;
65+
return Objects.deepEquals(indices, other.indices) &&
66+
Objects.equals(indicesOptions, other.indicesOptions);
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.common.ParseField;
23+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
24+
import org.elasticsearch.common.xcontent.XContentParser;
25+
26+
import java.util.Collections;
27+
import java.util.List;
28+
import java.util.Objects;
29+
30+
public class RemoveIndexLifecyclePolicyResponse {
31+
32+
public static final ParseField HAS_FAILURES_FIELD = new ParseField("has_failures");
33+
public static final ParseField FAILED_INDEXES_FIELD = new ParseField("failed_indexes");
34+
@SuppressWarnings("unchecked")
35+
public static final ConstructingObjectParser<RemoveIndexLifecyclePolicyResponse, Void> PARSER = new ConstructingObjectParser<>(
36+
"change_policy_for_index_response", true, args -> new RemoveIndexLifecyclePolicyResponse((List<String>)args[0]));
37+
static {
38+
PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), FAILED_INDEXES_FIELD);
39+
// Needs to be declared but not used in constructing the response object
40+
PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), HAS_FAILURES_FIELD);
41+
}
42+
43+
private final List<String> failedIndexes;
44+
45+
public RemoveIndexLifecyclePolicyResponse(List<String> failedIndexes) {
46+
if (failedIndexes == null) {
47+
throw new IllegalArgumentException(FAILED_INDEXES_FIELD.getPreferredName() + " cannot be null");
48+
}
49+
this.failedIndexes = Collections.unmodifiableList(failedIndexes);
50+
}
51+
52+
public List<String> getFailedIndexes() {
53+
return failedIndexes;
54+
}
55+
56+
public boolean hasFailures() {
57+
return failedIndexes.isEmpty() == false;
58+
}
59+
60+
public static RemoveIndexLifecyclePolicyResponse fromXContent(XContentParser parser) {
61+
return PARSER.apply(parser, null);
62+
}
63+
64+
@Override
65+
public int hashCode() {
66+
return Objects.hash(failedIndexes);
67+
}
68+
69+
@Override
70+
public boolean equals(Object obj) {
71+
if (obj == null) {
72+
return false;
73+
}
74+
if (getClass() != obj.getClass()) {
75+
return false;
76+
}
77+
RemoveIndexLifecyclePolicyResponse other = (RemoveIndexLifecyclePolicyResponse) obj;
78+
return Objects.equals(failedIndexes, other.failedIndexes);
79+
}
80+
}

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import org.elasticsearch.client.indexlifecycle.Phase;
4242
import org.elasticsearch.client.indexlifecycle.PhaseExecutionInfo;
4343
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
44+
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest;
45+
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyResponse;
4446
import org.elasticsearch.client.indexlifecycle.RolloverAction;
4547
import org.elasticsearch.client.indexlifecycle.SetIndexLifecyclePolicyRequest;
4648
import org.elasticsearch.client.indexlifecycle.SetIndexLifecyclePolicyResponse;
@@ -52,6 +54,7 @@
5254
import org.hamcrest.Matchers;
5355

5456
import java.io.IOException;
57+
import java.util.ArrayList;
5558
import java.util.Arrays;
5659
import java.util.Collections;
5760
import java.util.HashMap;
@@ -88,6 +91,44 @@ public void testSetIndexLifecyclePolicy() throws Exception {
8891
assertThat(settingsResponse.getSetting("baz", "index.lifecycle.name"), equalTo(policyName));
8992
}
9093

94+
public void testRemoveIndexLifecyclePolicy() throws Exception {
95+
String policyName = randomAlphaOfLength(10);
96+
LifecyclePolicy policy = createRandomPolicy(policyName);
97+
PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy);
98+
assertAcked(execute(putRequest, highLevelClient().indexLifecycle()::putLifecyclePolicy,
99+
highLevelClient().indexLifecycle()::putLifecyclePolicyAsync));
100+
101+
createIndex("foo", Settings.builder().put("index.lifecycle.name", "bar").build());
102+
createIndex("baz", Settings.builder().put("index.lifecycle.name", "eggplant").build());
103+
createIndex("rbh", Settings.builder().put("index.lifecycle.name", "whatisthis").build());
104+
SetIndexLifecyclePolicyRequest setReq = new SetIndexLifecyclePolicyRequest(policyName, "foo", "baz", "rbh");
105+
SetIndexLifecyclePolicyResponse setResp = execute(setReq, highLevelClient().indexLifecycle()::setIndexLifecyclePolicy,
106+
highLevelClient().indexLifecycle()::setIndexLifecyclePolicyAsync);
107+
assertThat(setResp.hasFailures(), is(false));
108+
assertThat(setResp.getFailedIndexes().isEmpty(), is(true));
109+
110+
GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices("foo", "baz", "rbh");
111+
GetSettingsResponse settingsResponse = highLevelClient().indices().getSettings(getSettingsRequest, RequestOptions.DEFAULT);
112+
assertThat(settingsResponse.getSetting("foo", "index.lifecycle.name"), equalTo(policyName));
113+
assertThat(settingsResponse.getSetting("baz", "index.lifecycle.name"), equalTo(policyName));
114+
assertThat(settingsResponse.getSetting("rbh", "index.lifecycle.name"), equalTo(policyName));
115+
116+
List<String> indices = new ArrayList<>();
117+
indices.add("foo");
118+
indices.add("rbh");
119+
RemoveIndexLifecyclePolicyRequest removeReq = new RemoveIndexLifecyclePolicyRequest(indices);
120+
RemoveIndexLifecyclePolicyResponse removeResp = execute(removeReq, highLevelClient().indexLifecycle()::removeIndexLifecyclePolicy,
121+
highLevelClient().indexLifecycle()::removeIndexLifecyclePolicyAsync);
122+
assertThat(removeResp.hasFailures(), is(false));
123+
assertThat(removeResp.getFailedIndexes().isEmpty(), is(true));
124+
125+
getSettingsRequest = new GetSettingsRequest().indices("foo", "baz", "rbh");
126+
settingsResponse = highLevelClient().indices().getSettings(getSettingsRequest, RequestOptions.DEFAULT);
127+
assertNull(settingsResponse.getSetting("foo", "index.lifecycle.name"));
128+
assertThat(settingsResponse.getSetting("baz", "index.lifecycle.name"), equalTo(policyName));
129+
assertNull(settingsResponse.getSetting("rbh", "index.lifecycle.name"));
130+
}
131+
91132
public void testStartStopILM() throws Exception {
92133
String policyName = randomAlphaOfLength(10);
93134
LifecyclePolicy policy = createRandomPolicy(policyName);

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.elasticsearch.client.indexlifecycle.LifecyclePolicy;
6161
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
6262
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
63+
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest;
6364
import org.elasticsearch.common.CheckedBiConsumer;
6465
import org.elasticsearch.common.Strings;
6566
import org.elasticsearch.common.bytes.BytesArray;
@@ -1602,6 +1603,20 @@ public void testSetIndexLifecyclePolicy() throws Exception {
16021603
assertThat(request.getParameters(), equalTo(expectedParams));
16031604
}
16041605

1606+
public void testRemoveIndexLifecyclePolicy() {
1607+
Map<String, String> expectedParams = new HashMap<>();
1608+
String[] indices = randomIndicesNames(0, 10);
1609+
IndicesOptions indicesOptions = setRandomIndicesOptions(IndicesOptions.strictExpandOpen(), expectedParams);
1610+
RemoveIndexLifecyclePolicyRequest req = new RemoveIndexLifecyclePolicyRequest(Arrays.asList(indices), indicesOptions);
1611+
setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_TIMEOUT, expectedParams);
1612+
1613+
Request request = RequestConverters.removeIndexLifecyclePolicy(req);
1614+
assertThat(request.getMethod(), equalTo(HttpDelete.METHOD_NAME));
1615+
String idxString = Strings.arrayToCommaDelimitedString(indices);
1616+
assertThat(request.getEndpoint(), equalTo("/" + (idxString.isEmpty() ? "" : (idxString + "/")) + "_ilm"));
1617+
assertThat(request.getParameters(), equalTo(expectedParams));
1618+
}
1619+
16051620
public void testStartILM() throws Exception {
16061621
StartILMRequest req = new StartILMRequest();
16071622
Map<String, String> expectedParams = new HashMap<>();
@@ -1734,6 +1749,24 @@ static void setRandomIndicesOptions(Consumer<IndicesOptions> setter, Supplier<In
17341749
}
17351750
}
17361751

1752+
static IndicesOptions setRandomIndicesOptions(IndicesOptions indicesOptions, Map<String, String> expectedParams) {
1753+
if (randomBoolean()) {
1754+
indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
1755+
}
1756+
expectedParams.put("ignore_unavailable", Boolean.toString(indicesOptions.ignoreUnavailable()));
1757+
expectedParams.put("allow_no_indices", Boolean.toString(indicesOptions.allowNoIndices()));
1758+
if (indicesOptions.expandWildcardsOpen() && indicesOptions.expandWildcardsClosed()) {
1759+
expectedParams.put("expand_wildcards", "open,closed");
1760+
} else if (indicesOptions.expandWildcardsOpen()) {
1761+
expectedParams.put("expand_wildcards", "open");
1762+
} else if (indicesOptions.expandWildcardsClosed()) {
1763+
expectedParams.put("expand_wildcards", "closed");
1764+
} else {
1765+
expectedParams.put("expand_wildcards", "none");
1766+
}
1767+
return indicesOptions;
1768+
}
1769+
17371770
static void setRandomIncludeDefaults(GetIndexRequest request, Map<String, String> expectedParams) {
17381771
if (randomBoolean()) {
17391772
boolean includeDefaults = randomBoolean();

0 commit comments

Comments
 (0)