Skip to content

Commit 231d916

Browse files
committed
HLRC: Add Delete License API (#32586)
Relates to #29827
1 parent a25f1ab commit 231d916

File tree

19 files changed

+265
-56
lines changed

19 files changed

+265
-56
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.elasticsearch.common.xcontent.XContentFactory;
3030
import org.elasticsearch.common.xcontent.XContentParser;
3131
import org.elasticsearch.common.xcontent.XContentType;
32+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest;
33+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseResponse;
3234
import org.elasticsearch.protocol.xpack.license.GetLicenseRequest;
3335
import org.elasticsearch.protocol.xpack.license.GetLicenseResponse;
3436
import org.elasticsearch.protocol.xpack.license.PutLicenseRequest;
@@ -98,6 +100,27 @@ public void getLicenseAsync(GetLicenseRequest request, RequestOptions options, A
98100
response -> new GetLicenseResponse(convertResponseToJson(response)), listener, emptySet());
99101
}
100102

103+
/**
104+
* Deletes license from the cluster.
105+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
106+
* @return the response
107+
* @throws IOException in case there is a problem sending the request or parsing back the response
108+
*/
109+
public DeleteLicenseResponse deleteLicense(DeleteLicenseRequest request, RequestOptions options) throws IOException {
110+
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::deleteLicense, options,
111+
DeleteLicenseResponse::fromXContent, emptySet());
112+
}
113+
114+
/**
115+
* Asynchronously deletes license from the cluster.
116+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
117+
* @param listener the listener to be notified upon request completion
118+
*/
119+
public void deleteLicenseAsync(DeleteLicenseRequest request, RequestOptions options, ActionListener<DeleteLicenseResponse> listener) {
120+
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::deleteLicense, options,
121+
DeleteLicenseResponse::fromXContent, listener, emptySet());
122+
}
123+
101124
/**
102125
* Converts an entire response into a json string
103126
*

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
import org.elasticsearch.index.rankeval.RankEvalRequest;
109109
import org.elasticsearch.protocol.xpack.XPackInfoRequest;
110110
import org.elasticsearch.protocol.xpack.XPackUsageRequest;
111+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest;
111112
import org.elasticsearch.protocol.xpack.license.GetLicenseRequest;
112113
import org.elasticsearch.protocol.xpack.license.PutLicenseRequest;
113114
import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoRequest;
@@ -1189,7 +1190,6 @@ static Request putLicense(PutLicenseRequest putLicenseRequest) {
11891190
return request;
11901191
}
11911192

1192-
11931193
static Request getLicense(GetLicenseRequest getLicenseRequest) {
11941194
String endpoint = new EndpointBuilder()
11951195
.addPathPartAsIs("_xpack")
@@ -1201,6 +1201,14 @@ static Request getLicense(GetLicenseRequest getLicenseRequest) {
12011201
return request;
12021202
}
12031203

1204+
static Request deleteLicense(DeleteLicenseRequest deleteLicenseRequest) {
1205+
Request request = new Request(HttpDelete.METHOD_NAME, "/_xpack/license");
1206+
Params parameters = new Params(request);
1207+
parameters.withTimeout(deleteLicenseRequest.timeout());
1208+
parameters.withMasterTimeout(deleteLicenseRequest.masterNodeTimeout());
1209+
return request;
1210+
}
1211+
12041212
static Request putMachineLearningJob(PutJobRequest putJobRequest) throws IOException {
12051213
String endpoint = new EndpointBuilder()
12061214
.addPathPartAsIs("_xpack")

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
2626
import org.elasticsearch.client.RequestOptions;
2727
import org.elasticsearch.client.RestHighLevelClient;
28+
import org.elasticsearch.common.Booleans;
29+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest;
30+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseResponse;
2831
import org.elasticsearch.protocol.xpack.license.GetLicenseRequest;
2932
import org.elasticsearch.protocol.xpack.license.GetLicenseResponse;
3033
import org.elasticsearch.protocol.xpack.license.LicensesStatus;
@@ -47,7 +50,7 @@
4750
*/
4851
public class LicensingDocumentationIT extends ESRestHighLevelClientTestCase {
4952

50-
public void testPutLicense() throws Exception {
53+
public void testLicense() throws Exception {
5154
assumeTrue("License is only valid when tested against snapshot/test builds", Build.CURRENT.isSnapshot());
5255
RestHighLevelClient client = highLevelClient();
5356
String license = "{\"license\": {\"uid\":\"893361dc-9749-4997-93cb-802e3d7fa4a8\",\"type\":\"gold\"," +
@@ -86,7 +89,7 @@ public void testPutLicense() throws Exception {
8689
// tag::put-license-execute-listener
8790
ActionListener<PutLicenseResponse> listener = new ActionListener<PutLicenseResponse>() {
8891
@Override
89-
public void onResponse(PutLicenseResponse indexResponse) {
92+
public void onResponse(PutLicenseResponse putLicenseResponse) {
9093
// <1>
9194
}
9295

@@ -108,6 +111,51 @@ public void onFailure(Exception e) {
108111

109112
assertTrue(latch.await(30L, TimeUnit.SECONDS));
110113
}
114+
115+
// we cannot actually delete the license, otherwise the remaining tests won't work
116+
if (Booleans.isTrue("true")) {
117+
return;
118+
}
119+
{
120+
//tag::delete-license-execute
121+
DeleteLicenseRequest request = new DeleteLicenseRequest();
122+
123+
DeleteLicenseResponse response = client.license().deleteLicense(request, RequestOptions.DEFAULT);
124+
//end::delete-license-execute
125+
126+
//tag::delete-license-response
127+
boolean acknowledged = response.isAcknowledged(); // <1>
128+
//end::delete-license-response
129+
130+
assertTrue(acknowledged);
131+
}
132+
{
133+
DeleteLicenseRequest request = new DeleteLicenseRequest();
134+
// tag::delete-license-execute-listener
135+
ActionListener<DeleteLicenseResponse> listener = new ActionListener<DeleteLicenseResponse>() {
136+
@Override
137+
public void onResponse(DeleteLicenseResponse deleteLicenseResponse) {
138+
// <1>
139+
}
140+
141+
@Override
142+
public void onFailure(Exception e) {
143+
// <2>
144+
}
145+
};
146+
// end::delete-license-execute-listener
147+
148+
// Replace the empty listener by a blocking listener in test
149+
final CountDownLatch latch = new CountDownLatch(1);
150+
listener = new LatchedActionListener<>(listener, latch);
151+
152+
// tag::delete-license-execute-async
153+
client.license().deleteLicenseAsync(
154+
request, RequestOptions.DEFAULT, listener); // <1>
155+
// end::delete-license-execute-async
156+
157+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
158+
}
111159
}
112160

113161
public void testGetLicense() throws Exception {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
[[java-rest-high-delete-license]]
2+
=== Delete License
3+
4+
[[java-rest-high-delete-license-execution]]
5+
==== Execution
6+
7+
The license can be deleted using the `deleteLicense()` method:
8+
9+
["source","java",subs="attributes,callouts,macros"]
10+
--------------------------------------------------
11+
include-tagged::{doc-tests}/LicensingDocumentationIT.java[delete-license-execute]
12+
--------------------------------------------------
13+
14+
[[java-rest-high-delete-license-response]]
15+
==== Response
16+
17+
The returned `DeleteLicenseResponse` contains the `acknowledged` flag, which
18+
returns true if the request was processed by all nodes.
19+
20+
["source","java",subs="attributes,callouts,macros"]
21+
--------------------------------------------------
22+
include-tagged::{doc-tests}/LicensingDocumentationIT.java[delete-license-response]
23+
--------------------------------------------------
24+
<1> Check the acknowledge flag. It should be true if license deletion is acknowledged.
25+
26+
[[java-rest-high-delete-license-async]]
27+
==== Asynchronous Execution
28+
29+
This request can be executed asynchronously:
30+
31+
["source","java",subs="attributes,callouts,macros"]
32+
--------------------------------------------------
33+
include-tagged::{doc-tests}/LicensingDocumentationIT.java[delete-license-execute-async]
34+
--------------------------------------------------
35+
<1> The `DeleteLicenseRequest` to execute and the `ActionListener` to use when
36+
the execution completes
37+
38+
The asynchronous method does not block and returns immediately. Once it is
39+
completed the `ActionListener` is called back using the `onResponse` method
40+
if the execution successfully completed or using the `onFailure` method if
41+
it failed.
42+
43+
A typical listener for `DeleteLicenseResponse` looks like:
44+
45+
["source","java",subs="attributes,callouts,macros"]
46+
--------------------------------------------------
47+
include-tagged::{doc-tests}/LicensingDocumentationIT.java[delete-license-execute-listener]
48+
--------------------------------------------------
49+
<1> Called when the execution is successfully completed. The response is
50+
provided as an argument
51+
<2> Called in case of failure. The raised exception is provided as an argument

docs/java-rest/high-level/supported-apis.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,11 @@ The Java High Level REST Client supports the following Licensing APIs:
194194

195195
* <<java-rest-high-put-license>>
196196
* <<java-rest-high-get-license>>
197+
* <<java-rest-high-delete-license>>
197198

198199
include::licensing/put-license.asciidoc[]
199200
include::licensing/get-license.asciidoc[]
201+
include::licensing/delete-license.asciidoc[]
200202

201203
== Migration APIs
202204

x-pack/plugin/core/src/main/java/org/elasticsearch/license/DeleteLicenseAction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import org.elasticsearch.action.Action;
99
import org.elasticsearch.client.ElasticsearchClient;
10+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest;
11+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseResponse;
1012

1113
public class DeleteLicenseAction extends Action<DeleteLicenseRequest, DeleteLicenseResponse, DeleteLicenseRequestBuilder> {
1214

x-pack/plugin/core/src/main/java/org/elasticsearch/license/DeleteLicenseRequest.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

x-pack/plugin/core/src/main/java/org/elasticsearch/license/DeleteLicenseRequestBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import org.elasticsearch.action.support.master.AcknowledgedRequestBuilder;
99
import org.elasticsearch.client.ElasticsearchClient;
10+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest;
11+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseResponse;
1012

1113
public class DeleteLicenseRequestBuilder extends AcknowledgedRequestBuilder<DeleteLicenseRequest, DeleteLicenseResponse,
1214
DeleteLicenseRequestBuilder> {

x-pack/plugin/core/src/main/java/org/elasticsearch/license/DeleteLicenseResponse.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.env.Environment;
2929
import org.elasticsearch.gateway.GatewayService;
3030
import org.elasticsearch.protocol.xpack.XPackInfoResponse;
31+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest;
3132
import org.elasticsearch.protocol.xpack.license.LicensesStatus;
3233
import org.elasticsearch.protocol.xpack.license.PutLicenseResponse;
3334
import org.elasticsearch.watcher.ResourceWatcherService;

x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicensingClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import org.elasticsearch.action.ActionListener;
99
import org.elasticsearch.client.ElasticsearchClient;
10+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest;
11+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseResponse;
1012
import org.elasticsearch.protocol.xpack.license.GetLicenseRequest;
1113
import org.elasticsearch.protocol.xpack.license.PutLicenseResponse;
1214

x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestDeleteLicenseAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.elasticsearch.license;
77

88
import org.elasticsearch.common.settings.Settings;
9+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest;
910
import org.elasticsearch.rest.RestController;
1011
import org.elasticsearch.rest.RestRequest;
1112
import org.elasticsearch.rest.action.RestToXContentListener;

x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportDeleteLicenseAction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
1818
import org.elasticsearch.common.inject.Inject;
1919
import org.elasticsearch.common.settings.Settings;
20+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest;
21+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseResponse;
2022
import org.elasticsearch.threadpool.ThreadPool;
2123
import org.elasticsearch.transport.TransportService;
2224

x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicensesManagerServiceTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.common.settings.Settings;
1212
import org.elasticsearch.common.unit.TimeValue;
1313
import org.elasticsearch.plugins.Plugin;
14+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest;
1415
import org.elasticsearch.protocol.xpack.license.LicensesStatus;
1516
import org.elasticsearch.test.ESSingleNodeTestCase;
1617
import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin;

x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicensesTransportTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.common.xcontent.XContentType;
1313
import org.elasticsearch.node.Node;
1414
import org.elasticsearch.plugins.Plugin;
15+
import org.elasticsearch.protocol.xpack.license.DeleteLicenseResponse;
1516
import org.elasticsearch.protocol.xpack.license.LicensesStatus;
1617
import org.elasticsearch.protocol.xpack.license.PutLicenseResponse;
1718
import org.elasticsearch.test.ESSingleNodeTestCase;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
package org.elasticsearch.protocol.xpack.license;
20+
21+
import org.elasticsearch.action.ActionRequestValidationException;
22+
import org.elasticsearch.action.support.master.AcknowledgedRequest;
23+
24+
25+
public class DeleteLicenseRequest extends AcknowledgedRequest<DeleteLicenseRequest> {
26+
27+
@Override
28+
public ActionRequestValidationException validate() {
29+
return null;
30+
}
31+
}

0 commit comments

Comments
 (0)