Skip to content

Commit 87d9252

Browse files
authored
Fix cat tasks api params in spec and handler (#66296)
This commit fixes the cat tasks api parameter specification and the handler so that the parameters are consumed during request preparation. Closes #59493 Backport of #66272
1 parent 3dddf72 commit 87d9252

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

rest-api-spec/src/main/resources/rest-api-spec/api/cat.tasks.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"type":"string",
2424
"description":"a short version of the Accept header, e.g. json, yaml"
2525
},
26-
"node_id":{
26+
"nodes":{
2727
"type":"list",
2828
"description":"A comma-separated list of node IDs or names to limit the returned information; use `_local` to return information from the node you're connecting to, leave empty to get information from all nodes"
2929
},
@@ -35,9 +35,9 @@
3535
"type":"boolean",
3636
"description":"Return detailed task information (default: false)"
3737
},
38-
"parent_task":{
39-
"type":"number",
40-
"description":"Return tasks with specified parent task id. Set to -1 to return all."
38+
"parent_task_id":{
39+
"type":"string",
40+
"description":"Return tasks with specified parent task id (node_id:task_number). Set to -1 to return all."
4141
},
4242
"h":{
4343
"type":"list",

server/src/main/java/org/elasticsearch/rest/action/cat/RestTasksAction.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.rest.action.cat;
2121

22+
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest;
2223
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse;
2324
import org.elasticsearch.action.admin.cluster.node.tasks.list.TaskGroup;
2425
import org.elasticsearch.client.node.NodeClient;
@@ -72,8 +73,9 @@ protected void documentation(StringBuilder sb) {
7273

7374
@Override
7475
public RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
76+
final ListTasksRequest listTasksRequest = generateListTasksRequest(request);
7577
return channel ->
76-
client.admin().cluster().listTasks(generateListTasksRequest(request), new RestResponseListener<ListTasksResponse>(channel) {
78+
client.admin().cluster().listTasks(listTasksRequest, new RestResponseListener<ListTasksResponse>(channel) {
7779
@Override
7880
public RestResponse buildResponse(ListTasksResponse listTasksResponse) throws Exception {
7981
return RestTable.buildResponse(buildTable(request, listTasksResponse), channel);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.cat;
21+
22+
import org.elasticsearch.action.ActionListener;
23+
import org.elasticsearch.action.ActionRequest;
24+
import org.elasticsearch.action.ActionResponse;
25+
import org.elasticsearch.action.ActionType;
26+
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse;
27+
import org.elasticsearch.cluster.node.DiscoveryNodes;
28+
import org.elasticsearch.common.collect.MapBuilder;
29+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
30+
import org.elasticsearch.test.ESTestCase;
31+
import org.elasticsearch.test.client.NoOpNodeClient;
32+
import org.elasticsearch.test.rest.FakeRestChannel;
33+
import org.elasticsearch.test.rest.FakeRestRequest;
34+
35+
import static java.util.Collections.emptyList;
36+
import static org.hamcrest.Matchers.is;
37+
38+
public class RestTasksActionTests extends ESTestCase {
39+
40+
public void testConsumesParameters() throws Exception {
41+
RestTasksAction action = new RestTasksAction(() -> DiscoveryNodes.EMPTY_NODES);
42+
FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY)
43+
.withParams(MapBuilder.<String, String>newMapBuilder()
44+
.put("parent_task_id", "the node:3")
45+
.put("nodes", "node1,node2")
46+
.put("actions", "*")
47+
.map()
48+
).build();
49+
FakeRestChannel fakeRestChannel = new FakeRestChannel(fakeRestRequest, false, 1);
50+
try (NoOpNodeClient nodeClient = buildNodeClient()) {
51+
action.handleRequest(fakeRestRequest, fakeRestChannel, nodeClient);
52+
}
53+
54+
assertThat(fakeRestChannel.errors().get(), is(0));
55+
assertThat(fakeRestChannel.responses().get(), is(1));
56+
}
57+
58+
private NoOpNodeClient buildNodeClient() {
59+
return new NoOpNodeClient(getTestName()) {
60+
@Override
61+
@SuppressWarnings("unchecked")
62+
public <Request extends ActionRequest, Response extends ActionResponse>
63+
void doExecute(ActionType<Response> action, Request request, ActionListener<Response> listener) {
64+
listener.onResponse((Response) new ListTasksResponse(emptyList(), emptyList(), emptyList()));
65+
}
66+
};
67+
}
68+
}

0 commit comments

Comments
 (0)