From 0c6d19c40ca6f375a7d867361c6b5b528b83551a Mon Sep 17 00:00:00 2001 From: Areek Zillur Date: Wed, 8 Jun 2016 14:16:06 -0400 Subject: [PATCH] add body support for create index request --- .../indices/rollover/RolloverRequest.java | 40 +++++++-- .../rollover/RolloverRequestBuilder.java | 17 ++++ .../rollover/TransportRolloverAction.java | 19 ++-- .../admin/indices/rollover/RolloverIT.java | 27 ++++++ .../rollover/RolloverRequestTests.java | 90 +++++++++++++++++++ .../TransportRolloverActionTests.java | 20 ++++- 6 files changed, 201 insertions(+), 12 deletions(-) create mode 100644 core/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequestTests.java diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequest.java b/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequest.java index dd1facdb6e2..19659171a67 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequest.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequest.java @@ -21,6 +21,7 @@ package org.elasticsearch.action.admin.indices.rollover; 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; @@ -37,6 +38,8 @@ import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; import java.util.HashSet; +import java.util.Map; +import java.util.Objects; import java.util.Set; import static org.elasticsearch.action.ValidateActions.addValidationError; @@ -50,13 +53,26 @@ public class RolloverRequest extends AcknowledgedRequest implem private String sourceAlias; private boolean simulate; private Set conditions = new HashSet<>(2); + private CreateIndexRequest createIndexRequest = new CreateIndexRequest("_na_"); - public static ObjectParser, ParseFieldMatcherSupplier> TLP_PARSER = + public static ObjectParser PARSER = new ObjectParser<>("conditions", null); static { - TLP_PARSER.declareField((parser, conditions, parseFieldMatcherSupplier) -> - Condition.PARSER.parse(parser, conditions, () -> ParseFieldMatcher.EMPTY), + PARSER.declareField((parser, request, parseFieldMatcherSupplier) -> + Condition.PARSER.parse(parser, request.conditions, parseFieldMatcherSupplier), new ParseField("conditions"), ObjectParser.ValueType.OBJECT); + PARSER.declareField((parser, request, parseFieldMatcherSupplier) -> + request.createIndexRequest.settings(parser.map()), + new ParseField("settings"), ObjectParser.ValueType.OBJECT); + PARSER.declareField((parser, request, parseFieldMatcherSupplier) -> { + for (Map.Entry mappingsEntry : parser.map().entrySet()) { + request.createIndexRequest.mapping(mappingsEntry.getKey(), + (Map) mappingsEntry.getValue()); + } + }, new ParseField("mappings"), ObjectParser.ValueType.OBJECT); + PARSER.declareField((parser, request, parseFieldMatcherSupplier) -> + request.createIndexRequest.aliases(parser.map()), + new ParseField("aliases"), ObjectParser.ValueType.OBJECT); } RolloverRequest() {} @@ -67,10 +83,13 @@ public class RolloverRequest extends AcknowledgedRequest implem @Override public ActionRequestValidationException validate() { - ActionRequestValidationException validationException = null; + ActionRequestValidationException validationException = createIndexRequest == null ? null : createIndexRequest.validate(); if (sourceAlias == null) { validationException = addValidationError("source alias is missing", validationException); } + if (createIndexRequest == null) { + validationException = addValidationError("create index request is missing", validationException); + } return validationException; } @@ -83,6 +102,8 @@ public class RolloverRequest extends AcknowledgedRequest implem for (int i = 0; i < size; i++) { this.conditions.add(in.readNamedWriteable(Condition.class)); } + createIndexRequest = new CreateIndexRequest(); + createIndexRequest.readFrom(in); } @Override @@ -94,6 +115,7 @@ public class RolloverRequest extends AcknowledgedRequest implem for (Condition condition : conditions) { out.writeNamedWriteable(condition); } + createIndexRequest.writeTo(out); } @Override @@ -134,11 +156,19 @@ public class RolloverRequest extends AcknowledgedRequest implem return sourceAlias; } + public CreateIndexRequest getCreateIndexRequest() { + return createIndexRequest; + } + + public void setCreateIndexRequest(CreateIndexRequest createIndexRequest) { + this.createIndexRequest = Objects.requireNonNull(createIndexRequest, "create index request must not be null");; + } + public void source(BytesReference source) { XContentType xContentType = XContentFactory.xContentType(source); if (xContentType != null) { try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(source)) { - TLP_PARSER.parse(parser, this.conditions, () -> ParseFieldMatcher.EMPTY); + PARSER.parse(parser, this, () -> ParseFieldMatcher.EMPTY); } catch (IOException e) { throw new ElasticsearchParseException("failed to parse source for rollover index", e); } diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequestBuilder.java b/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequestBuilder.java index ea495bc6eba..bef680110ab 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequestBuilder.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequestBuilder.java @@ -18,8 +18,10 @@ */ package org.elasticsearch.action.admin.indices.rollover; +import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder; import org.elasticsearch.client.ElasticsearchClient; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; @@ -51,4 +53,19 @@ public class RolloverRequestBuilder extends MasterNodeOperationRequestBuilder() { @Override public void onResponse(ClusterStateUpdateResponse response) { @@ -216,10 +215,18 @@ public class TransportRolloverAction extends TransportMasterNodeAction conditions = request.getConditions(); + assertThat(conditions.size(), equalTo(2)); + for (Condition condition : conditions) { + if (condition instanceof MaxAgeCondition) { + MaxAgeCondition maxAgeCondition = (MaxAgeCondition) condition; + assertThat(maxAgeCondition.value.getMillis(), equalTo(TimeValue.timeValueHours(24 * 10).getMillis())); + } else if (condition instanceof MaxDocsCondition) { + MaxDocsCondition maxDocsCondition = (MaxDocsCondition) condition; + assertThat(maxDocsCondition.value, equalTo(100L)); + } else { + fail("unexpected condition " + condition); + } + } + } + + public void testParsingWithIndexSettings() throws Exception { + final RolloverRequest request = new RolloverRequest(randomAsciiOfLength(10)); + final XContentBuilder builder = XContentFactory.jsonBuilder() + .startObject() + .startObject("conditions") + .field("max_age", "10d") + .field("max_docs", 100) + .endObject() + .startObject("mappings") + .startObject("type1") + .startObject("properties") + .startObject("field1") + .field("type", "string") + .field("index", "not_analyzed") + .endObject() + .endObject() + .endObject() + .endObject() + .startObject("settings") + .field("number_of_shards", 10) + .endObject() + .startObject("aliases") + .startObject("alias1").endObject() + .endObject() + .endObject(); + request.source(builder.bytes()); + Set conditions = request.getConditions(); + assertThat(conditions.size(), equalTo(2)); + assertThat(request.getCreateIndexRequest().mappings().size(), equalTo(1)); + assertThat(request.getCreateIndexRequest().aliases().size(), equalTo(1)); + assertThat(request.getCreateIndexRequest().settings().getAsInt("number_of_shards", 0), equalTo(10)); + } +} diff --git a/core/src/test/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverActionTests.java b/core/src/test/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverActionTests.java index af51645f655..28c0a6e3787 100644 --- a/core/src/test/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverActionTests.java +++ b/core/src/test/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverActionTests.java @@ -21,6 +21,7 @@ package org.elasticsearch.action.admin.indices.rollover; import org.elasticsearch.Version; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesClusterStateUpdateRequest; +import org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest; import org.elasticsearch.cluster.metadata.AliasAction; import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData; @@ -86,7 +87,6 @@ public class TransportRolloverActionTests extends ESTestCase { } } - public void testCreateUpdateAliasRequest() throws Exception { String sourceAlias = randomAsciiOfLength(10); String sourceIndex = randomAsciiOfLength(10); @@ -158,4 +158,22 @@ public class TransportRolloverActionTests extends ESTestCase { assertThat(TransportRolloverAction.generateRolloverIndexName("index-name-1"), equalTo("index-name-2")); assertThat(TransportRolloverAction.generateRolloverIndexName("index-name-2"), equalTo("index-name-3")); } + + public void testCreateIndexRequest() throws Exception { + String alias = randomAsciiOfLength(10); + String rolloverIndex = randomAsciiOfLength(10); + final RolloverRequest rolloverRequest = new RolloverRequest(alias); + final Settings settings = Settings.builder() + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()) + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0) + .build(); + rolloverRequest.getCreateIndexRequest().settings(settings); + final CreateIndexClusterStateUpdateRequest createIndexRequest = + TransportRolloverAction.prepareCreateIndexRequest(rolloverIndex, rolloverRequest); + assertThat(createIndexRequest.settings(), equalTo(settings)); + assertThat(createIndexRequest.index(), equalTo(rolloverIndex)); + assertThat(createIndexRequest.cause(), equalTo("rollover_index")); + } }