forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a template parameter to override auto_create_index value (elastic…
…#61858) Closes elastic#20640. This PR introduces a new parameter to v2 templates, `allow_auto_create`, which allows templates to override the cluster setting `auto_create_index`. Notes: * `AutoCreateIndex` now looks for a matching v2 template, and if its `allow_auto_create` setting is true, it overrides the usual logic. * `TransportBulkAction` previously used `AutoCreateIndex` to check whether missing indices should be created. We now rely on `AutoCreateAction`, which was already differentiating between creating indices and creating data streams. I've updated `AutoCreateAction` to use `AutoCreateIndex`. Data streams are also influenced by `allow_auto_create`, in that their default auto-create behaviour can be disabled with this setting. * Most of the Java file changes are due to introducing an extra constructor parameter to `ComposableIndexTemplate`. * I've added the new setting to various x-pack templates * I added a YAML test to check that watches can be created even when `auto_create_index` is `false`.
- Loading branch information
1 parent
9a690cd
commit 6cd648e
Showing
46 changed files
with
770 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
qa/smoke-test-http/src/test/java/org/elasticsearch/http/AutoCreateIndexIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* 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.http; | ||
|
||
import org.elasticsearch.action.support.AutoCreateIndex; | ||
import org.elasticsearch.client.Request; | ||
import org.elasticsearch.client.Response; | ||
import org.elasticsearch.client.ResponseException; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.io.Streams; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.json.JsonXContent; | ||
import org.elasticsearch.test.rest.ESRestTestCase; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.hamcrest.Matchers.containsString; | ||
|
||
public class AutoCreateIndexIT extends ESRestTestCase { | ||
|
||
/** | ||
* Check that setting {@link AutoCreateIndex#AUTO_CREATE_INDEX_SETTING} to <code>false</code> | ||
* disable the automatic creation on indices. | ||
*/ | ||
public void testCannotAutoCreateIndexWhenDisabled() throws IOException { | ||
configureAutoCreateIndex(false); | ||
|
||
// Attempt to add a document to a non-existing index. Auto-creating the index should fail owing to the setting above. | ||
final Request indexDocumentRequest = new Request("POST", "recipe_kr/_doc/123456"); | ||
indexDocumentRequest.setJsonEntity("{ \"name\": \"Kimchi\" }"); | ||
final ResponseException responseException = expectThrows(ResponseException.class, this::indexDocument); | ||
|
||
assertThat( | ||
Streams.copyToString(new InputStreamReader(responseException.getResponse().getEntity().getContent(), UTF_8)), | ||
containsString("no such index [recipe_kr] and [action.auto_create_index] is [false]") | ||
); | ||
} | ||
|
||
/** | ||
* Check that automatically creating an index is allowed, even when {@link AutoCreateIndex#AUTO_CREATE_INDEX_SETTING} | ||
* is <code>false</code>, when the index name matches a template and that template has <code>allow_auto_create</code> | ||
* set to <code>true</code>. | ||
*/ | ||
public void testCanAutoCreateIndexWhenAllowedByTemplate() throws IOException { | ||
configureAutoCreateIndex(false); | ||
|
||
createTemplateWithAllowAutoCreate(true); | ||
|
||
// Attempt to add a document to a non-existing index. Auto-creating the index should succeed because the index name | ||
// matches the template pattern | ||
assertOK(this.indexDocument()); | ||
} | ||
|
||
/** | ||
* Check that automatically creating an index is disallowed when the index name matches a template and that template has | ||
* <code>allow_auto_create</code> explicitly to <code>false</code>, even when {@link AutoCreateIndex#AUTO_CREATE_INDEX_SETTING} | ||
* is set to <code>true</code>. | ||
*/ | ||
public void testCannotAutoCreateIndexWhenDisallowedByTemplate() throws IOException { | ||
configureAutoCreateIndex(true); | ||
|
||
createTemplateWithAllowAutoCreate(false); | ||
|
||
// Attempt to add a document to a non-existing index. Auto-creating the index should succeed because the index name | ||
// matches the template pattern | ||
final ResponseException responseException = expectThrows(ResponseException.class, this::indexDocument); | ||
|
||
assertThat( | ||
Streams.copyToString(new InputStreamReader(responseException.getResponse().getEntity().getContent(), UTF_8)), | ||
containsString("no such index [composable template [recipe*] forbids index auto creation]") | ||
); | ||
} | ||
|
||
|
||
private void configureAutoCreateIndex(boolean value) throws IOException { | ||
XContentBuilder builder = JsonXContent.contentBuilder() | ||
.startObject() | ||
.startObject("transient") | ||
.field(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), value) | ||
.endObject() | ||
.endObject(); | ||
|
||
final Request settingsRequest = new Request("PUT", "_cluster/settings"); | ||
settingsRequest.setJsonEntity(Strings.toString(builder)); | ||
final Response settingsResponse = client().performRequest(settingsRequest); | ||
assertOK(settingsResponse); | ||
} | ||
|
||
private void createTemplateWithAllowAutoCreate(Boolean allowAutoCreate) throws IOException { | ||
XContentBuilder builder = JsonXContent.contentBuilder() | ||
.startObject() | ||
.array("index_patterns", "recipe*") | ||
.field("allow_auto_create", allowAutoCreate) | ||
.endObject(); | ||
|
||
final Request createTemplateRequest = new Request("PUT", "_index_template/recipe_template"); | ||
createTemplateRequest.setJsonEntity(Strings.toString(builder)); | ||
final Response createTemplateResponse = client().performRequest(createTemplateRequest); | ||
assertOK(createTemplateResponse); | ||
} | ||
|
||
private Response indexDocument() throws IOException { | ||
final Request indexDocumentRequest = new Request("POST", "recipe_kr/_doc/123456"); | ||
indexDocumentRequest.setJsonEntity("{ \"name\": \"Kimchi\" }"); | ||
return client().performRequest(indexDocumentRequest); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.