Search in sources :

Example 1 with RestToXContentListener

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

the class RestRenderSearchTemplateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    // Creates the render template request
    SearchTemplateRequest renderRequest;
    try (XContentParser parser = request.contentOrSourceParamParser()) {
        renderRequest = SearchTemplateRequest.fromXContent(parser);
    }
    renderRequest.setSimulate(true);
    String id = request.param("id");
    if (id != null) {
        renderRequest.setScriptType(ScriptType.STORED);
        renderRequest.setScript(id);
    }
    return channel -> client.execute(SearchTemplateAction.INSTANCE, renderRequest, new RestToXContentListener<>(channel));
}
Also used : XContentParser(org.opensearch.common.xcontent.XContentParser) ScriptType(org.opensearch.script.ScriptType) POST(org.opensearch.rest.RestRequest.Method.POST) List(java.util.List) NodeClient(org.opensearch.client.node.NodeClient) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Arrays.asList(java.util.Arrays.asList) IOException(java.io.IOException) BaseRestHandler(org.opensearch.rest.BaseRestHandler) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 2 with RestToXContentListener

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

the class RestPutIndexTemplateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest(request.param("name"));
    if (request.hasParam("template")) {
        deprecationLogger.deprecate("put_index_template_deprecated_parameter", "Deprecated parameter [template] used, replaced by [index_patterns]");
        putRequest.patterns(Collections.singletonList(request.param("template")));
    } else {
        putRequest.patterns(Arrays.asList(request.paramAsStringArray("index_patterns", Strings.EMPTY_ARRAY)));
    }
    putRequest.order(request.paramAsInt("order", putRequest.order()));
    putRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putRequest.masterNodeTimeout()));
    putRequest.create(request.paramAsBoolean("create", false));
    putRequest.cause(request.param("cause", ""));
    Map<String, Object> sourceAsMap = XContentHelper.convertToMap(request.requiredContent(), false, request.getXContentType()).v2();
    sourceAsMap = RestCreateIndexAction.prepareMappings(sourceAsMap);
    putRequest.source(sourceAsMap);
    return channel -> client.admin().indices().putTemplate(putRequest, new RestToXContentListener<>(channel));
}
Also used : POST(org.opensearch.rest.RestRequest.Method.POST) Arrays(java.util.Arrays) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) RestRequest(org.opensearch.rest.RestRequest) PutIndexTemplateRequest(org.opensearch.action.admin.indices.template.put.PutIndexTemplateRequest) IOException(java.io.IOException) Strings(org.opensearch.common.Strings) DeprecationLogger(org.opensearch.common.logging.DeprecationLogger) XContentHelper(org.opensearch.common.xcontent.XContentHelper) List(java.util.List) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) BaseRestHandler(org.opensearch.rest.BaseRestHandler) PUT(org.opensearch.rest.RestRequest.Method.PUT) Collections(java.util.Collections) PutIndexTemplateRequest(org.opensearch.action.admin.indices.template.put.PutIndexTemplateRequest)

Example 3 with RestToXContentListener

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

the class RestRecoveryAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final RecoveryRequest recoveryRequest = new RecoveryRequest(Strings.splitStringByCommaToArray(request.param("index")));
    recoveryRequest.detailed(request.paramAsBoolean("detailed", false));
    recoveryRequest.activeOnly(request.paramAsBoolean("active_only", false));
    recoveryRequest.indicesOptions(IndicesOptions.fromRequest(request, recoveryRequest.indicesOptions()));
    return channel -> client.admin().indices().recoveries(recoveryRequest, new RestToXContentListener<>(channel));
}
Also used : List(java.util.List) NodeClient(org.opensearch.client.node.NodeClient) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Arrays.asList(java.util.Arrays.asList) IOException(java.io.IOException) IndicesOptions(org.opensearch.action.support.IndicesOptions) BaseRestHandler(org.opensearch.rest.BaseRestHandler) RecoveryRequest(org.opensearch.action.admin.indices.recovery.RecoveryRequest) Strings(org.opensearch.common.Strings) RecoveryRequest(org.opensearch.action.admin.indices.recovery.RecoveryRequest)

Example 4 with RestToXContentListener

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

