Search in sources :

Example 1 with InvalidIndexTemplateException

use of org.elasticsearch.indices.InvalidIndexTemplateException in project elasticsearch by elastic.

the class MetaDataIndexTemplateService method validate.

private void validate(PutRequest request) {
    List<String> validationErrors = new ArrayList<>();
    if (request.name.contains(" ")) {
        validationErrors.add("name must not contain a space");
    }
    if (request.name.contains(",")) {
        validationErrors.add("name must not contain a ','");
    }
    if (request.name.contains("#")) {
        validationErrors.add("name must not contain a '#'");
    }
    if (request.name.startsWith("_")) {
        validationErrors.add("name must not start with '_'");
    }
    if (!request.name.toLowerCase(Locale.ROOT).equals(request.name)) {
        validationErrors.add("name must be lower cased");
    }
    for (String indexPattern : request.indexPatterns) {
        if (indexPattern.contains(" ")) {
            validationErrors.add("template must not contain a space");
        }
        if (indexPattern.contains(",")) {
            validationErrors.add("template must not contain a ','");
        }
        if (indexPattern.contains("#")) {
            validationErrors.add("template must not contain a '#'");
        }
        if (indexPattern.startsWith("_")) {
            validationErrors.add("template must not start with '_'");
        }
        if (!Strings.validFileNameExcludingAstrix(indexPattern)) {
            validationErrors.add("template must not contain the following characters " + Strings.INVALID_FILENAME_CHARS);
        }
    }
    try {
        indexScopedSettings.validate(request.settings);
    } catch (IllegalArgumentException iae) {
        validationErrors.add(iae.getMessage());
        for (Throwable t : iae.getSuppressed()) {
            validationErrors.add(t.getMessage());
        }
    }
    List<String> indexSettingsValidation = metaDataCreateIndexService.getIndexSettingsValidationErrors(request.settings);
    validationErrors.addAll(indexSettingsValidation);
    if (!validationErrors.isEmpty()) {
        ValidationException validationException = new ValidationException();
        validationException.addValidationErrors(validationErrors);
        throw new InvalidIndexTemplateException(request.name, validationException.getMessage());
    }
    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.indexPatterns.contains(alias.name())) {
            throw new IllegalArgumentException("Alias [" + alias.name() + "] cannot be the same as any pattern in [" + String.join(", ", request.indexPatterns) + "]");
        }
    }
}
Also used : ValidationException(org.elasticsearch.common.ValidationException) Alias(org.elasticsearch.action.admin.indices.alias.Alias) ArrayList(java.util.ArrayList) InvalidIndexTemplateException(org.elasticsearch.indices.InvalidIndexTemplateException)

Example 2 with InvalidIndexTemplateException

use of org.elasticsearch.indices.InvalidIndexTemplateException in project elasticsearch by elastic.

the class SearchPhaseExecutionExceptionTests method testToAndFromXContent.

public void testToAndFromXContent() throws IOException {
    final XContent xContent = randomFrom(XContentType.values()).xContent();
    ShardSearchFailure[] shardSearchFailures = new ShardSearchFailure[randomIntBetween(1, 5)];
    for (int i = 0; i < shardSearchFailures.length; i++) {
        Exception cause = randomFrom(new ParsingException(1, 2, "foobar", null), new InvalidIndexTemplateException("foo", "bar"), new TimestampParsingException("foo", null), new NullPointerException());
        shardSearchFailures[i] = new ShardSearchFailure(cause, new SearchShardTarget("node_" + i, new Index("test", "_na_"), i));
    }
    final String phase = randomFrom("query", "search", "other");
    SearchPhaseExecutionException actual = new SearchPhaseExecutionException(phase, "unexpected failures", shardSearchFailures);
    BytesReference exceptionBytes = XContentHelper.toXContent(actual, xContent.type(), randomBoolean());
    ElasticsearchException parsedException;
    try (XContentParser parser = createParser(xContent, exceptionBytes)) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        parsedException = ElasticsearchException.fromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertNull(parser.nextToken());
    }
    assertNotNull(parsedException);
    assertThat(parsedException.getHeaderKeys(), hasSize(0));
    assertThat(parsedException.getMetadataKeys(), hasSize(1));
    assertThat(parsedException.getMetadata("es.phase"), hasItem(phase));
    // SearchPhaseExecutionException has no cause field
    assertNull(parsedException.getCause());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) InvalidIndexTemplateException(org.elasticsearch.indices.InvalidIndexTemplateException) Index(org.elasticsearch.index.Index) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) InvalidIndexTemplateException(org.elasticsearch.indices.InvalidIndexTemplateException) IndexShardClosedException(org.elasticsearch.index.shard.IndexShardClosedException) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) TimestampParsingException(org.elasticsearch.action.TimestampParsingException) XContent(org.elasticsearch.common.xcontent.XContent) ToXContent(org.elasticsearch.common.xcontent.ToXContent) ParsingException(org.elasticsearch.common.ParsingException) TimestampParsingException(org.elasticsearch.action.TimestampParsingException) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) TimestampParsingException(org.elasticsearch.action.TimestampParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 3 with InvalidIndexTemplateException

use of org.elasticsearch.indices.InvalidIndexTemplateException in project elasticsearch by elastic.

the class ExceptionSerializationTests method testInvalidIndexTemplateException.

public void testInvalidIndexTemplateException() throws IOException {
    InvalidIndexTemplateException ex = serialize(new InvalidIndexTemplateException("foo", "bar"));
    assertEquals(ex.getMessage(), "index_template [foo] invalid, cause [bar]");
    assertEquals(ex.name(), "foo");
    ex = serialize(new InvalidIndexTemplateException(null, "bar"));
    assertEquals(ex.getMessage(), "index_template [null] invalid, cause [bar]");
    assertEquals(ex.name(), null);
}
Also used : InvalidIndexTemplateException(org.elasticsearch.indices.InvalidIndexTemplateException)

Aggregations

InvalidIndexTemplateException (org.elasticsearch.indices.InvalidIndexTemplateException)3 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 TimestampParsingException (org.elasticsearch.action.TimestampParsingException)1 Alias (org.elasticsearch.action.admin.indices.alias.Alias)1 ParsingException (org.elasticsearch.common.ParsingException)1 ValidationException (org.elasticsearch.common.ValidationException)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 ToXContent (org.elasticsearch.common.xcontent.ToXContent)1 XContent (org.elasticsearch.common.xcontent.XContent)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1 Index (org.elasticsearch.index.Index)1 IndexShardClosedException (org.elasticsearch.index.shard.IndexShardClosedException)1 SearchShardTarget (org.elasticsearch.search.SearchShardTarget)1