Skip to content

Commit 37b54c9

Browse files
committed
HLRC: Add delete template API (#36320)
Relates #27205
1 parent 4c22028 commit 37b54c9

File tree

8 files changed

+169
-5
lines changed

8 files changed

+169
-5
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
5555
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
5656
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
57+
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
5758
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
5859
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
5960
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
@@ -1316,4 +1317,32 @@ public void unfreezeAsync(UnfreezeIndexRequest request, RequestOptions options,
13161317
ShardsAcknowledgedResponse::fromXContent, listener, emptySet());
13171318
}
13181319

1320+
/**
1321+
* Delete an index template using the Index Templates API
1322+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
1323+
* on elastic.co</a>
1324+
*
1325+
* @param request the request
1326+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1327+
* @throws IOException in case there is a problem sending the request or parsing back the response
1328+
*/
1329+
public AcknowledgedResponse deleteTemplate(DeleteIndexTemplateRequest request, RequestOptions options) throws IOException {
1330+
return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::deleteTemplate,
1331+
options, AcknowledgedResponse::fromXContent, emptySet());
1332+
}
1333+
1334+
/**
1335+
* Asynchronously delete an index template using the Index Templates API
1336+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
1337+
* on elastic.co</a>
1338+
*
1339+
* @param request the request
1340+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1341+
* @param listener the listener to be notified upon request completion
1342+
*/
1343+
public void deleteTemplateAsync(DeleteIndexTemplateRequest request, RequestOptions options,
1344+
ActionListener<AcknowledgedResponse> listener) {
1345+
restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::deleteTemplate,
1346+
options, AcknowledgedResponse::fromXContent, listener, emptySet());
1347+
}
13191348
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
4646
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
4747
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
48+
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
4849
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
4950
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
5051
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest;
@@ -428,4 +429,13 @@ static Request unfreezeIndex(UnfreezeIndexRequest unfreezeIndexRequest) {
428429
parameters.withWaitForActiveShards(unfreezeIndexRequest.getWaitForActiveShards(), ActiveShardCount.DEFAULT);
429430
return request;
430431
}
432+
433+
static Request deleteTemplate(DeleteIndexTemplateRequest deleteIndexTemplateRequest) {
434+
String name = deleteIndexTemplateRequest.name();
435+
String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_template").addPathPart(name).build();
436+
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
437+
RequestConverters.Params params = new RequestConverters.Params(request);
438+
params.withMasterTimeout(deleteIndexTemplateRequest.masterNodeTimeout());
439+
return request;
440+
}
431441
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
6262
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
6363
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
64+
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
6465
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
6566
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
6667
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
@@ -1351,7 +1352,7 @@ public void testInvalidValidateQuery() throws IOException{
13511352
assertFalse(response.isValid());
13521353
}
13531354

