Search in sources :

Example 81 with NodeClient

use of org.elasticsearch.client.node.NodeClient in project elasticsearch by elastic.

the class RestCancelTasksAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final String[] nodesIds = Strings.splitStringByCommaToArray(request.param("nodes"));
    final TaskId taskId = new TaskId(request.param("task_id"));
    final String[] actions = Strings.splitStringByCommaToArray(request.param("actions"));
    final TaskId parentTaskId = new TaskId(request.param("parent_task_id"));
    final String groupBy = request.param("group_by", "nodes");
    CancelTasksRequest cancelTasksRequest = new CancelTasksRequest();
    cancelTasksRequest.setTaskId(taskId);
    cancelTasksRequest.setNodes(nodesIds);
    cancelTasksRequest.setActions(actions);
    cancelTasksRequest.setParentTaskId(parentTaskId);
    return channel -> client.admin().cluster().cancelTasks(cancelTasksRequest, listTasksResponseListener(nodesInCluster, groupBy, channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) RestListTasksAction.listTasksResponseListener(org.elasticsearch.rest.action.admin.cluster.RestListTasksAction.listTasksResponseListener) CancelTasksRequest(org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) TaskId(org.elasticsearch.tasks.TaskId) Supplier(java.util.function.Supplier) Strings(org.elasticsearch.common.Strings) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) TaskId(org.elasticsearch.tasks.TaskId) CancelTasksRequest(org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest)

Example 82 with NodeClient

use of org.elasticsearch.client.node.NodeClient in project elasticsearch by elastic.

the class RestClusterRerouteAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ClusterRerouteRequest clusterRerouteRequest = createRequest(request);
    // by default, return everything but metadata
    final String metric = request.param("metric");
    if (metric == null) {
        request.params().put("metric", DEFAULT_METRICS);
    }
    return channel -> client.admin().cluster().reroute(clusterRerouteRequest, new AcknowledgedRestListener<ClusterRerouteResponse>(channel) {

        @Override
        protected void addCustomFields(XContentBuilder builder, ClusterRerouteResponse response) throws IOException {
            builder.startObject("state");
            settingsFilter.addFilterSettingParams(request);
            response.getState().toXContent(builder, request);
            builder.endObject();
            if (clusterRerouteRequest.explain()) {
                assert response.getExplanations() != null;
                response.getExplanations().toXContent(builder, ToXContent.EMPTY_PARAMS);
            }
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) SettingsFilter(org.elasticsearch.common.settings.SettingsFilter) ClusterRerouteRequest(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequest) ToXContent(org.elasticsearch.common.xcontent.ToXContent) Set(java.util.Set) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) ClusterRerouteResponse(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse) Strings(org.elasticsearch.common.Strings) HashSet(java.util.HashSet) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) AllocationCommands(org.elasticsearch.cluster.routing.allocation.command.AllocationCommands) ValueType(org.elasticsearch.common.xcontent.ObjectParser.ValueType) ObjectParser(org.elasticsearch.common.xcontent.ObjectParser) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) Requests(org.elasticsearch.client.Requests) ParseField(org.elasticsearch.common.ParseField) Collections(java.util.Collections) EnumSet(java.util.EnumSet) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener) ClusterRerouteRequest(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequest) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ClusterRerouteResponse(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse)

Example 83 with NodeClient

use of org.elasticsearch.client.node.NodeClient in project elasticsearch by elastic.

the class RestClusterUpdateSettingsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final ClusterUpdateSettingsRequest clusterUpdateSettingsRequest = Requests.clusterUpdateSettingsRequest();
    clusterUpdateSettingsRequest.timeout(request.paramAsTime("timeout", clusterUpdateSettingsRequest.timeout()));
    clusterUpdateSettingsRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterUpdateSettingsRequest.masterNodeTimeout()));
    Map<String, Object> source;
    try (XContentParser parser = request.contentParser()) {
        source = parser.map();
    }
    if (source.containsKey("transient")) {
        clusterUpdateSettingsRequest.transientSettings((Map) source.get("transient"));
    }
    if (source.containsKey("persistent")) {
        clusterUpdateSettingsRequest.persistentSettings((Map) source.get("persistent"));
    }
    return channel -> client.admin().cluster().updateSettings(clusterUpdateSettingsRequest, new AcknowledgedRestListener<ClusterUpdateSettingsResponse>(channel) {

        @Override
        protected void addCustomFields(XContentBuilder builder, ClusterUpdateSettingsResponse response) throws IOException {
            builder.startObject("persistent");
            response.getPersistentSettings().toXContent(builder, request);
            builder.endObject();
            builder.startObject("transient");
            response.getTransientSettings().toXContent(builder, request);
            builder.endObject();
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Set(java.util.Set) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ClusterUpdateSettingsRequest(org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest) Settings(org.elasticsearch.common.settings.Settings) Map(java.util.Map) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) ClusterUpdateSettingsResponse(org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse) Requests(org.elasticsearch.client.Requests) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener) IOException(java.io.IOException) ClusterUpdateSettingsResponse(org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse) ClusterUpdateSettingsRequest(org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest) XContentParser(org.elasticsearch.common.xcontent.XContentParser) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 84 with NodeClient

use of org.elasticsearch.client.node.NodeClient in project elasticsearch by elastic.

the class RestCreateSnapshotAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    CreateSnapshotRequest createSnapshotRequest = createSnapshotRequest(request.param("repository"), request.param("snapshot"));
    request.applyContentParser(p -> createSnapshotRequest.source(p.mapOrdered()));
    createSnapshotRequest.masterNodeTimeout(request.paramAsTime("master_timeout", createSnapshotRequest.masterNodeTimeout()));
    createSnapshotRequest.waitForCompletion(request.paramAsBoolean("wait_for_completion", false));
    return channel -> client.admin().cluster().createSnapshot(createSnapshotRequest, new RestToXContentListener<>(channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Requests.createSnapshotRequest(org.elasticsearch.client.Requests.createSnapshotRequest) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) RestToXContentListener(org.elasticsearch.rest.action.RestToXContentListener) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) CreateSnapshotRequest(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) PUT(org.elasticsearch.rest.RestRequest.Method.PUT) CreateSnapshotRequest(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest)

Example 85 with NodeClient

use of org.elasticsearch.client.node.NodeClient in project elasticsearch by elastic.

the class BaseRestHandlerTests method testDefaultResponseParameters.

public void testDefaultResponseParameters() throws Exception {
    final AtomicBoolean executed = new AtomicBoolean();
    BaseRestHandler handler = new BaseRestHandler(Settings.EMPTY) {

        @Override
        protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
            return channel -> executed.set(true);
        }
    };
    final HashMap<String, String> params = new HashMap<>();
    params.put("format", randomAsciiOfLength(8));
    params.put("filter_path", randomAsciiOfLength(8));
    params.put("pretty", randomFrom("true", "false", "", null));
    params.put("human", null);
    RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withParams(params).build();
    RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1);
    handler.handleRequest(request, channel, mock(NodeClient.class));
    assertTrue(executed.get());
}
Also used : FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) AbstractCatAction(org.elasticsearch.rest.action.cat.AbstractCatAction) Table(org.elasticsearch.common.Table) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException) HashMap(java.util.HashMap) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Settings(org.elasticsearch.common.settings.Settings) NodeClient(org.elasticsearch.client.node.NodeClient) ESTestCase(org.elasticsearch.test.ESTestCase) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeClient(org.elasticsearch.client.node.NodeClient) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel)

Aggregations

NodeClient (org.elasticsearch.client.node.NodeClient)125 Settings (org.elasticsearch.common.settings.Settings)115 RestRequest (org.elasticsearch.rest.RestRequest)108 RestController (org.elasticsearch.rest.RestController)107 IOException (java.io.IOException)103 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)88 Strings (org.elasticsearch.common.Strings)61 GET (org.elasticsearch.rest.RestRequest.Method.GET)57 RestResponse (org.elasticsearch.rest.RestResponse)50 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)36 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)35 POST (org.elasticsearch.rest.RestRequest.Method.POST)34 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)30 RestBuilderListener (org.elasticsearch.rest.action.RestBuilderListener)27 Set (java.util.Set)24 Table (org.elasticsearch.common.Table)24 OK (org.elasticsearch.rest.RestStatus.OK)23 AcknowledgedRestListener (org.elasticsearch.rest.action.AcknowledgedRestListener)23 RestResponseListener (org.elasticsearch.rest.action.RestResponseListener)20 Collections (java.util.Collections)18