From f5918db1a63a0ea59a514b4decad1aa1352f01f4 Mon Sep 17 00:00:00 2001 From: Jake Landis Date: Thu, 30 Aug 2018 13:38:04 -0500 Subject: [PATCH 1/6] HLRC: Add ILM Status to HLRC Adds support for the Index Lifecycle Management Status to the Java High-Level Rest Client. Relates to #33100 --- .../client/IndexLifecycleClient.java | 29 ++++++ .../client/RequestConverters.java | 12 +++ .../elasticsearch/client/TimedRequest.java | 1 - .../indexlifecycle/StatusILMResponse.java | 99 +++++++++++++++++++ .../client/IndexLifecycleIT.java | 31 +++--- .../client/RequestConvertersTests.java | 12 +++ .../client/TimedRequestTests.java | 42 ++++++++ .../StatusILMResponseTests.java | 66 +++++++++++++ 8 files changed, 273 insertions(+), 19 deletions(-) create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/StatusILMResponse.java create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/TimedRequestTests.java create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/StatusILMResponseTests.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 78816a4f78703..01cf7b90d7f1a 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 @@ -22,6 +22,7 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest; +import org.elasticsearch.client.indexlifecycle.StatusILMResponse; import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest; import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleResponse; import org.elasticsearch.protocol.xpack.indexlifecycle.SetIndexLifecyclePolicyRequest; @@ -139,6 +140,34 @@ public AcknowledgedResponse stopILM(StopILMRequest request, RequestOptions optio AcknowledgedResponse::fromXContent, emptySet()); } + /** + * Get the status of index lifecycle management + * See + * the docs for more. + * + * @param request the request with user defined timeouts. + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + */ + public StatusILMResponse StatusILM(TimedRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::statusILM, options, + StatusILMResponse::fromXContent, emptySet()); + } + + /** + * Asynchronously get the status of index lifecycle management + * See + * the docs for more. + * + * @param request the request with user defined timeouts. + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion + */ + public void StatusILMAsync(TimedRequest request, RequestOptions options, + ActionListener listener) { + restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::statusILM, options, + StatusILMResponse::fromXContent, listener, emptySet()); + } + /** * Asynchronously stop the Index Lifecycle Management feature. * See 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 4fb4edcf1afdc..026f87bfc8cd5 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 @@ -1241,6 +1241,18 @@ static Request stopILM(StopILMRequest stopILMRequest) { return request; } + static Request statusILM(TimedRequest ilmStatusRequest){ + Request request = new Request(HttpGet.METHOD_NAME, + new EndpointBuilder() + .addPathPartAsIs("_ilm") + .addPathPartAsIs("status") + .build()); + Params params = new Params(request); + params.withMasterTimeout(ilmStatusRequest.masterNodeTimeout()); + params.withTimeout(ilmStatusRequest.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, diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/TimedRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/TimedRequest.java index 40388eae288e6..ba92e8dbee167 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/TimedRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/TimedRequest.java @@ -36,7 +36,6 @@ public class TimedRequest implements Validatable { public void setTimeout(TimeValue timeout) { this.timeout = timeout; - } public void setMasterTimeout(TimeValue masterTimeout) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/StatusILMResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/StatusILMResponse.java new file mode 100644 index 0000000000000..acdacfa10a4ee --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/StatusILMResponse.java @@ -0,0 +1,99 @@ +/* + * 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.indexlifecycle; + +import org.elasticsearch.action.admin.indices.shrink.ShrinkAction; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.util.EnumSet; +import java.util.Locale; +import java.util.Objects; + +/** + * The current status of index lifecycle management. See {@link OperationMode} for available statuses. + */ +public class StatusILMResponse { + + private final OperationMode operationMode; + @SuppressWarnings("unchecked") + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "operation_mode", a -> new StatusILMResponse((String) a[0])); + + static { + PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("operation_mode")); + } + + //package private for testing + StatusILMResponse(String operationMode) { + this.operationMode = OperationMode.fromString(operationMode); + } + + public OperationMode getOperationMode() { + return operationMode; + } + + public static StatusILMResponse fromXContent(XContentParser parser) { + return PARSER.apply(parser, null); + } + + /** + * Enum representing the different modes that Index Lifecycle Service can operate in. + */ + public enum OperationMode { + /** + * This represents a state where no policies are executed + */ + STOPPED, + + /** + * this represents a state where only sensitive actions (like {@link ShrinkAction}) will be executed + * until they finish, at which point the operation mode will move to STOPPED. + */ + STOPPING, + + /** + * Normal operation where all policies are executed as normal. + */ + RUNNING; + + static OperationMode fromString(String string) { + return EnumSet.allOf(OperationMode.class).stream() + .filter(e -> string.equalsIgnoreCase(e.name())).findFirst() + .orElseThrow(() -> new IllegalArgumentException(String.format(Locale.ENGLISH, "%s is not a valid operation_mode", string))); + } + } + + // generated + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + StatusILMResponse that = (StatusILMResponse) o; + return operationMode == that.operationMode; + } + + // generated + @Override + public int hashCode() { + return Objects.hash(operationMode); + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java index c2ce908efe3e3..7308dca03b70f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java @@ -22,11 +22,11 @@ import org.apache.http.HttpEntity; import org.apache.http.entity.ContentType; import org.apache.http.nio.entity.NStringEntity; -import org.apache.http.util.EntityUtils; import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest; import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest; +import org.elasticsearch.client.indexlifecycle.StatusILMResponse; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest; import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleResponse; @@ -169,35 +169,30 @@ public void testStartStopILM() throws Exception { createIndex("baz", Settings.builder().put("index.lifecycle.name", "eggplant").build()); createIndex("squash", Settings.EMPTY); - // TODO: NORELEASE convert this to using the high level client once - // there are APIs for it - Request statusReq = new Request("GET", "/_ilm/status"); - Response statusResponse = client().performRequest(statusReq); - String statusResponseString = EntityUtils.toString(statusResponse.getEntity()); - assertEquals("{\"operation_mode\":\"RUNNING\"}", statusResponseString); + TimedRequest statusRequest = new TimedRequest(); + StatusILMResponse statusResponse = execute(statusRequest, highLevelClient().indexLifecycle()::StatusILM, + highLevelClient().indexLifecycle()::StatusILMAsync); + assertEquals(statusResponse.getOperationMode(), StatusILMResponse.OperationMode.RUNNING); StopILMRequest stopReq = new StopILMRequest(); AcknowledgedResponse stopResponse = execute(stopReq, highLevelClient().indexLifecycle()::stopILM, highLevelClient().indexLifecycle()::stopILMAsync); assertTrue(stopResponse.isAcknowledged()); - // TODO: NORELEASE convert this to using the high level client once there are APIs for it - statusReq = new Request("GET", "/_ilm/status"); - statusResponse = client().performRequest(statusReq); - statusResponseString = EntityUtils.toString(statusResponse.getEntity()); - assertThat(statusResponseString, - Matchers.anyOf(equalTo("{\"operation_mode\":\"STOPPING\"}"), equalTo("{\"operation_mode\":\"STOPPED\"}"))); + + statusResponse = execute(statusRequest, highLevelClient().indexLifecycle()::StatusILM, + highLevelClient().indexLifecycle()::StatusILMAsync); + assertThat(statusResponse.getOperationMode(), + Matchers.anyOf(equalTo(StatusILMResponse.OperationMode.STOPPING), equalTo(StatusILMResponse.OperationMode.STOPPED))); StartILMRequest startReq = new StartILMRequest(); AcknowledgedResponse startResponse = execute(startReq, highLevelClient().indexLifecycle()::startILM, highLevelClient().indexLifecycle()::startILMAsync); assertTrue(startResponse.isAcknowledged()); - // TODO: NORELEASE convert this to using the high level client once there are APIs for it - statusReq = new Request("GET", "/_ilm/status"); - statusResponse = client().performRequest(statusReq); - statusResponseString = EntityUtils.toString(statusResponse.getEntity()); - assertEquals("{\"operation_mode\":\"RUNNING\"}", statusResponseString); + statusResponse = execute(statusRequest, highLevelClient().indexLifecycle()::StatusILM, + highLevelClient().indexLifecycle()::StatusILMAsync); + assertEquals(statusResponse.getOperationMode(), StatusILMResponse.OperationMode.RUNNING); } public void testExplainLifecycle() throws Exception { 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 13de640a91fe8..e050c0c91b289 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 @@ -2755,6 +2755,18 @@ public void testStopILM() throws Exception { assertThat(request.getParameters(), equalTo(expectedParams)); } + public void testStatusILM() throws Exception { + TimedRequest req = new TimedRequest(); + Map expectedParams = new HashMap<>(); + setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_TIMEOUT, expectedParams); + setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_MASTER_TIMEOUT, expectedParams); + + Request request = RequestConverters.statusILM(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); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/TimedRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/TimedRequestTests.java new file mode 100644 index 0000000000000..1faf14a9547b1 --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/TimedRequestTests.java @@ -0,0 +1,42 @@ +/* + * 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.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.test.ESTestCase; + +public class TimedRequestTests extends ESTestCase { + + public void testDefaults() { + TimedRequest timedRequest = new TimedRequest(); + assertEquals(timedRequest.timeout(), TimedRequest.DEFAULT_TIMEOUT); + assertEquals(timedRequest.masterNodeTimeout(), TimedRequest.DEFAULT_MASTER_TIMEOUT); + } + + public void testNonDefaults() { + TimedRequest timedRequest = new TimedRequest(); + TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(0, 1000)); + TimeValue masterTimeout = TimeValue.timeValueSeconds(randomIntBetween(0,1000)); + timedRequest.setTimeout(timeout); + timedRequest.setMasterTimeout(masterTimeout); + assertEquals(timedRequest.timeout(), timeout); + assertEquals(timedRequest.masterNodeTimeout(), masterTimeout); + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/StatusILMResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/StatusILMResponseTests.java new file mode 100644 index 0000000000000..46fb76749a206 --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/StatusILMResponseTests.java @@ -0,0 +1,66 @@ +/* + * 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.indexlifecycle; + +import org.elasticsearch.common.xcontent.DeprecationHandler; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.protocol.xpack.indexlifecycle.OperationMode; +import org.elasticsearch.test.ESTestCase; +import org.hamcrest.CoreMatchers; + +import java.io.IOException; +import java.util.EnumSet; +import java.util.stream.Collectors; + +public class StatusILMResponseTests extends ESTestCase { + + public void testClientServerStatuses() { + assertEquals( + EnumSet.allOf(StatusILMResponse.OperationMode.class).stream().map(Enum::name).collect(Collectors.toSet()), + EnumSet.allOf(OperationMode.class).stream().map(Enum::name).collect(Collectors.toSet())); + } + + public void testFromName() { + EnumSet.allOf(StatusILMResponse.OperationMode.class) + .forEach(e -> assertEquals(StatusILMResponse.OperationMode.fromString(e.name()), e)); + } + + public void testInvalidStatus() { + String invalidName = randomAlphaOfLength(10); + Exception e = expectThrows(IllegalArgumentException.class, () -> StatusILMResponse.OperationMode.fromString(invalidName)); + assertThat(e.getMessage(), CoreMatchers.containsString(invalidName + " is not a valid operation_mode")); + } + + public void testValidStatuses() { + EnumSet.allOf(StatusILMResponse.OperationMode.class) + .forEach(e -> assertEquals(new StatusILMResponse(e.name()).getOperationMode(), e)); + } + + public void testXContent() throws IOException { + XContentType xContentType = XContentType.JSON; + String mode = randomFrom(EnumSet.allOf(StatusILMResponse.OperationMode.class) + .stream().map(Enum::name).collect(Collectors.toList())); + XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, "{\"operation_mode\" : \"" + mode + "\"}"); + assertEquals(StatusILMResponse.fromXContent(parser).getOperationMode(), StatusILMResponse.OperationMode.fromString(mode)); + } +} From 916b535f608d2d0469762edd4ca4096d361c87e8 Mon Sep 17 00:00:00 2001 From: Jake Landis Date: Thu, 30 Aug 2018 15:58:32 -0500 Subject: [PATCH 2/6] review changes * Fixed incorrect method uppercase * Renamed statsILM -> lifecycleManagementStatus * removed "generated" comments --- .../client/IndexLifecycleClient.java | 12 +++++----- ...=> LifecycleManagementStatusResponse.java} | 14 +++++------ .../client/IndexLifecycleIT.java | 23 +++++++++++-------- ...fecycleManagementStatusResponseTests.java} | 20 ++++++++-------- 4 files changed, 36 insertions(+), 33 deletions(-) rename client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/{StatusILMResponse.java => LifecycleManagementStatusResponse.java} (85%) rename client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/{StatusILMResponseTests.java => LifecycleManagementStatusResponseTests.java} (69%) 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 01cf7b90d7f1a..ee59d957fed76 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 @@ -22,7 +22,7 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest; -import org.elasticsearch.client.indexlifecycle.StatusILMResponse; +import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusResponse; import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest; import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleResponse; import org.elasticsearch.protocol.xpack.indexlifecycle.SetIndexLifecyclePolicyRequest; @@ -148,9 +148,9 @@ public AcknowledgedResponse stopILM(StopILMRequest request, RequestOptions optio * @param request the request with user defined timeouts. * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized */ - public StatusILMResponse StatusILM(TimedRequest request, RequestOptions options) throws IOException { + public LifecycleManagementStatusResponse lifecycleManagementStatus(TimedRequest request, RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::statusILM, options, - StatusILMResponse::fromXContent, emptySet()); + LifecycleManagementStatusResponse::fromXContent, emptySet()); } /** @@ -162,10 +162,10 @@ public StatusILMResponse StatusILM(TimedRequest request, RequestOptions options) * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @param listener the listener to be notified upon request completion */ - public void StatusILMAsync(TimedRequest request, RequestOptions options, - ActionListener listener) { + public void lifecycleManagementStatusAsync(TimedRequest request, RequestOptions options, + ActionListener listener) { restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::statusILM, options, - StatusILMResponse::fromXContent, listener, emptySet()); + LifecycleManagementStatusResponse::fromXContent, listener, emptySet()); } /** diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/StatusILMResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponse.java similarity index 85% rename from client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/StatusILMResponse.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponse.java index acdacfa10a4ee..ac27c03234eb2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/StatusILMResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponse.java @@ -31,19 +31,19 @@ /** * The current status of index lifecycle management. See {@link OperationMode} for available statuses. */ -public class StatusILMResponse { +public class LifecycleManagementStatusResponse { private final OperationMode operationMode; @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "operation_mode", a -> new StatusILMResponse((String) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "operation_mode", a -> new LifecycleManagementStatusResponse((String) a[0])); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("operation_mode")); } //package private for testing - StatusILMResponse(String operationMode) { + LifecycleManagementStatusResponse(String operationMode) { this.operationMode = OperationMode.fromString(operationMode); } @@ -51,7 +51,7 @@ public OperationMode getOperationMode() { return operationMode; } - public static StatusILMResponse fromXContent(XContentParser parser) { + public static LifecycleManagementStatusResponse fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } @@ -82,16 +82,14 @@ static OperationMode fromString(String string) { } } - // generated @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - StatusILMResponse that = (StatusILMResponse) o; + LifecycleManagementStatusResponse that = (LifecycleManagementStatusResponse) o; return operationMode == that.operationMode; } - // generated @Override public int hashCode() { return Objects.hash(operationMode); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java index 7308dca03b70f..176bf44963b58 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java @@ -26,7 +26,7 @@ import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest; -import org.elasticsearch.client.indexlifecycle.StatusILMResponse; +import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusResponse; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest; import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleResponse; @@ -170,9 +170,11 @@ public void testStartStopILM() throws Exception { createIndex("squash", Settings.EMPTY); TimedRequest statusRequest = new TimedRequest(); - StatusILMResponse statusResponse = execute(statusRequest, highLevelClient().indexLifecycle()::StatusILM, - highLevelClient().indexLifecycle()::StatusILMAsync); - assertEquals(statusResponse.getOperationMode(), StatusILMResponse.OperationMode.RUNNING); + LifecycleManagementStatusResponse statusResponse = execute( + statusRequest, + highLevelClient().indexLifecycle()::lifecycleManagementStatus, + highLevelClient().indexLifecycle()::lifecycleManagementStatusAsync); + assertEquals(statusResponse.getOperationMode(), LifecycleManagementStatusResponse.OperationMode.RUNNING); StopILMRequest stopReq = new StopILMRequest(); AcknowledgedResponse stopResponse = execute(stopReq, highLevelClient().indexLifecycle()::stopILM, @@ -180,19 +182,20 @@ public void testStartStopILM() throws Exception { assertTrue(stopResponse.isAcknowledged()); - statusResponse = execute(statusRequest, highLevelClient().indexLifecycle()::StatusILM, - highLevelClient().indexLifecycle()::StatusILMAsync); + statusResponse = execute(statusRequest, highLevelClient().indexLifecycle()::lifecycleManagementStatus, + highLevelClient().indexLifecycle()::lifecycleManagementStatusAsync); assertThat(statusResponse.getOperationMode(), - Matchers.anyOf(equalTo(StatusILMResponse.OperationMode.STOPPING), equalTo(StatusILMResponse.OperationMode.STOPPED))); + Matchers.anyOf(equalTo(LifecycleManagementStatusResponse.OperationMode.STOPPING), + equalTo(LifecycleManagementStatusResponse.OperationMode.STOPPED))); StartILMRequest startReq = new StartILMRequest(); AcknowledgedResponse startResponse = execute(startReq, highLevelClient().indexLifecycle()::startILM, highLevelClient().indexLifecycle()::startILMAsync); assertTrue(startResponse.isAcknowledged()); - statusResponse = execute(statusRequest, highLevelClient().indexLifecycle()::StatusILM, - highLevelClient().indexLifecycle()::StatusILMAsync); - assertEquals(statusResponse.getOperationMode(), StatusILMResponse.OperationMode.RUNNING); + statusResponse = execute(statusRequest, highLevelClient().indexLifecycle()::lifecycleManagementStatus, + highLevelClient().indexLifecycle()::lifecycleManagementStatusAsync); + assertEquals(statusResponse.getOperationMode(), LifecycleManagementStatusResponse.OperationMode.RUNNING); } public void testExplainLifecycle() throws Exception { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/StatusILMResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponseTests.java similarity index 69% rename from client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/StatusILMResponseTests.java rename to client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponseTests.java index 46fb76749a206..039ce499bd093 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/StatusILMResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponseTests.java @@ -31,36 +31,38 @@ import java.util.EnumSet; import java.util.stream.Collectors; -public class StatusILMResponseTests extends ESTestCase { +public class LifecycleManagementStatusResponseTests extends ESTestCase { public void testClientServerStatuses() { assertEquals( - EnumSet.allOf(StatusILMResponse.OperationMode.class).stream().map(Enum::name).collect(Collectors.toSet()), + EnumSet.allOf(LifecycleManagementStatusResponse.OperationMode.class).stream().map(Enum::name).collect(Collectors.toSet()), EnumSet.allOf(OperationMode.class).stream().map(Enum::name).collect(Collectors.toSet())); } public void testFromName() { - EnumSet.allOf(StatusILMResponse.OperationMode.class) - .forEach(e -> assertEquals(StatusILMResponse.OperationMode.fromString(e.name()), e)); + EnumSet.allOf(LifecycleManagementStatusResponse.OperationMode.class) + .forEach(e -> assertEquals(LifecycleManagementStatusResponse.OperationMode.fromString(e.name()), e)); } public void testInvalidStatus() { String invalidName = randomAlphaOfLength(10); - Exception e = expectThrows(IllegalArgumentException.class, () -> StatusILMResponse.OperationMode.fromString(invalidName)); + Exception e = expectThrows(IllegalArgumentException.class, + () -> LifecycleManagementStatusResponse.OperationMode.fromString(invalidName)); assertThat(e.getMessage(), CoreMatchers.containsString(invalidName + " is not a valid operation_mode")); } public void testValidStatuses() { - EnumSet.allOf(StatusILMResponse.OperationMode.class) - .forEach(e -> assertEquals(new StatusILMResponse(e.name()).getOperationMode(), e)); + EnumSet.allOf(LifecycleManagementStatusResponse.OperationMode.class) + .forEach(e -> assertEquals(new LifecycleManagementStatusResponse(e.name()).getOperationMode(), e)); } public void testXContent() throws IOException { XContentType xContentType = XContentType.JSON; - String mode = randomFrom(EnumSet.allOf(StatusILMResponse.OperationMode.class) + String mode = randomFrom(EnumSet.allOf(LifecycleManagementStatusResponse.OperationMode.class) .stream().map(Enum::name).collect(Collectors.toList())); XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, "{\"operation_mode\" : \"" + mode + "\"}"); - assertEquals(StatusILMResponse.fromXContent(parser).getOperationMode(), StatusILMResponse.OperationMode.fromString(mode)); + assertEquals(LifecycleManagementStatusResponse.fromXContent(parser).getOperationMode(), + LifecycleManagementStatusResponse.OperationMode.fromString(mode)); } } From 8b06ccd6a61f5075ace5f8f60ffe3f5691a8ff47 Mon Sep 17 00:00:00 2001 From: Jake Landis Date: Fri, 31 Aug 2018 09:19:26 -0500 Subject: [PATCH 3/6] fix imports from merge --- .../test/java/org/elasticsearch/client/IndexLifecycleIT.java | 4 +++- .../LifecycleManagementStatusResponseTests.java | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java index 5c827c3f962d1..f7b8ac01ee9ed 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java @@ -24,14 +24,16 @@ import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.indexlifecycle.AllocateAction; import org.elasticsearch.client.indexlifecycle.DeleteAction; +import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest; import org.elasticsearch.client.indexlifecycle.ForceMergeAction; import org.elasticsearch.client.indexlifecycle.LifecycleAction; +import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusResponse; import org.elasticsearch.client.indexlifecycle.LifecyclePolicy; 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.settings.Settings; -import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest; import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleResponse; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponseTests.java index 039ce499bd093..c3cc76153eae4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponseTests.java @@ -23,7 +23,6 @@ import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.protocol.xpack.indexlifecycle.OperationMode; import org.elasticsearch.test.ESTestCase; import org.hamcrest.CoreMatchers; From 4f77a09a041725f03d6cffe0f8874700fae488bd Mon Sep 17 00:00:00 2001 From: Jake Landis Date: Fri, 31 Aug 2018 09:52:28 -0500 Subject: [PATCH 4/6] use o.e.c.i.OperationMode and remove inner class of the same name --- .../client/IndexLifecycleClient.java | 4 +-- .../client/RequestConverters.java | 6 ++-- .../LifecycleManagementStatusResponse.java | 30 ---------------- .../client/indexlifecycle/OperationMode.java | 9 +++++ .../client/IndexLifecycleIT.java | 9 ++--- .../client/RequestConvertersTests.java | 4 +-- ...ifecycleManagementStatusResponseTests.java | 36 +++++++------------ .../indexlifecycle/OperationModeTests.java | 13 +++++++ 8 files changed, 47 insertions(+), 64 deletions(-) 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 b71dc2c0de6ce..a551091b66198 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 @@ -179,7 +179,7 @@ public AcknowledgedResponse stopILM(StopILMRequest request, RequestOptions optio * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized */ public LifecycleManagementStatusResponse lifecycleManagementStatus(TimedRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::statusILM, options, + return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::lifecycleManagementStatus, options, LifecycleManagementStatusResponse::fromXContent, emptySet()); } @@ -194,7 +194,7 @@ public LifecycleManagementStatusResponse lifecycleManagementStatus(TimedRequest */ public void lifecycleManagementStatusAsync(TimedRequest request, RequestOptions options, ActionListener listener) { - restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::statusILM, options, + restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::lifecycleManagementStatus, options, LifecycleManagementStatusResponse::fromXContent, listener, emptySet()); } 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 26986f35e5be0..954dee004f113 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 @@ -1256,15 +1256,15 @@ static Request stopILM(StopILMRequest stopILMRequest) { return request; } - static Request statusILM(TimedRequest ilmStatusRequest){ + static Request lifecycleManagementStatus(TimedRequest lifecycleManagementStatusRequest){ Request request = new Request(HttpGet.METHOD_NAME, new EndpointBuilder() .addPathPartAsIs("_ilm") .addPathPartAsIs("status") .build()); Params params = new Params(request); - params.withMasterTimeout(ilmStatusRequest.masterNodeTimeout()); - params.withTimeout(ilmStatusRequest.timeout()); + params.withMasterTimeout(lifecycleManagementStatusRequest.masterNodeTimeout()); + params.withTimeout(lifecycleManagementStatusRequest.timeout()); return request; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponse.java index ac27c03234eb2..a5db0d2fbe175 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponse.java @@ -19,13 +19,10 @@ package org.elasticsearch.client.indexlifecycle; -import org.elasticsearch.action.admin.indices.shrink.ShrinkAction; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.XContentParser; -import java.util.EnumSet; -import java.util.Locale; import java.util.Objects; /** @@ -55,33 +52,6 @@ public static LifecycleManagementStatusResponse fromXContent(XContentParser pars return PARSER.apply(parser, null); } - /** - * Enum representing the different modes that Index Lifecycle Service can operate in. - */ - public enum OperationMode { - /** - * This represents a state where no policies are executed - */ - STOPPED, - - /** - * this represents a state where only sensitive actions (like {@link ShrinkAction}) will be executed - * until they finish, at which point the operation mode will move to STOPPED. - */ - STOPPING, - - /** - * Normal operation where all policies are executed as normal. - */ - RUNNING; - - static OperationMode fromString(String string) { - return EnumSet.allOf(OperationMode.class).stream() - .filter(e -> string.equalsIgnoreCase(e.name())).findFirst() - .orElseThrow(() -> new IllegalArgumentException(String.format(Locale.ENGLISH, "%s is not a valid operation_mode", string))); - } - } - @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/OperationMode.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/OperationMode.java index a0894ccc474fb..e253f4285ddef 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/OperationMode.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/OperationMode.java @@ -20,6 +20,9 @@ import org.elasticsearch.action.admin.indices.shrink.ShrinkAction; +import java.util.EnumSet; +import java.util.Locale; + /** * Enum representing the different modes that Index Lifecycle Service can operate in. */ @@ -56,4 +59,10 @@ public boolean isValidChange(OperationMode nextMode) { }; public abstract boolean isValidChange(OperationMode nextMode); + + static OperationMode fromString(String string) { + return EnumSet.allOf(OperationMode.class).stream() + .filter(e -> string.equalsIgnoreCase(e.name())).findFirst() + .orElseThrow(() -> new IllegalArgumentException(String.format(Locale.ENGLISH, "%s is not a valid operation_mode", string))); + } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java index f7b8ac01ee9ed..ca38d3437daf1 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java @@ -29,6 +29,7 @@ import org.elasticsearch.client.indexlifecycle.LifecycleAction; import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusResponse; import org.elasticsearch.client.indexlifecycle.LifecyclePolicy; +import org.elasticsearch.client.indexlifecycle.OperationMode; import org.elasticsearch.client.indexlifecycle.Phase; import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest; import org.elasticsearch.client.indexlifecycle.RolloverAction; @@ -93,7 +94,7 @@ public void testStartStopILM() throws Exception { statusRequest, highLevelClient().indexLifecycle()::lifecycleManagementStatus, highLevelClient().indexLifecycle()::lifecycleManagementStatusAsync); - assertEquals(statusResponse.getOperationMode(), LifecycleManagementStatusResponse.OperationMode.RUNNING); + assertEquals(statusResponse.getOperationMode(), OperationMode.RUNNING); StopILMRequest stopReq = new StopILMRequest(); AcknowledgedResponse stopResponse = execute(stopReq, highLevelClient().indexLifecycle()::stopILM, @@ -104,8 +105,8 @@ public void testStartStopILM() throws Exception { statusResponse = execute(statusRequest, highLevelClient().indexLifecycle()::lifecycleManagementStatus, highLevelClient().indexLifecycle()::lifecycleManagementStatusAsync); assertThat(statusResponse.getOperationMode(), - Matchers.anyOf(equalTo(LifecycleManagementStatusResponse.OperationMode.STOPPING), - equalTo(LifecycleManagementStatusResponse.OperationMode.STOPPED))); + Matchers.anyOf(equalTo(OperationMode.STOPPING), + equalTo(OperationMode.STOPPED))); StartILMRequest startReq = new StartILMRequest(); AcknowledgedResponse startResponse = execute(startReq, highLevelClient().indexLifecycle()::startILM, @@ -114,7 +115,7 @@ public void testStartStopILM() throws Exception { statusResponse = execute(statusRequest, highLevelClient().indexLifecycle()::lifecycleManagementStatus, highLevelClient().indexLifecycle()::lifecycleManagementStatusAsync); - assertEquals(statusResponse.getOperationMode(), LifecycleManagementStatusResponse.OperationMode.RUNNING); + assertEquals(statusResponse.getOperationMode(), OperationMode.RUNNING); } public void testExplainLifecycle() throws Exception { 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 ae0b7e839957f..1fd276acd2127 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 @@ -2772,13 +2772,13 @@ public void testStopILM() throws Exception { assertThat(request.getParameters(), equalTo(expectedParams)); } - public void testStatusILM() throws Exception { + public void testLifecycleManagementStatus() throws Exception { TimedRequest req = new TimedRequest(); Map expectedParams = new HashMap<>(); setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_TIMEOUT, expectedParams); setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_MASTER_TIMEOUT, expectedParams); - Request request = RequestConverters.statusILM(req); + Request request = RequestConverters.lifecycleManagementStatus(req); assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME)); assertThat(request.getEndpoint(), equalTo("/_ilm/status")); assertThat(request.getParameters(), equalTo(expectedParams)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponseTests.java index c3cc76153eae4..144039b8995c6 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponseTests.java @@ -32,36 +32,26 @@ public class LifecycleManagementStatusResponseTests extends ESTestCase { - public void testClientServerStatuses() { - assertEquals( - EnumSet.allOf(LifecycleManagementStatusResponse.OperationMode.class).stream().map(Enum::name).collect(Collectors.toSet()), - EnumSet.allOf(OperationMode.class).stream().map(Enum::name).collect(Collectors.toSet())); - } - - public void testFromName() { - EnumSet.allOf(LifecycleManagementStatusResponse.OperationMode.class) - .forEach(e -> assertEquals(LifecycleManagementStatusResponse.OperationMode.fromString(e.name()), e)); - } - - public void testInvalidStatus() { - String invalidName = randomAlphaOfLength(10); - Exception e = expectThrows(IllegalArgumentException.class, - () -> LifecycleManagementStatusResponse.OperationMode.fromString(invalidName)); - assertThat(e.getMessage(), CoreMatchers.containsString(invalidName + " is not a valid operation_mode")); - } - - public void testValidStatuses() { - EnumSet.allOf(LifecycleManagementStatusResponse.OperationMode.class) + public void testAllValidStatuses() { + EnumSet.allOf(OperationMode.class) .forEach(e -> assertEquals(new LifecycleManagementStatusResponse(e.name()).getOperationMode(), e)); } public void testXContent() throws IOException { XContentType xContentType = XContentType.JSON; - String mode = randomFrom(EnumSet.allOf(LifecycleManagementStatusResponse.OperationMode.class) + String mode = randomFrom(EnumSet.allOf(OperationMode.class) .stream().map(Enum::name).collect(Collectors.toList())); XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, "{\"operation_mode\" : \"" + mode + "\"}"); - assertEquals(LifecycleManagementStatusResponse.fromXContent(parser).getOperationMode(), - LifecycleManagementStatusResponse.OperationMode.fromString(mode)); + assertEquals(LifecycleManagementStatusResponse.fromXContent(parser).getOperationMode(), OperationMode.fromString(mode)); + } + + public void testXContentInvalid() throws IOException { + XContentType xContentType = XContentType.JSON; + String mode = randomAlphaOfLength(10); + XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, "{\"operation_mode\" : \"" + mode + "\"}"); + Exception e = expectThrows(IllegalArgumentException.class, () -> LifecycleManagementStatusResponse.fromXContent(parser)); + assertThat(e.getMessage(), CoreMatchers.containsString("failed to parse field [operation_mode]")); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/OperationModeTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/OperationModeTests.java index 873b864734c8d..27651ba4a8c41 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/OperationModeTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/OperationModeTests.java @@ -20,6 +20,9 @@ package org.elasticsearch.client.indexlifecycle; import org.elasticsearch.test.ESTestCase; +import org.hamcrest.CoreMatchers; + +import java.util.EnumSet; public class OperationModeTests extends ESTestCase { @@ -36,4 +39,14 @@ public void testIsValidChange() { assertFalse(OperationMode.STOPPED.isValidChange(OperationMode.STOPPING)); assertFalse(OperationMode.STOPPED.isValidChange(OperationMode.STOPPED)); } + + public void testFromName() { + EnumSet.allOf(OperationMode.class).forEach(e -> assertEquals(OperationMode.fromString(e.name()), e)); + } + + public void testFromNameInvalid() { + String invalidName = randomAlphaOfLength(10); + Exception e = expectThrows(IllegalArgumentException.class, () -> OperationMode.fromString(invalidName)); + assertThat(e.getMessage(), CoreMatchers.containsString(invalidName + " is not a valid operation_mode")); + } } From d755bd04fc55bd09f498723375befe34ee04bb1e Mon Sep 17 00:00:00 2001 From: Jake Landis Date: Tue, 4 Sep 2018 12:06:02 -0500 Subject: [PATCH 5/6] added LifecycleManagementStatusRequest removed direct usage of TimedRequest --- .../client/IndexLifecycleClient.java | 10 ++++--- .../client/RequestConverters.java | 3 +- .../elasticsearch/client/TimedRequest.java | 2 +- .../LifecycleManagementStatusRequest.java | 28 +++++++++++++++++++ .../client/IndexLifecycleIT.java | 3 +- .../client/RequestConvertersTests.java | 3 +- .../client/TimedRequestTests.java | 4 +-- 7 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusRequest.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 a551091b66198..67c0b43b91ee8 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 @@ -22,6 +22,7 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest; +import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest; import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusResponse; import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest; import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest; @@ -175,10 +176,11 @@ public AcknowledgedResponse stopILM(StopILMRequest request, RequestOptions optio * See * the docs for more. * - * @param request the request with user defined timeouts. + * @param request the request * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized */ - public LifecycleManagementStatusResponse lifecycleManagementStatus(TimedRequest request, RequestOptions options) throws IOException { + public LifecycleManagementStatusResponse lifecycleManagementStatus(LifecycleManagementStatusRequest request, RequestOptions options) + throws IOException { return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::lifecycleManagementStatus, options, LifecycleManagementStatusResponse::fromXContent, emptySet()); } @@ -188,11 +190,11 @@ public LifecycleManagementStatusResponse lifecycleManagementStatus(TimedRequest * See * the docs for more. * - * @param request the request with user defined timeouts. + * @param request the request * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @param listener the listener to be notified upon request completion */ - public void lifecycleManagementStatusAsync(TimedRequest request, RequestOptions options, + public void lifecycleManagementStatusAsync(LifecycleManagementStatusRequest request, RequestOptions options, ActionListener listener) { restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::lifecycleManagementStatus, options, LifecycleManagementStatusResponse::fromXContent, listener, emptySet()); 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 b514498966248..e9ab9819e3e71 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 @@ -88,6 +88,7 @@ import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.action.update.UpdateRequest; +import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest; import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest; import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest; import org.elasticsearch.client.security.RefreshPolicy; @@ -1285,7 +1286,7 @@ static Request stopILM(StopILMRequest stopILMRequest) { return request; } - static Request lifecycleManagementStatus(TimedRequest lifecycleManagementStatusRequest){ + static Request lifecycleManagementStatus(LifecycleManagementStatusRequest lifecycleManagementStatusRequest){ Request request = new Request(HttpGet.METHOD_NAME, new EndpointBuilder() .addPathPartAsIs("_ilm") diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/TimedRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/TimedRequest.java index ba92e8dbee167..ee0eb8a46b920 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/TimedRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/TimedRequest.java @@ -26,7 +26,7 @@ * Please note, any requests that use a ackTimeout should set timeout as they * represent the same backing field on the server. */ -public class TimedRequest implements Validatable { +public abstract class TimedRequest implements Validatable { public static final TimeValue DEFAULT_TIMEOUT = TimeValue.timeValueSeconds(30); public static final TimeValue DEFAULT_MASTER_TIMEOUT = TimeValue.timeValueSeconds(30); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusRequest.java new file mode 100644 index 0000000000000..5db3d2d8c4e11 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusRequest.java @@ -0,0 +1,28 @@ +/* + * 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.indexlifecycle; + +import org.elasticsearch.client.TimedRequest; + +/** + * A {@link TimedRequest} to get the current status of index lifecycle management. + */ +public class LifecycleManagementStatusRequest extends TimedRequest { +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java index ca38d3437daf1..62eb769414e42 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java @@ -27,6 +27,7 @@ import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest; import org.elasticsearch.client.indexlifecycle.ForceMergeAction; import org.elasticsearch.client.indexlifecycle.LifecycleAction; +import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest; import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusResponse; import org.elasticsearch.client.indexlifecycle.LifecyclePolicy; import org.elasticsearch.client.indexlifecycle.OperationMode; @@ -89,7 +90,7 @@ public void testStartStopILM() throws Exception { createIndex("baz", Settings.builder().put("index.lifecycle.name", "eggplant").build()); createIndex("squash", Settings.EMPTY); - TimedRequest statusRequest = new TimedRequest(); + LifecycleManagementStatusRequest statusRequest = new LifecycleManagementStatusRequest(); LifecycleManagementStatusResponse statusResponse = execute( statusRequest, highLevelClient().indexLifecycle()::lifecycleManagementStatus, 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 f17997a05e167..23fd4575d6f85 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 @@ -97,6 +97,7 @@ import org.elasticsearch.action.support.replication.ReplicationRequest; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.client.RequestConverters.EndpointBuilder; +import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest; import org.elasticsearch.client.indexlifecycle.LifecyclePolicy; import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest; import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest; @@ -2829,7 +2830,7 @@ public void testStopILM() throws Exception { } public void testLifecycleManagementStatus() throws Exception { - TimedRequest req = new TimedRequest(); + LifecycleManagementStatusRequest req = new LifecycleManagementStatusRequest(); Map expectedParams = new HashMap<>(); setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_TIMEOUT, expectedParams); setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_MASTER_TIMEOUT, expectedParams); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/TimedRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/TimedRequestTests.java index 1faf14a9547b1..34f477eff7f8c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/TimedRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/TimedRequestTests.java @@ -25,13 +25,13 @@ public class TimedRequestTests extends ESTestCase { public void testDefaults() { - TimedRequest timedRequest = new TimedRequest(); + TimedRequest timedRequest = new TimedRequest(){}; assertEquals(timedRequest.timeout(), TimedRequest.DEFAULT_TIMEOUT); assertEquals(timedRequest.masterNodeTimeout(), TimedRequest.DEFAULT_MASTER_TIMEOUT); } public void testNonDefaults() { - TimedRequest timedRequest = new TimedRequest(); + TimedRequest timedRequest = new TimedRequest(){}; TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(0, 1000)); TimeValue masterTimeout = TimeValue.timeValueSeconds(randomIntBetween(0,1000)); timedRequest.setTimeout(timeout); From 80462d54f0590a43d4294b9e17f4de0016351ee2 Mon Sep 17 00:00:00 2001 From: Jake Landis Date: Wed, 5 Sep 2018 13:07:13 -0500 Subject: [PATCH 6/6] use Locale.ROOT and string constant --- .../indexlifecycle/LifecycleManagementStatusResponse.java | 5 +++-- .../elasticsearch/client/indexlifecycle/OperationMode.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponse.java index a5db0d2fbe175..c1586d7e1c738 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/LifecycleManagementStatusResponse.java @@ -31,12 +31,13 @@ public class LifecycleManagementStatusResponse { private final OperationMode operationMode; + private static final String OPERATION_MODE = "operation_mode"; @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "operation_mode", a -> new LifecycleManagementStatusResponse((String) a[0])); + OPERATION_MODE, a -> new LifecycleManagementStatusResponse((String) a[0])); static { - PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("operation_mode")); + PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField(OPERATION_MODE)); } //package private for testing diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/OperationMode.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/OperationMode.java index e253f4285ddef..81634e5824ec8 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/OperationMode.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/OperationMode.java @@ -63,6 +63,6 @@ public boolean isValidChange(OperationMode nextMode) { static OperationMode fromString(String string) { return EnumSet.allOf(OperationMode.class).stream() .filter(e -> string.equalsIgnoreCase(e.name())).findFirst() - .orElseThrow(() -> new IllegalArgumentException(String.format(Locale.ENGLISH, "%s is not a valid operation_mode", string))); + .orElseThrow(() -> new IllegalArgumentException(String.format(Locale.ROOT, "%s is not a valid operation_mode", string))); } }