|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.http; |
| 21 | + |
| 22 | +import org.elasticsearch.action.support.AutoCreateIndex; |
| 23 | +import org.elasticsearch.client.Request; |
| 24 | +import org.elasticsearch.client.Response; |
| 25 | +import org.elasticsearch.client.ResponseException; |
| 26 | +import org.elasticsearch.common.Strings; |
| 27 | +import org.elasticsearch.common.io.Streams; |
| 28 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 29 | +import org.elasticsearch.common.xcontent.json.JsonXContent; |
| 30 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.io.InputStreamReader; |
| 34 | + |
| 35 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 36 | +import static org.hamcrest.Matchers.containsString; |
| 37 | + |
| 38 | +public class AutoCreateIndexIT extends ESRestTestCase { |
| 39 | + |
| 40 | + /** |
| 41 | + * Check that setting {@link AutoCreateIndex#AUTO_CREATE_INDEX_SETTING} to <code>false</code> |
| 42 | + * disable the automatic creation on indices. |
| 43 | + */ |
| 44 | + public void testCannotAutoCreateIndexWhenDisabled() throws IOException { |
| 45 | + configureAutoCreateIndex(false); |
| 46 | + |
| 47 | + // Attempt to add a document to a non-existing index. Auto-creating the index should fail owing to the setting above. |
| 48 | + final Request indexDocumentRequest = new Request("POST", "recipe_kr/_doc/123456"); |
| 49 | + indexDocumentRequest.setJsonEntity("{ \"name\": \"Kimchi\" }"); |
| 50 | + final ResponseException responseException = expectThrows(ResponseException.class, this::indexDocument); |
| 51 | + |
| 52 | + assertThat( |
| 53 | + Streams.copyToString(new InputStreamReader(responseException.getResponse().getEntity().getContent(), UTF_8)), |
| 54 | + containsString("no such index [recipe_kr] and [action.auto_create_index] is [false]") |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Check that automatically creating an index is allowed, even when {@link AutoCreateIndex#AUTO_CREATE_INDEX_SETTING} |
| 60 | + * is <code>false</code>, when the index name matches a template and that template has <code>allow_auto_create</code> |
| 61 | + * set to <code>true</code>. |
| 62 | + */ |
| 63 | + public void testCanAutoCreateIndexWhenAllowedByTemplate() throws IOException { |
| 64 | + configureAutoCreateIndex(false); |
| 65 | + |
| 66 | + createTemplateWithAllowAutoCreate(true); |
| 67 | + |
| 68 | + // Attempt to add a document to a non-existing index. Auto-creating the index should succeed because the index name |
| 69 | + // matches the template pattern |
| 70 | + assertOK(this.indexDocument()); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Check that automatically creating an index is disallowed when the index name matches a template and that template has |
| 75 | + * <code>allow_auto_create</code> explicitly to <code>false</code>, even when {@link AutoCreateIndex#AUTO_CREATE_INDEX_SETTING} |
| 76 | + * is set to <code>true</code>. |
| 77 | + */ |
| 78 | + public void testCannotAutoCreateIndexWhenDisallowedByTemplate() throws IOException { |
| 79 | + configureAutoCreateIndex(true); |
| 80 | + |
| 81 | + createTemplateWithAllowAutoCreate(false); |
| 82 | + |
| 83 | + // Attempt to add a document to a non-existing index. Auto-creating the index should succeed because the index name |
| 84 | + // matches the template pattern |
| 85 | + final ResponseException responseException = expectThrows(ResponseException.class, this::indexDocument); |
| 86 | + |
| 87 | + assertThat( |
| 88 | + Streams.copyToString(new InputStreamReader(responseException.getResponse().getEntity().getContent(), UTF_8)), |
| 89 | + containsString("no such index [composable template [recipe*] forbids index auto creation]") |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + private void configureAutoCreateIndex(boolean value) throws IOException { |
| 95 | + XContentBuilder builder = JsonXContent.contentBuilder() |
| 96 | + .startObject() |
| 97 | + .startObject("transient") |
| 98 | + .field(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), value) |
| 99 | + .endObject() |
| 100 | + .endObject(); |
| 101 | + |
| 102 | + final Request settingsRequest = new Request("PUT", "_cluster/settings"); |
| 103 | + settingsRequest.setJsonEntity(Strings.toString(builder)); |
| 104 | + final Response settingsResponse = client().performRequest(settingsRequest); |
| 105 | + assertOK(settingsResponse); |
| 106 | + } |
| 107 | + |
| 108 | + private void createTemplateWithAllowAutoCreate(Boolean allowAutoCreate) throws IOException { |
| 109 | + XContentBuilder builder = JsonXContent.contentBuilder() |
| 110 | + .startObject() |
| 111 | + .array("index_patterns", "recipe*") |
| 112 | + .field("allow_auto_create", allowAutoCreate) |
| 113 | + .endObject(); |
| 114 | + |
| 115 | + final Request createTemplateRequest = new Request("PUT", "_index_template/recipe_template"); |
| 116 | + createTemplateRequest.setJsonEntity(Strings.toString(builder)); |
| 117 | + final Response createTemplateResponse = client().performRequest(createTemplateRequest); |
| 118 | + assertOK(createTemplateResponse); |
| 119 | + } |
| 120 | + |
| 121 | + private Response indexDocument() throws IOException { |
| 122 | + final Request indexDocumentRequest = new Request("POST", "recipe_kr/_doc/123456"); |
| 123 | + indexDocumentRequest.setJsonEntity("{ \"name\": \"Kimchi\" }"); |
| 124 | + return client().performRequest(indexDocumentRequest); |
| 125 | + } |
| 126 | +} |
0 commit comments