Skip to content

Commit 32a7d7a

Browse files
authored
HLRC: Add delete template API (#36320)
Relates #27205
1 parent 844ff80 commit 32a7d7a

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
@@ -53,6 +53,7 @@
5353
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
5454
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
5555
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
56+
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
5657
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
5758
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
5859
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
@@ -886,4 +887,32 @@ public void unfreezeAsync(UnfreezeIndexRequest request, RequestOptions options,
886887
ShardsAcknowledgedResponse::fromXContent, listener, emptySet());
887888
}
888889

890+
/**
891+
* Delete an index template using the Index Templates API
892+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
893+
* on elastic.co</a>
894+
*
895+
* @param request the request
896+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
897+
* @throws IOException in case there is a problem sending the request or parsing back the response
898+
*/
899+
public AcknowledgedResponse deleteTemplate(DeleteIndexTemplateRequest request, RequestOptions options) throws IOException {
900+
return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::deleteTemplate,
901+
options, AcknowledgedResponse::fromXContent, emptySet());
902+
}
903+
904+
/**
905+
* Asynchronously delete an index template using the Index Templates API
906+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
907+
* on elastic.co</a>
908+
*
909+
* @param request the request
910+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
911+
* @param listener the listener to be notified upon request completion
912+
*/
913+
public void deleteTemplateAsync(DeleteIndexTemplateRequest request, RequestOptions options,
914+
ActionListener<AcknowledgedResponse> listener) {
915+
restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::deleteTemplate,
916+
options, AcknowledgedResponse::fromXContent, listener, emptySet());
917+
}
889918
}

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;
@@ -427,4 +428,13 @@ static Request unfreezeIndex(UnfreezeIndexRequest unfreezeIndexRequest) {
427428
parameters.withWaitForActiveShards(unfreezeIndexRequest.getWaitForActiveShards());
428429
return request;
429430
}
431+
432+
static Request deleteTemplate(DeleteIndexTemplateRequest deleteIndexTemplateRequest) {
433+
String name = deleteIndexTemplateRequest.name();
434+
String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_template").addPathPart(name).build();
435+
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
436+
RequestConverters.Params params = new RequestConverters.Params(request);
437+
params.withMasterTimeout(deleteIndexTemplateRequest.masterNodeTimeout());
438+
return request;
439+
}
430440
}

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
@@ -60,6 +60,7 @@
6060
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
6161
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
6262
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
63+
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
6364
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
6465
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
6566
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
@@ -1313,7 +1314,7 @@ public void testInvalidValidateQuery() throws IOException{
13131314
assertFalse(response.isValid());
13141315
}
13151316

