-
Notifications
You must be signed in to change notification settings - Fork 25.6k
add start trial API to HLRC #32799
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
add start trial API to HLRC #32799
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,6 +110,7 @@ | |
| import org.elasticsearch.protocol.xpack.XPackUsageRequest; | ||
| import org.elasticsearch.protocol.xpack.license.GetLicenseRequest; | ||
| import org.elasticsearch.protocol.xpack.license.PutLicenseRequest; | ||
| import org.elasticsearch.protocol.xpack.license.PostStartTrialRequest; | ||
| import org.elasticsearch.protocol.xpack.ml.PutJobRequest; | ||
| import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; | ||
| import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; | ||
|
|
@@ -1189,6 +1190,24 @@ static Request getLicense(GetLicenseRequest getLicenseRequest) { | |
| return request; | ||
| } | ||
|
|
||
| static Request postStartTrial(PostStartTrialRequest postStartTrialRequest) { | ||
| Request request = new Request(HttpPost.METHOD_NAME, new EndpointBuilder() | ||
| .addPathPartAsIs("_xpack") | ||
| .addPathPartAsIs("license") | ||
| .addPathPartAsIs("start_trial") | ||
| .build()); | ||
|
|
||
| Params parameters = new Params(request); | ||
| parameters.withMasterTimeout(postStartTrialRequest.masterNodeTimeout()); | ||
| if (postStartTrialRequest.isAcknowledged()) { | ||
| parameters.putParam("acknowledge", "true"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since this exists in 2 places now, can you extract it into a helper method like the other helpers in this class? |
||
| } | ||
| if (postStartTrialRequest.getType() != null) { | ||
| parameters.putParam("type", postStartTrialRequest.getType()); | ||
| } | ||
| return request; | ||
| } | ||
|
|
||
| static Request putMachineLearningJob(PutJobRequest putJobRequest) throws IOException { | ||
| String endpoint = new EndpointBuilder() | ||
| .addPathPartAsIs("_xpack") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,6 +126,7 @@ | |
| import org.elasticsearch.index.rankeval.RatedRequest; | ||
| import org.elasticsearch.index.rankeval.RestRankEvalAction; | ||
| import org.elasticsearch.protocol.xpack.XPackInfoRequest; | ||
| import org.elasticsearch.protocol.xpack.license.PostStartTrialRequest; | ||
| import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; | ||
| import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; | ||
| import org.elasticsearch.repositories.fs.FsRepository; | ||
|
|
@@ -2592,6 +2593,27 @@ public void testXPackDeleteWatch() { | |
| assertThat(request.getEntity(), nullValue()); | ||
| } | ||
|
|
||
| public void testPostStartTrial() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These typically set an expectedParams map ( |
||
| PostStartTrialRequest postStartTrialRequest = new PostStartTrialRequest(); | ||
|
|
||
| final boolean acknowledged = randomBoolean(); | ||
| postStartTrialRequest.acknowledge(acknowledged); | ||
|
|
||
| final String type = randomBoolean() | ||
| ? null | ||
| : randomAlphaOfLength(10); | ||
| postStartTrialRequest.setType(type); | ||
|
|
||
| Request request = RequestConverters.postStartTrial(postStartTrialRequest); | ||
| if (acknowledged) { | ||
| assertEquals(Boolean.toString(acknowledged), request.getParameters().get("acknowledge")); | ||
| } | ||
|
|
||
| if (type != null) { | ||
| assertEquals(type, request.getParameters().get("type")); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Randomize the {@link FetchSourceContext} request parameters. | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,8 @@ | |
| import org.elasticsearch.protocol.xpack.license.GetLicenseRequest; | ||
| import org.elasticsearch.protocol.xpack.license.GetLicenseResponse; | ||
| import org.elasticsearch.protocol.xpack.license.LicensesStatus; | ||
| import org.elasticsearch.protocol.xpack.license.PostStartTrialRequest; | ||
| import org.elasticsearch.protocol.xpack.license.PostStartTrialResponse; | ||
| import org.elasticsearch.protocol.xpack.license.PutLicenseRequest; | ||
| import org.elasticsearch.protocol.xpack.license.PutLicenseResponse; | ||
|
|
||
|
|
@@ -36,6 +38,7 @@ | |
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.hamcrest.Matchers.containsString; | ||
| import static org.hamcrest.Matchers.empty; | ||
| import static org.hamcrest.Matchers.endsWith; | ||
| import static org.hamcrest.Matchers.hasSize; | ||
| import static org.hamcrest.Matchers.not; | ||
|
|
@@ -167,4 +170,63 @@ public void onFailure(Exception e) { | |
| assertThat(currentLicense, endsWith("}")); | ||
| } | ||
| } | ||
|
|
||
| public void testPostStartTrial() throws Exception { | ||
| RestHighLevelClient client = highLevelClient(); | ||
| { | ||
| // tag::post-start-trial-execute | ||
| PostStartTrialRequest request = new PostStartTrialRequest(); | ||
| request.acknowledge(false); | ||
|
|
||
| PostStartTrialResponse response = client.license().postStartTrial(request, RequestOptions.DEFAULT); | ||
| // end::post-start-trial-execute | ||
|
|
||
| // tag::post-start-trial-response | ||
| boolean acknowledged = response.isAcknowledged(); | ||
| boolean trialWasStarted = response.isTrialWasStarted(); | ||
| String errorMessage = response.getErrorMessage(); | ||
| String type = response.getType(); | ||
| String acknowledgeMessage = response.getAcknowledgeMessage(); // todo rename this to acknowledgeheader for consistency | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will this todo be put in the docs? I dont know the answer, heh. Can you validate it does not get put in the docs?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The whole line or just the comment? I was going to remove the comment but I'd intended the rest of the line to appear in the docs
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I skimmed that a little too fast, yeah the todo is not intended to remain |
||
| Map<String, String[]> acknowledgeMessages = response.getAcknowledgeMessages(); | ||
| // end::post-start-trial-response | ||
|
|
||
| assertFalse(acknowledged); | ||
| assertFalse(trialWasStarted); | ||
| assertEquals("Operation failed: Needs acknowledgement.", errorMessage); | ||
| assertNull(type); | ||
| assertThat(acknowledgeMessage, containsString("To begin your free trial, call /start_trial again and specify " + | ||
| "the \"acknowledge=true\" parameter.")); | ||
| assertThat(acknowledgeMessages.entrySet(), not(empty())); | ||
| } | ||
|
|
||
| { | ||
| PostStartTrialRequest request = new PostStartTrialRequest(); | ||
|
|
||
| // tag::post-start-trial-execute-listener | ||
| ActionListener<PostStartTrialResponse> listener = new ActionListener<PostStartTrialResponse>() { | ||
| @Override | ||
| public void onResponse(PostStartTrialResponse postStartTrialResponse) { | ||
| // <1> | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(Exception e) { | ||
| // <2> | ||
| } | ||
| }; | ||
| // end::post-start-trial-execute-listener | ||
|
|
||
| final CountDownLatch latch = new CountDownLatch(1); | ||
| listener = new LatchedActionListener<>(listener, latch); | ||
|
|
||
| // tag::post-start-trial-execute-async | ||
| client.license().postStartTrialAsync(request, RequestOptions.DEFAULT, listener); | ||
| // end::post-start-trial-execute-async | ||
|
|
||
| assertTrue(latch.await(30L, TimeUnit.SECONDS)); | ||
|
|
||
| // todo add some other cases with randomization | ||
| // todo add a case that succeeds in starting the trial | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure there is an IT test case for this anywhere, although I need to look more thoroughly
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea id def add a IT test for it and only keep the DocumentationIT to a minimum, to validate that it actually does what you are documenting. |
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| [[java-rest-high-post-start-trial]] | ||
| === Post Start Trial | ||
|
|
||
| [[java-rest-high-post-start-license-execution]] | ||
| ==== Execution | ||
|
|
||
| This API creates and enables a trial license using the `postStartTrial()` | ||
| method. | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| --------------------------------------------------- | ||
| include-tagged::{doc_tests}/LicensingDocumentationIT.java[post-start-trial-execute] | ||
| --------------------------------------------------- | ||
|
|
||
| [[java-rest-high-post-start-license-response]] | ||
| ==== Response | ||
|
|
||
| The returned `PostStartTrialResponse` returns a field indicating whether the | ||
| trial was started. If it was started, the response returns a the type of | ||
| license started. If it was not started, it returns an error message describing | ||
| why. | ||
|
|
||
| Acknowledgement messages may also be returned if this API was called without | ||
| the `acknowledge` flag set to `true`. In this case you need to display the | ||
| messages to the end user and if they agree, resubmit the license with the | ||
| `acknowledge` flag set to `true`. Please note that the request will still | ||
| return a 200 return code even if requires an acknowledgement. So, it is | ||
| necessary to check the `acknowledged` flag. | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/LicensingDocumentationIT.java[post-start-trial-response] | ||
| -------------------------------------------------- | ||
|
|
||
| [[java-rest-high-post-start-trial-async]] | ||
|
|
||
| ==== Asynchronous execution | ||
|
|
||
| This request can be executed asynchronously: | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/LicensingDocumentationIT.java[post-start-trial-execute-async] | ||
| -------------------------------------------------- | ||
|
|
||
| The asynchronous method does not block and returns immediately. Once it is | ||
| completed the `ActionListener` is called back using the `onResponse` method | ||
| if the execution successfully completed or using the `onFailure` method if | ||
| it failed. | ||
|
|
||
| A typical listener for `PostStartTrialResponse` looks like: | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/LicensingDocumentationIT.java[post-start-trial-execute-listener] | ||
| -------------------------------------------------- | ||
| <1> Called when the execution is successfully completed. The response is | ||
| provided as an argument | ||
| <2> Called in case of failure. The raised exception is provided as an argument |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the trial is already started it will return a 403 code but without the exception structure that hlrc is expecting