HLRestClient: Follow-up for put index template api (#30592)

This commit addresses some comments given after the original PR was in.

Follow-up #30400
This commit is contained in:
Nhat Nguyen 2018-05-15 08:06:58 -04:00 committed by GitHub
parent 17d65c1f06
commit d1c28c60fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 31 deletions

View File

@ -37,6 +37,7 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
@ -45,6 +46,7 @@ import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.common.xcontent.support.XContentMapValues;
import java.io.IOException; import java.io.IOException;
@ -543,9 +545,6 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
if (customs.isEmpty() == false) {
throw new IllegalArgumentException("Custom data type is no longer supported in index template [" + customs + "]");
}
builder.field("index_patterns", indexPatterns); builder.field("index_patterns", indexPatterns);
builder.field("order", order); builder.field("order", order);
if (version != null) { if (version != null) {
@ -558,8 +557,10 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
builder.startObject("mappings"); builder.startObject("mappings");
for (Map.Entry<String, String> entry : mappings.entrySet()) { for (Map.Entry<String, String> entry : mappings.entrySet()) {
Map<String, Object> mapping = XContentHelper.convertToMap(new BytesArray(entry.getValue()), false).v2(); builder.field(entry.getKey());
builder.field(entry.getKey(), mapping); XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, entry.getValue());
builder.copyCurrentStructure(parser);
} }
builder.endObject(); builder.endObject();
@ -568,6 +569,11 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
alias.toXContent(builder, params); alias.toXContent(builder, params);
} }
builder.endObject(); builder.endObject();
for (Map.Entry<String, IndexMetaData.Custom> entry : customs.entrySet()) {
builder.field(entry.getKey(), entry.getValue(), params);
}
return builder; return builder;
} }
} }

View File

@ -23,18 +23,18 @@ import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.yaml.YamlXContent; import org.elasticsearch.common.xcontent.yaml.YamlXContent;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.AbstractXContentTestCase;
import java.io.IOException; import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Base64; import java.util.Base64;
import java.util.Collections; import java.util.Collections;
@ -45,7 +45,7 @@ import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is; import static org.hamcrest.core.Is.is;
public class PutIndexTemplateRequestTests extends ESTestCase { public class PutIndexTemplateRequestTests extends AbstractXContentTestCase<PutIndexTemplateRequest> {
// bwc for #21009 // bwc for #21009
public void testPutIndexTemplateRequest510() throws IOException { public void testPutIndexTemplateRequest510() throws IOException {
@ -137,13 +137,14 @@ public class PutIndexTemplateRequestTests extends ESTestCase {
assertThat(noError, is(nullValue())); assertThat(noError, is(nullValue()));
} }
private PutIndexTemplateRequest randomPutIndexTemplateRequest() throws IOException { @Override
protected PutIndexTemplateRequest createTestInstance() {
PutIndexTemplateRequest request = new PutIndexTemplateRequest(); PutIndexTemplateRequest request = new PutIndexTemplateRequest();
request.name("test"); request.name("test");
if (randomBoolean()){ if (randomBoolean()) {
request.version(randomInt()); request.version(randomInt());
} }
if (randomBoolean()){ if (randomBoolean()) {
request.order(randomInt()); request.order(randomInt());
} }
request.patterns(Arrays.asList(generateRandomStringArray(20, 100, false, false))); request.patterns(Arrays.asList(generateRandomStringArray(20, 100, false, false)));
@ -159,30 +160,39 @@ public class PutIndexTemplateRequestTests extends ESTestCase {
request.alias(alias); request.alias(alias);
} }
if (randomBoolean()) { if (randomBoolean()) {
try {
request.mapping("doc", XContentFactory.jsonBuilder().startObject() request.mapping("doc", XContentFactory.jsonBuilder().startObject()
.startObject("doc").startObject("properties") .startObject("doc").startObject("properties")
.startObject("field-" + randomInt()).field("type", randomFrom("keyword", "text")).endObject() .startObject("field-" + randomInt()).field("type", randomFrom("keyword", "text")).endObject()
.endObject().endObject().endObject()); .endObject().endObject().endObject());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
} }
if (randomBoolean()){ }
if (randomBoolean()) {
request.settings(Settings.builder().put("setting1", randomLong()).put("setting2", randomTimeValue()).build()); request.settings(Settings.builder().put("setting1", randomLong()).put("setting2", randomTimeValue()).build());
} }
return request; return request;
} }
public void testFromToXContentPutTemplateRequest() throws Exception { @Override
for (int i = 0; i < 10; i++) { protected PutIndexTemplateRequest doParseInstance(XContentParser parser) throws IOException {
PutIndexTemplateRequest expected = randomPutIndexTemplateRequest(); return new PutIndexTemplateRequest().source(parser.map());
XContentType xContentType = randomFrom(XContentType.values());
BytesReference shuffled = toShuffledXContent(expected, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
PutIndexTemplateRequest parsed = new PutIndexTemplateRequest().source(shuffled, xContentType);
assertNotSame(expected, parsed);
assertThat(parsed.version(), equalTo(expected.version()));
assertThat(parsed.order(), equalTo(expected.order()));
assertThat(parsed.patterns(), equalTo(expected.patterns()));
assertThat(parsed.aliases(), equalTo(expected.aliases()));
assertThat(parsed.mappings(), equalTo(expected.mappings()));
assertThat(parsed.settings(), equalTo(expected.settings()));
} }
@Override
protected void assertEqualInstances(PutIndexTemplateRequest expected, PutIndexTemplateRequest actual) {
assertNotSame(expected, actual);
assertThat(actual.version(), equalTo(expected.version()));
assertThat(actual.order(), equalTo(expected.order()));
assertThat(actual.patterns(), equalTo(expected.patterns()));
assertThat(actual.aliases(), equalTo(expected.aliases()));
assertThat(actual.mappings(), equalTo(expected.mappings()));
assertThat(actual.settings(), equalTo(expected.settings()));
}
@Override
protected boolean supportsUnknownFields() {
return false;
} }
} }

View File

@ -0,0 +1,45 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.action.admin.indices.template.put;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.test.AbstractStreamableXContentTestCase;
public class PutIndexTemplateResponseTests extends AbstractStreamableXContentTestCase<PutIndexTemplateResponse> {
@Override
protected PutIndexTemplateResponse doParseInstance(XContentParser parser) {
return PutIndexTemplateResponse.fromXContent(parser);
}
@Override
protected PutIndexTemplateResponse createTestInstance() {
return new PutIndexTemplateResponse(randomBoolean());
}
@Override
protected PutIndexTemplateResponse createBlankInstance() {
return new PutIndexTemplateResponse();
}
@Override
protected PutIndexTemplateResponse mutateInstance(PutIndexTemplateResponse response) {
return new PutIndexTemplateResponse(response.isAcknowledged() == false);
}
}