1316-
public void testGetIndexTemplate() throws Exception {
1317+
public void testCRUDIndexTemplate() throws Exception {
13171318
RestHighLevelClient client = highLevelClient();
13181319

13191320
PutIndexTemplateRequest putTemplate1 = new PutIndexTemplateRequest().name("template-1")
@@ -1355,9 +1356,22 @@ public void testGetIndexTemplate() throws Exception {
13551356
assertThat(getBoth.getIndexTemplates().stream().map(IndexTemplateMetaData::getName).toArray(),
13561357
arrayContainingInAnyOrder("template-1", "template-2"));
13571358

1358-
ElasticsearchException notFound = expectThrows(ElasticsearchException.class, () -> execute(
1359-
new GetIndexTemplatesRequest().names("the-template-*"), client.indices()::getTemplate, client.indices()::getTemplateAsync));
1360-
assertThat(notFound.status(), equalTo(RestStatus.NOT_FOUND));
1359+
assertTrue(execute(new DeleteIndexTemplateRequest("template-1"),
1360+
client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync).isAcknowledged());
1361+
assertThat(expectThrows(ElasticsearchException.class, () -> execute(new GetIndexTemplatesRequest().names("template-1"),
1362+
client.indices()::getTemplate, client.indices()::getTemplateAsync)).status(), equalTo(RestStatus.NOT_FOUND));
1363+
assertThat(expectThrows(ElasticsearchException.class, () -> execute(new DeleteIndexTemplateRequest("template-1"),
1364+
client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync)).status(), equalTo(RestStatus.NOT_FOUND));
1365+
1366+
assertThat(execute(new GetIndexTemplatesRequest("template-*"),
1367+
client.indices()::getTemplate, client.indices()::getTemplateAsync).getIndexTemplates(), hasSize(1));
1368+
assertThat(execute(new GetIndexTemplatesRequest("template-*"),
1369+
client.indices()::getTemplate, client.indices()::getTemplateAsync).getIndexTemplates().get(0).name(), equalTo("template-2"));
1370+
1371+
assertTrue(execute(new DeleteIndexTemplateRequest("template-*"),
1372+
client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync).isAcknowledged());
1373+
assertThat(expectThrows(ElasticsearchException.class, () -> execute(new GetIndexTemplatesRequest().names("template-*"),
1374+
client.indices()::getTemplate, client.indices()::getTemplateAsync)).status(), equalTo(RestStatus.NOT_FOUND));
13611375
}
13621376

13631377
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;
@@ -890,4 +891,21 @@ public void testGetTemplateRequest() throws Exception {
890891
Assert.assertThat(request.getParameters(), equalTo(expectedParams));
891892
Assert.assertThat(request.getEntity(), nullValue());
892893
}
894+
895+
public void testDeleteTemplateRequest() {
896+
Map<String, String> encodes = new HashMap<>();
897+
encodes.put("log", "log");
898+
encodes.put("1", "1");
899+
encodes.put("template#1", "template%231");
900+
encodes.put("template-*", "template-*");
901+
encodes.put("foo^bar", "foo%5Ebar");
902+
DeleteIndexTemplateRequest deleteTemplateRequest = new DeleteIndexTemplateRequest().name(randomFrom(encodes.keySet()));
903+
Map<String, String> expectedParams = new HashMap<>();
904+
RequestConvertersTests.setRandomMasterTimeout(deleteTemplateRequest, expectedParams);
905+
Request request = IndicesRequestConverters.deleteTemplate(deleteTemplateRequest);
906+
Assert.assertThat(request.getMethod(), equalTo(HttpDelete.METHOD_NAME));
907+
Assert.assertThat(request.getEndpoint(), equalTo("/_template/" + encodes.get(deleteTemplateRequest.name())));
908+
Assert.assertThat(request.getParameters(), equalTo(expectedParams));
909+
Assert.assertThat(request.getEntity(), nullValue());
910+
}
893911
}

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
@@ -669,7 +669,6 @@ public void testApiNamingConventions() throws Exception {
669669
"create",
670670
"get_source",
671671
"indices.delete_alias",
672-
"indices.delete_template",
673672
"indices.exists_template",
674673
"indices.exists_type",
675674
"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;
@@ -2686,4 +2687,64 @@ public void onFailure(Exception e) {
26862687
// end::unfreeze-index-notfound
26872688
}
26882689
}
2690+
2691+
public void testDeleteTemplate() throws Exception {
2692+
RestHighLevelClient client = highLevelClient();
2693+
{
2694+
PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template");
2695+
putRequest.patterns(Arrays.asList("pattern-1", "log-*"));
2696+
putRequest.settings(Settings.builder().put("index.number_of_shards", 3));
2697+
assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
2698+
}
2699+
2700+
// tag::delete-template-request
2701+
DeleteIndexTemplateRequest request = new DeleteIndexTemplateRequest();
2702+
request.name("my-template"); // <1>
2703+
// end::delete-template-request
2704+
2705+
// tag::delete-template-request-masterTimeout
2706+
request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); // <1>
2707+
request.masterNodeTimeout("1m"); // <2>
2708+
// end::delete-template-request-masterTimeout
2709+
2710+
// tag::delete-template-execute
2711+
AcknowledgedResponse deleteTemplateAcknowledge = client.indices().deleteTemplate(request, RequestOptions.DEFAULT);
2712+
// end::delete-template-execute
2713+
2714+
// tag::delete-template-response
2715+
boolean acknowledged = deleteTemplateAcknowledge.isAcknowledged(); // <1>
2716+
// end::delete-template-response
2717+
assertThat(acknowledged, equalTo(true));
2718+
2719+
{
2720+
PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template");
2721+
putRequest.patterns(Arrays.asList("pattern-1", "log-*"));
2722+
putRequest.settings(Settings.builder().put("index.number_of_shards", 3));
2723+
assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
2724+
}
2725+
// tag::delete-template-execute-listener
2726+
ActionListener<AcknowledgedResponse> listener =
2727+
new ActionListener<AcknowledgedResponse>() {
2728+
@Override
2729+
public void onResponse(AcknowledgedResponse response) {
2730+
// <1>
2731+
}
2732+
2733+
@Override
2734+
public void onFailure(Exception e) {
2735+
// <2>
2736+
}
2737+
};
2738+
// end::delete-template-execute-listener
2739+
2740+
// Replace the empty listener by a blocking listener in test
2741+
final CountDownLatch latch = new CountDownLatch(1);
2742+
listener = new LatchedActionListener<>(listener, latch);
2743+
2744+
// tag::delete-template-execute-async
2745+
client.indices().deleteTemplateAsync(request, RequestOptions.DEFAULT, listener); // <1>
2746+
// end::get-templates-execute-async
2747+
2748+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
2749+
}
26892750
}
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)