Skip to content

Commit bb51cdb

Browse files
authored
Add HLRC docs for Get Lifecycle Policy (#35612)
Adds docs for the Get Lifecycle Policy API to the HLRC documentation.
1 parent cce9648 commit bb51cdb

File tree

4 files changed

+154
-4
lines changed

4 files changed

+154
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public class IndexLifecycleClient {
4747
}
4848

4949
/**
50-
* Retrieve one or more lifecycle policy definition
51-
* See <a href="https://fix-me-when-we-have-docs.com">
50+
* Retrieve one or more lifecycle policy definition. See
51+
* <a href="https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-ilm-ilm-get-lifecycle-policy.html">
5252
* the docs</a> for more.
5353
* @param request the request
5454
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
@@ -62,8 +62,8 @@ public GetLifecyclePolicyResponse getLifecyclePolicy(GetLifecyclePolicyRequest r
6262
}
6363

6464
/**
65-
* Asynchronously retrieve one or more lifecycle policy definition
66-
* See <a href="https://fix-me-when-we-have-docs.com">
65+
* Asynchronously retrieve one or more lifecycle policy definition. See
66+
* <a href="https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-ilm-ilm-get-lifecycle-policy.html">
6767
* the docs</a> for more.
6868
* @param request the request
6969
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@
2929
import org.elasticsearch.client.core.AcknowledgedResponse;
3030
import org.elasticsearch.client.indexlifecycle.DeleteAction;
3131
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
32+
import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyRequest;
33+
import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyResponse;
3234
import org.elasticsearch.client.indexlifecycle.LifecycleAction;
3335
import org.elasticsearch.client.indexlifecycle.LifecyclePolicy;
36+
import org.elasticsearch.client.indexlifecycle.LifecyclePolicyMetadata;
3437
import org.elasticsearch.client.indexlifecycle.Phase;
3538
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
3639
import org.elasticsearch.client.indexlifecycle.RolloverAction;
40+
import org.elasticsearch.client.indexlifecycle.ShrinkAction;
41+
import org.elasticsearch.common.collect.ImmutableOpenMap;
3742
import org.elasticsearch.common.unit.ByteSizeUnit;
3843
import org.elasticsearch.common.unit.ByteSizeValue;
3944
import org.elasticsearch.common.unit.TimeValue;
@@ -119,6 +124,108 @@ public void onFailure(Exception e) {
119124

120125
}
121126

127+
public void testGetLifecyclePolicy() throws IOException, InterruptedException {
128+
RestHighLevelClient client = highLevelClient();
129+
130+
LifecyclePolicy myPolicyAsPut;
131+
LifecyclePolicy otherPolicyAsPut;
132+
// Set up some policies so we have something to get
133+
{
134+
Map<String, Phase> phases = new HashMap<>();
135+
Map<String, LifecycleAction> hotActions = new HashMap<>();
136+
hotActions.put(RolloverAction.NAME, new RolloverAction(
137+
new ByteSizeValue(50, ByteSizeUnit.GB), null, null));
138+
phases.put("hot", new Phase("hot", TimeValue.ZERO, hotActions));
139+
140+
Map<String, LifecycleAction> deleteActions =
141+
Collections.singletonMap(DeleteAction.NAME,
142+
new DeleteAction());
143+
phases.put("delete",
144+
new Phase("delete",
145+
new TimeValue(90, TimeUnit.DAYS), deleteActions));
146+
147+
myPolicyAsPut = new LifecyclePolicy("my_policy", phases);
148+
PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(myPolicyAsPut);
149+
150+
Map<String, Phase> otherPolicyPhases = new HashMap<>(phases);
151+
Map<String, LifecycleAction> warmActions = Collections.singletonMap(ShrinkAction.NAME, new ShrinkAction(1));
152+
otherPolicyPhases.put("warm", new Phase("warm", new TimeValue(30, TimeUnit.DAYS), warmActions));
153+
otherPolicyAsPut = new LifecyclePolicy("other_policy", otherPolicyPhases);
154+
155+
PutLifecyclePolicyRequest putRequest2 = new PutLifecyclePolicyRequest(otherPolicyAsPut);
156+
157+
AcknowledgedResponse putResponse = client.indexLifecycle().
158+
putLifecyclePolicy(putRequest, RequestOptions.DEFAULT);
159+
assertTrue(putResponse.isAcknowledged());
160+
AcknowledgedResponse putResponse2 = client.indexLifecycle().
161+
putLifecyclePolicy(putRequest2, RequestOptions.DEFAULT);
162+
assertTrue(putResponse2.isAcknowledged());
163+
}
164+
165+
// tag::ilm-get-lifecycle-policy-request
166+
GetLifecyclePolicyRequest allRequest =
167+
new GetLifecyclePolicyRequest(); // <1>
168+
GetLifecyclePolicyRequest request =
169+
new GetLifecyclePolicyRequest("my_policy", "other_policy"); // <2>
170+
// end::ilm-get-lifecycle-policy-request
171+
172+
// tag::ilm-get-lifecycle-policy-execute
173+
GetLifecyclePolicyResponse response = client.indexLifecycle()
174+
.getLifecyclePolicy(request, RequestOptions.DEFAULT);
175+
// end::ilm-get-lifecycle-policy-execute
176+
177+
// tag::ilm-get-lifecycle-policy-response
178+
ImmutableOpenMap<String, LifecyclePolicyMetadata> policies =
179+
response.getPolicies();
180+
LifecyclePolicyMetadata myPolicyMetadata =
181+
policies.get("my_policy"); // <1>
182+
String myPolicyName = myPolicyMetadata.getName();
183+
long version = myPolicyMetadata.getVersion();
184+
String lastModified = myPolicyMetadata.getModifiedDateString();
185+
long lastModifiedDate = myPolicyMetadata.getModifiedDate();
186+
LifecyclePolicy myPolicy = myPolicyMetadata.getPolicy(); // <2>
187+
// end::ilm-get-lifecycle-policy-response
188+
189+
assertEquals(myPolicyAsPut, myPolicy);
190+
assertEquals("my_policy", myPolicyName);
191+
assertNotNull(lastModified);
192+
assertNotEquals(0, lastModifiedDate);
193+
194+
LifecyclePolicyMetadata otherPolicyMetadata = policies.get("other_policy");
195+
assertEquals(otherPolicyAsPut, otherPolicyMetadata.getPolicy());
196+
assertEquals("other_policy", otherPolicyMetadata.getName());
197+
assertNotNull(otherPolicyMetadata.getModifiedDateString());
198+
assertNotEquals(0, otherPolicyMetadata.getModifiedDate());
199+
200+
// tag::ilm-get-lifecycle-policy-execute-listener
201+
ActionListener<GetLifecyclePolicyResponse> listener =
202+
new ActionListener<GetLifecyclePolicyResponse>() {
203+
@Override
204+
public void onResponse(GetLifecyclePolicyResponse response)
205+
{
206+
ImmutableOpenMap<String, LifecyclePolicyMetadata>
207+
policies = response.getPolicies(); // <1>
208+
}
209+
210+
@Override
211+
public void onFailure(Exception e) {
212+
// <2>
213+
}
214+
};
215+
// end::ilm-get-lifecycle-policy-execute-listener
216+
217+
// Replace the empty listener by a blocking listener in test
218+
final CountDownLatch latch = new CountDownLatch(1);
219+
listener = new LatchedActionListener<>(listener, latch);
220+
221+
// tag::ilm-get-lifecycle-policy-execute-async
222+
client.indexLifecycle().getLifecyclePolicyAsync(request,
223+
RequestOptions.DEFAULT, listener); // <1>
224+
// end::ilm-get-lifecycle-policy-execute-async
225+
226+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
227+
}
228+
122229
static Map<String, Object> toMap(Response response) throws IOException {
123230
return XContentHelper.convertToMap(JsonXContent.jsonXContent, EntityUtils.toString(response.getEntity()), false);
124231
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--
2+
:api: ilm-get-lifecycle-policy
3+
:request: GetLifecyclePolicyRequest
4+
:response: GetLifecyclePolicyResponse
5+
--
6+
7+
[id="{upid}-{api}"]
8+
=== Get Lifecycle Policy API
9+
10+
11+
[id="{upid}-{api}-request"]
12+
==== Request
13+
14+
The Get Lifecycle Policy API allows you to retrieve the definition of an Index
15+
Lifecycle Management Policy from the cluster.
16+
17+
["source","java",subs="attributes,callouts,macros"]
18+
--------------------------------------------------
19+
include-tagged::{doc-tests-file}[{api}-request]
20+
--------------------------------------------------
21+
<1> Gets all policies.
22+
<2> Gets `my_policy` and `other_policy`
23+
24+
[id="{upid}-{api}-response"]
25+
==== Response
26+
27+
The returned +{response}+ contains a map of `LifecyclePolicyMetadata`,
28+
accessible by the name of the policy, which contains data about each policy,
29+
as well as the policy definition.
30+
31+
["source","java",subs="attributes,callouts,macros"]
32+
--------------------------------------------------
33+
include-tagged::{doc-tests-file}[{api}-response]
34+
--------------------------------------------------
35+
<1> The retrieved policies are retrieved by name.
36+
<2> The policy definition itself.
37+
38+
include::../execution.asciidoc[]
39+
40+

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,5 +444,8 @@ The Java High Level REST Client supports the following Index Lifecycle
444444
Management APIs:
445445

446446
* <<{upid}-ilm-put-lifecycle-policy>>
447+
* <<{upid}-ilm-get-lifecycle-policy>>
447448

448449
include::ilm/put_lifecycle_policy.asciidoc[]
450+
include::ilm/get_lifecycle_policy.asciidoc[]
451+

0 commit comments

Comments
 (0)