1354-
public void testGetIndexTemplate() throws Exception {
1355+
public void testCRUDIndexTemplate() throws Exception {
13551356
RestHighLevelClient client = highLevelClient();
13561357

13571358
PutIndexTemplateRequest putTemplate1 = new PutIndexTemplateRequest().name("template-1")
@@ -1393,9 +1394,22 @@ public void testGetIndexTemplate() throws Exception {
13931394
assertThat(getBoth.getIndexTemplates().stream().map(IndexTemplateMetaData::getName).toArray(),
13941395
arrayContainingInAnyOrder("template-1", "template-2"));
13951396

1396-
ElasticsearchException notFound = expectThrows(ElasticsearchException.class, () -> execute(
1397-
new GetIndexTemplatesRequest().names("the-template-*"), client.indices()::getTemplate, client.indices()::getTemplateAsync));
1398-
assertThat(notFound.status(), equalTo(RestStatus.NOT_FOUND));
1397+
assertTrue(execute(new DeleteIndexTemplateRequest("template-1"),
1398+
client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync).isAcknowledged());
1399+
assertThat(expectThrows(ElasticsearchException.class, () -> execute(new GetIndexTemplatesRequest().names("template-1"),
1400+
client.indices()::getTemplate, client.indices()::getTemplateAsync)).status(), equalTo(RestStatus.NOT_FOUND));
1401+
assertThat(expectThrows(ElasticsearchException.class, () -> execute(new DeleteIndexTemplateRequest("template-1"),
1402+
client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync)).status(), equalTo(RestStatus.NOT_FOUND));
1403+
1404+
assertThat(execute(new GetIndexTemplatesRequest("template-*"),
1405+
client.indices()::getTemplate, client.indices()::getTemplateAsync).getIndexTemplates(), hasSize(1));
1406+
assertThat(execute(new GetIndexTemplatesRequest("template-*"),
1407+
client.indices()::getTemplate, client.indices()::getTemplateAsync).getIndexTemplates().get(0).name(), equalTo("template-2"));
1408+
1409+
assertTrue(execute(new DeleteIndexTemplateRequest("template-*"),
1410+
client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync).isAcknowledged());
1411+
assertThat(expectThrows(ElasticsearchException.class, () -> execute(new GetIndexTemplatesRequest().names("template-*"),
1412+
client.indices()::getTemplate, client.indices()::getTemplateAsync)).status(), equalTo(RestStatus.NOT_FOUND));
13991413
}
14001414

14011415
public void testAnalyze() throws Exception {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
4949
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
5050
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
51+
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
5152
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
5253
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
5354
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest;
@@ -892,4 +893,21 @@ public void testGetTemplateRequest() throws Exception {
892893
Assert.assertThat(request.getParameters(), equalTo(expectedParams));
893894
Assert.assertThat(request.getEntity(), nullValue());
894895
}
896+
897+
public void testDeleteTemplateRequest() {
898+
Map<String, String> encodes = new HashMap<>();
899+
encodes.put("log", "log");
900+
encodes.put("1", "1");
901+
encodes.put("template#1", "template%231");
902+
encodes.put("template-*", "template-*");
903+
encodes.put("foo^bar", "foo%5Ebar");
904+
DeleteIndexTemplateRequest deleteTemplateRequest = new DeleteIndexTemplateRequest().name(randomFrom(encodes.keySet()));
905+
Map<String, String> expectedParams = new HashMap<>();
906+
RequestConvertersTests.setRandomMasterTimeout(deleteTemplateRequest, expectedParams);
907+
Request request = IndicesRequestConverters.deleteTemplate(deleteTemplateRequest);
908+
Assert.assertThat(request.getMethod(), equalTo(HttpDelete.METHOD_NAME));
909+
Assert.assertThat(request.getEndpoint(), equalTo("/_template/" + encodes.get(deleteTemplateRequest.name())));
910+
Assert.assertThat(request.getParameters(), equalTo(expectedParams));
911+
Assert.assertThat(request.getEntity(), nullValue());
912+
}
895913
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,6 @@ public void testApiNamingConventions() throws Exception {
684684
"create",
685685
"get_source",
686686
"indices.delete_alias",
687-
"indices.delete_template",
688687
"indices.exists_template",
689688
"indices.exists_type",
690689
"indices.get_upgrade",

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
6060
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
6161
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
62+
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
6263
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
6364
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
6465
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
@@ -2674,4 +2675,64 @@ public void onFailure(Exception e) {
26742675
// end::unfreeze-index-notfound
26752676
}
26762677
}
2678+
2679+
public void testDeleteTemplate() throws Exception {
2680+
RestHighLevelClient client = highLevelClient();
2681+
{
2682+
PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template");
2683+
putRequest.patterns(Arrays.asList("pattern-1", "log-*"));
2684+
putRequest.settings(Settings.builder().put("index.number_of_shards", 3));
2685+
assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
2686+
}
2687+
2688+
// tag::delete-template-request
2689+
DeleteIndexTemplateRequest request = new DeleteIndexTemplateRequest();
2690+
request.name("my-template"); // <1>
2691+
// end::delete-template-request
2692+
2693+
// tag::delete-template-request-masterTimeout
2694+
request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); // <1>
2695+
request.masterNodeTimeout("1m"); // <2>
2696+
// end::delete-template-request-masterTimeout
2697+
2698+
// tag::delete-template-execute
2699+
AcknowledgedResponse deleteTemplateAcknowledge = client.indices().deleteTemplate(request, RequestOptions.DEFAULT);
2700+
// end::delete-template-execute
2701+
2702+
// tag::delete-template-response
2703+
boolean acknowledged = deleteTemplateAcknowledge.isAcknowledged(); // <1>
2704+
// end::delete-template-response
2705+
assertThat(acknowledged, equalTo(true));
2706+
2707+
{
2708+
PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template");
2709+
putRequest.patterns(Arrays.asList("pattern-1", "log-*"));
2710+
putRequest.settings(Settings.builder().put("index.number_of_shards", 3));
2711+
assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
2712+
}
2713+
// tag::delete-template-execute-listener
2714+
ActionListener<AcknowledgedResponse> listener =
2715+
new ActionListener<AcknowledgedResponse>() {
2716+
@Override
2717+
public void onResponse(AcknowledgedResponse response) {
2718+
// <1>
2719+
}
2720+
2721+
@Override
2722+
public void onFailure(Exception e) {
2723+
// <2>
2724+
}
2725+
};
2726+
// end::delete-template-execute-listener
2727+
2728+
// Replace the empty listener by a blocking listener in test
2729+
final CountDownLatch latch = new CountDownLatch(1);
2730+
listener = new LatchedActionListener<>(listener, latch);
2731+
2732+
// tag::delete-template-execute-async
2733+
client.indices().deleteTemplateAsync(request, RequestOptions.DEFAULT, listener); // <1>
2734+
// end::delete-template-execute-async
2735+
2736+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
2737+
}
26772738
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--
2+
:api: delete-template
3+
:request: DeleteIndexTemplateRequest
4+
:response: AcknowledgedResponse
5+
--
6+
7+
[id="{upid}-{api}"]
8+
=== Delete Template API
9+
10+
[id="{upid}-{api}-request"]
11+
==== Request
12+
13+
The Delete Template API allows you to delete an index template.
14+
15+
["source","java",subs="attributes,callouts,macros"]
16+
--------------------------------------------------
17+
include-tagged::{doc-tests-file}[{api}-request]
18+
--------------------------------------------------
19+
<1> The name of an index template to delete.
20+
21+
[id="{upid}-{api}-response"]
22+
==== Response
23+
24+
The returned +{response}+ indicates if the delete template request was received.
25+
26+
["source","java",subs="attributes,callouts,macros"]
27+
--------------------------------------------------
28+
include-tagged::{doc-tests-file}[{api}-response]
29+
--------------------------------------------------
30+
<1> Whether or not the delete template request was acknowledged.
31+
32+
include::../execution.asciidoc[]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ include::indices/get_templates.asciidoc[]
148148
include::indices/get_index.asciidoc[]
149149
include::indices/freeze_index.asciidoc[]
150150
include::indices/unfreeze_index.asciidoc[]
151+
include::indices/delete_template.asciidoc[]
151152

152153
== Cluster APIs
153154

0 commit comments

Comments
 (0)