Skip to content

Commit 7011cba

Browse files
authored
Fix cat tasks api params in spec and handler (#66272)
This commit fixes the cat tasks api parameter specification and the handler so that the parameters are consumed during request preparation. Closes #59493
1 parent 69e5ea1 commit 7011cba

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-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
@@ -24,7 +24,7 @@
2424
"type":"string",
2525
"description":"a short version of the Accept header, e.g. json, yaml"
2626
},
27-
"node_id":{
27+
"nodes":{
2828
"type":"list",
2929
"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"
3030
},
@@ -36,9 +36,9 @@
3636
"type":"boolean",
3737
"description":"Return detailed task information (default: false)"
3838
},
39-
"parent_task":{
40-
"type":"number",
41-
"description":"Return tasks with specified parent task id. Set to -1 to return all."
39+
"parent_task_id":{
40+
"type":"string",
41+
"description":"Return tasks with specified parent task id (node_id:task_number). Set to -1 to return all."
4242
},
4343
"h":{
4444
"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;
@@ -71,8 +72,9 @@ protected void documentation(StringBuilder sb) {
7172

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

0 commit comments

Comments
 (0)