Skip to content

Commit f568bd5

Browse files
committed
HLRC: Add Put Lifecycle Policy API to HLRC (#33250)
* Move Lifecycle objects from protcol to core * HLRC: Add Put Lifecycle API to the HLRC * Use HLRC Put for ILM integration tests
1 parent d34dcb2 commit f568bd5

25 files changed

+320
-276
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.action.ActionListener;
2323
import org.elasticsearch.action.support.master.AcknowledgedResponse;
2424
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
25+
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
2526
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest;
2627
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleResponse;
2728
import org.elasticsearch.protocol.xpack.indexlifecycle.SetIndexLifecyclePolicyRequest;
@@ -40,6 +41,35 @@ public class IndexLifecycleClient {
4041
this.restHighLevelClient = restHighLevelClient;
4142
}
4243

44+
/**
45+
* Create or modify a lifecycle definition
46+
* See <a href="https://fix-me-when-we-have-docs.com">
47+
* the docs</a> for more.
48+
* @param request the request
49+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
50+
* @return the response
51+
* @throws IOException in case there is a problem sending the request or parsing back the response
52+
*/
53+
public AcknowledgedResponse putLifecyclePolicy(PutLifecyclePolicyRequest request,
54+
RequestOptions options) throws IOException {
55+
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::putLifecyclePolicy, options,
56+
AcknowledgedResponse::fromXContent, emptySet());
57+
}
58+
59+
/**
60+
* Asynchronously create or modify a lifecycle definition
61+
* See <a href="https://fix-me-when-we-have-docs.com">
62+
* the docs</a> for more.
63+
* @param request the request
64+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
65+
* @param listener the listener to be notified upon request completion
66+
*/
67+
public void putLifecyclePolicyAsync(PutLifecyclePolicyRequest request, RequestOptions options,
68+
ActionListener<AcknowledgedResponse> listener) {
69+
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::putLifecyclePolicy, options,
70+
AcknowledgedResponse::fromXContent, listener, emptySet());
71+
}
72+
4373
/**
4474
* Delete a lifecycle definition
4575
* 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: 14 additions & 0 deletions
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.PutLifecyclePolicyRequest;
9192
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
9293
import org.elasticsearch.cluster.health.ClusterHealthStatus;
9394
import org.elasticsearch.common.Nullable;
@@ -1186,6 +1187,19 @@ static Request xpackUsage(XPackUsageRequest usageRequest) {
11861187
return request;
11871188
}
11881189

1190+
static Request putLifecyclePolicy(PutLifecyclePolicyRequest putLifecycleRequest) throws IOException {
1191+
String endpoint = new EndpointBuilder()
1192+
.addPathPartAsIs("_ilm")
1193+
.addPathPartAsIs(putLifecycleRequest.getName())
1194+
.build();
1195+
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
1196+
Params params = new Params(request);
1197+
params.withMasterTimeout(putLifecycleRequest.masterNodeTimeout());
1198+
params.withTimeout(putLifecycleRequest.timeout());
1199+
request.setEntity(createEntity(putLifecycleRequest, REQUEST_BODY_CONTENT_TYPE));
1200+
return request;
1201+
}
1202+
11891203
static Request deleteLifecyclePolicy(DeleteLifecyclePolicyRequest deleteLifecyclePolicyRequest) {
11901204
Request request = new Request(HttpDelete.METHOD_NAME,
11911205
new EndpointBuilder()

x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/AllocateAction.java renamed to client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/AllocateAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.elasticsearch.protocol.xpack.indexlifecycle;
19+
package org.elasticsearch.client.indexlifecycle;
2020

2121
import org.elasticsearch.common.ParseField;
2222
import org.elasticsearch.common.Strings;

x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/DeleteAction.java renamed to client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/DeleteAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.elasticsearch.protocol.xpack.indexlifecycle;
19+
package org.elasticsearch.client.indexlifecycle;
2020

2121
import org.elasticsearch.common.Strings;
2222
import org.elasticsearch.common.xcontent.ObjectParser;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.elasticsearch.protocol.xpack.indexlifecycle;
19+
package org.elasticsearch.client.indexlifecycle;
2020

2121
import org.elasticsearch.common.ParseField;
2222
import org.elasticsearch.common.Strings;

x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/LifecycleAction.java renamed to client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecycleAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.elasticsearch.protocol.xpack.indexlifecycle;
19+
package org.elasticsearch.client.indexlifecycle;
2020

2121
/**
2222
* interface for index lifecycle management actions

x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/LifecyclePolicy.java renamed to client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecyclePolicy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.elasticsearch.protocol.xpack.indexlifecycle;
19+
package org.elasticsearch.client.indexlifecycle;
2020

2121
import org.elasticsearch.common.ParseField;
2222
import org.elasticsearch.common.Strings;

x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/OperationMode.java renamed to client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/OperationMode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.elasticsearch.protocol.xpack.indexlifecycle;
19+
package org.elasticsearch.client.indexlifecycle;
2020

2121
import org.elasticsearch.action.admin.indices.shrink.ShrinkAction;
2222

x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/Phase.java renamed to client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/Phase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.elasticsearch.protocol.xpack.indexlifecycle;
19+
package org.elasticsearch.client.indexlifecycle;
2020

2121
import org.elasticsearch.common.ParseField;
2222
import org.elasticsearch.common.Strings;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
import org.elasticsearch.common.xcontent.ToXContentObject;
25+
import org.elasticsearch.common.xcontent.XContentBuilder;
26+
27+
import java.io.IOException;
28+
import java.util.Objects;
29+
30+
public class PutLifecyclePolicyRequest extends TimedRequest implements ToXContentObject {
31+
32+
private final LifecyclePolicy policy;
33+
34+
public PutLifecyclePolicyRequest(LifecyclePolicy policy) {
35+
if (policy == null) {
36+
throw new IllegalArgumentException("policy definition cannot be null");
37+
}
38+
if (Strings.isNullOrEmpty(policy.getName())) {
39+
throw new IllegalArgumentException("policy name must be present");
40+
}
41+
this.policy = policy;
42+
}
43+
44+
public String getName() {
45+
return policy.getName();
46+
}
47+
48+
public LifecyclePolicy getLifecyclePolicy() {
49+
return policy;
50+
}
51+
52+
@Override
53+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
54+
builder.startObject();
55+
builder.field("policy", policy);
56+
builder.endObject();
57+
return builder;
58+
}
59+
60+
@Override
61+
public boolean equals(Object o) {
62+
if (this == o) return true;
63+
if (o == null || getClass() != o.getClass()) return false;
64+
PutLifecyclePolicyRequest that = (PutLifecyclePolicyRequest) o;
65+
return Objects.equals(getLifecyclePolicy(), that.getLifecyclePolicy());
66+
}
67+
68+
@Override
69+
public int hashCode() {
70+
return Objects.hash(getLifecyclePolicy());
71+
}
72+
}

x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ReadOnlyAction.java renamed to client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/ReadOnlyAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.elasticsearch.protocol.xpack.indexlifecycle;
19+
package org.elasticsearch.client.indexlifecycle;
2020

2121
import org.elasticsearch.common.Strings;
2222
import org.elasticsearch.common.xcontent.ObjectParser;

x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/RolloverAction.java renamed to client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/RolloverAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.elasticsearch.protocol.xpack.indexlifecycle;
19+
package org.elasticsearch.client.indexlifecycle;
2020

2121
import org.elasticsearch.common.ParseField;
2222
import org.elasticsearch.common.Strings;

x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ShrinkAction.java renamed to client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/ShrinkAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.elasticsearch.protocol.xpack.indexlifecycle;
19+
package org.elasticsearch.client.indexlifecycle;
2020

2121
import org.elasticsearch.common.ParseField;
2222
import org.elasticsearch.common.Strings;

0 commit comments

Comments
 (0)