Skip to content

Commit 008e64b

Browse files
authored
HLRC: Add Get Lifecycle Policy API to HLRC (#33323)
Adds Request and Reponse classes for accessing lifecycle policies. Changes existing tests to use these classes where appropriate. Sets up SPI configuration to allow parsing *Actions from XContent.
1 parent 8fa8dea commit 008e64b

14 files changed

+625
-36
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
@@ -22,6 +22,8 @@
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.GetLifecyclePolicyRequest;
26+
import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyResponse;
2527
import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest;
2628
import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusResponse;
2729
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
@@ -43,6 +45,35 @@ public class IndexLifecycleClient {
4345
this.restHighLevelClient = restHighLevelClient;
4446
}
4547

48+
/**
49+
* Retrieve one or more lifecycle policy definition
50+
* See <a href="https://fix-me-when-we-have-docs.com">
51+
* the docs</a> for more.
52+
* @param request the request
53+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
54+
* @return the response
55+
* @throws IOException in case there is a problem sending the request or parsing back the response
56+
*/
57+
public GetLifecyclePolicyResponse getLifecyclePolicy(GetLifecyclePolicyRequest request,
58+
RequestOptions options) throws IOException {
59+
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::getLifecyclePolicy, options,
60+
GetLifecyclePolicyResponse::fromXContent, emptySet());
61+
}
62+
63+
/**
64+
* Asynchronously retrieve one or more lifecycle policy definition
65+
* See <a href="https://fix-me-when-we-have-docs.com">
66+
* the docs</a> for more.
67+
* @param request the request
68+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
69+
* @param listener the listener to be notified upon request completion
70+
*/
71+
public void getLifecyclePolicyAsync(GetLifecyclePolicyRequest request, RequestOptions options,
72+
ActionListener<GetLifecyclePolicyResponse> listener) {
73+
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::getLifecyclePolicy, options,
74+
GetLifecyclePolicyResponse::fromXContent, listener, emptySet());
75+
}
76+
4677
/**
4778
* Create or modify a lifecycle definition
4879
* 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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
import org.elasticsearch.action.support.IndicesOptions;
8585
import org.elasticsearch.action.support.WriteRequest;
8686
import org.elasticsearch.action.update.UpdateRequest;
87+
import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyRequest;
8788
import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest;
8889
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
8990
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
@@ -1164,6 +1165,18 @@ static Request xpackUsage(XPackUsageRequest usageRequest) {
11641165
return request;
11651166
}
11661167

1168+
static Request getLifecyclePolicy(GetLifecyclePolicyRequest getLifecyclePolicyRequest) {
1169+
String endpoint = new EndpointBuilder()
1170+
.addPathPartAsIs("_ilm")
1171+
.addCommaSeparatedPathParts(getLifecyclePolicyRequest.getPolicyNames())
1172+
.build();
1173+
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
1174+
Params params = new Params(request);
1175+
params.withMasterTimeout(getLifecyclePolicyRequest.masterNodeTimeout());
1176+
params.withTimeout(getLifecyclePolicyRequest.timeout());
1177+
return request;
1178+
}
1179+
11671180
static Request putLifecyclePolicy(PutLifecyclePolicyRequest putLifecycleRequest) throws IOException {
11681181
String endpoint = new EndpointBuilder()
11691182
.addPathPartAsIs("_ilm")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
21+
package org.elasticsearch.client.indexlifecycle;
22+
23+
import org.elasticsearch.client.TimedRequest;
24+
import org.elasticsearch.common.Strings;
25+
26+
import java.util.Arrays;
27+
28+
public class GetLifecyclePolicyRequest extends TimedRequest {
29+
30+
private final String[] policyNames;
31+
32+
public GetLifecyclePolicyRequest(String... policyNames) {
33+
if (policyNames == null) {
34+
this.policyNames = Strings.EMPTY_ARRAY;
35+
} else {
36+
for (String name : policyNames) {
37+
if (name == null) {
38+
throw new IllegalArgumentException("cannot include null policy name");
39+
}
40+
}
41+
this.policyNames = policyNames;
42+
}
43+
}
44+
45+
public String[] getPolicyNames() {
46+
return policyNames;
47+
}
48+
49+
@Override
50+
public boolean equals(Object o) {
51+
if (this == o) return true;
52+
if (o == null || getClass() != o.getClass()) return false;
53+
GetLifecyclePolicyRequest request = (GetLifecyclePolicyRequest) o;
54+
return Arrays.equals(getPolicyNames(), request.getPolicyNames());
55+
}
56+
57+
@Override
58+
public int hashCode() {
59+
return Arrays.hashCode(getPolicyNames());
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
21+
package org.elasticsearch.client.indexlifecycle;
22+
23+
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
24+
import org.elasticsearch.common.collect.ImmutableOpenMap;
25+
import org.elasticsearch.common.xcontent.ToXContent;
26+
import org.elasticsearch.common.xcontent.ToXContentObject;
27+
import org.elasticsearch.common.xcontent.XContentBuilder;
28+
import org.elasticsearch.common.xcontent.XContentParser;
29+
30+
import java.io.IOException;
31+
import java.util.Objects;
32+
33+
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
34+
35+
public class GetLifecyclePolicyResponse implements ToXContentObject {
36+
37+
private final ImmutableOpenMap<String, LifecyclePolicyMetadata> policies;
38+
39+
public GetLifecyclePolicyResponse(ImmutableOpenMap<String, LifecyclePolicyMetadata> policies) {
40+
this.policies = policies;
41+
}
42+
43+
public ImmutableOpenMap<String, LifecyclePolicyMetadata> getPolicies() {
44+
return policies;
45+
}
46+
47+
@Override
48+
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
49+
builder.startObject();
50+
for (ObjectObjectCursor<String, LifecyclePolicyMetadata> stringLifecyclePolicyObjectObjectCursor : policies) {
51+
builder.field(stringLifecyclePolicyObjectObjectCursor.key, stringLifecyclePolicyObjectObjectCursor.value);
52+
}
53+
builder.endObject();
54+
return builder;
55+
}
56+
57+
public static GetLifecyclePolicyResponse fromXContent(XContentParser parser) throws IOException {
58+
ImmutableOpenMap.Builder<String, LifecyclePolicyMetadata> policies = ImmutableOpenMap.builder();
59+
60+
if (parser.currentToken() == null) {
61+
parser.nextToken();
62+
}
63+
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
64+
parser.nextToken();
65+
66+
while (!parser.isClosed()) {
67+
if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
68+
String policyName = parser.currentName();
69+
LifecyclePolicyMetadata policyDefinion = LifecyclePolicyMetadata.parse(parser, policyName);
70+
policies.put(policyName, policyDefinion);
71+
} else {
72+
parser.nextToken();
73+
}
74+
}
75+
76+
return new GetLifecyclePolicyResponse(policies.build());
77+
}
78+
79+
@Override
80+
public boolean equals(Object o) {
81+
if (this == o) return true;
82+
if (o == null || getClass() != o.getClass()) return false;
83+
GetLifecyclePolicyResponse that = (GetLifecyclePolicyResponse) o;
84+
return Objects.equals(getPolicies(), that.getPolicies());
85+
}
86+
87+
@Override
88+
public int hashCode() {
89+
return Objects.hash(getPolicies());
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.NamedXContentRegistry;
24+
import org.elasticsearch.plugins.spi.NamedXContentProvider;
25+
26+
import java.util.Arrays;
27+
import java.util.List;
28+
29+
public class IndexLifecycleNamedXContentProvider implements NamedXContentProvider {
30+
31+
32+
@Override
33+
public List<NamedXContentRegistry.Entry> getNamedXContentParsers() {
34+
return Arrays.asList(
35+
// ILM
36+
new NamedXContentRegistry.Entry(LifecycleAction.class,
37+
new ParseField(AllocateAction.NAME),
38+
AllocateAction::parse),
39+
new NamedXContentRegistry.Entry(LifecycleAction.class,
40+
new ParseField(DeleteAction.NAME),
41+
DeleteAction::parse),
42+
new NamedXContentRegistry.Entry(LifecycleAction.class,
43+
new ParseField(ForceMergeAction.NAME),
44+
ForceMergeAction::parse),
45+
new NamedXContentRegistry.Entry(LifecycleAction.class,
46+
new ParseField(ReadOnlyAction.NAME),
47+
ReadOnlyAction::parse),
48+
new NamedXContentRegistry.Entry(LifecycleAction.class,
49+
new ParseField(RolloverAction.NAME),
50+
RolloverAction::parse),
51+
new NamedXContentRegistry.Entry(LifecycleAction.class,
52+
new ParseField(ShrinkAction.NAME),
53+
ShrinkAction::parse)
54+
);
55+
}
56+
}

0 commit comments

Comments
 (0)