From 099baafe0d2b4ee7cd41d9f077d97cb857948911 Mon Sep 17 00:00:00 2001 From: Gordon Brown Date: Thu, 25 Oct 2018 10:35:49 -0600 Subject: [PATCH] HLRC: Split ILM request converters (#34759) In an effort to encapsulate different clients, the request converters are being shuffled around. This splits the Index Lifecycle Management request converters. --- .../client/IndexLifecycleClient.java | 40 ++-- .../IndexLifecycleRequestConverters.java | 154 ++++++++++++++++ .../client/RequestConverters.java | 123 +------------ .../IndexLifecycleRequestConvertersTests.java | 171 ++++++++++++++++++ .../client/RequestConvertersTests.java | 134 +------------- 5 files changed, 348 insertions(+), 274 deletions(-) create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleRequestConverters.java create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleRequestConvertersTests.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleClient.java index 8c5c9089454..88456f8dcc0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleClient.java @@ -57,7 +57,7 @@ public class IndexLifecycleClient { */ public GetLifecyclePolicyResponse getLifecyclePolicy(GetLifecyclePolicyRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::getLifecyclePolicy, options, + return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::getLifecyclePolicy, options, GetLifecyclePolicyResponse::fromXContent, emptySet()); } @@ -71,7 +71,7 @@ public class IndexLifecycleClient { */ public void getLifecyclePolicyAsync(GetLifecyclePolicyRequest request, RequestOptions options, ActionListener listener) { - restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::getLifecyclePolicy, options, + restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::getLifecyclePolicy, options, GetLifecyclePolicyResponse::fromXContent, listener, emptySet()); } @@ -86,7 +86,7 @@ public class IndexLifecycleClient { */ public AcknowledgedResponse putLifecyclePolicy(PutLifecyclePolicyRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::putLifecyclePolicy, options, + return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::putLifecyclePolicy, options, AcknowledgedResponse::fromXContent, emptySet()); } @@ -100,7 +100,7 @@ public class IndexLifecycleClient { */ public void putLifecyclePolicyAsync(PutLifecyclePolicyRequest request, RequestOptions options, ActionListener listener) { - restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::putLifecyclePolicy, options, + restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::putLifecyclePolicy, options, AcknowledgedResponse::fromXContent, listener, emptySet()); } @@ -115,7 +115,7 @@ public class IndexLifecycleClient { */ public AcknowledgedResponse deleteLifecyclePolicy(DeleteLifecyclePolicyRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::deleteLifecyclePolicy, options, + return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::deleteLifecyclePolicy, options, AcknowledgedResponse::fromXContent, emptySet()); } @@ -129,7 +129,7 @@ public class IndexLifecycleClient { */ public void deleteLifecyclePolicyAsync(DeleteLifecyclePolicyRequest request, RequestOptions options, ActionListener listener) { - restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::deleteLifecyclePolicy, options, + restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::deleteLifecyclePolicy, options, AcknowledgedResponse::fromXContent, listener, emptySet()); } @@ -144,8 +144,8 @@ public class IndexLifecycleClient { */ public RemoveIndexLifecyclePolicyResponse removeIndexLifecyclePolicy(RemoveIndexLifecyclePolicyRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::removeIndexLifecyclePolicy, options, - RemoveIndexLifecyclePolicyResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::removeIndexLifecyclePolicy, + options, RemoveIndexLifecyclePolicyResponse::fromXContent, emptySet()); } /** @@ -158,7 +158,7 @@ public class IndexLifecycleClient { */ public void removeIndexLifecyclePolicyAsync(RemoveIndexLifecyclePolicyRequest request, RequestOptions options, ActionListener listener) { - restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::removeIndexLifecyclePolicy, options, + restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::removeIndexLifecyclePolicy, options, RemoveIndexLifecyclePolicyResponse::fromXContent, listener, emptySet()); } @@ -172,7 +172,7 @@ public class IndexLifecycleClient { * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse startILM(StartILMRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::startILM, options, + return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::startILM, options, AcknowledgedResponse::fromXContent, emptySet()); } @@ -185,7 +185,7 @@ public class IndexLifecycleClient { * @param listener the listener to be notified upon request completion */ public void startILMAsync(StartILMRequest request, RequestOptions options, ActionListener listener) { - restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::startILM, options, + restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::startILM, options, AcknowledgedResponse::fromXContent, listener, emptySet()); } @@ -199,7 +199,7 @@ public class IndexLifecycleClient { * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse stopILM(StopILMRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::stopILM, options, + return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::stopILM, options, AcknowledgedResponse::fromXContent, emptySet()); } @@ -213,8 +213,8 @@ public class IndexLifecycleClient { */ public LifecycleManagementStatusResponse lifecycleManagementStatus(LifecycleManagementStatusRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::lifecycleManagementStatus, options, - LifecycleManagementStatusResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::lifecycleManagementStatus, + options, LifecycleManagementStatusResponse::fromXContent, emptySet()); } /** @@ -228,7 +228,7 @@ public class IndexLifecycleClient { */ public void lifecycleManagementStatusAsync(LifecycleManagementStatusRequest request, RequestOptions options, ActionListener listener) { - restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::lifecycleManagementStatus, options, + restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::lifecycleManagementStatus, options, LifecycleManagementStatusResponse::fromXContent, listener, emptySet()); } @@ -241,7 +241,7 @@ public class IndexLifecycleClient { * @param listener the listener to be notified upon request completion */ public void stopILMAsync(StopILMRequest request, RequestOptions options, ActionListener listener) { - restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::stopILM, options, + restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::stopILM, options, AcknowledgedResponse::fromXContent, listener, emptySet()); } @@ -255,7 +255,7 @@ public class IndexLifecycleClient { * @throws IOException in case there is a problem sending the request or parsing back the response */ public ExplainLifecycleResponse explainLifecycle(ExplainLifecycleRequest request,RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::explainLifecycle, options, + return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::explainLifecycle, options, ExplainLifecycleResponse::fromXContent, emptySet()); } @@ -269,7 +269,7 @@ public class IndexLifecycleClient { */ public void explainLifecycleAsync(ExplainLifecycleRequest request, RequestOptions options, ActionListener listener) { - restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::explainLifecycle, options, + restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::explainLifecycle, options, ExplainLifecycleResponse::fromXContent, listener, emptySet()); } @@ -283,7 +283,7 @@ public class IndexLifecycleClient { * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse retryLifecycleStep(RetryLifecyclePolicyRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::retryLifecycle, options, + return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::retryLifecycle, options, AcknowledgedResponse::fromXContent, emptySet()); } @@ -297,7 +297,7 @@ public class IndexLifecycleClient { */ public void retryLifecycleStepAsync(RetryLifecyclePolicyRequest request, RequestOptions options, ActionListener listener) { - restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::retryLifecycle, options, + restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::retryLifecycle, options, AcknowledgedResponse::fromXContent, listener, emptySet()); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleRequestConverters.java new file mode 100644 index 00000000000..f61fac3323a --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleRequestConverters.java @@ -0,0 +1,154 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.client; + +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest; +import org.elasticsearch.client.indexlifecycle.ExplainLifecycleRequest; +import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyRequest; +import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest; +import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest; +import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest; +import org.elasticsearch.client.indexlifecycle.RetryLifecyclePolicyRequest; +import org.elasticsearch.client.indexlifecycle.StartILMRequest; +import org.elasticsearch.client.indexlifecycle.StopILMRequest; +import org.elasticsearch.common.Strings; + +import java.io.IOException; + +final class IndexLifecycleRequestConverters { + + private IndexLifecycleRequestConverters() {} + + static Request getLifecyclePolicy(GetLifecyclePolicyRequest getLifecyclePolicyRequest) { + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_ilm") + .addCommaSeparatedPathParts(getLifecyclePolicyRequest.getPolicyNames()).build(); + Request request = new Request(HttpGet.METHOD_NAME, endpoint); + RequestConverters.Params params = new RequestConverters.Params(request); + params.withMasterTimeout(getLifecyclePolicyRequest.masterNodeTimeout()); + params.withTimeout(getLifecyclePolicyRequest.timeout()); + return request; + } + + static Request putLifecyclePolicy(PutLifecyclePolicyRequest putLifecycleRequest) throws IOException { + String endpoint = new RequestConverters.EndpointBuilder() + .addPathPartAsIs("_ilm") + .addPathPartAsIs(putLifecycleRequest.getName()) + .build(); + Request request = new Request(HttpPut.METHOD_NAME, endpoint); + RequestConverters.Params params = new RequestConverters.Params(request); + params.withMasterTimeout(putLifecycleRequest.masterNodeTimeout()); + params.withTimeout(putLifecycleRequest.timeout()); + request.setEntity(RequestConverters.createEntity(putLifecycleRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE)); + return request; + } + + static Request deleteLifecyclePolicy(DeleteLifecyclePolicyRequest deleteLifecyclePolicyRequest) { + Request request = new Request(HttpDelete.METHOD_NAME, + new RequestConverters.EndpointBuilder() + .addPathPartAsIs("_ilm") + .addPathPartAsIs(deleteLifecyclePolicyRequest.getLifecyclePolicy()) + .build()); + RequestConverters.Params params = new RequestConverters.Params(request); + params.withMasterTimeout(deleteLifecyclePolicyRequest.masterNodeTimeout()); + params.withTimeout(deleteLifecyclePolicyRequest.timeout()); + return request; + } + + static Request removeIndexLifecyclePolicy(RemoveIndexLifecyclePolicyRequest removePolicyRequest) { + String[] indices = removePolicyRequest.indices() == null ? + Strings.EMPTY_ARRAY : removePolicyRequest.indices().toArray(new String[] {}); + Request request = new Request(HttpDelete.METHOD_NAME, + new RequestConverters.EndpointBuilder() + .addCommaSeparatedPathParts(indices) + .addPathPartAsIs("_ilm") + .build()); + RequestConverters.Params params = new RequestConverters.Params(request); + params.withIndicesOptions(removePolicyRequest.indicesOptions()); + params.withMasterTimeout(removePolicyRequest.masterNodeTimeout()); + return request; + } + + static Request startILM(StartILMRequest startILMRequest) { + Request request = new Request(HttpPost.METHOD_NAME, + new RequestConverters.EndpointBuilder() + .addPathPartAsIs("_ilm") + .addPathPartAsIs("start") + .build()); + RequestConverters.Params params = new RequestConverters.Params(request); + params.withMasterTimeout(startILMRequest.masterNodeTimeout()); + params.withTimeout(startILMRequest.timeout()); + return request; + } + + static Request stopILM(StopILMRequest stopILMRequest) { + Request request = new Request(HttpPost.METHOD_NAME, + new RequestConverters.EndpointBuilder() + .addPathPartAsIs("_ilm") + .addPathPartAsIs("stop") + .build()); + RequestConverters.Params params = new RequestConverters.Params(request); + params.withMasterTimeout(stopILMRequest.masterNodeTimeout()); + params.withTimeout(stopILMRequest.timeout()); + return request; + } + + static Request lifecycleManagementStatus(LifecycleManagementStatusRequest lifecycleManagementStatusRequest){ + Request request = new Request(HttpGet.METHOD_NAME, + new RequestConverters.EndpointBuilder() + .addPathPartAsIs("_ilm") + .addPathPartAsIs("status") + .build()); + RequestConverters.Params params = new RequestConverters.Params(request); + params.withMasterTimeout(lifecycleManagementStatusRequest.masterNodeTimeout()); + params.withTimeout(lifecycleManagementStatusRequest.timeout()); + return request; + } + + static Request explainLifecycle(ExplainLifecycleRequest explainLifecycleRequest) { + String[] indices = explainLifecycleRequest.indices() == null ? Strings.EMPTY_ARRAY : explainLifecycleRequest.indices(); + Request request = new Request(HttpGet.METHOD_NAME, + new RequestConverters.EndpointBuilder() + .addCommaSeparatedPathParts(indices) + .addPathPartAsIs("_ilm") + .addPathPartAsIs("explain") + .build()); + RequestConverters.Params params = new RequestConverters.Params(request); + params.withIndicesOptions(explainLifecycleRequest.indicesOptions()); + params.withMasterTimeout(explainLifecycleRequest.masterNodeTimeout()); + return request; + } + + static Request retryLifecycle(RetryLifecyclePolicyRequest retryLifecyclePolicyRequest) { + Request request = new Request(HttpPost.METHOD_NAME, + new RequestConverters.EndpointBuilder() + .addCommaSeparatedPathParts(retryLifecyclePolicyRequest.getIndices()) + .addPathPartAsIs("_ilm") + .addPathPartAsIs("retry") + .build()); + RequestConverters.Params params = new RequestConverters.Params(request); + params.withMasterTimeout(retryLifecyclePolicyRequest.masterNodeTimeout()); + params.withTimeout(retryLifecyclePolicyRequest.timeout()); + return request; + } +} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java index 2cc5efb9a4c..ec8bf035a40 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java @@ -19,7 +19,6 @@ package org.elasticsearch.client; -import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; @@ -50,15 +49,6 @@ import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.action.update.UpdateRequest; -import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest; -import org.elasticsearch.client.indexlifecycle.ExplainLifecycleRequest; -import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyRequest; -import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest; -import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest; -import org.elasticsearch.client.indexlifecycle.RetryLifecyclePolicyRequest; -import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest; -import org.elasticsearch.client.indexlifecycle.StartILMRequest; -import org.elasticsearch.client.indexlifecycle.StopILMRequest; import org.elasticsearch.client.security.RefreshPolicy; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.common.Nullable; @@ -93,6 +83,7 @@ import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.Charset; +import java.util.List; import java.util.Locale; import java.util.StringJoiner; @@ -605,118 +596,6 @@ final class RequestConverters { return request; } - static Request getLifecyclePolicy(GetLifecyclePolicyRequest getLifecyclePolicyRequest) { - String endpoint = new EndpointBuilder().addPathPartAsIs("_ilm") - .addCommaSeparatedPathParts(getLifecyclePolicyRequest.getPolicyNames()).build(); - Request request = new Request(HttpGet.METHOD_NAME, endpoint); - Params params = new Params(request); - params.withMasterTimeout(getLifecyclePolicyRequest.masterNodeTimeout()); - params.withTimeout(getLifecyclePolicyRequest.timeout()); - return request; - } - - static Request putLifecyclePolicy(PutLifecyclePolicyRequest putLifecycleRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ilm") - .addPathPartAsIs(putLifecycleRequest.getName()) - .build(); - Request request = new Request(HttpPut.METHOD_NAME, endpoint); - Params params = new Params(request); - params.withMasterTimeout(putLifecycleRequest.masterNodeTimeout()); - params.withTimeout(putLifecycleRequest.timeout()); - request.setEntity(createEntity(putLifecycleRequest, REQUEST_BODY_CONTENT_TYPE)); - return request; - } - - static Request deleteLifecyclePolicy(DeleteLifecyclePolicyRequest deleteLifecyclePolicyRequest) { - Request request = new Request(HttpDelete.METHOD_NAME, - new EndpointBuilder() - .addPathPartAsIs("_ilm") - .addPathPartAsIs(deleteLifecyclePolicyRequest.getLifecyclePolicy()) - .build()); - Params params = new Params(request); - params.withMasterTimeout(deleteLifecyclePolicyRequest.masterNodeTimeout()); - params.withTimeout(deleteLifecyclePolicyRequest.timeout()); - return request; - } - - static Request removeIndexLifecyclePolicy(RemoveIndexLifecyclePolicyRequest removePolicyRequest) { - String[] indices = removePolicyRequest.indices() == null ? - Strings.EMPTY_ARRAY : removePolicyRequest.indices().toArray(new String[] {}); - Request request = new Request(HttpDelete.METHOD_NAME, - new EndpointBuilder() - .addCommaSeparatedPathParts(indices) - .addPathPartAsIs("_ilm") - .build()); - Params params = new Params(request); - params.withIndicesOptions(removePolicyRequest.indicesOptions()); - params.withMasterTimeout(removePolicyRequest.masterNodeTimeout()); - return request; - } - - static Request startILM(StartILMRequest startILMRequest) { - Request request = new Request(HttpPost.METHOD_NAME, - new EndpointBuilder() - .addPathPartAsIs("_ilm") - .addPathPartAsIs("start") - .build()); - Params params = new Params(request); - params.withMasterTimeout(startILMRequest.masterNodeTimeout()); - params.withTimeout(startILMRequest.timeout()); - return request; - } - - static Request stopILM(StopILMRequest stopILMRequest) { - Request request = new Request(HttpPost.METHOD_NAME, - new EndpointBuilder() - .addPathPartAsIs("_ilm") - .addPathPartAsIs("stop") - .build()); - Params params = new Params(request); - params.withMasterTimeout(stopILMRequest.masterNodeTimeout()); - params.withTimeout(stopILMRequest.timeout()); - return request; - } - - static Request lifecycleManagementStatus(LifecycleManagementStatusRequest lifecycleManagementStatusRequest){ - Request request = new Request(HttpGet.METHOD_NAME, - new EndpointBuilder() - .addPathPartAsIs("_ilm") - .addPathPartAsIs("status") - .build()); - Params params = new Params(request); - params.withMasterTimeout(lifecycleManagementStatusRequest.masterNodeTimeout()); - params.withTimeout(lifecycleManagementStatusRequest.timeout()); - return request; - } - - static Request explainLifecycle(ExplainLifecycleRequest explainLifecycleRequest) { - String[] indices = explainLifecycleRequest.indices() == null ? Strings.EMPTY_ARRAY : explainLifecycleRequest.indices(); - Request request = new Request(HttpGet.METHOD_NAME, - new EndpointBuilder() - .addCommaSeparatedPathParts(indices) - .addPathPartAsIs("_ilm") - .addPathPartAsIs("explain") - .build()); - Params params = new Params(request); - params.withIndicesOptions(explainLifecycleRequest.indicesOptions()); - params.withMasterTimeout(explainLifecycleRequest.masterNodeTimeout()); - return request; - } - - static Request retryLifecycle(RetryLifecyclePolicyRequest retryLifecyclePolicyRequest) { - Request request = new Request(HttpPost.METHOD_NAME, - new EndpointBuilder() - .addCommaSeparatedPathParts(retryLifecyclePolicyRequest.getIndices()) - .addPathPartAsIs("_ilm") - .addPathPartAsIs("retry") - .build()); - Params params = new Params(request); - params.withMasterTimeout(retryLifecyclePolicyRequest.masterNodeTimeout()); - params.withTimeout(retryLifecyclePolicyRequest.timeout()); - return request; - } - static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) throws IOException { BytesRef source = XContentHelper.toXContent(toXContent, xContentType, false).toBytesRef(); return new ByteArrayEntity(source.bytes, source.offset, source.length, createContentType(xContentType)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleRequestConvertersTests.java new file mode 100644 index 00000000000..bc878f328ef --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleRequestConvertersTests.java @@ -0,0 +1,171 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.client; + +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.elasticsearch.action.support.IndicesOptions; +import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest; +import org.elasticsearch.client.indexlifecycle.ExplainLifecycleRequest; +import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyRequest; +import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest; +import org.elasticsearch.client.indexlifecycle.LifecyclePolicy; +import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest; +import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest; +import org.elasticsearch.client.indexlifecycle.RetryLifecyclePolicyRequest; +import org.elasticsearch.client.indexlifecycle.StartILMRequest; +import org.elasticsearch.client.indexlifecycle.StopILMRequest; +import org.elasticsearch.common.Strings; +import org.elasticsearch.test.ESTestCase; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import static org.elasticsearch.client.RequestConvertersTests.randomIndicesNames; +import static org.elasticsearch.client.RequestConvertersTests.setRandomIndicesOptions; +import static org.elasticsearch.client.RequestConvertersTests.setRandomMasterTimeout; +import static org.elasticsearch.client.RequestConvertersTests.setRandomTimeoutTimeValue; +import static org.elasticsearch.client.indexlifecycle.LifecyclePolicyTests.createRandomPolicy; +import static org.hamcrest.CoreMatchers.equalTo; + +public class IndexLifecycleRequestConvertersTests extends ESTestCase { + + public void testGetLifecyclePolicy() { + String[] policies = rarely() ? null : randomIndicesNames(0, 10); + GetLifecyclePolicyRequest req = new GetLifecyclePolicyRequest(policies); + Map expectedParams = new HashMap<>(); + setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); + setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); + + Request request = IndexLifecycleRequestConverters.getLifecyclePolicy(req); + assertEquals(request.getMethod(), HttpGet.METHOD_NAME); + String policiesStr = Strings.arrayToCommaDelimitedString(policies); + assertEquals(request.getEndpoint(), "/_ilm" + (policiesStr.isEmpty() ? "" : ("/" + policiesStr))); + assertEquals(request.getParameters(), expectedParams); + } + + public void testPutLifecyclePolicy() throws Exception { + String name = randomAlphaOfLengthBetween(2, 20); + LifecyclePolicy policy = createRandomPolicy(name); + PutLifecyclePolicyRequest req = new PutLifecyclePolicyRequest(policy); + Map expectedParams = new HashMap<>(); + setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); + setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); + + Request request = IndexLifecycleRequestConverters.putLifecyclePolicy(req); + assertEquals(HttpPut.METHOD_NAME, request.getMethod()); + assertEquals("/_ilm/" + name, request.getEndpoint()); + assertEquals(expectedParams, request.getParameters()); + } + + public void testDeleteLifecycle() { + String lifecycleName = randomAlphaOfLengthBetween(2,20); + DeleteLifecyclePolicyRequest req = new DeleteLifecyclePolicyRequest(lifecycleName); + Map expectedParams = new HashMap<>(); + setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); + setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); + + Request request = IndexLifecycleRequestConverters.deleteLifecyclePolicy(req); + assertEquals(request.getMethod(), HttpDelete.METHOD_NAME); + assertEquals(request.getEndpoint(), "/_ilm/" + lifecycleName); + assertEquals(request.getParameters(), expectedParams); + } + + public void testRemoveIndexLifecyclePolicy() { + Map expectedParams = new HashMap<>(); + String[] indices = randomIndicesNames(0, 10); + IndicesOptions indicesOptions = setRandomIndicesOptions(IndicesOptions.strictExpandOpen(), expectedParams); + RemoveIndexLifecyclePolicyRequest req = new RemoveIndexLifecyclePolicyRequest(Arrays.asList(indices), indicesOptions); + setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); + + Request request = IndexLifecycleRequestConverters.removeIndexLifecyclePolicy(req); + assertThat(request.getMethod(), equalTo(HttpDelete.METHOD_NAME)); + String idxString = Strings.arrayToCommaDelimitedString(indices); + assertThat(request.getEndpoint(), equalTo("/" + (idxString.isEmpty() ? "" : (idxString + "/")) + "_ilm")); + assertThat(request.getParameters(), equalTo(expectedParams)); + } + + public void testStartILM() throws Exception { + StartILMRequest req = new StartILMRequest(); + Map expectedParams = new HashMap<>(); + setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); + setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); + + Request request = IndexLifecycleRequestConverters.startILM(req); + assertThat(request.getMethod(), equalTo(HttpPost.METHOD_NAME)); + assertThat(request.getEndpoint(), equalTo("/_ilm/start")); + assertThat(request.getParameters(), equalTo(expectedParams)); + } + + public void testStopILM() throws Exception { + StopILMRequest req = new StopILMRequest(); + Map expectedParams = new HashMap<>(); + setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); + setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); + + Request request = IndexLifecycleRequestConverters.stopILM(req); + assertThat(request.getMethod(), equalTo(HttpPost.METHOD_NAME)); + assertThat(request.getEndpoint(), equalTo("/_ilm/stop")); + assertThat(request.getParameters(), equalTo(expectedParams)); + } + + public void testLifecycleManagementStatus() throws Exception { + LifecycleManagementStatusRequest req = new LifecycleManagementStatusRequest(); + Map expectedParams = new HashMap<>(); + setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); + setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); + + Request request = IndexLifecycleRequestConverters.lifecycleManagementStatus(req); + assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME)); + assertThat(request.getEndpoint(), equalTo("/_ilm/status")); + assertThat(request.getParameters(), equalTo(expectedParams)); + } + + public void testExplainLifecycle() throws Exception { + ExplainLifecycleRequest req = new ExplainLifecycleRequest(); + String[] indices = rarely() ? null : randomIndicesNames(0, 10); + req.indices(indices); + Map expectedParams = new HashMap<>(); + setRandomMasterTimeout(req, expectedParams); + setRandomIndicesOptions(req::indicesOptions, req::indicesOptions, expectedParams); + + Request request = IndexLifecycleRequestConverters.explainLifecycle(req); + assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME)); + String idxString = Strings.arrayToCommaDelimitedString(indices); + assertThat(request.getEndpoint(), equalTo("/" + (idxString.isEmpty() ? "" : (idxString + "/")) + "_ilm/explain")); + assertThat(request.getParameters(), equalTo(expectedParams)); + } + + public void testRetryLifecycle() throws Exception { + String[] indices = randomIndicesNames(1, 10); + RetryLifecyclePolicyRequest req = new RetryLifecyclePolicyRequest(indices); + Map expectedParams = new HashMap<>(); + setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); + setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); + Request request = IndexLifecycleRequestConverters.retryLifecycle(req); + assertThat(request.getMethod(), equalTo(HttpPost.METHOD_NAME)); + String idxString = Strings.arrayToCommaDelimitedString(indices); + assertThat(request.getEndpoint(), equalTo("/" + (idxString.isEmpty() ? "" : (idxString + "/")) + "_ilm/retry")); + assertThat(request.getParameters(), equalTo(expectedParams)); + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java index 113380bd08d..2641b37279d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java @@ -55,16 +55,6 @@ import org.elasticsearch.action.support.master.MasterNodeRequest; import org.elasticsearch.action.support.replication.ReplicationRequest; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.client.RequestConverters.EndpointBuilder; -import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest; -import org.elasticsearch.client.indexlifecycle.ExplainLifecycleRequest; -import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyRequest; -import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest; -import org.elasticsearch.client.indexlifecycle.LifecyclePolicy; -import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest; -import org.elasticsearch.client.indexlifecycle.RetryLifecyclePolicyRequest; -import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest; -import org.elasticsearch.client.indexlifecycle.StartILMRequest; -import org.elasticsearch.client.indexlifecycle.StopILMRequest; import org.elasticsearch.common.CheckedBiConsumer; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; @@ -129,7 +119,6 @@ import static java.util.Collections.emptyMap; import static java.util.Collections.singletonMap; import static org.elasticsearch.client.RequestConverters.REQUEST_BODY_CONTENT_TYPE; import static org.elasticsearch.client.RequestConverters.enforceSameContentType; -import static org.elasticsearch.client.indexlifecycle.LifecyclePolicyTests.createRandomPolicy; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.search.RandomSearchRequestGenerator.randomSearchRequest; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent; @@ -1468,125 +1457,6 @@ public class RequestConvertersTests extends ESTestCase { + "previous requests have content-type [" + xContentType + "]", exception.getMessage()); } - public void testGetLifecyclePolicy() { - String[] policies = rarely() ? null : randomIndicesNames(0, 10); - GetLifecyclePolicyRequest req = new GetLifecyclePolicyRequest(policies); - Map expectedParams = new HashMap<>(); - setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); - setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); - - Request request = RequestConverters.getLifecyclePolicy(req); - assertEquals(request.getMethod(), HttpGet.METHOD_NAME); - String policiesStr = Strings.arrayToCommaDelimitedString(policies); - assertEquals(request.getEndpoint(), "/_ilm" + (policiesStr.isEmpty() ? "" : ("/" + policiesStr))); - assertEquals(request.getParameters(), expectedParams); - } - - public void testPutLifecyclePolicy() throws Exception { - String name = randomAlphaOfLengthBetween(2, 20); - LifecyclePolicy policy = createRandomPolicy(name); - PutLifecyclePolicyRequest req = new PutLifecyclePolicyRequest(policy); - Map expectedParams = new HashMap<>(); - setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); - setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); - - Request request = RequestConverters.putLifecyclePolicy(req); - assertEquals(HttpPut.METHOD_NAME, request.getMethod()); - assertEquals("/_ilm/" + name, request.getEndpoint()); - assertEquals(expectedParams, request.getParameters()); - } - - public void testDeleteLifecycle() { - String lifecycleName = randomAlphaOfLengthBetween(2,20); - DeleteLifecyclePolicyRequest req = new DeleteLifecyclePolicyRequest(lifecycleName); - Map expectedParams = new HashMap<>(); - setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); - setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); - - Request request = RequestConverters.deleteLifecyclePolicy(req); - assertEquals(request.getMethod(), HttpDelete.METHOD_NAME); - assertEquals(request.getEndpoint(), "/_ilm/" + lifecycleName); - assertEquals(request.getParameters(), expectedParams); - } - - public void testRemoveIndexLifecyclePolicy() { - Map expectedParams = new HashMap<>(); - String[] indices = randomIndicesNames(0, 10); - IndicesOptions indicesOptions = setRandomIndicesOptions(IndicesOptions.strictExpandOpen(), expectedParams); - RemoveIndexLifecyclePolicyRequest req = new RemoveIndexLifecyclePolicyRequest(Arrays.asList(indices), indicesOptions); - setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); - - Request request = RequestConverters.removeIndexLifecyclePolicy(req); - assertThat(request.getMethod(), equalTo(HttpDelete.METHOD_NAME)); - String idxString = Strings.arrayToCommaDelimitedString(indices); - assertThat(request.getEndpoint(), equalTo("/" + (idxString.isEmpty() ? "" : (idxString + "/")) + "_ilm")); - assertThat(request.getParameters(), equalTo(expectedParams)); - } - - public void testStartILM() throws Exception { - StartILMRequest req = new StartILMRequest(); - Map expectedParams = new HashMap<>(); - setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); - setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); - - Request request = RequestConverters.startILM(req); - assertThat(request.getMethod(), equalTo(HttpPost.METHOD_NAME)); - assertThat(request.getEndpoint(), equalTo("/_ilm/start")); - assertThat(request.getParameters(), equalTo(expectedParams)); - } - - public void testStopILM() throws Exception { - StopILMRequest req = new StopILMRequest(); - Map expectedParams = new HashMap<>(); - setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); - setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); - - Request request = RequestConverters.stopILM(req); - assertThat(request.getMethod(), equalTo(HttpPost.METHOD_NAME)); - assertThat(request.getEndpoint(), equalTo("/_ilm/stop")); - assertThat(request.getParameters(), equalTo(expectedParams)); - } - - public void testLifecycleManagementStatus() throws Exception { - LifecycleManagementStatusRequest req = new LifecycleManagementStatusRequest(); - Map expectedParams = new HashMap<>(); - setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); - setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); - - Request request = RequestConverters.lifecycleManagementStatus(req); - assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME)); - assertThat(request.getEndpoint(), equalTo("/_ilm/status")); - assertThat(request.getParameters(), equalTo(expectedParams)); - } - - public void testExplainLifecycle() throws Exception { - ExplainLifecycleRequest req = new ExplainLifecycleRequest(); - String[] indices = rarely() ? null : randomIndicesNames(0, 10); - req.indices(indices); - Map expectedParams = new HashMap<>(); - setRandomMasterTimeout(req, expectedParams); - setRandomIndicesOptions(req::indicesOptions, req::indicesOptions, expectedParams); - - Request request = RequestConverters.explainLifecycle(req); - assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME)); - String idxString = Strings.arrayToCommaDelimitedString(indices); - assertThat(request.getEndpoint(), equalTo("/" + (idxString.isEmpty() ? "" : (idxString + "/")) + "_ilm/explain")); - assertThat(request.getParameters(), equalTo(expectedParams)); - } - - public void testRetryLifecycle() throws Exception { - String[] indices = randomIndicesNames(1, 10); - RetryLifecyclePolicyRequest req = new RetryLifecyclePolicyRequest(indices); - Map expectedParams = new HashMap<>(); - setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); - setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); - Request request = RequestConverters.retryLifecycle(req); - assertThat(request.getMethod(), equalTo(HttpPost.METHOD_NAME)); - String idxString = Strings.arrayToCommaDelimitedString(indices); - assertThat(request.getEndpoint(), equalTo("/" + (idxString.isEmpty() ? "" : (idxString + "/")) + "_ilm/retry")); - assertThat(request.getParameters(), equalTo(expectedParams)); - } - /** * Randomize the {@link FetchSourceContext} request parameters. */ @@ -1736,7 +1606,7 @@ public class RequestConvertersTests extends ESTestCase { } } - private static void setRandomTimeoutTimeValue(Consumer setter, TimeValue defaultTimeout, + static void setRandomTimeoutTimeValue(Consumer setter, TimeValue defaultTimeout, Map expectedParams) { if (randomBoolean()) { TimeValue timeout = TimeValue.parseTimeValue(randomTimeValue(), "random_timeout"); @@ -1767,7 +1637,7 @@ public class RequestConvertersTests extends ESTestCase { } } - private static void setRandomMasterTimeout(Consumer setter, TimeValue defaultTimeout, Map expectedParams) { + static void setRandomMasterTimeout(Consumer setter, TimeValue defaultTimeout, Map expectedParams) { if (randomBoolean()) { TimeValue masterTimeout = TimeValue.parseTimeValue(randomTimeValue(), "random_master_timeout"); setter.accept(masterTimeout);