|
| 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.documentation; |
| 21 | + |
| 22 | +import org.apache.http.util.EntityUtils; |
| 23 | +import org.elasticsearch.action.ActionListener; |
| 24 | +import org.elasticsearch.action.LatchedActionListener; |
| 25 | +import org.elasticsearch.client.ESRestHighLevelClientTestCase; |
| 26 | +import org.elasticsearch.client.RequestOptions; |
| 27 | +import org.elasticsearch.client.Response; |
| 28 | +import org.elasticsearch.client.RestHighLevelClient; |
| 29 | +import org.elasticsearch.client.core.AcknowledgedResponse; |
| 30 | +import org.elasticsearch.client.indexlifecycle.DeleteAction; |
| 31 | +import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest; |
| 32 | +import org.elasticsearch.client.indexlifecycle.LifecycleAction; |
| 33 | +import org.elasticsearch.client.indexlifecycle.LifecyclePolicy; |
| 34 | +import org.elasticsearch.client.indexlifecycle.Phase; |
| 35 | +import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest; |
| 36 | +import org.elasticsearch.client.indexlifecycle.RolloverAction; |
| 37 | +import org.elasticsearch.common.unit.ByteSizeUnit; |
| 38 | +import org.elasticsearch.common.unit.ByteSizeValue; |
| 39 | +import org.elasticsearch.common.unit.TimeValue; |
| 40 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 41 | +import org.elasticsearch.common.xcontent.json.JsonXContent; |
| 42 | + |
| 43 | +import java.io.IOException; |
| 44 | +import java.util.Collections; |
| 45 | +import java.util.HashMap; |
| 46 | +import java.util.Map; |
| 47 | +import java.util.concurrent.CountDownLatch; |
| 48 | +import java.util.concurrent.TimeUnit; |
| 49 | + |
| 50 | +public class ILMDocumentationIT extends ESRestHighLevelClientTestCase { |
| 51 | + |
| 52 | + public void testPutLifecyclePolicy() throws Exception { |
| 53 | + RestHighLevelClient client = highLevelClient(); |
| 54 | + |
| 55 | + // tag::ilm-put-lifecycle-policy-request |
| 56 | + Map<String, Phase> phases = new HashMap<>(); |
| 57 | + Map<String, LifecycleAction> hotActions = new HashMap<>(); |
| 58 | + hotActions.put(RolloverAction.NAME, new RolloverAction( |
| 59 | + new ByteSizeValue(50, ByteSizeUnit.GB), null, null)); |
| 60 | + phases.put("hot", new Phase("hot", TimeValue.ZERO, hotActions)); // <1> |
| 61 | + |
| 62 | + Map<String, LifecycleAction> deleteActions = |
| 63 | + Collections.singletonMap(DeleteAction.NAME, new DeleteAction()); |
| 64 | + phases.put("delete", new Phase("delete", |
| 65 | + new TimeValue(90, TimeUnit.DAYS), deleteActions)); // <2> |
| 66 | + |
| 67 | + LifecyclePolicy policy = new LifecyclePolicy("my_policy", |
| 68 | + phases); // <3> |
| 69 | + PutLifecyclePolicyRequest request = |
| 70 | + new PutLifecyclePolicyRequest(policy); |
| 71 | + // end::ilm-put-lifecycle-policy-request |
| 72 | + |
| 73 | + // tag::ilm-put-lifecycle-policy-execute |
| 74 | + AcknowledgedResponse response = client.indexLifecycle(). |
| 75 | + putLifecyclePolicy(request, RequestOptions.DEFAULT); |
| 76 | + // end::ilm-put-lifecycle-policy-execute |
| 77 | + |
| 78 | + // tag::ilm-put-lifecycle-policy-response |
| 79 | + boolean acknowledged = response.isAcknowledged(); // <1> |
| 80 | + // end::ilm-put-lifecycle-policy-response |
| 81 | + |
| 82 | + assertTrue(acknowledged); |
| 83 | + |
| 84 | + // Delete the policy so it can be added again |
| 85 | + { |
| 86 | + DeleteLifecyclePolicyRequest deleteRequest = |
| 87 | + new DeleteLifecyclePolicyRequest("my_policy"); |
| 88 | + AcknowledgedResponse deleteResponse = client.indexLifecycle() |
| 89 | + .deleteLifecyclePolicy(deleteRequest, |
| 90 | + RequestOptions.DEFAULT); |
| 91 | + assertTrue(deleteResponse.isAcknowledged()); |
| 92 | + } |
| 93 | + |
| 94 | + // tag::ilm-put-lifecycle-policy-execute-listener |
| 95 | + ActionListener<AcknowledgedResponse> listener = |
| 96 | + new ActionListener<AcknowledgedResponse>() { |
| 97 | + @Override |
| 98 | + public void onResponse(AcknowledgedResponse response) { |
| 99 | + boolean acknowledged = response.isAcknowledged(); // <1> |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void onFailure(Exception e) { |
| 104 | + // <2> |
| 105 | + } |
| 106 | + }; |
| 107 | + // end::ilm-put-lifecycle-policy-execute-listener |
| 108 | + |
| 109 | + // Replace the empty listener by a blocking listener in test |
| 110 | + final CountDownLatch latch = new CountDownLatch(1); |
| 111 | + listener = new LatchedActionListener<>(listener, latch); |
| 112 | + |
| 113 | + // tag::ilm-put-lifecycle-policy-execute-async |
| 114 | + client.indexLifecycle().putLifecyclePolicyAsync(request, |
| 115 | + RequestOptions.DEFAULT, listener); // <1> |
| 116 | + // end::ilm-put-lifecycle-policy-execute-async |
| 117 | + |
| 118 | + assertTrue(latch.await(30L, TimeUnit.SECONDS)); |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + static Map<String, Object> toMap(Response response) throws IOException { |
| 123 | + return XContentHelper.convertToMap(JsonXContent.jsonXContent, EntityUtils.toString(response.getEntity()), false); |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments