Skip to content

Commit a32d1b9

Browse files
committed
Remove comma-separated feature parsing for GetIndicesAction
This removes the parsing of things like `GET /idx/_aliases,_mappings`, instead, a user must choose between retriving all index metadata with `GET /idx`, or only a specific form such as `GET /idx/_settings`. Relates to (and is a prerequisite of) #24437
1 parent b9e2a1f commit a32d1b9

File tree

14 files changed

+367
-68
lines changed

14 files changed

+367
-68
lines changed

core/src/main/java/org/elasticsearch/action/ActionModule.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@
253253
import org.elasticsearch.rest.action.admin.indices.RestFlushAction;
254254
import org.elasticsearch.rest.action.admin.indices.RestForceMergeAction;
255255
import org.elasticsearch.rest.action.admin.indices.RestGetAliasesAction;
256+
import org.elasticsearch.rest.action.admin.indices.RestGetAllAliasesAction;
257+
import org.elasticsearch.rest.action.admin.indices.RestGetAllMappingsAction;
258+
import org.elasticsearch.rest.action.admin.indices.RestGetAllSettingsAction;
256259
import org.elasticsearch.rest.action.admin.indices.RestGetFieldMappingAction;
257260
import org.elasticsearch.rest.action.admin.indices.RestGetIndexTemplateAction;
258261
import org.elasticsearch.rest.action.admin.indices.RestGetIndicesAction;
@@ -541,6 +544,9 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
541544
registerHandler.accept(new RestDeleteSnapshotAction(settings, restController));
542545
registerHandler.accept(new RestSnapshotsStatusAction(settings, restController));
543546

547+
registerHandler.accept(new RestGetAllAliasesAction(settings, restController));
548+
registerHandler.accept(new RestGetAllMappingsAction(settings, restController));
549+
registerHandler.accept(new RestGetAllSettingsAction(settings, restController, indexScopedSettings, settingsFilter));
544550
registerHandler.accept(new RestTypesExistsAction(settings, restController));
545551
registerHandler.accept(new RestGetIndicesAction(settings, restController, indexScopedSettings, settingsFilter));
546552
registerHandler.accept(new RestIndicesStatsAction(settings, restController));

core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetAliasesAction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public RestGetAliasesAction(final Settings settings, final RestController contro
5757
super(settings);
5858
controller.registerHandler(GET, "/_alias/{name}", this);
5959
controller.registerHandler(HEAD, "/_alias/{name}", this);
60+
controller.registerHandler(GET, "/{index}/_alias", this);
61+
controller.registerHandler(HEAD, "/{index}/_alias", this);
6062
controller.registerHandler(GET, "/{index}/_alias/{name}", this);
6163
controller.registerHandler(HEAD, "/{index}/_alias/{name}", this);
6264
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* 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+
20+
package org.elasticsearch.rest.action.admin.indices;
21+
22+
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
23+
import org.elasticsearch.action.admin.indices.get.GetIndexRequest.Feature;
24+
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
25+
import org.elasticsearch.action.support.IndicesOptions;
26+
import org.elasticsearch.client.node.NodeClient;
27+
import org.elasticsearch.cluster.metadata.AliasMetaData;
28+
import org.elasticsearch.common.Strings;
29+
import org.elasticsearch.common.settings.IndexScopedSettings;
30+
import org.elasticsearch.common.settings.Settings;
31+
import org.elasticsearch.common.settings.SettingsFilter;
32+
import org.elasticsearch.common.xcontent.ToXContent.Params;
33+
import org.elasticsearch.common.xcontent.XContentBuilder;
34+
import org.elasticsearch.rest.BaseRestHandler;
35+
import org.elasticsearch.rest.BytesRestResponse;
36+
import org.elasticsearch.rest.RestController;
37+
import org.elasticsearch.rest.RestRequest;
38+
import org.elasticsearch.rest.RestResponse;
39+
import org.elasticsearch.rest.action.RestBuilderListener;
40+
41+
import java.io.IOException;
42+
import java.util.List;
43+
import java.util.Set;
44+
45+
import static org.elasticsearch.rest.RestRequest.Method.GET;
46+
import static org.elasticsearch.rest.RestRequest.Method.HEAD;
47+
import static org.elasticsearch.rest.RestStatus.OK;
48+
49+
/**
50+
* The REST handler for retrieving all aliases
51+
*/
52+
public class RestGetAllAliasesAction extends BaseRestHandler {
53+
54+
public RestGetAllAliasesAction(final Settings settings, final RestController controller) {
55+
super(settings);
56+
controller.registerHandler(GET, "/_alias", this);
57+
controller.registerHandler(GET, "/_aliases", this);
58+
}
59+
60+
@Override
61+
public String getName() {
62+
return "get_all_aliases_action";
63+
}
64+
65+
@Override
66+
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
67+
final GetIndexRequest getIndexRequest = new GetIndexRequest();
68+
getIndexRequest.indices(Strings.EMPTY_ARRAY);
69+
getIndexRequest.features(Feature.ALIASES);
70+
getIndexRequest.indicesOptions(IndicesOptions.fromRequest(request, getIndexRequest.indicesOptions()));
71+
getIndexRequest.local(request.paramAsBoolean("local", getIndexRequest.local()));
72+
getIndexRequest.humanReadable(request.paramAsBoolean("human", false));
73+
return channel -> client.admin().indices().getIndex(getIndexRequest, new RestBuilderListener<GetIndexResponse>(channel) {
74+
75+
@Override
76+
public RestResponse buildResponse(final GetIndexResponse response, final XContentBuilder builder) throws Exception {
77+
builder.startObject();
78+
{
79+
for (final String index : response.indices()) {
80+
builder.startObject(index);
81+
{
82+
writeAliases(response.aliases().get(index), builder, request);
83+
}
84+
builder.endObject();
85+
}
86+
}
87+
builder.endObject();
88+
89+
return new BytesRestResponse(OK, builder);
90+
}
91+
92+
private void writeAliases(final List<AliasMetaData> aliases, final XContentBuilder builder,
93+
final Params params) throws IOException {
94+
builder.startObject("aliases");
95+
{
96+
if (aliases != null) {
97+
for (final AliasMetaData alias : aliases) {
98+
AliasMetaData.Builder.toXContent(alias, builder, params);
99+
}
100+
}
101+
}
102+
builder.endObject();
103+
}
104+
});
105+
}
106+
107+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* 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+
20+
package org.elasticsearch.rest.action.admin.indices;
21+
22+
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
23+
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
24+
import org.elasticsearch.action.admin.indices.get.GetIndexRequest.Feature;
25+
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
26+
import org.elasticsearch.action.support.IndicesOptions;
27+
import org.elasticsearch.client.node.NodeClient;
28+
import org.elasticsearch.cluster.metadata.AliasMetaData;
29+
import org.elasticsearch.cluster.metadata.MappingMetaData;
30+
import org.elasticsearch.common.Strings;
31+
import org.elasticsearch.common.collect.ImmutableOpenMap;
32+
import org.elasticsearch.common.settings.IndexScopedSettings;
33+
import org.elasticsearch.common.settings.Settings;
34+
import org.elasticsearch.common.settings.SettingsFilter;
35+
import org.elasticsearch.common.xcontent.ToXContent.Params;
36+
import org.elasticsearch.common.xcontent.XContentBuilder;
37+
import org.elasticsearch.rest.BaseRestHandler;
38+
import org.elasticsearch.rest.BytesRestResponse;
39+
import org.elasticsearch.rest.RestController;
40+
import org.elasticsearch.rest.RestRequest;
41+
import org.elasticsearch.rest.RestResponse;
42+
import org.elasticsearch.rest.action.RestBuilderListener;
43+
44+
import java.io.IOException;
45+
import java.util.List;
46+
import java.util.Set;
47+
48+
import static org.elasticsearch.rest.RestRequest.Method.GET;
49+
import static org.elasticsearch.rest.RestRequest.Method.HEAD;
50+
import static org.elasticsearch.rest.RestStatus.OK;
51+
52+
/**
53+
* The REST handler for retrieving all mappings
54+
*/
55+
public class RestGetAllMappingsAction extends BaseRestHandler {
56+
57+
public RestGetAllMappingsAction(final Settings settings, final RestController controller) {
58+
super(settings);
59+
controller.registerHandler(GET, "/_mapping", this);
60+
controller.registerHandler(GET, "/_mappings", this);
61+
}
62+
63+
@Override
64+
public String getName() {
65+
return "get_all_mappings_action";
66+
}
67+
68+
@Override
69+
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
70+
final GetIndexRequest getIndexRequest = new GetIndexRequest();
71+
getIndexRequest.indices(Strings.EMPTY_ARRAY);
72+
getIndexRequest.features(Feature.MAPPINGS);
73+
getIndexRequest.indicesOptions(IndicesOptions.fromRequest(request, getIndexRequest.indicesOptions()));
74+
getIndexRequest.local(request.paramAsBoolean("local", getIndexRequest.local()));
75+
getIndexRequest.humanReadable(request.paramAsBoolean("human", false));
76+
return channel -> client.admin().indices().getIndex(getIndexRequest, new RestBuilderListener<GetIndexResponse>(channel) {
77+
78+
@Override
79+
public RestResponse buildResponse(final GetIndexResponse response, final XContentBuilder builder) throws Exception {
80+
builder.startObject();
81+
{
82+
for (final String index : response.indices()) {
83+
builder.startObject(index);
84+
{
85+
writeMappings(response.mappings().get(index), builder);
86+
}
87+
builder.endObject();
88+
}
89+
}
90+
builder.endObject();
91+
92+
return new BytesRestResponse(OK, builder);
93+
}
94+
95+
private void writeMappings(final ImmutableOpenMap<String, MappingMetaData> mappings,
96+
final XContentBuilder builder) throws IOException {
97+
builder.startObject("mappings");
98+
{
99+
for (final ObjectObjectCursor<String, MappingMetaData> typeEntry : mappings) {
100+
builder.field(typeEntry.key);
101+
builder.map(typeEntry.value.sourceAsMap());
102+
}
103+
}
104+
builder.endObject();
105+
}
106+
});
107+
}
108+
109+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* 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+
20+
package org.elasticsearch.rest.action.admin.indices;
21+
22+
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
23+
import org.elasticsearch.action.admin.indices.get.GetIndexRequest.Feature;
24+
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
25+
import org.elasticsearch.action.support.IndicesOptions;
26+
import org.elasticsearch.client.node.NodeClient;
27+
import org.elasticsearch.cluster.metadata.AliasMetaData;
28+
import org.elasticsearch.common.Strings;
29+
import org.elasticsearch.common.settings.IndexScopedSettings;
30+
import org.elasticsearch.common.settings.Settings;
31+
import org.elasticsearch.common.settings.SettingsFilter;
32+
import org.elasticsearch.common.xcontent.ToXContent.Params;
33+
import org.elasticsearch.common.xcontent.XContentBuilder;
34+
import org.elasticsearch.rest.BaseRestHandler;
35+
import org.elasticsearch.rest.BytesRestResponse;
36+
import org.elasticsearch.rest.RestController;
37+
import org.elasticsearch.rest.RestRequest;
38+
import org.elasticsearch.rest.RestResponse;
39+
import org.elasticsearch.rest.action.RestBuilderListener;
40+
41+
import java.io.IOException;
42+
import java.util.List;
43+
import java.util.Set;
44+
45+
import static org.elasticsearch.rest.RestRequest.Method.GET;
46+
import static org.elasticsearch.rest.RestRequest.Method.HEAD;
47+
import static org.elasticsearch.rest.RestStatus.OK;
48+
49+
/**
50+
* The REST handler for retrieving all settings
51+
*/
52+
public class RestGetAllSettingsAction extends BaseRestHandler {
53+
54+
private final IndexScopedSettings indexScopedSettings;
55+
private final SettingsFilter settingsFilter;
56+
57+
public RestGetAllSettingsAction(final Settings settings, final RestController controller,
58+
final IndexScopedSettings indexScopedSettings, final SettingsFilter settingsFilter) {
59+
super(settings);
60+
this.indexScopedSettings = indexScopedSettings;
61+
controller.registerHandler(GET, "/_settings", this);
62+
this.settingsFilter = settingsFilter;
63+
}
64+
65+
@Override
66+
public String getName() {
67+
return "get_all_settings_action";
68+
}
69+
70+
@Override
71+
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
72+
final GetIndexRequest getIndexRequest = new GetIndexRequest();
73+
getIndexRequest.indices(Strings.EMPTY_ARRAY);
74+
getIndexRequest.features(Feature.SETTINGS);
75+
getIndexRequest.indicesOptions(IndicesOptions.fromRequest(request, getIndexRequest.indicesOptions()));
76+
getIndexRequest.local(request.paramAsBoolean("local", getIndexRequest.local()));
77+
getIndexRequest.humanReadable(request.paramAsBoolean("human", false));
78+
// This is required so the "flat_settings" parameter counts as consumed
79+
request.paramAsBoolean("flat_settings", false);
80+
final boolean defaults = request.paramAsBoolean("include_defaults", false);
81+
return channel -> client.admin().indices().getIndex(getIndexRequest, new RestBuilderListener<GetIndexResponse>(channel) {
82+
83+
@Override
84+
public RestResponse buildResponse(final GetIndexResponse response, final XContentBuilder builder) throws Exception {
85+
builder.startObject();
86+
{
87+
for (final String index : response.indices()) {
88+
builder.startObject(index);
89+
{
90+
writeSettings(response.settings().get(index), builder, request, defaults);
91+
}
92+
builder.endObject();
93+
}
94+
}
95+
builder.endObject();
96+
97+
return new BytesRestResponse(OK, builder);
98+
}
99+
100+
101+
private void writeSettings(final Settings settings, final XContentBuilder builder,
102+
final Params params, final boolean defaults) throws IOException {
103+
builder.startObject("settings");
104+
{
105+
settings.toXContent(builder, params);
106+
}
107+
builder.endObject();
108+
if (defaults) {
109+
builder.startObject("defaults");
110+
{
111+
settingsFilter
112+
.filter(indexScopedSettings.diff(settings, RestGetAllSettingsAction.this.settings))
113+
.toXContent(builder, request);
114+
}
115+
builder.endObject();
116+
}
117+
}
118+
});
119+
}
120+
121+
}

