From 0b7d18d7330e9e9cf4274bdd4be9c487d3ddd72e Mon Sep 17 00:00:00 2001 From: Luca Cavanna Date: Tue, 13 Nov 2018 15:52:59 +0100 Subject: [PATCH] HLRC: add support for get license basic/trial status API (#33176) Relates to #29827 --- .../elasticsearch/client/LicenseClient.java | 24 ++++++ .../client/LicenseRequestConverters.java | 8 ++ .../org/elasticsearch/client/Validatable.java | 3 + .../license/GetBasicStatusResponse.java | 79 ++++++++++++++++++ .../license/GetTrialStatusResponse.java | 80 +++++++++++++++++++ .../org/elasticsearch/client/LicenseIT.java | 12 +++ .../client/LicenseRequestConvertersTests.java | 16 ++++ .../client/RestHighLevelClientTests.java | 15 ++-- .../LicensingDocumentationIT.java | 28 +++++++ .../licensing/get-basic-status.asciidoc | 23 ++++++ .../licensing/get-trial-status.asciidoc | 23 ++++++ .../high-level/supported-apis.asciidoc | 4 + .../license/GetBasicStatusResponse.java | 32 +++++++- .../license/GetTrialStatusResponse.java | 32 +++++++- .../license/RestGetBasicStatus.java | 21 +---- .../license/RestGetTrialStatus.java | 21 +---- ...bstractHlrcStreamableXContentTestCase.java | 6 ++ .../license/GetBasicStatusResponseTests.java | 34 ++++++++ .../license/GetTrialStatusResponseTests.java | 34 ++++++++ 19 files changed, 448 insertions(+), 47 deletions(-) create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetBasicStatusResponse.java create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetTrialStatusResponse.java create mode 100644 docs/java-rest/high-level/licensing/get-basic-status.asciidoc create mode 100644 docs/java-rest/high-level/licensing/get-trial-status.asciidoc create mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/GetBasicStatusResponseTests.java create mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/GetTrialStatusResponseTests.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseClient.java index 6eda4d81fa4..0f74a003315 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseClient.java @@ -26,6 +26,8 @@ import org.elasticsearch.client.license.StartTrialRequest; import org.elasticsearch.client.license.StartTrialResponse; import org.elasticsearch.client.license.StartBasicRequest; import org.elasticsearch.client.license.StartBasicResponse; +import org.elasticsearch.client.license.GetBasicStatusResponse; +import org.elasticsearch.client.license.GetTrialStatusResponse; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.xcontent.DeprecationHandler; @@ -172,6 +174,28 @@ public final class LicenseClient { StartBasicResponse::fromXContent, listener, emptySet()); } + /** + * Retrieve the license trial status + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @return the response + * @throws IOException in case there is a problem sending the request or parsing back the response + */ + public GetTrialStatusResponse getTrialStatus(RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(Validatable.EMPTY, + request -> LicenseRequestConverters.getLicenseTrialStatus(), options, GetTrialStatusResponse::fromXContent, emptySet()); + } + + /** + * Retrieve the license basic status + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @return the response + * @throws IOException in case there is a problem sending the request or parsing back the response + */ + public GetBasicStatusResponse getBasicStatus(RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(Validatable.EMPTY, + request -> LicenseRequestConverters.getLicenseBasicStatus(), options, GetBasicStatusResponse::fromXContent, emptySet()); + } + /** * Converts an entire response into a json string * diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseRequestConverters.java index 7bda5f552ff..e072dd945d0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseRequestConverters.java @@ -88,4 +88,12 @@ final class LicenseRequestConverters { } return request; } + + static Request getLicenseTrialStatus() { + return new Request(HttpGet.METHOD_NAME, "/_xpack/license/trial_status"); + } + + static Request getLicenseBasicStatus() { + return new Request(HttpGet.METHOD_NAME, "/_xpack/license/basic_status"); + } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/Validatable.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/Validatable.java index fe4a1fc42cb..cfc2f40c6bb 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/Validatable.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/Validatable.java @@ -24,6 +24,9 @@ import java.util.Optional; * Defines a validation layer for Requests. */ public interface Validatable { + + Validatable EMPTY = new Validatable() {}; + /** * Perform validation. This method does not have to be overridden in the event that no validation needs to be done, * or the validation was done during object construction time. A {@link ValidationException} that is not null is diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetBasicStatusResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetBasicStatusResponse.java new file mode 100644 index 00000000000..436012e39a1 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetBasicStatusResponse.java @@ -0,0 +1,79 @@ +/* + * 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.license; + +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; +import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.util.Objects; + +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; + +/** + * Response class for license get basic status API + */ +public class GetBasicStatusResponse { + + private static final ParseField ELIGIBLE_TO_START_BASIC = new ParseField("eligible_to_start_basic"); + + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_basic_status_response", true, a -> new GetBasicStatusResponse((boolean) a[0])); + + static { + PARSER.declareField(constructorArg(), (parser, context) -> parser.booleanValue(), ELIGIBLE_TO_START_BASIC, + ObjectParser.ValueType.BOOLEAN); + } + + private final boolean eligibleToStartBasic; + + GetBasicStatusResponse(boolean eligibleToStartBasic) { + this.eligibleToStartBasic = eligibleToStartBasic; + } + + /** + * Returns whether the license is eligible to start basic or not + */ + public boolean isEligibleToStartBasic() { + return eligibleToStartBasic; + } + + public static GetBasicStatusResponse fromXContent(XContentParser parser) { + return PARSER.apply(parser, null); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + GetBasicStatusResponse that = (GetBasicStatusResponse) o; + return eligibleToStartBasic == that.eligibleToStartBasic; + } + + @Override + public int hashCode() { + return Objects.hash(eligibleToStartBasic); + } +} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetTrialStatusResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetTrialStatusResponse.java new file mode 100644 index 00000000000..baefe806184 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetTrialStatusResponse.java @@ -0,0 +1,80 @@ +/* + * 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.license; + +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; +import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.util.Objects; + +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; + +/** + * Response class for license get trial status API + */ +public class GetTrialStatusResponse { + + private static final ParseField ELIGIBLE_TO_START_TRIAL = new ParseField("eligible_to_start_trial"); + + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_trial_status_response", true, a -> new GetTrialStatusResponse((boolean) a[0])); + + static { + PARSER.declareField(constructorArg(), (parser, context) -> parser.booleanValue(), ELIGIBLE_TO_START_TRIAL, + ObjectParser.ValueType.BOOLEAN); + } + + private final boolean eligibleToStartTrial; + + GetTrialStatusResponse(boolean eligibleToStartTrial) { + this.eligibleToStartTrial = eligibleToStartTrial; + } + + /** + * Returns whether the license is eligible to start trial or not + */ + public boolean isEligibleToStartTrial() { + return eligibleToStartTrial; + } + + public static GetTrialStatusResponse fromXContent(XContentParser parser) { + return PARSER.apply(parser, null); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + GetTrialStatusResponse that = (GetTrialStatusResponse) o; + return eligibleToStartTrial == that.eligibleToStartTrial; + } + + @Override + public int hashCode() { + return Objects.hash(eligibleToStartTrial); + } + +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseIT.java index 151e18f01e7..caa0ba4e8ad 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseIT.java @@ -22,8 +22,10 @@ package org.elasticsearch.client; import org.elasticsearch.Build; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.license.DeleteLicenseRequest; +import org.elasticsearch.client.license.GetBasicStatusResponse; import org.elasticsearch.client.license.GetLicenseRequest; import org.elasticsearch.client.license.GetLicenseResponse; +import org.elasticsearch.client.license.GetTrialStatusResponse; import org.elasticsearch.client.license.LicensesStatus; import org.elasticsearch.client.license.PutLicenseRequest; import org.elasticsearch.client.license.PutLicenseResponse; @@ -198,4 +200,14 @@ public class LicenseIT extends ESRestHighLevelClientTestCase { final AcknowledgedResponse response = highLevelClient().license().deleteLicense(request, RequestOptions.DEFAULT); assertThat(response.isAcknowledged(), equalTo(true)); } + + public void testGetTrialStatus() throws IOException { + GetTrialStatusResponse trialStatus = highLevelClient().license().getTrialStatus(RequestOptions.DEFAULT); + assertFalse(trialStatus.isEligibleToStartTrial()); + } + + public void testGetBasicStatus() throws IOException { + GetBasicStatusResponse basicStatus = highLevelClient().license().getBasicStatus(RequestOptions.DEFAULT); + assertTrue(basicStatus.isEligibleToStartBasic()); + } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseRequestConvertersTests.java index 731764785eb..52844bee53a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseRequestConvertersTests.java @@ -128,4 +128,20 @@ public class LicenseRequestConvertersTests extends ESTestCase { assertThat(request.getParameters(), equalTo(expectedParams)); assertThat(request.getEntity(), is(nullValue())); } + + public void testGetLicenseTrialStatus() { + Request request = LicenseRequestConverters.getLicenseTrialStatus(); + assertEquals(HttpGet.METHOD_NAME, request.getMethod()); + assertEquals("/_xpack/license/trial_status", request.getEndpoint()); + assertEquals(request.getParameters().size(), 0); + assertNull(request.getEntity()); + } + + public void testGetLicenseBasicStatus() { + Request request = LicenseRequestConverters.getLicenseBasicStatus(); + assertEquals(HttpGet.METHOD_NAME, request.getMethod()); + assertEquals("/_xpack/license/basic_status", request.getEndpoint()); + assertEquals(request.getParameters().size(), 0); + assertNull(request.getEntity()); + } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java index 62e6afe6ffb..4d7ce88f991 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java @@ -736,7 +736,6 @@ public class RestHighLevelClientTests extends ESTestCase { assertSubmitTaskMethod(methods, method, apiName, restSpec); } else { assertSyncMethod(method, apiName); - boolean remove = apiSpec.remove(apiName); if (remove == false) { if (deprecatedMethods.contains(apiName)) { @@ -771,7 +770,7 @@ public class RestHighLevelClientTests extends ESTestCase { assertThat("Some API are not supported but they should be: " + apiSpec, apiSpec.size(), equalTo(0)); } - private void assertSyncMethod(Method method, String apiName) { + private static void assertSyncMethod(Method method, String apiName) { //A few methods return a boolean rather than a response object if (apiName.equals("ping") || apiName.contains("exist")) { assertThat("the return type for method [" + method + "] is incorrect", @@ -787,7 +786,8 @@ public class RestHighLevelClientTests extends ESTestCase { assertEquals("incorrect number of exceptions for method [" + method + "]", 1, method.getExceptionTypes().length); //a few methods don't accept a request object as argument if (apiName.equals("ping") || apiName.equals("info") || apiName.equals("security.get_ssl_certificates") - || apiName.equals("security.authenticate")) { + || apiName.equals("security.authenticate") || apiName.equals("license.get_trial_status") + || apiName.equals("license.get_basic_status")) { assertEquals("incorrect number of arguments for method [" + method + "]", 1, method.getParameterTypes().length); assertThat("the parameter to method [" + method + "] is the wrong type", method.getParameterTypes()[0], equalTo(RequestOptions.class)); @@ -800,7 +800,7 @@ public class RestHighLevelClientTests extends ESTestCase { } } - private void assertAsyncMethod(Map methods, Method method, String apiName) { + private static void assertAsyncMethod(Map methods, Method method, String apiName) { assertTrue("async method [" + method.getName() + "] doesn't have corresponding sync method", methods.containsKey(apiName.substring(0, apiName.length() - 6))); assertThat("async method [" + method + "] should return void", method.getReturnType(), equalTo(Void.TYPE)); @@ -820,7 +820,8 @@ public class RestHighLevelClientTests extends ESTestCase { } } - private void assertSubmitTaskMethod(Map methods, Method method, String apiName, ClientYamlSuiteRestSpec restSpec) { + private static void assertSubmitTaskMethod(Map methods, Method method, String apiName, + ClientYamlSuiteRestSpec restSpec) { String methodName = extractMethodName(apiName); assertTrue("submit task method [" + method.getName() + "] doesn't have corresponding sync method", methods.containsKey(methodName)); @@ -834,11 +835,11 @@ public class RestHighLevelClientTests extends ESTestCase { restSpec.getApi(methodName).getParams(), Matchers.hasKey("wait_for_completion")); } - private String extractMethodName(String apiName) { + private static String extractMethodName(String apiName) { return apiName.substring(SUBMIT_TASK_PREFIX.length(), apiName.length() - SUBMIT_TASK_SUFFIX.length()); } - private boolean isSubmitTaskMethod(String apiName) { + private static boolean isSubmitTaskMethod(String apiName) { return apiName.startsWith(SUBMIT_TASK_PREFIX) && apiName.endsWith(SUBMIT_TASK_SUFFIX); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java index 325c1dbb082..aab21fb14df 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java @@ -30,6 +30,8 @@ import org.elasticsearch.client.license.StartTrialRequest; import org.elasticsearch.client.license.StartTrialResponse; import org.elasticsearch.client.license.StartBasicRequest; import org.elasticsearch.client.license.StartBasicResponse; +import org.elasticsearch.client.license.GetBasicStatusResponse; +import org.elasticsearch.client.license.GetTrialStatusResponse; import org.elasticsearch.common.Booleans; import org.junit.After; import org.junit.BeforeClass; @@ -336,4 +338,30 @@ public class LicensingDocumentationIT extends ESRestHighLevelClientTestCase { assertTrue(latch.await(30L, TimeUnit.SECONDS)); } } + + public void testGetTrialStatus() throws IOException { + RestHighLevelClient client = highLevelClient(); + { + //tag::get-trial-status-execute + GetTrialStatusResponse response = client.license().getTrialStatus(RequestOptions.DEFAULT); + //end::get-trial-status-execute + + //tag::get-trial-status-response + boolean eligibleToStartTrial = response.isEligibleToStartTrial(); // <1> + //end::get-trial-status-response + } + } + + public void testGetBasicStatus() throws IOException { + RestHighLevelClient client = highLevelClient(); + { + //tag::get-basic-status-execute + GetBasicStatusResponse response = client.license().getBasicStatus(RequestOptions.DEFAULT); + //end::get-basic-status-execute + + //tag::get-basic-status-response + boolean eligibleToStartbasic = response.isEligibleToStartBasic(); // <1> + //end::get-basic-status-response + } + } } diff --git a/docs/java-rest/high-level/licensing/get-basic-status.asciidoc b/docs/java-rest/high-level/licensing/get-basic-status.asciidoc new file mode 100644 index 00000000000..ffca094a592 --- /dev/null +++ b/docs/java-rest/high-level/licensing/get-basic-status.asciidoc @@ -0,0 +1,23 @@ +[[java-rest-high-get-basic-status]] +=== Get Basic Status + +[[java-rest-high-get-basic-status-execution]] +==== Execution + +The basic status of the license can be retrieved as follows: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/LicensingDocumentationIT.java[get-basic-status-execute] +-------------------------------------------------- + +[[java-rest-high-get-basic-status-response]] +==== Response + +The returned `GetTrialStatusResponse` holds only a `boolean` flag: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/LicensingDocumentationIT.java[get-basic-status-response] +-------------------------------------------------- +<1> Whether the license is eligible to start basic or not diff --git a/docs/java-rest/high-level/licensing/get-trial-status.asciidoc b/docs/java-rest/high-level/licensing/get-trial-status.asciidoc new file mode 100644 index 00000000000..f117f9aa838 --- /dev/null +++ b/docs/java-rest/high-level/licensing/get-trial-status.asciidoc @@ -0,0 +1,23 @@ +[[java-rest-high-get-trial-status]] +=== Get Trial Status + +[[java-rest-high-get-trial-status-execution]] +==== Execution + +The trial status of the license can be retrieved as follows: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/LicensingDocumentationIT.java[get-trial-status-execute] +-------------------------------------------------- + +[[java-rest-high-get-trial-status-response]] +==== Response + +The returned `GetTrialStatusResponse` holds only a `boolean` flag: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/LicensingDocumentationIT.java[get-trial-status-response] +-------------------------------------------------- +<1> Whether the license is eligible to start trial or not diff --git a/docs/java-rest/high-level/supported-apis.asciidoc b/docs/java-rest/high-level/supported-apis.asciidoc index dedd8326e21..9f3674e224b 100644 --- a/docs/java-rest/high-level/supported-apis.asciidoc +++ b/docs/java-rest/high-level/supported-apis.asciidoc @@ -220,12 +220,16 @@ The Java High Level REST Client supports the following Licensing APIs: * <> * <> * <> +* <> +* <> include::licensing/put-license.asciidoc[] include::licensing/get-license.asciidoc[] include::licensing/delete-license.asciidoc[] include::licensing/start-trial.asciidoc[] include::licensing/start-basic.asciidoc[] +include::licensing/get-trial-status.asciidoc[] +include::licensing/get-basic-status.asciidoc[] == Machine Learning APIs :upid: {mainid}-x-pack-ml diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetBasicStatusResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetBasicStatusResponse.java index 2f1cb7be378..21f8c51afa6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetBasicStatusResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetBasicStatusResponse.java @@ -8,17 +8,20 @@ package org.elasticsearch.license; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; +import java.util.Objects; -class GetBasicStatusResponse extends ActionResponse { +public class GetBasicStatusResponse extends ActionResponse implements ToXContentObject { private boolean eligibleToStartBasic; GetBasicStatusResponse() { } - GetBasicStatusResponse(boolean eligibleToStartBasic) { + public GetBasicStatusResponse(boolean eligibleToStartBasic) { this.eligibleToStartBasic = eligibleToStartBasic; } @@ -35,4 +38,29 @@ class GetBasicStatusResponse extends ActionResponse { public void writeTo(StreamOutput out) throws IOException { out.writeBoolean(eligibleToStartBasic); } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + builder.field("eligible_to_start_basic", eligibleToStartBasic); + builder.endObject(); + return builder; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + GetBasicStatusResponse that = (GetBasicStatusResponse) o; + return eligibleToStartBasic == that.eligibleToStartBasic; + } + + @Override + public int hashCode() { + return Objects.hash(eligibleToStartBasic); + } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetTrialStatusResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetTrialStatusResponse.java index 6712e68efae..1f6c28b3603 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetTrialStatusResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetTrialStatusResponse.java @@ -8,17 +8,20 @@ package org.elasticsearch.license; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; +import java.util.Objects; -class GetTrialStatusResponse extends ActionResponse { +public class GetTrialStatusResponse extends ActionResponse implements ToXContentObject { private boolean eligibleToStartTrial; GetTrialStatusResponse() { } - GetTrialStatusResponse(boolean eligibleToStartTrial) { + public GetTrialStatusResponse(boolean eligibleToStartTrial) { this.eligibleToStartTrial = eligibleToStartTrial; } @@ -35,4 +38,29 @@ class GetTrialStatusResponse extends ActionResponse { public void writeTo(StreamOutput out) throws IOException { out.writeBoolean(eligibleToStartTrial); } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + GetTrialStatusResponse that = (GetTrialStatusResponse) o; + return eligibleToStartTrial == that.eligibleToStartTrial; + } + + @Override + public int hashCode() { + return Objects.hash(eligibleToStartTrial); + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + builder.field("eligible_to_start_trial", eligibleToStartTrial); + builder.endObject(); + return builder; + } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetBasicStatus.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetBasicStatus.java index 7490ae74f1e..ab17371cd0f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetBasicStatus.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetBasicStatus.java @@ -6,18 +6,12 @@ package org.elasticsearch.license; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.RestResponse; -import org.elasticsearch.rest.RestStatus; -import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xpack.core.XPackClient; import org.elasticsearch.xpack.core.rest.XPackRestHandler; -import java.io.IOException; - import static org.elasticsearch.rest.RestRequest.Method.GET; public class RestGetBasicStatus extends XPackRestHandler { @@ -28,17 +22,8 @@ public class RestGetBasicStatus extends XPackRestHandler { } @Override - protected RestChannelConsumer doPrepareRequest(RestRequest request, XPackClient client) throws IOException { - return channel -> client.licensing().prepareGetStartBasic().execute( - new RestBuilderListener(channel) { - @Override - public RestResponse buildResponse(GetBasicStatusResponse response, XContentBuilder builder) throws Exception { - builder.startObject(); - builder.field("eligible_to_start_basic", response.isEligibleToStartBasic()); - builder.endObject(); - return new BytesRestResponse(RestStatus.OK, builder); - } - }); + protected RestChannelConsumer doPrepareRequest(RestRequest request, XPackClient client) { + return channel -> client.licensing().prepareGetStartBasic().execute(new RestToXContentListener<>(channel)); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetTrialStatus.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetTrialStatus.java index a136f2a88a6..1e54c559d62 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetTrialStatus.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetTrialStatus.java @@ -6,18 +6,12 @@ package org.elasticsearch.license; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.RestResponse; -import org.elasticsearch.rest.RestStatus; -import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xpack.core.XPackClient; import org.elasticsearch.xpack.core.rest.XPackRestHandler; -import java.io.IOException; - import static org.elasticsearch.rest.RestRequest.Method.GET; public class RestGetTrialStatus extends XPackRestHandler { @@ -28,17 +22,8 @@ public class RestGetTrialStatus extends XPackRestHandler { } @Override - protected RestChannelConsumer doPrepareRequest(RestRequest request, XPackClient client) throws IOException { - return channel -> client.licensing().prepareGetStartTrial().execute( - new RestBuilderListener(channel) { - @Override - public RestResponse buildResponse(GetTrialStatusResponse response, XContentBuilder builder) throws Exception { - builder.startObject(); - builder.field("eligible_to_start_trial", response.isEligibleToStartTrial()); - builder.endObject(); - return new BytesRestResponse(RestStatus.OK, builder); - } - }); + protected RestChannelConsumer doPrepareRequest(RestRequest request, XPackClient client) { + return channel -> client.licensing().prepareGetStartTrial().execute(new RestToXContentListener<>(channel)); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java index d81fd5aed79..5efcb7c8245 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java @@ -43,4 +43,10 @@ public abstract class AbstractHlrcStreamableXContentTestCase { + @Override + public GetBasicStatusResponse doHlrcParseInstance(XContentParser parser) { + return GetBasicStatusResponse.fromXContent(parser); + } + + @Override + public org.elasticsearch.license.GetBasicStatusResponse convertHlrcToInternal(GetBasicStatusResponse instance) { + return new org.elasticsearch.license.GetBasicStatusResponse(instance.isEligibleToStartBasic()); + } + + @Override + protected org.elasticsearch.license.GetBasicStatusResponse createBlankInstance() { + return new org.elasticsearch.license.GetBasicStatusResponse(false); + } + + @Override + protected org.elasticsearch.license.GetBasicStatusResponse createTestInstance() { + return new org.elasticsearch.license.GetBasicStatusResponse(randomBoolean()); + } +} diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/GetTrialStatusResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/GetTrialStatusResponseTests.java new file mode 100644 index 00000000000..504f5f83b51 --- /dev/null +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/GetTrialStatusResponseTests.java @@ -0,0 +1,34 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.protocol.xpack.license; + +import org.elasticsearch.client.license.GetTrialStatusResponse; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.protocol.AbstractHlrcStreamableXContentTestCase; + +public class GetTrialStatusResponseTests extends + AbstractHlrcStreamableXContentTestCase { + + @Override + public GetTrialStatusResponse doHlrcParseInstance(XContentParser parser) { + return GetTrialStatusResponse.fromXContent(parser); + } + + @Override + public org.elasticsearch.license.GetTrialStatusResponse convertHlrcToInternal(GetTrialStatusResponse instance) { + return new org.elasticsearch.license.GetTrialStatusResponse(instance.isEligibleToStartTrial()); + } + + @Override + protected org.elasticsearch.license.GetTrialStatusResponse createBlankInstance() { + return new org.elasticsearch.license.GetTrialStatusResponse(false); + } + + @Override + protected org.elasticsearch.license.GetTrialStatusResponse createTestInstance() { + return new org.elasticsearch.license.GetTrialStatusResponse(randomBoolean()); + } +}