Skip to content

Commit

Permalink
HLRC: Add delete template API (#36320)
Browse files Browse the repository at this point in the history
Relates #27205
  • Loading branch information
dnhatn committed Dec 7, 2018
1 parent 4c22028 commit 37b54c9
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
Expand Down Expand Up @@ -1316,4 +1317,32 @@ public void unfreezeAsync(UnfreezeIndexRequest request, RequestOptions options,
ShardsAcknowledgedResponse::fromXContent, listener, emptySet());
}

/**
* Delete an index template using the Index Templates API
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
* on elastic.co</a>
*
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @throws IOException in case there is a problem sending the request or parsing back the response
*/
public AcknowledgedResponse deleteTemplate(DeleteIndexTemplateRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::deleteTemplate,
options, AcknowledgedResponse::fromXContent, emptySet());
}

/**
* Asynchronously delete an index template using the Index Templates API
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
* on elastic.co</a>
*
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion
*/
public void deleteTemplateAsync(DeleteIndexTemplateRequest request, RequestOptions options,
ActionListener<AcknowledgedResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::deleteTemplate,
options, AcknowledgedResponse::fromXContent, listener, emptySet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest;
Expand Down Expand Up @@ -428,4 +429,13 @@ static Request unfreezeIndex(UnfreezeIndexRequest unfreezeIndexRequest) {
parameters.withWaitForActiveShards(unfreezeIndexRequest.getWaitForActiveShards(), ActiveShardCount.DEFAULT);
return request;
}

static Request deleteTemplate(DeleteIndexTemplateRequest deleteIndexTemplateRequest) {
String name = deleteIndexTemplateRequest.name();
String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_template").addPathPart(name).build();
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
RequestConverters.Params params = new RequestConverters.Params(request);
params.withMasterTimeout(deleteIndexTemplateRequest.masterNodeTimeout());
return request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
Expand Down Expand Up @@ -1351,7 +1352,7 @@ public void testInvalidValidateQuery() throws IOException{
assertFalse(response.isValid());
}

public void testGetIndexTemplate() throws Exception {
public void testCRUDIndexTemplate() throws Exception {
RestHighLevelClient client = highLevelClient();

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

ElasticsearchException notFound = expectThrows(ElasticsearchException.class, () -> execute(
new GetIndexTemplatesRequest().names("the-template-*"), client.indices()::getTemplate, client.indices()::getTemplateAsync));
assertThat(notFound.status(), equalTo(RestStatus.NOT_FOUND));
assertTrue(execute(new DeleteIndexTemplateRequest("template-1"),
client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync).isAcknowledged());
assertThat(expectThrows(ElasticsearchException.class, () -> execute(new GetIndexTemplatesRequest().names("template-1"),
client.indices()::getTemplate, client.indices()::getTemplateAsync)).status(), equalTo(RestStatus.NOT_FOUND));
assertThat(expectThrows(ElasticsearchException.class, () -> execute(new DeleteIndexTemplateRequest("template-1"),
client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync)).status(), equalTo(RestStatus.NOT_FOUND));

assertThat(execute(new GetIndexTemplatesRequest("template-*"),
client.indices()::getTemplate, client.indices()::getTemplateAsync).getIndexTemplates(), hasSize(1));
assertThat(execute(new GetIndexTemplatesRequest("template-*"),
client.indices()::getTemplate, client.indices()::getTemplateAsync).getIndexTemplates().get(0).name(), equalTo("template-2"));

assertTrue(execute(new DeleteIndexTemplateRequest("template-*"),
client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync).isAcknowledged());
assertThat(expectThrows(ElasticsearchException.class, () -> execute(new GetIndexTemplatesRequest().names("template-*"),
client.indices()::getTemplate, client.indices()::getTemplateAsync)).status(), equalTo(RestStatus.NOT_FOUND));
}

public void testAnalyze() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest;
Expand Down Expand Up @@ -892,4 +893,21 @@ public void testGetTemplateRequest() throws Exception {
Assert.assertThat(request.getParameters(), equalTo(expectedParams));
Assert.assertThat(request.getEntity(), nullValue());
}

public void testDeleteTemplateRequest() {
Map<String, String> encodes = new HashMap<>();
encodes.put("log", "log");
encodes.put("1", "1");
encodes.put("template#1", "template%231");
encodes.put("template-*", "template-*");
encodes.put("foo^bar", "foo%5Ebar");
DeleteIndexTemplateRequest deleteTemplateRequest = new DeleteIndexTemplateRequest().name(randomFrom(encodes.keySet()));
Map<String, String> expectedParams = new HashMap<>();
RequestConvertersTests.setRandomMasterTimeout(deleteTemplateRequest, expectedParams);
Request request = IndicesRequestConverters.deleteTemplate(deleteTemplateRequest);
Assert.assertThat(request.getMethod(), equalTo(HttpDelete.METHOD_NAME));
Assert.assertThat(request.getEndpoint(), equalTo("/_template/" + encodes.get(deleteTemplateRequest.name())));
Assert.assertThat(request.getParameters(), equalTo(expectedParams));
Assert.assertThat(request.getEntity(), nullValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,6 @@ public void testApiNamingConventions() throws Exception {
"create",
"get_source",
"indices.delete_alias",
"indices.delete_template",
"indices.exists_template",
"indices.exists_type",
"indices.get_upgrade",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
Expand Down Expand Up @@ -2674,4 +2675,64 @@ public void onFailure(Exception e) {
// end::unfreeze-index-notfound
}
}

public void testDeleteTemplate() throws Exception {
RestHighLevelClient client = highLevelClient();
{
PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template");
putRequest.patterns(Arrays.asList("pattern-1", "log-*"));
putRequest.settings(Settings.builder().put("index.number_of_shards", 3));
assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
}

// tag::delete-template-request
DeleteIndexTemplateRequest request = new DeleteIndexTemplateRequest();
request.name("my-template"); // <1>
// end::delete-template-request

// tag::delete-template-request-masterTimeout
request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); // <1>
request.masterNodeTimeout("1m"); // <2>
// end::delete-template-request-masterTimeout

// tag::delete-template-execute
AcknowledgedResponse deleteTemplateAcknowledge = client.indices().deleteTemplate(request, RequestOptions.DEFAULT);
// end::delete-template-execute

// tag::delete-template-response
boolean acknowledged = deleteTemplateAcknowledge.isAcknowledged(); // <1>
// end::delete-template-response
assertThat(acknowledged, equalTo(true));

{
PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template");
putRequest.patterns(Arrays.asList("pattern-1", "log-*"));
putRequest.settings(Settings.builder().put("index.number_of_shards", 3));
assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
}
// tag::delete-template-execute-listener
ActionListener<AcknowledgedResponse> listener =
new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(AcknowledgedResponse response) {
// <1>
}

@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::delete-template-execute-listener

// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);

// tag::delete-template-execute-async
client.indices().deleteTemplateAsync(request, RequestOptions.DEFAULT, listener); // <1>
// end::delete-template-execute-async

assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}
32 changes: 32 additions & 0 deletions docs/java-rest/high-level/indices/delete_template.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--
:api: delete-template
:request: DeleteIndexTemplateRequest
:response: AcknowledgedResponse
--

[id="{upid}-{api}"]
=== Delete Template API

[id="{upid}-{api}-request"]
==== Request

The Delete Template API allows you to delete an index template.

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-request]
--------------------------------------------------
<1> The name of an index template to delete.

[id="{upid}-{api}-response"]
==== Response

The returned +{response}+ indicates if the delete template request was received.

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-response]
--------------------------------------------------
<1> Whether or not the delete template request was acknowledged.

include::../execution.asciidoc[]
1 change: 1 addition & 0 deletions docs/java-rest/high-level/supported-apis.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ include::indices/get_templates.asciidoc[]
include::indices/get_index.asciidoc[]
include::indices/freeze_index.asciidoc[]
include::indices/unfreeze_index.asciidoc[]
include::indices/delete_template.asciidoc[]

== Cluster APIs

Expand Down

0 comments on commit 37b54c9

Please sign in to comment.