Skip to content

Commit b01a58b

Browse files
authored
HLRC: add support for get license basic/trial status API (#33176)
Relates to #29827
1 parent 11f0950 commit b01a58b

19 files changed

+448
-47
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseClient.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.elasticsearch.client.license.StartTrialResponse;
2727
import org.elasticsearch.client.license.StartBasicRequest;
2828
import org.elasticsearch.client.license.StartBasicResponse;
29+
import org.elasticsearch.client.license.GetBasicStatusResponse;
30+
import org.elasticsearch.client.license.GetTrialStatusResponse;
2931
import org.elasticsearch.common.Strings;
3032
import org.elasticsearch.common.io.Streams;
3133
import org.elasticsearch.common.xcontent.DeprecationHandler;
@@ -172,6 +174,28 @@ public void startBasicAsync(StartBasicRequest request, RequestOptions options,
172174
StartBasicResponse::fromXContent, listener, emptySet());
173175
}
174176

177+
/**
178+
* Retrieve the license trial status
179+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
180+
* @return the response
181+
* @throws IOException in case there is a problem sending the request or parsing back the response
182+
*/
183+
public GetTrialStatusResponse getTrialStatus(RequestOptions options) throws IOException {
184+
return restHighLevelClient.performRequestAndParseEntity(Validatable.EMPTY,
185+
request -> LicenseRequestConverters.getLicenseTrialStatus(), options, GetTrialStatusResponse::fromXContent, emptySet());
186+
}
187+
188+
/**
189+
* Retrieve the license basic status
190+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
191+
* @return the response
192+
* @throws IOException in case there is a problem sending the request or parsing back the response
193+
*/
194+
public GetBasicStatusResponse getBasicStatus(RequestOptions options) throws IOException {
195+
return restHighLevelClient.performRequestAndParseEntity(Validatable.EMPTY,
196+
request -> LicenseRequestConverters.getLicenseBasicStatus(), options, GetBasicStatusResponse::fromXContent, emptySet());
197+
}
198+
175199
/**
176200
* Converts an entire response into a json string
177201
*

client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseRequestConverters.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,12 @@ static Request startBasic(StartBasicRequest startBasicRequest) {
8888
}
8989
return request;
9090
}
91+
92+
static Request getLicenseTrialStatus() {
93+
return new Request(HttpGet.METHOD_NAME, "/_xpack/license/trial_status");
94+
}
95+
96+
static Request getLicenseBasicStatus() {
97+
return new Request(HttpGet.METHOD_NAME, "/_xpack/license/basic_status");
98+
}
9199
}

client/rest-high-level/src/main/java/org/elasticsearch/client/Validatable.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
* Defines a validation layer for Requests.
2525
*/
2626
public interface Validatable {
27+
28+
Validatable EMPTY = new Validatable() {};
29+
2730
/**
2831
* Perform validation. This method does not have to be overridden in the event that no validation needs to be done,
2932
* or the validation was done during object construction time. A {@link ValidationException} that is not null is
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.license;
21+
22+
import org.elasticsearch.common.ParseField;
23+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
24+
import org.elasticsearch.common.xcontent.ObjectParser;
25+
import org.elasticsearch.common.xcontent.XContentParser;
26+
27+
import java.util.Objects;
28+
29+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
30+
31+
/**
32+
* Response class for license get basic status API
33+
*/
34+
public class GetBasicStatusResponse {
35+
36+
private static final ParseField ELIGIBLE_TO_START_BASIC = new ParseField("eligible_to_start_basic");
37+
38+
private static final ConstructingObjectParser<GetBasicStatusResponse, Void> PARSER = new ConstructingObjectParser<>(
39+
"get_basic_status_response", true, a -> new GetBasicStatusResponse((boolean) a[0]));
40+
41+
static {
42+
PARSER.declareField(constructorArg(), (parser, context) -> parser.booleanValue(), ELIGIBLE_TO_START_BASIC,
43+
ObjectParser.ValueType.BOOLEAN);
44+
}
45+
46+
private final boolean eligibleToStartBasic;
47+
48+
GetBasicStatusResponse(boolean eligibleToStartBasic) {
49+
this.eligibleToStartBasic = eligibleToStartBasic;
50+
}
51+
52+
/**
53+
* Returns whether the license is eligible to start basic or not
54+
*/
55+
public boolean isEligibleToStartBasic() {
56+
return eligibleToStartBasic;
57+
}
58+
59+
public static GetBasicStatusResponse fromXContent(XContentParser parser) {
60+
return PARSER.apply(parser, null);
61+
}
62+
63+
@Override
64+
public boolean equals(Object o) {
65+
if (this == o) {
66+
return true;
67+
}
68+
if (o == null || getClass() != o.getClass()) {
69+
return false;
70+
}
71+
GetBasicStatusResponse that = (GetBasicStatusResponse) o;
72+
return eligibleToStartBasic == that.eligibleToStartBasic;
73+
}
74+
75+
@Override
76+
public int hashCode() {
77+
return Objects.hash(eligibleToStartBasic);
78+
}
79+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.license;
21+
22+
import org.elasticsearch.common.ParseField;
23+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
24+
import org.elasticsearch.common.xcontent.ObjectParser;
25+
import org.elasticsearch.common.xcontent.XContentParser;
26+
27+
import java.util.Objects;
28+
29+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
30+
31+
/**
32+
* Response class for license get trial status API
33+
*/
34+
public class GetTrialStatusResponse {
35+
36+
private static final ParseField ELIGIBLE_TO_START_TRIAL = new ParseField("eligible_to_start_trial");
37+
38+
private static final ConstructingObjectParser<GetTrialStatusResponse, Void> PARSER = new ConstructingObjectParser<>(
39+
"get_trial_status_response", true, a -> new GetTrialStatusResponse((boolean) a[0]));
40+
41+
static {
42+
PARSER.declareField(constructorArg(), (parser, context) -> parser.booleanValue(), ELIGIBLE_TO_START_TRIAL,
43+
ObjectParser.ValueType.BOOLEAN);
44+
}
45+
46+
private final boolean eligibleToStartTrial;
47+
48+
GetTrialStatusResponse(boolean eligibleToStartTrial) {
49+
this.eligibleToStartTrial = eligibleToStartTrial;
50+
}
51+
52+
/**
53+
* Returns whether the license is eligible to start trial or not
54+
*/
55+
public boolean isEligibleToStartTrial() {
56+
return eligibleToStartTrial;
57+
}
58+
59+
public static GetTrialStatusResponse fromXContent(XContentParser parser) {
60+
return PARSER.apply(parser, null);
61+
}
62+
63+
@Override
64+
public boolean equals(Object o) {
65+
if (this == o) {
66+
return true;
67+
}
68+
if (o == null || getClass() != o.getClass()) {
69+
return false;
70+
}
71+
GetTrialStatusResponse that = (GetTrialStatusResponse) o;
72+
return eligibleToStartTrial == that.eligibleToStartTrial;
73+
}
74+
75+
@Override
76+
public int hashCode() {
77+
return Objects.hash(eligibleToStartTrial);
78+
}
79+
80+
}

client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseIT.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
import org.elasticsearch.Build;
2323
import org.elasticsearch.action.support.master.AcknowledgedResponse;
2424
import org.elasticsearch.client.license.DeleteLicenseRequest;
25+
import org.elasticsearch.client.license.GetBasicStatusResponse;
2526
import org.elasticsearch.client.license.GetLicenseRequest;
2627
import org.elasticsearch.client.license.GetLicenseResponse;
28+
import org.elasticsearch.client.license.GetTrialStatusResponse;
2729
import org.elasticsearch.client.license.LicensesStatus;
2830
import org.elasticsearch.client.license.PutLicenseRequest;
2931
import org.elasticsearch.client.license.PutLicenseResponse;
@@ -198,4 +200,14 @@ public void testDeleteLicense() throws Exception {
198200
final AcknowledgedResponse response = highLevelClient().license().deleteLicense(request, RequestOptions.DEFAULT);
199201
assertThat(response.isAcknowledged(), equalTo(true));
200202
}
203+
204+
public void testGetTrialStatus() throws IOException {
205+
GetTrialStatusResponse trialStatus = highLevelClient().license().getTrialStatus(RequestOptions.DEFAULT);
206+
assertFalse(trialStatus.isEligibleToStartTrial());
207+
}
208+
209+
public void testGetBasicStatus() throws IOException {
210+
GetBasicStatusResponse basicStatus = highLevelClient().license().getBasicStatus(RequestOptions.DEFAULT);
211+
assertTrue(basicStatus.isEligibleToStartBasic());
212+
}
201213
}

client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseRequestConvertersTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,20 @@ public void testStartBasic() {
128128
assertThat(request.getParameters(), equalTo(expectedParams));
129129
assertThat(request.getEntity(), is(nullValue()));
130130
}
131+
132+
public void testGetLicenseTrialStatus() {
133+
Request request = LicenseRequestConverters.getLicenseTrialStatus();
134+
assertEquals(HttpGet.METHOD_NAME, request.getMethod());
135+
assertEquals("/_xpack/license/trial_status", request.getEndpoint());
136+
assertEquals(request.getParameters().size(), 0);
137+
assertNull(request.getEntity());
138+
}
139+
140+
public void testGetLicenseBasicStatus() {
141+
Request request = LicenseRequestConverters.getLicenseBasicStatus();
142+
assertEquals(HttpGet.METHOD_NAME, request.getMethod());
143+
assertEquals("/_xpack/license/basic_status", request.getEndpoint());
144+
assertEquals(request.getParameters().size(), 0);
145+
assertNull(request.getEntity());
146+
}
131147
}

