|
| 1 | +/* |
| 2 | + * Licensed to ElasticSearch and Shay Banon under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. ElasticSearch licenses this |
| 6 | + * file to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.rest.action.cat; |
| 20 | + |
| 21 | +import com.carrotsearch.hppc.cursors.ObjectObjectCursor; |
| 22 | +import org.elasticsearch.action.ActionListener; |
| 23 | +import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest; |
| 24 | +import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; |
| 25 | +import org.elasticsearch.client.Client; |
| 26 | +import org.elasticsearch.cluster.metadata.AliasMetaData; |
| 27 | +import org.elasticsearch.common.Strings; |
| 28 | +import org.elasticsearch.common.Table; |
| 29 | +import org.elasticsearch.common.collect.ImmutableOpenMap; |
| 30 | +import org.elasticsearch.common.inject.Inject; |
| 31 | +import org.elasticsearch.common.settings.Settings; |
| 32 | +import org.elasticsearch.rest.RestChannel; |
| 33 | +import org.elasticsearch.rest.RestController; |
| 34 | +import org.elasticsearch.rest.RestRequest; |
| 35 | +import org.elasticsearch.rest.XContentThrowableRestResponse; |
| 36 | +import org.elasticsearch.rest.action.support.RestTable; |
| 37 | + |
| 38 | +import java.io.IOException; |
| 39 | +import java.util.Iterator; |
| 40 | + |
| 41 | +import static org.elasticsearch.rest.RestRequest.Method.GET; |
| 42 | + |
| 43 | +/** |
| 44 | + * |
| 45 | + */ |
| 46 | +public class RestAliasAction extends AbstractCatAction { |
| 47 | + |
| 48 | + @Inject |
| 49 | + public RestAliasAction(Settings settings, Client client, RestController controller) { |
| 50 | + super(settings, client); |
| 51 | + controller.registerHandler(GET, "/_cat/aliases", this); |
| 52 | + controller.registerHandler(GET, "/_cat/aliases/{alias}", this); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + @Override |
| 57 | + void doRequest(final RestRequest request, final RestChannel channel) { |
| 58 | + final ClusterStateRequest clusterStateRequest = new ClusterStateRequest(); |
| 59 | + clusterStateRequest.filterMetaData(true); |
| 60 | + clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local())); |
| 61 | + clusterStateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterStateRequest.masterNodeTimeout())); |
| 62 | + clusterStateRequest.filterAll().filterMetaData(false); |
| 63 | + |
| 64 | + client.admin().cluster().state(clusterStateRequest, new ActionListener<ClusterStateResponse>() { |
| 65 | + |
| 66 | + @Override |
| 67 | + public void onResponse(ClusterStateResponse response) { |
| 68 | + try { |
| 69 | + Table tab = buildTable(request, response); |
| 70 | + channel.sendResponse(RestTable.buildResponse(tab, request, channel)); |
| 71 | + } catch (Throwable e) { |
| 72 | + onFailure(e); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onFailure(Throwable e) { |
| 78 | + try { |
| 79 | + channel.sendResponse(new XContentThrowableRestResponse(request, e)); |
| 80 | + } catch (IOException e1) { |
| 81 | + logger.error("Failed to send failure response", e1); |
| 82 | + } |
| 83 | + } |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + void documentation(StringBuilder sb) { |
| 89 | + sb.append("/_cat_alias"); |
| 90 | + sb.append("/_cat_alias/{alias}"); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + Table getTableWithHeader(RestRequest request) { |
| 95 | + final Table table = new Table(); |
| 96 | + table.startHeaders(); |
| 97 | + table.addCell("alias", "desc:alias name"); |
| 98 | + table.addCell("index", "desc:index alias points to"); |
| 99 | + table.addCell("filter", "desc:filter"); |
| 100 | + table.addCell("index_routing", "desc:index routing"); |
| 101 | + table.addCell("search_routing", "desc:search routing"); |
| 102 | + table.endHeaders(); |
| 103 | + return table; |
| 104 | + } |
| 105 | + |
| 106 | + private Table buildTable(RestRequest request, ClusterStateResponse response) { |
| 107 | + Table table = getTableWithHeader(request); |
| 108 | + |
| 109 | + for (ObjectObjectCursor<String, ImmutableOpenMap<String, AliasMetaData>> cursor : response.getState().getMetaData().aliases()) { |
| 110 | + String aliasName = cursor.key; |
| 111 | + Iterator<ObjectObjectCursor<String,AliasMetaData>> iterator = cursor.value.iterator(); |
| 112 | + while (iterator.hasNext()) { |
| 113 | + ObjectObjectCursor<String, AliasMetaData> iteratorCursor = iterator.next(); |
| 114 | + String indexName = iteratorCursor.key; |
| 115 | + AliasMetaData aliasMetaData = iteratorCursor.value; |
| 116 | + |
| 117 | + table.startRow(); |
| 118 | + table.addCell(aliasName); |
| 119 | + table.addCell(indexName); |
| 120 | + table.addCell(aliasMetaData.filteringRequired() ? "*" : "-"); |
| 121 | + String indexRouting = Strings.hasLength(aliasMetaData.indexRouting()) ? aliasMetaData.indexRouting() : "-"; |
| 122 | + table.addCell(indexRouting); |
| 123 | + String searchRouting = Strings.hasLength(aliasMetaData.searchRouting()) ? aliasMetaData.searchRouting() : "-"; |
| 124 | + table.addCell(searchRouting); |
| 125 | + table.endRow(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return table; |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments