Support for REST get ALL templates.
/_template shows: No handler found for uri [/_template] and method [GET] It would make sense to list the templates as they are listed in the /_cluster/state call. Closes #2532.
This commit is contained in:
parent
e3b826f87e
commit
ea4988e9dc
|
@ -39,7 +39,7 @@ curl -XDELETE localhost:9200/_template/template_1
|
|||
--------------------------------------------------
|
||||
|
||||
[float]
|
||||
=== GETting a Template
|
||||
=== GETting templates
|
||||
|
||||
Index templates are identified by a name (in the above case
|
||||
`template_1`) and can be retrieved using the following:
|
||||
|
@ -54,12 +54,21 @@ You can also match several templates by using wildcards like:
|
|||
[source,js]
|
||||
--------------------------------------------------
|
||||
curl -XGET localhost:9200/_template/temp*
|
||||
curl -XGET localhost:9200/_template/template_1,template_2
|
||||
--------------------------------------------------
|
||||
|
||||
To get list of all index templates you can use
|
||||
<<cluster-state,Cluster State>> API
|
||||
and check for the metadata/templates section of the response.
|
||||
|
||||
added::[0.90.4] Or run:
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
curl -XGET localhost:9200/_template/
|
||||
--------------------------------------------------
|
||||
|
||||
|
||||
[float]
|
||||
=== Multiple Template Matching
|
||||
|
||||
|
|
|
@ -18,8 +18,10 @@
|
|||
*/
|
||||
package org.elasticsearch.action.admin.indices.template.get;
|
||||
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.action.ActionRequestValidationException;
|
||||
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
|
||||
|
@ -32,19 +34,31 @@ import static org.elasticsearch.action.ValidateActions.addValidationError;
|
|||
*/
|
||||
public class GetIndexTemplatesRequest extends MasterNodeOperationRequest<GetIndexTemplatesRequest> {
|
||||
|
||||
private String name;
|
||||
private String[] names;
|
||||
|
||||
public GetIndexTemplatesRequest() {}
|
||||
|
||||
@Deprecated
|
||||
public GetIndexTemplatesRequest(String name) {
|
||||
this.name = name;
|
||||
this.names = new String[1];
|
||||
this.names[0] = name;
|
||||
}
|
||||
|
||||
public GetIndexTemplatesRequest(String... names) {
|
||||
this.names = names;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ActionRequestValidationException validate() {
|
||||
ActionRequestValidationException validationException = null;
|
||||
if (name == null) {
|
||||
validationException = addValidationError("name is missing", validationException);
|
||||
if (names == null) {
|
||||
validationException = addValidationError("names is null", validationException);
|
||||
} else {
|
||||
for (String name : names) {
|
||||
if (name == null || !Strings.hasText(name)) {
|
||||
validationException = addValidationError("name is missing", validationException);
|
||||
}
|
||||
}
|
||||
}
|
||||
return validationException;
|
||||
}
|
||||
|
@ -52,27 +66,57 @@ public class GetIndexTemplatesRequest extends MasterNodeOperationRequest<GetInde
|
|||
/**
|
||||
* Sets the name of the index template.
|
||||
*/
|
||||
@Deprecated
|
||||
public GetIndexTemplatesRequest name(String name) {
|
||||
this.name = name;
|
||||
this.names = new String[1];
|
||||
this.names[0] = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The name of the index template.
|
||||
*/
|
||||
@Deprecated
|
||||
public String name() {
|
||||
return this.name;
|
||||
if (this.names != null && this.names.length > 0) {
|
||||
return this.names[0];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the names of the index templates.
|
||||
*/
|
||||
public GetIndexTemplatesRequest names(String... names) {
|
||||
this.names = names;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The names of the index templates.
|
||||
*/
|
||||
public String[] names() {
|
||||
return this.names;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFrom(StreamInput in) throws IOException {
|
||||
super.readFrom(in);
|
||||
name = in.readString();
|
||||
if (in.getVersion().onOrAfter(Version.V_0_90_4)) {
|
||||
names = in.readStringArray();
|
||||
} else {
|
||||
names = new String[1];
|
||||
names[0] = in.readString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
super.writeTo(out);
|
||||
out.writeString(name);
|
||||
if (out.getVersion().onOrAfter(Version.V_0_90_4)) {
|
||||
out.writeStringArray(names);
|
||||
} else {
|
||||
out.writeString(names[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,10 +32,15 @@ public class GetIndexTemplatesRequestBuilder extends MasterNodeOperationRequestB
|
|||
super((InternalIndicesAdminClient) indicesClient, new GetIndexTemplatesRequest());
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public GetIndexTemplatesRequestBuilder(IndicesAdminClient indicesClient, String name) {
|
||||
super((InternalIndicesAdminClient) indicesClient, new GetIndexTemplatesRequest(name));
|
||||
}
|
||||
|
||||
public GetIndexTemplatesRequestBuilder(IndicesAdminClient indicesClient, String... names) {
|
||||
super((InternalIndicesAdminClient) indicesClient, new GetIndexTemplatesRequest(names));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doExecute(ActionListener<GetIndexTemplatesResponse> listener) {
|
||||
((IndicesAdminClient) client).getTemplates(request, listener);
|
||||
|
|
|
@ -68,14 +68,21 @@ public class TransportGetIndexTemplatesAction extends TransportMasterNodeOperati
|
|||
protected void masterOperation(GetIndexTemplatesRequest request, ClusterState state, ActionListener<GetIndexTemplatesResponse> listener) throws ElasticSearchException {
|
||||
List<IndexTemplateMetaData> results = Lists.newArrayList();
|
||||
|
||||
if (Regex.isSimpleMatchPattern(request.name())) {
|
||||
for (Map.Entry<String, IndexTemplateMetaData> entry : state.metaData().templates().entrySet()) {
|
||||
if (Regex.simpleMatch(request.name(), entry.getKey())) {
|
||||
results.add(entry.getValue());
|
||||
// If we did not ask for a specific name, then we return all templates
|
||||
if (request.names().length == 0) {
|
||||
results.addAll(state.metaData().templates().values());
|
||||
}
|
||||
|
||||
for (String name : request.names()) {
|
||||
if (Regex.isSimpleMatchPattern(name)) {
|
||||
for (Map.Entry<String, IndexTemplateMetaData> entry : state.metaData().templates().entrySet()) {
|
||||
if (Regex.simpleMatch(name, entry.getKey())) {
|
||||
results.add(entry.getValue());
|
||||
}
|
||||
}
|
||||
} else if (state.metaData().templates().containsKey(name)) {
|
||||
results.add(state.metaData().templates().get(name));
|
||||
}
|
||||
} else if (state.metaData().templates().containsKey(request.name())) {
|
||||
results.add(state.metaData().templates().get(request.name()));
|
||||
}
|
||||
|
||||
listener.onResponse(new GetIndexTemplatesResponse(results));
|
||||
|
|
|
@ -654,8 +654,14 @@ public interface IndicesAdminClient {
|
|||
*
|
||||
* @param name The name of the template.
|
||||
*/
|
||||
@Deprecated
|
||||
GetIndexTemplatesRequestBuilder prepareGetTemplates(String name);
|
||||
|
||||
/**
|
||||
* Gets an index template (optional).
|
||||
*/
|
||||
GetIndexTemplatesRequestBuilder prepareGetTemplates(String... name);
|
||||
|
||||
/**
|
||||
* Validate a query for correctness.
|
||||
*
|
||||
|
|
|
@ -509,11 +509,16 @@ public abstract class AbstractIndicesAdminClient implements InternalIndicesAdmin
|
|||
execute(GetIndexTemplatesAction.INSTANCE, request, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override @Deprecated
|
||||
public GetIndexTemplatesRequestBuilder prepareGetTemplates(String name) {
|
||||
return new GetIndexTemplatesRequestBuilder(this, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GetIndexTemplatesRequestBuilder prepareGetTemplates(String... names) {
|
||||
return new GetIndexTemplatesRequestBuilder(this, names);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ActionFuture<DeleteIndexTemplateResponse> deleteTemplate(final DeleteIndexTemplateRequest request) {
|
||||
return execute(DeleteIndexTemplateAction.INSTANCE, request);
|
||||
|
|
|
@ -26,6 +26,7 @@ import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequ
|
|||
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
|
@ -48,12 +49,15 @@ public class RestGetIndexTemplateAction extends BaseRestHandler {
|
|||
public RestGetIndexTemplateAction(Settings settings, Client client, RestController controller) {
|
||||
super(settings, client);
|
||||
|
||||
controller.registerHandler(GET, "/_template", this);
|
||||
controller.registerHandler(GET, "/_template/{name}", this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleRequest(final RestRequest request, final RestChannel channel) {
|
||||
GetIndexTemplatesRequest getIndexTemplatesRequest = new GetIndexTemplatesRequest(request.param("name"));
|
||||
final String[] names = Strings.splitStringByCommaToArray(request.param("name"));
|
||||
|
||||
GetIndexTemplatesRequest getIndexTemplatesRequest = new GetIndexTemplatesRequest(names);
|
||||
getIndexTemplatesRequest.listenerThreaded(false);
|
||||
|
||||
client.admin().indices().getTemplates(getIndexTemplatesRequest, new ActionListener<GetIndexTemplatesResponse>() {
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
package org.elasticsearch.indices.template;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.elasticsearch.action.ActionRequestValidationException;
|
||||
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.common.Priority;
|
||||
|
@ -32,7 +33,6 @@ import java.util.Arrays;
|
|||
import java.util.List;
|
||||
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
/**
|
||||
|
@ -218,5 +218,49 @@ public class SimpleIndexTemplateTests extends AbstractSharedClusterTest {
|
|||
templateNames.add(getTemplate1Response.getIndexTemplates().get(0).name());
|
||||
templateNames.add(getTemplate1Response.getIndexTemplates().get(1).name());
|
||||
assertThat(templateNames, containsInAnyOrder("template_1", "template_2"));
|
||||
|
||||
logger.info("--> get all templates");
|
||||
getTemplate1Response = client().admin().indices().prepareGetTemplates().execute().actionGet();
|
||||
assertThat(getTemplate1Response.getIndexTemplates(), hasSize(3));
|
||||
|
||||
templateNames = Lists.newArrayList();
|
||||
templateNames.add(getTemplate1Response.getIndexTemplates().get(0).name());
|
||||
templateNames.add(getTemplate1Response.getIndexTemplates().get(1).name());
|
||||
templateNames.add(getTemplate1Response.getIndexTemplates().get(2).name());
|
||||
assertThat(templateNames, containsInAnyOrder("template_1", "template_2", "template3"));
|
||||
|
||||
logger.info("--> get templates template_1 and template_2");
|
||||
getTemplate1Response = client().admin().indices().prepareGetTemplates("template_1", "template_2").execute().actionGet();
|
||||
assertThat(getTemplate1Response.getIndexTemplates(), hasSize(2));
|
||||
|
||||
templateNames = Lists.newArrayList();
|
||||
templateNames.add(getTemplate1Response.getIndexTemplates().get(0).name());
|
||||
templateNames.add(getTemplate1Response.getIndexTemplates().get(1).name());
|
||||
assertThat(templateNames, containsInAnyOrder("template_1", "template_2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThatInvalidGetIndexTemplatesFails() throws Exception {
|
||||
logger.info("--> get template null");
|
||||
testExpectActionRequestValidationException(null);
|
||||
|
||||
logger.info("--> get template empty");
|
||||
testExpectActionRequestValidationException("");
|
||||
|
||||
logger.info("--> get template 'a', '', 'c'");
|
||||
testExpectActionRequestValidationException("a", "", "c");
|
||||
|
||||
logger.info("--> get template 'a', null, 'c'");
|
||||
testExpectActionRequestValidationException("a", null, "c");
|
||||
}
|
||||
|
||||
private void testExpectActionRequestValidationException(String... names) {
|
||||
try {
|
||||
client().admin().indices().prepareGetTemplates(names).execute().actionGet();
|
||||
fail("We should have raised an ActionRequestValidationException for " + names);
|
||||
} catch (ActionRequestValidationException e) {
|
||||
// That's fine!
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue