Search in sources :

Example 56 with RestToXContentListener

use of org.opensearch.rest.action.RestToXContentListener in project OpenSearch by opensearch-project.

the class RestListTasksAction method listTasksResponseListener.

/**
 * Standard listener for extensions of {@link ListTasksResponse} that supports {@code group_by=nodes}.
 */
public static <T extends ListTasksResponse> ActionListener<T> listTasksResponseListener(Supplier<DiscoveryNodes> nodesInCluster, String groupBy, final RestChannel channel) {
    if ("nodes".equals(groupBy)) {
        return new RestBuilderListener<T>(channel) {

            @Override
            public RestResponse buildResponse(T response, XContentBuilder builder) throws Exception {
                builder.startObject();
                response.toXContentGroupedByNode(builder, channel.request(), nodesInCluster.get());
                builder.endObject();
                return new BytesRestResponse(RestStatus.OK, builder);
            }
        };
    } else if ("parents".equals(groupBy)) {
        return new RestBuilderListener<T>(channel) {

            @Override
            public RestResponse buildResponse(T response, XContentBuilder builder) throws Exception {
                builder.startObject();
                response.toXContentGroupedByParents(builder, channel.request());
                builder.endObject();
                return new BytesRestResponse(RestStatus.OK, builder);
            }
        };
    } else if ("none".equals(groupBy)) {
        return new RestToXContentListener<>(channel);
    } else {
        throw new IllegalArgumentException("[group_by] must be one of [nodes], [parents] or [none] but was [" + groupBy + "]");
    }
}
Also used : RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) GET(org.opensearch.rest.RestRequest.Method.GET) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestBuilderListener(org.opensearch.rest.action.RestBuilderListener) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

Example 57 with RestToXContentListener

use of org.opensearch.rest.action.RestToXContentListener in project OpenSearch by opensearch-project.

the class RestCleanupRepositoryAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    CleanupRepositoryRequest cleanupRepositoryRequest = cleanupRepositoryRequest(request.param("repository"));
    cleanupRepositoryRequest.timeout(request.paramAsTime("timeout", cleanupRepositoryRequest.timeout()));
    cleanupRepositoryRequest.masterNodeTimeout(request.paramAsTime("master_timeout", cleanupRepositoryRequest.masterNodeTimeout()));
    return channel -> client.admin().cluster().cleanupRepository(cleanupRepositoryRequest, new RestToXContentListener<>(channel));
}
Also used : CleanupRepositoryRequest(org.opensearch.action.admin.cluster.repositories.cleanup.CleanupRepositoryRequest) POST(org.opensearch.rest.RestRequest.Method.POST) List(java.util.List) NodeClient(org.opensearch.client.node.NodeClient) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) BaseRestHandler(org.opensearch.rest.BaseRestHandler) Requests.cleanupRepositoryRequest(org.opensearch.client.Requests.cleanupRepositoryRequest) Collections.singletonList(java.util.Collections.singletonList) CleanupRepositoryRequest(org.opensearch.action.admin.cluster.repositories.cleanup.CleanupRepositoryRequest)

Example 58 with RestToXContentListener

use of org.opensearch.rest.action.RestToXContentListener in project OpenSearch by opensearch-project.

the class RestCloseIndexAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    CloseIndexRequest closeIndexRequest = new CloseIndexRequest(Strings.splitStringByCommaToArray(request.param("index")));
    closeIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", closeIndexRequest.masterNodeTimeout()));
    closeIndexRequest.timeout(request.paramAsTime("timeout", closeIndexRequest.timeout()));
    closeIndexRequest.indicesOptions(IndicesOptions.fromRequest(request, closeIndexRequest.indicesOptions()));
    String waitForActiveShards = request.param("wait_for_active_shards");
    if (waitForActiveShards != null) {
        closeIndexRequest.waitForActiveShards(ActiveShardCount.parseString(waitForActiveShards));
    }
    return channel -> client.admin().indices().close(closeIndexRequest, new RestToXContentListener<>(channel));
}
Also used : CloseIndexRequest(org.opensearch.action.admin.indices.close.CloseIndexRequest) POST(org.opensearch.rest.RestRequest.Method.POST) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) IndicesOptions(org.opensearch.action.support.IndicesOptions) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) Strings(org.opensearch.common.Strings) List(java.util.List) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Arrays.asList(java.util.Arrays.asList) BaseRestHandler(org.opensearch.rest.BaseRestHandler) CloseIndexRequest(org.opensearch.action.admin.indices.close.CloseIndexRequest)