client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,6 @@ public void testApiNamingConventions() throws Exception {
753753
assertSubmitTaskMethod(methods, method, apiName, restSpec);
754754
} else {
755755
assertSyncMethod(method, apiName);
756-
757756
boolean remove = apiSpec.remove(apiName);
758757
if (remove == false) {
759758
if (deprecatedMethods.contains(apiName)) {
@@ -788,7 +787,7 @@ public void testApiNamingConventions() throws Exception {
788787
assertThat("Some API are not supported but they should be: " + apiSpec, apiSpec.size(), equalTo(0));
789788
}
790789

791-
private void assertSyncMethod(Method method, String apiName) {
790+
private static void assertSyncMethod(Method method, String apiName) {
792791
//A few methods return a boolean rather than a response object
793792
if (apiName.equals("ping") || apiName.contains("exist")) {
794793
assertThat("the return type for method [" + method + "] is incorrect",
@@ -804,7 +803,8 @@ private void assertSyncMethod(Method method, String apiName) {
804803
assertEquals("incorrect number of exceptions for method [" + method + "]", 1, method.getExceptionTypes().length);
805804
//a few methods don't accept a request object as argument
806805
if (apiName.equals("ping") || apiName.equals("info") || apiName.equals("security.get_ssl_certificates")
807-
|| apiName.equals("security.authenticate")) {
806+
|| apiName.equals("security.authenticate") || apiName.equals("license.get_trial_status")
807+
|| apiName.equals("license.get_basic_status")) {
808808
assertEquals("incorrect number of arguments for method [" + method + "]", 1, method.getParameterTypes().length);
809809
assertThat("the parameter to method [" + method + "] is the wrong type",
810810
method.getParameterTypes()[0], equalTo(RequestOptions.class));
@@ -817,7 +817,7 @@ private void assertSyncMethod(Method method, String apiName) {
817817
}
818818
}
819819

820-
private void assertAsyncMethod(Map<String, Method> methods, Method method, String apiName) {
820+
private static void assertAsyncMethod(Map<String, Method> methods, Method method, String apiName) {
821821
assertTrue("async method [" + method.getName() + "] doesn't have corresponding sync method",
822822
methods.containsKey(apiName.substring(0, apiName.length() - 6)));
823823
assertThat("async method [" + method + "] should return void", method.getReturnType(), equalTo(Void.TYPE));
@@ -837,7 +837,8 @@ private void assertAsyncMethod(Map<String, Method> methods, Method method, Strin
837837
}
838838
}
839839

840-
private void assertSubmitTaskMethod(Map<String, Method> methods, Method method, String apiName, ClientYamlSuiteRestSpec restSpec) {
840+
private static void assertSubmitTaskMethod(Map<String, Method> methods, Method method, String apiName,
841+
ClientYamlSuiteRestSpec restSpec) {
841842
String methodName = extractMethodName(apiName);
842843
assertTrue("submit task method [" + method.getName() + "] doesn't have corresponding sync method",
843844
methods.containsKey(methodName));
@@ -851,11 +852,11 @@ private void assertSubmitTaskMethod(Map<String, Method> methods, Method method,
851852
restSpec.getApi(methodName).getParams(), Matchers.hasKey("wait_for_completion"));
852853
}
853854

854-
private String extractMethodName(String apiName) {
855+
private static String extractMethodName(String apiName) {
855856
return apiName.substring(SUBMIT_TASK_PREFIX.length(), apiName.length() - SUBMIT_TASK_SUFFIX.length());
856857
}
857858

858-
private boolean isSubmitTaskMethod(String apiName) {
859+
private static boolean isSubmitTaskMethod(String apiName) {
859860
return apiName.startsWith(SUBMIT_TASK_PREFIX) && apiName.endsWith(SUBMIT_TASK_SUFFIX);
860861
}
861862

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.elasticsearch.client.license.StartTrialResponse;
3131
import org.elasticsearch.client.license.StartBasicRequest;
3232
import org.elasticsearch.client.license.StartBasicResponse;
33+
import org.elasticsearch.client.license.GetBasicStatusResponse;
34+
import org.elasticsearch.client.license.GetTrialStatusResponse;
3335
import org.elasticsearch.common.Booleans;
3436
import org.junit.After;
3537
import org.junit.BeforeClass;
@@ -336,4 +338,30 @@ public void onFailure(Exception e) {
336338
assertTrue(latch.await(30L, TimeUnit.SECONDS));
337339
}
338340
}
341+
342+
public void testGetTrialStatus() throws IOException {
343+
RestHighLevelClient client = highLevelClient();
344+
{
345+
//tag::get-trial-status-execute
346+
GetTrialStatusResponse response = client.license().getTrialStatus(RequestOptions.DEFAULT);
347+
//end::get-trial-status-execute
348+
349+
//tag::get-trial-status-response
350+
boolean eligibleToStartTrial = response.isEligibleToStartTrial(); // <1>
351+
//end::get-trial-status-response
352+
}
353+
}
354+
355+
public void testGetBasicStatus() throws IOException {
356+
RestHighLevelClient client = highLevelClient();
357+
{
358+
//tag::get-basic-status-execute
359+
GetBasicStatusResponse response = client.license().getBasicStatus(RequestOptions.DEFAULT);
360+
//end::get-basic-status-execute
361+
362+
//tag::get-basic-status-response
363+
boolean eligibleToStartbasic = response.isEligibleToStartBasic(); // <1>
364+
//end::get-basic-status-response
365+
}
366+
}
339367
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[[java-rest-high-get-basic-status]]
2+
=== Get Basic Status
3+
4+
[[java-rest-high-get-basic-status-execution]]
5+
==== Execution
6+
7+
The basic status of the license can be retrieved as follows:
8+
9+
["source","java",subs="attributes,callouts,macros"]
10+
--------------------------------------------------
11+
include-tagged::{doc-tests}/LicensingDocumentationIT.java[get-basic-status-execute]
12+
--------------------------------------------------
13+
14+
[[java-rest-high-get-basic-status-response]]
15+
==== Response
16+
17+
The returned `GetTrialStatusResponse` holds only a `boolean` flag:
18+
19+
["source","java",subs="attributes,callouts,macros"]
20+
--------------------------------------------------
21+
include-tagged::{doc-tests}/LicensingDocumentationIT.java[get-basic-status-response]
22+
--------------------------------------------------
23+
<1> Whether the license is eligible to start basic or not

0 commit comments

Comments
 (0)