mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-24 17:09:48 +00:00
Added index templates REST support for HEAD and proper 404
* Added HEAD support for index templates to find out of they exist * Returning a 404 instead of a 200 if a GET hits on a non-existing index template Closes #3434
This commit is contained in:
parent
f2168d32c1
commit
94d3e27940
@ -62,6 +62,7 @@ import org.elasticsearch.rest.action.admin.indices.stats.RestIndicesStatsAction;
|
||||
import org.elasticsearch.rest.action.admin.indices.status.RestIndicesStatusAction;
|
||||
import org.elasticsearch.rest.action.admin.indices.template.delete.RestDeleteIndexTemplateAction;
|
||||
import org.elasticsearch.rest.action.admin.indices.template.get.RestGetIndexTemplateAction;
|
||||
import org.elasticsearch.rest.action.admin.indices.template.head.RestHeadIndexTemplateAction;
|
||||
import org.elasticsearch.rest.action.admin.indices.template.put.RestPutIndexTemplateAction;
|
||||
import org.elasticsearch.rest.action.admin.indices.validate.query.RestValidateQueryAction;
|
||||
import org.elasticsearch.rest.action.admin.indices.warmer.delete.RestDeleteWarmerAction;
|
||||
@ -147,6 +148,7 @@ public class RestActionModule extends AbstractModule {
|
||||
bind(RestGetIndexTemplateAction.class).asEagerSingleton();
|
||||
bind(RestPutIndexTemplateAction.class).asEagerSingleton();
|
||||
bind(RestDeleteIndexTemplateAction.class).asEagerSingleton();
|
||||
bind(RestHeadIndexTemplateAction.class).asEagerSingleton();
|
||||
|
||||
bind(RestPutWarmerAction.class).asEagerSingleton();
|
||||
bind(RestDeleteWarmerAction.class).asEagerSingleton();
|
||||
|
@ -39,6 +39,7 @@ import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.elasticsearch.rest.RestRequest.Method.GET;
|
||||
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
|
||||
import static org.elasticsearch.rest.RestStatus.OK;
|
||||
|
||||
/**
|
||||
@ -76,16 +77,21 @@ public class RestGetIndexTemplateAction extends BaseRestHandler {
|
||||
|
||||
try {
|
||||
MetaData metaData = response.getState().metaData();
|
||||
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
|
||||
builder.startObject();
|
||||
|
||||
for (IndexTemplateMetaData indexMetaData : metaData.templates().values()) {
|
||||
IndexTemplateMetaData.Builder.toXContent(indexMetaData, builder, params);
|
||||
if (metaData.templates().values().size() == 0) {
|
||||
channel.sendResponse(new StringRestResponse(NOT_FOUND));
|
||||
} else {
|
||||
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
|
||||
builder.startObject();
|
||||
|
||||
for (IndexTemplateMetaData indexMetaData : metaData.templates().values()) {
|
||||
IndexTemplateMetaData.Builder.toXContent(indexMetaData, builder, params);
|
||||
}
|
||||
|
||||
builder.endObject();
|
||||
|
||||
channel.sendResponse(new XContentRestResponse(request, OK, builder));
|
||||
}
|
||||
|
||||
builder.endObject();
|
||||
|
||||
channel.sendResponse(new XContentRestResponse(request, OK, builder));
|
||||
} catch (Throwable e) {
|
||||
onFailure(e);
|
||||
}
|
||||
|
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Licensed to ElasticSearch and Shay Banon under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. ElasticSearch licenses this
|
||||
* file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.rest.action.admin.indices.template.head;
|
||||
|
||||
import org.elasticsearch.ExceptionsHelper;
|
||||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
|
||||
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.client.Requests;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.rest.*;
|
||||
|
||||
import static org.elasticsearch.rest.RestRequest.Method.HEAD;
|
||||
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
|
||||
import static org.elasticsearch.rest.RestStatus.OK;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class RestHeadIndexTemplateAction extends BaseRestHandler {
|
||||
|
||||
@Inject
|
||||
public RestHeadIndexTemplateAction(Settings settings, Client client, RestController controller) {
|
||||
super(settings, client);
|
||||
|
||||
controller.registerHandler(HEAD, "/_template/{name}", this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleRequest(final RestRequest request, final RestChannel channel) {
|
||||
ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest()
|
||||
.filterRoutingTable(true)
|
||||
.filterNodes(true)
|
||||
.filteredIndexTemplates(request.param("name"))
|
||||
.filterOutIndices();
|
||||
|
||||
clusterStateRequest.listenerThreaded(false);
|
||||
|
||||
client.admin().cluster().state(clusterStateRequest, new ActionListener<ClusterStateResponse>() {
|
||||
@Override
|
||||
public void onResponse(ClusterStateResponse response) {
|
||||
try {
|
||||
boolean templateExists = response.getState().metaData().templates().size() > 0;
|
||||
|
||||
if (templateExists) {
|
||||
channel.sendResponse(new StringRestResponse(OK));
|
||||
} else {
|
||||
channel.sendResponse(new StringRestResponse(NOT_FOUND));
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
onFailure(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable e) {
|
||||
try {
|
||||
channel.sendResponse(new StringRestResponse(ExceptionsHelper.status(e)));
|
||||
} catch (Exception e1) {
|
||||
logger.error("Failed to send failure response", e1);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user