Example 59 with RestToXContentListener

use of org.opensearch.rest.action.RestToXContentListener in project OpenSearch by opensearch-project.

the class RestCreateIndexAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    CreateIndexRequest createIndexRequest = new CreateIndexRequest(request.param("index"));
    if (request.hasContent()) {
        Map<String, Object> sourceAsMap = XContentHelper.convertToMap(request.requiredContent(), false, request.getXContentType()).v2();
        sourceAsMap = prepareMappings(sourceAsMap);
        createIndexRequest.source(sourceAsMap, LoggingDeprecationHandler.INSTANCE);
    }
    createIndexRequest.timeout(request.paramAsTime("timeout", createIndexRequest.timeout()));
    createIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", createIndexRequest.masterNodeTimeout()));
    createIndexRequest.waitForActiveShards(ActiveShardCount.parseString(request.param("wait_for_active_shards")));
    return channel -> client.admin().indices().create(createIndexRequest, new RestToXContentListener<>(channel));
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) RestRequest(org.opensearch.rest.RestRequest) LoggingDeprecationHandler(org.opensearch.common.xcontent.LoggingDeprecationHandler) IOException(java.io.IOException) HashMap(java.util.HashMap) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) Collections.singletonList(java.util.Collections.singletonList) XContentHelper(org.opensearch.common.xcontent.XContentHelper) List(java.util.List) MapperService(org.opensearch.index.mapper.MapperService) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) BaseRestHandler(org.opensearch.rest.BaseRestHandler) PUT(org.opensearch.rest.RestRequest.Method.PUT) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest)

Example 60 with RestToXContentListener

use of org.opensearch.rest.action.RestToXContentListener in project OpenSearch by opensearch-project.

the class RestDeleteIndexTemplateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    DeleteIndexTemplateRequest deleteIndexTemplateRequest = new DeleteIndexTemplateRequest(request.param("name"));
    deleteIndexTemplateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", deleteIndexTemplateRequest.masterNodeTimeout()));
    return channel -> client.admin().indices().deleteTemplate(deleteIndexTemplateRequest, new RestToXContentListener<>(channel));
}
Also used : DELETE(org.opensearch.rest.RestRequest.Method.DELETE) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) List(java.util.List) NodeClient(org.opensearch.client.node.NodeClient) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) BaseRestHandler(org.opensearch.rest.BaseRestHandler) Collections.singletonList(java.util.Collections.singletonList) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)

Aggregations

RestToXContentListener (org.opensearch.rest.action.RestToXContentListener)70 List (java.util.List)69 BaseRestHandler (org.opensearch.rest.BaseRestHandler)69 RestRequest (org.opensearch.rest.RestRequest)69 NodeClient (org.opensearch.client.node.NodeClient)68 IOException (java.io.IOException)67 Strings (org.opensearch.common.Strings)37 Arrays.asList (java.util.Arrays.asList)35 Collections.unmodifiableList (java.util.Collections.unmodifiableList)35 POST (org.opensearch.rest.RestRequest.Method.POST)30 GET (org.opensearch.rest.RestRequest.Method.GET)23 IndicesOptions (org.opensearch.action.support.IndicesOptions)21 Collections.singletonList (java.util.Collections.singletonList)18 PUT (org.opensearch.rest.RestRequest.Method.PUT)14 ImmutableList (com.google.common.collect.ImmutableList)11 Locale (java.util.Locale)11 Settings (org.opensearch.common.settings.Settings)10 Set (java.util.Set)9 XContentParser (org.opensearch.common.xcontent.XContentParser)9 DELETE (org.opensearch.rest.RestRequest.Method.DELETE)8