HLRC: Add delete template API (#36320)

Relates #27205
This commit is contained in:
Nhat Nguyen 2018-12-07 10:46:03 -05:00 committed by GitHub
parent 844ff80778
commit 32a7d7abd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 169 additions and 5 deletions

View File

@ -53,6 +53,7 @@ import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
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;
@ -886,4 +887,32 @@ public final class IndicesClient {
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());
}
}

View File

@ -45,6 +45,7 @@ import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
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;
@ -427,4 +428,13 @@ final class IndicesRequestConverters {
parameters.withWaitForActiveShards(unfreezeIndexRequest.getWaitForActiveShards());
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;
}
}

View File

@ -60,6 +60,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.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;
@ -1313,7 +1314,7 @@ public class IndicesClientIT extends ESRestHighLevelClientTestCase {
assertFalse(response.isValid());
}
public void testGetIndexTemplate() throws Exception {
public void testCRUDIndexTemplate() throws Exception {
RestHighLevelClient client = highLevelClient();
PutIndexTemplateRequest putTemplate1 = new PutIndexTemplateRequest().name("template-1")
@ -1355,9 +1356,22 @@ public class IndicesClientIT extends ESRestHighLevelClientTestCase {
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 {

View File

@ -48,6 +48,7 @@ import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
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;
@ -890,4 +891,21 @@ public class IndicesRequestConvertersTests extends ESTestCase {
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());
}
}

View File

@ -669,7 +669,6 @@ public class RestHighLevelClientTests extends ESTestCase {
"create",
"get_source",
"indices.delete_alias",
"indices.delete_template",
"indices.exists_template",
"indices.exists_type",
"indices.get_upgrade",

View File

@ -59,6 +59,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.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;
@ -2686,4 +2687,64 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase
// 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::get-templates-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}

View File

@ -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[]

View File

@ -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