the class RestForceMergeAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final ForceMergeRequest mergeRequest = new ForceMergeRequest(Strings.splitStringByCommaToArray(request.param("index")));
    mergeRequest.indicesOptions(IndicesOptions.fromRequest(request, mergeRequest.indicesOptions()));
    mergeRequest.maxNumSegments(request.paramAsInt("max_num_segments", mergeRequest.maxNumSegments()));
    mergeRequest.onlyExpungeDeletes(request.paramAsBoolean("only_expunge_deletes", mergeRequest.onlyExpungeDeletes()));
    mergeRequest.flush(request.paramAsBoolean("flush", mergeRequest.flush()));
    if (mergeRequest.onlyExpungeDeletes() && mergeRequest.maxNumSegments() != ForceMergeRequest.Defaults.MAX_NUM_SEGMENTS) {
        deprecationLogger.deprecate("force_merge_expunge_deletes_and_max_num_segments_deprecation", "setting only_expunge_deletes and max_num_segments at the same time is deprecated and will be rejected in a future version");
    }
    return channel -> client.admin().indices().forceMerge(mergeRequest, new RestToXContentListener<>(channel));
}
Also used : ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) 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) ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) Strings(org.opensearch.common.Strings) DeprecationLogger(org.opensearch.common.logging.DeprecationLogger) List(java.util.List) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Arrays.asList(java.util.Arrays.asList) BaseRestHandler(org.opensearch.rest.BaseRestHandler)

Example 5 with RestToXContentListener

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

the class RestGetIndexTemplateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final String[] names = Strings.splitStringByCommaToArray(request.param("name"));
    final GetIndexTemplatesRequest getIndexTemplatesRequest = new GetIndexTemplatesRequest(names);
    getIndexTemplatesRequest.local(request.paramAsBoolean("local", getIndexTemplatesRequest.local()));
    getIndexTemplatesRequest.masterNodeTimeout(request.paramAsTime("master_timeout", getIndexTemplatesRequest.masterNodeTimeout()));
    final boolean implicitAll = getIndexTemplatesRequest.names().length == 0;
    return channel -> client.admin().indices().getTemplates(getIndexTemplatesRequest, new RestToXContentListener<GetIndexTemplatesResponse>(channel) {

        @Override
        protected RestStatus getStatus(final GetIndexTemplatesResponse response) {
            final boolean templateExists = response.getIndexTemplates().isEmpty() == false;
            return (templateExists || implicitAll) ? OK : NOT_FOUND;
        }
    });
}
Also used : OK(org.opensearch.rest.RestStatus.OK) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) RestStatus(org.opensearch.rest.RestStatus) Strings(org.opensearch.common.Strings) GetIndexTemplatesRequest(org.opensearch.action.admin.indices.template.get.GetIndexTemplatesRequest) NOT_FOUND(org.opensearch.rest.RestStatus.NOT_FOUND) List(java.util.List) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Arrays.asList(java.util.Arrays.asList) GetIndexTemplatesResponse(org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse) BaseRestHandler(org.opensearch.rest.BaseRestHandler) HEAD(org.opensearch.rest.RestRequest.Method.HEAD) RestStatus(org.opensearch.rest.RestStatus) GetIndexTemplatesResponse(org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse) GetIndexTemplatesRequest(org.opensearch.action.admin.indices.template.get.GetIndexTemplatesRequest)

Aggregations

IOException (java.io.IOException)60 RestToXContentListener (org.opensearch.rest.action.RestToXContentListener)60 List (java.util.List)59 NodeClient (org.opensearch.client.node.NodeClient)59 BaseRestHandler (org.opensearch.rest.BaseRestHandler)59 RestRequest (org.opensearch.rest.RestRequest)59 Arrays.asList (java.util.Arrays.asList)35 Collections.unmodifiableList (java.util.Collections.unmodifiableList)35 Strings (org.opensearch.common.Strings)33 POST (org.opensearch.rest.RestRequest.Method.POST)29 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)13 Set (java.util.Set)8 DELETE (org.opensearch.rest.RestRequest.Method.DELETE)8 Map (java.util.Map)7 Settings (org.opensearch.common.settings.Settings)7 XContentParser (org.opensearch.common.xcontent.XContentParser)7 Collections (java.util.Collections)5