ソースを参照

Add HLRC docs for Get Lifecycle Policy (#35612)

Adds docs for the Get Lifecycle Policy API to the HLRC documentation.
Gordon Brown 7 年 前
コミット
bb51cdb6de

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

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

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

@@ -29,11 +29,16 @@ import org.elasticsearch.client.RestHighLevelClient;
 import org.elasticsearch.client.core.AcknowledgedResponse;
 import org.elasticsearch.client.indexlifecycle.DeleteAction;
 import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
+import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyRequest;
+import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyResponse;
 import org.elasticsearch.client.indexlifecycle.LifecycleAction;
 import org.elasticsearch.client.indexlifecycle.LifecyclePolicy;
+import org.elasticsearch.client.indexlifecycle.LifecyclePolicyMetadata;
 import org.elasticsearch.client.indexlifecycle.Phase;
 import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
 import org.elasticsearch.client.indexlifecycle.RolloverAction;
+import org.elasticsearch.client.indexlifecycle.ShrinkAction;
+import org.elasticsearch.common.collect.ImmutableOpenMap;
 import org.elasticsearch.common.unit.ByteSizeUnit;
 import org.elasticsearch.common.unit.ByteSizeValue;
 import org.elasticsearch.common.unit.TimeValue;
@@ -119,6 +124,108 @@ public class ILMDocumentationIT extends ESRestHighLevelClientTestCase {
 
     }
 
+    public void testGetLifecyclePolicy() throws IOException, InterruptedException {
+        RestHighLevelClient client = highLevelClient();
+
+        LifecyclePolicy myPolicyAsPut;
+        LifecyclePolicy otherPolicyAsPut;
+        // Set up some policies so we have something to get
+        {
+            Map<String, Phase> phases = new HashMap<>();
+            Map<String, LifecycleAction> hotActions = new HashMap<>();
+            hotActions.put(RolloverAction.NAME, new RolloverAction(
+                new ByteSizeValue(50, ByteSizeUnit.GB), null, null));
+            phases.put("hot", new Phase("hot", TimeValue.ZERO, hotActions));
+
+            Map<String, LifecycleAction> deleteActions =
+                Collections.singletonMap(DeleteAction.NAME,
+                    new DeleteAction());
+            phases.put("delete",
+                new Phase("delete",
+                    new TimeValue(90, TimeUnit.DAYS), deleteActions));
+
+            myPolicyAsPut = new LifecyclePolicy("my_policy", phases);
+            PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(myPolicyAsPut);
+
+            Map<String, Phase> otherPolicyPhases = new HashMap<>(phases);
+            Map<String, LifecycleAction> warmActions = Collections.singletonMap(ShrinkAction.NAME, new ShrinkAction(1));
+            otherPolicyPhases.put("warm", new Phase("warm", new TimeValue(30, TimeUnit.DAYS), warmActions));
+            otherPolicyAsPut = new LifecyclePolicy("other_policy", otherPolicyPhases);
+
+            PutLifecyclePolicyRequest putRequest2 = new PutLifecyclePolicyRequest(otherPolicyAsPut);
+
+            AcknowledgedResponse putResponse = client.indexLifecycle().
+                putLifecyclePolicy(putRequest, RequestOptions.DEFAULT);
+            assertTrue(putResponse.isAcknowledged());
+            AcknowledgedResponse putResponse2 = client.indexLifecycle().
+                putLifecyclePolicy(putRequest2, RequestOptions.DEFAULT);
+            assertTrue(putResponse2.isAcknowledged());
+        }
+
+        // tag::ilm-get-lifecycle-policy-request
+        GetLifecyclePolicyRequest allRequest =
+            new GetLifecyclePolicyRequest(); // <1>
+        GetLifecyclePolicyRequest request =
+            new GetLifecyclePolicyRequest("my_policy", "other_policy"); // <2>
+        // end::ilm-get-lifecycle-policy-request
+
+        // tag::ilm-get-lifecycle-policy-execute
+        GetLifecyclePolicyResponse response = client.indexLifecycle()
+            .getLifecyclePolicy(request, RequestOptions.DEFAULT);
+        // end::ilm-get-lifecycle-policy-execute
+
+        // tag::ilm-get-lifecycle-policy-response
+        ImmutableOpenMap<String, LifecyclePolicyMetadata> policies =
+            response.getPolicies();
+        LifecyclePolicyMetadata myPolicyMetadata =
+            policies.get("my_policy"); // <1>
+        String myPolicyName = myPolicyMetadata.getName();
+        long version = myPolicyMetadata.getVersion();
+        String lastModified = myPolicyMetadata.getModifiedDateString();
+        long lastModifiedDate = myPolicyMetadata.getModifiedDate();
+        LifecyclePolicy myPolicy = myPolicyMetadata.getPolicy(); // <2>
+        // end::ilm-get-lifecycle-policy-response
+
+        assertEquals(myPolicyAsPut, myPolicy);
+        assertEquals("my_policy", myPolicyName);
+        assertNotNull(lastModified);
+        assertNotEquals(0, lastModifiedDate);
+
+        LifecyclePolicyMetadata otherPolicyMetadata = policies.get("other_policy");
+        assertEquals(otherPolicyAsPut, otherPolicyMetadata.getPolicy());
+        assertEquals("other_policy", otherPolicyMetadata.getName());
+        assertNotNull(otherPolicyMetadata.getModifiedDateString());
+        assertNotEquals(0, otherPolicyMetadata.getModifiedDate());
+
+        // tag::ilm-get-lifecycle-policy-execute-listener
+        ActionListener<GetLifecyclePolicyResponse> listener =
+            new ActionListener<GetLifecyclePolicyResponse>() {
+                @Override
+                public void onResponse(GetLifecyclePolicyResponse response)
+                {
+                    ImmutableOpenMap<String, LifecyclePolicyMetadata>
+                        policies = response.getPolicies(); // <1>
+                }
+
+                @Override
+                public void onFailure(Exception e) {
+                    // <2>
+                }
+            };
+        // end::ilm-get-lifecycle-policy-execute-listener
+
+        // Replace the empty listener by a blocking listener in test
+        final CountDownLatch latch = new CountDownLatch(1);
+        listener = new LatchedActionListener<>(listener, latch);
+
+        // tag::ilm-get-lifecycle-policy-execute-async
+        client.indexLifecycle().getLifecyclePolicyAsync(request,
+            RequestOptions.DEFAULT, listener); // <1>
+        // end::ilm-get-lifecycle-policy-execute-async
+
+        assertTrue(latch.await(30L, TimeUnit.SECONDS));
+    }
+
     static Map<String, Object> toMap(Response response) throws IOException {
         return XContentHelper.convertToMap(JsonXContent.jsonXContent, EntityUtils.toString(response.getEntity()), false);
     }

+ 40 - 0
docs/java-rest/high-level/ilm/get_lifecycle_policy.asciidoc

@@ -0,0 +1,40 @@
+--
+:api: ilm-get-lifecycle-policy
+:request: GetLifecyclePolicyRequest
+:response: GetLifecyclePolicyResponse
+--
+
+[id="{upid}-{api}"]
+=== Get Lifecycle Policy API
+
+
+[id="{upid}-{api}-request"]
+==== Request
+
+The Get Lifecycle Policy API allows you to retrieve the definition of an Index
+Lifecycle Management Policy from the cluster.
+
+["source","java",subs="attributes,callouts,macros"]
+--------------------------------------------------
+include-tagged::{doc-tests-file}[{api}-request]
+--------------------------------------------------
+<1> Gets all policies.
+<2> Gets `my_policy` and `other_policy`
+
+[id="{upid}-{api}-response"]
+==== Response
+
+The returned +{response}+ contains a map of `LifecyclePolicyMetadata`,
+accessible by the name of the policy, which contains data about each policy,
+as well as the policy definition.
+
+["source","java",subs="attributes,callouts,macros"]
+--------------------------------------------------
+include-tagged::{doc-tests-file}[{api}-response]
+--------------------------------------------------
+<1> The retrieved policies are retrieved by name.
+<2> The policy definition itself.
+
+include::../execution.asciidoc[]
+
+

+ 3 - 0
docs/java-rest/high-level/supported-apis.asciidoc

@@ -444,5 +444,8 @@ The Java High Level REST Client supports the following Index Lifecycle
 Management APIs:
 
 * <<{upid}-ilm-put-lifecycle-policy>>
+* <<{upid}-ilm-get-lifecycle-policy>>
 
 include::ilm/put_lifecycle_policy.asciidoc[]
+include::ilm/get_lifecycle_policy.asciidoc[]
+