-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HLRC: split tasks request converters (#33441)
In an effort to encapsulate the different clients, the request converters are being shuffled around. This splits the TasksClient request converters.
- Loading branch information
Showing
5 changed files
with
174 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
client/rest-high-level/src/main/java/org/elasticsearch/client/TasksRequestConverters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Licensed to Elasticsearch 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.client; | ||
|
||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest; | ||
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest; | ||
|
||
public class TasksRequestConverters { | ||
|
||
static Request cancelTasks(CancelTasksRequest cancelTasksRequest) { | ||
Request request = new Request(HttpPost.METHOD_NAME, "/_tasks/_cancel"); | ||
RequestConverters.Params params = new RequestConverters.Params(request); | ||
params.withTimeout(cancelTasksRequest.getTimeout()) | ||
.withTaskId(cancelTasksRequest.getTaskId()) | ||
.withNodes(cancelTasksRequest.getNodes()) | ||
.withParentTaskId(cancelTasksRequest.getParentTaskId()) | ||
.withActions(cancelTasksRequest.getActions()); | ||
return request; | ||
} | ||
|
||
static Request listTasks(ListTasksRequest listTaskRequest) { | ||
if (listTaskRequest.getTaskId() != null && listTaskRequest.getTaskId().isSet()) { | ||
throw new IllegalArgumentException("TaskId cannot be used for list tasks request"); | ||
} | ||
Request request = new Request(HttpGet.METHOD_NAME, "/_tasks"); | ||
RequestConverters.Params params = new RequestConverters.Params(request); | ||
params.withTimeout(listTaskRequest.getTimeout()) | ||
.withDetailed(listTaskRequest.getDetailed()) | ||
.withWaitForCompletion(listTaskRequest.getWaitForCompletion()) | ||
.withParentTaskId(listTaskRequest.getParentTaskId()) | ||
.withNodes(listTaskRequest.getNodes()) | ||
.withActions(listTaskRequest.getActions()) | ||
.putParam("group_by", "none"); | ||
return request; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...t/rest-high-level/src/test/java/org/elasticsearch/client/TasksRequestConvertersTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Licensed to Elasticsearch 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.client; | ||
|
||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest; | ||
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest; | ||
import org.elasticsearch.tasks.TaskId; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
public class TasksRequestConvertersTests extends ESTestCase { | ||
|
||
public void testCancelTasks() { | ||
CancelTasksRequest request = new CancelTasksRequest(); | ||
Map<String, String> expectedParams = new HashMap<>(); | ||
TaskId taskId = new TaskId(randomAlphaOfLength(5), randomNonNegativeLong()); | ||
TaskId parentTaskId = new TaskId(randomAlphaOfLength(5), randomNonNegativeLong()); | ||
request.setTaskId(taskId); | ||
request.setParentTaskId(parentTaskId); | ||
expectedParams.put("task_id", taskId.toString()); | ||
expectedParams.put("parent_task_id", parentTaskId.toString()); | ||
Request httpRequest = TasksRequestConverters.cancelTasks(request); | ||
assertThat(httpRequest, notNullValue()); | ||
assertThat(httpRequest.getMethod(), equalTo(HttpPost.METHOD_NAME)); | ||
assertThat(httpRequest.getEntity(), nullValue()); | ||
assertThat(httpRequest.getEndpoint(), equalTo("/_tasks/_cancel")); | ||
assertThat(httpRequest.getParameters(), equalTo(expectedParams)); | ||
} | ||
|
||
public void testListTasks() { | ||
{ | ||
ListTasksRequest request = new ListTasksRequest(); | ||
Map<String, String> expectedParams = new HashMap<>(); | ||
if (randomBoolean()) { | ||
request.setDetailed(randomBoolean()); | ||
if (request.getDetailed()) { | ||
expectedParams.put("detailed", "true"); | ||
} | ||
} | ||
if (randomBoolean()) { | ||
request.setWaitForCompletion(randomBoolean()); | ||
if (request.getWaitForCompletion()) { | ||
expectedParams.put("wait_for_completion", "true"); | ||
} | ||
} | ||
if (randomBoolean()) { | ||
String timeout = randomTimeValue(); | ||
request.setTimeout(timeout); | ||
expectedParams.put("timeout", timeout); | ||
} | ||
if (randomBoolean()) { | ||
if (randomBoolean()) { | ||
TaskId taskId = new TaskId(randomAlphaOfLength(5), randomNonNegativeLong()); | ||
request.setParentTaskId(taskId); | ||
expectedParams.put("parent_task_id", taskId.toString()); | ||
} else { | ||
request.setParentTask(TaskId.EMPTY_TASK_ID); | ||
} | ||
} | ||
if (randomBoolean()) { | ||
String[] nodes = generateRandomStringArray(10, 8, false); | ||
request.setNodes(nodes); | ||
if (nodes.length > 0) { | ||
expectedParams.put("nodes", String.join(",", nodes)); | ||
} | ||
} | ||
if (randomBoolean()) { | ||
String[] actions = generateRandomStringArray(10, 8, false); | ||
request.setActions(actions); | ||
if (actions.length > 0) { | ||
expectedParams.put("actions", String.join(",", actions)); | ||
} | ||
} | ||
expectedParams.put("group_by", "none"); | ||
Request httpRequest = TasksRequestConverters.listTasks(request); | ||
assertThat(httpRequest, notNullValue()); | ||
assertThat(httpRequest.getMethod(), equalTo(HttpGet.METHOD_NAME)); | ||
assertThat(httpRequest.getEntity(), nullValue()); | ||
assertThat(httpRequest.getEndpoint(), equalTo("/_tasks")); | ||
assertThat(httpRequest.getParameters(), equalTo(expectedParams)); | ||
} | ||
{ | ||
ListTasksRequest request = new ListTasksRequest(); | ||
request.setTaskId(new TaskId(randomAlphaOfLength(5), randomNonNegativeLong())); | ||
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () | ||
-> TasksRequestConverters.listTasks(request)); | ||
assertEquals("TaskId cannot be used for list tasks request", exception.getMessage()); | ||
} | ||
} | ||
} |