index template: Disallow index template pattern to be the same as an alias name

This can cause index creation to fail down the line.
This commit is contained in:
Martijn van Groningen 2015-12-02 15:31:10 +01:00
parent c5301e853a
commit d0fbd9d1c4
2 changed files with 19 additions and 6 deletions

View File

@ -217,6 +217,9 @@ public class MetaDataIndexTemplateService extends AbstractComponent {
for (Alias alias : request.aliases) {
//we validate the alias only partially, as we don't know yet to which index it'll get applied to
aliasValidator.validateAliasStandalone(alias);
if (request.template.equals(alias.name())) {
throw new IllegalArgumentException("Alias [" + alias.name() + "] cannot be the same as the template pattern [" + request.template + "]");
}
}
}

View File

@ -20,6 +20,8 @@
package org.elasticsearch.action.admin.indices.template.put;
import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.cluster.metadata.AliasValidator;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaDataCreateIndexService;
import org.elasticsearch.cluster.metadata.MetaDataIndexTemplateService;
@ -28,13 +30,10 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.indices.InvalidIndexTemplateException;
import org.elasticsearch.test.ESTestCase;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.*;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
public class MetaDataIndexTemplateServiceTests extends ESTestCase {
@ -68,6 +67,17 @@ public class MetaDataIndexTemplateServiceTests extends ESTestCase {
assertThat(throwables.get(0).getMessage(), containsString("index must have 1 or more primary shards"));
}
public void testIndexTemplateWithAliasNameEqualToTemplatePattern() {
PutRequest request = new PutRequest("api", "foobar_template");
request.template("foobar");
request.aliases(Collections.singleton(new Alias("foobar")));
List<Throwable> errors = putTemplate(request);
assertThat(errors.size(), equalTo(1));
assertThat(errors.get(0), instanceOf(IllegalArgumentException.class));
assertThat(errors.get(0).getMessage(), equalTo("Alias [foobar] cannot be the same as the template pattern [foobar]"));
}
private static List<Throwable> putTemplate(PutRequest request) {
MetaDataCreateIndexService createIndexService = new MetaDataCreateIndexService(
Settings.EMPTY,
@ -79,7 +89,7 @@ public class MetaDataIndexTemplateServiceTests extends ESTestCase {
new HashSet<>(),
null,
null);
MetaDataIndexTemplateService service = new MetaDataIndexTemplateService(Settings.EMPTY, null, createIndexService, null);
MetaDataIndexTemplateService service = new MetaDataIndexTemplateService(Settings.EMPTY, null, createIndexService, new AliasValidator(Settings.EMPTY));
final List<Throwable> throwables = new ArrayList<>();
service.putTemplate(request, new MetaDataIndexTemplateService.PutListener() {