core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetIndicesAction.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public RestGetIndicesAction(
6767
this.indexScopedSettings = indexScopedSettings;
6868
controller.registerHandler(GET, "/{index}", this);
6969
controller.registerHandler(HEAD, "/{index}", this);
70-
controller.registerHandler(GET, "/{index}/{type}", this);
7170
this.settingsFilter = settingsFilter;
7271
}
7372

@@ -79,18 +78,8 @@ public String getName() {
7978
@Override
8079
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
8180
String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
82-
String[] featureParams = request.paramAsStringArray("type", null);
83-
// Work out if the indices is a list of features
84-
if (featureParams == null && indices.length > 0 && indices[0] != null && indices[0].startsWith("_") && !"_all".equals(indices[0])) {
85-
featureParams = indices;
86-
indices = new String[]{"_all"};
87-
}
8881
final GetIndexRequest getIndexRequest = new GetIndexRequest();
8982
getIndexRequest.indices(indices);
90-
if (featureParams != null) {
91-
Feature[] features = Feature.convertToFeatures(featureParams);
92-
getIndexRequest.features(features);
93-
}
9483
getIndexRequest.indicesOptions(IndicesOptions.fromRequest(request, getIndexRequest.indicesOptions()));
9584
getIndexRequest.local(request.paramAsBoolean("local", getIndexRequest.local()));
9685
getIndexRequest.humanReadable(request.paramAsBoolean("human", false));

0 commit comments

Comments
 (0)