From ce211119d00459955cb06190a72e3ad42cede7b2 Mon Sep 17 00:00:00 2001 From: Areek Zillur Date: Thu, 9 Jun 2016 10:19:50 -0400 Subject: [PATCH] Add Shrink request source parser to parse create index request body Follow up to https://github.com/elastic/elasticsearch/pull/18732#discussion_r66407196 --- .../admin/indices/shrink/ShrinkRequest.java | 34 +++++++++++++++++++ .../admin/indices/RestShrinkIndexAction.java | 2 +- .../test/indices.shrink/10_basic.yaml | 3 +- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/shrink/ShrinkRequest.java b/core/src/main/java/org/elasticsearch/action/admin/indices/shrink/ShrinkRequest.java index a14a3f37a86..791d35220e2 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/shrink/ShrinkRequest.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/shrink/ShrinkRequest.java @@ -18,15 +18,25 @@ */ package org.elasticsearch.action.admin.indices.shrink; +import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.IndicesRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.master.AcknowledgedRequest; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParseFieldMatcher; +import org.elasticsearch.common.ParseFieldMatcherSupplier; +import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; +import java.util.Map; import java.util.Objects; import static org.elasticsearch.action.ValidateActions.addValidationError; @@ -36,6 +46,17 @@ import static org.elasticsearch.action.ValidateActions.addValidationError; */ public class ShrinkRequest extends AcknowledgedRequest implements IndicesRequest { + public static ObjectParser PARSER = + new ObjectParser<>("shrink_request", null); + static { + PARSER.declareField((parser, request, parseFieldMatcherSupplier) -> + request.getShrinkIndexRequest().settings(parser.map()), + new ParseField("settings"), ObjectParser.ValueType.OBJECT); + PARSER.declareField((parser, request, parseFieldMatcherSupplier) -> + request.getShrinkIndexRequest().aliases(parser.map()), + new ParseField("aliases"), ObjectParser.ValueType.OBJECT); + } + private CreateIndexRequest shrinkIndexRequest; private String sourceIndex; @@ -104,4 +125,17 @@ public class ShrinkRequest extends AcknowledgedRequest implements public String getSourceIndex() { return sourceIndex; } + + public void source(BytesReference source) { + XContentType xContentType = XContentFactory.xContentType(source); + if (xContentType != null) { + try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(source)) { + PARSER.parse(parser, this, () -> ParseFieldMatcher.EMPTY); + } catch (IOException e) { + throw new ElasticsearchParseException("failed to parse source for shrink index", e); + } + } else { + throw new ElasticsearchParseException("failed to parse content type for shrink index source"); + } + } } diff --git a/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestShrinkIndexAction.java b/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestShrinkIndexAction.java index 42117cbf328..f145bcbf02a 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestShrinkIndexAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestShrinkIndexAction.java @@ -52,7 +52,7 @@ public class RestShrinkIndexAction extends BaseRestHandler { } ShrinkRequest shrinkIndexRequest = new ShrinkRequest(request.param("target"), request.param("index")); if (request.hasContent()) { - shrinkIndexRequest.getShrinkIndexRequest().source(request.content()); + shrinkIndexRequest.source(request.content()); } shrinkIndexRequest.timeout(request.paramAsTime("timeout", shrinkIndexRequest.timeout())); shrinkIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", shrinkIndexRequest.masterNodeTimeout())); diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.shrink/10_basic.yaml b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.shrink/10_basic.yaml index 4ef428b9600..b1aac4952c4 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.shrink/10_basic.yaml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.shrink/10_basic.yaml @@ -54,7 +54,8 @@ index: "source" target: "target" body: - index.number_of_replicas: 0 + settings: + index.number_of_replicas: 0 - do: cluster.health: