Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HLRC: add support for get license basic/trial status API #33176

Merged
merged 5 commits into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
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;
Expand Down Expand Up @@ -172,6 +174,28 @@ public void startBasicAsync(StartBasicRequest request, RequestOptions options,
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());
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I went for exposing only sync versions of these two methods, I don't see how they would be used in an async fashion. Also, I went for not adding an empty request and mimic what we already did with ping and info, which are somehow an exception when you look at the method argument.


/**
* Converts an entire response into a json string
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,12 @@ static Request startBasic(StartBasicRequest startBasicRequest) {
}
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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
* 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GetBasicStatusResponse, Void> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<GetTrialStatusResponse, Void> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
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;
Expand Down Expand Up @@ -198,4 +200,14 @@ public void testDeleteLicense() throws Exception {
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,20 @@ public void testStartBasic() {
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,6 @@ public void testApiNamingConventions() throws Exception {
assertSubmitTaskMethod(methods, method, apiName, restSpec);
} else {
assertSyncMethod(method, apiName);

boolean remove = apiSpec.remove(apiName);
if (remove == false) {
if (deprecatedMethods.contains(apiName)) {
Expand Down Expand Up @@ -771,7 +770,7 @@ public void testApiNamingConventions() throws Exception {
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",
Expand All @@ -784,7 +783,8 @@ private void assertSyncMethod(Method method, String apiName) {
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));
Expand All @@ -797,7 +797,7 @@ private void assertSyncMethod(Method method, String apiName) {
}
}

private void assertAsyncMethod(Map<String, Method> methods, Method method, String apiName) {
private static void assertAsyncMethod(Map<String, Method> 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));
Expand All @@ -817,7 +817,8 @@ private void assertAsyncMethod(Map<String, Method> methods, Method method, Strin
}
}

private void assertSubmitTaskMethod(Map<String, Method> methods, Method method, String apiName, ClientYamlSuiteRestSpec restSpec) {
private static void assertSubmitTaskMethod(Map<String, Method> 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));
Expand All @@ -831,11 +832,11 @@ private void assertSubmitTaskMethod(Map<String, Method> methods, Method method,
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
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;
Expand Down Expand Up @@ -336,4 +338,30 @@ public void onFailure(Exception e) {
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
}
}
}
23 changes: 23 additions & 0 deletions docs/java-rest/high-level/licensing/get-basic-status.asciidoc
Original file line number Diff line number Diff line change
@@ -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