|
55 | 55 | import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; |
56 | 56 | import org.elasticsearch.action.admin.indices.shrink.ResizeResponse; |
57 | 57 | import org.elasticsearch.action.admin.indices.shrink.ResizeType; |
| 58 | +import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest; |
| 59 | +import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse; |
58 | 60 | import org.elasticsearch.action.index.IndexRequest; |
59 | 61 | import org.elasticsearch.action.support.IndicesOptions; |
60 | 62 | import org.elasticsearch.action.support.WriteRequest; |
61 | 63 | import org.elasticsearch.action.support.broadcast.BroadcastResponse; |
62 | 64 | import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 65 | +import org.elasticsearch.common.ValidationException; |
63 | 66 | import org.elasticsearch.common.settings.Setting; |
64 | 67 | import org.elasticsearch.common.settings.Settings; |
65 | 68 | import org.elasticsearch.common.unit.ByteSizeUnit; |
|
72 | 75 | import org.elasticsearch.rest.RestStatus; |
73 | 76 |
|
74 | 77 | import java.io.IOException; |
| 78 | +import java.util.Arrays; |
| 79 | +import java.util.Collections; |
75 | 80 | import java.util.Map; |
76 | 81 |
|
77 | 82 | import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS; |
| 83 | +import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractRawValues; |
| 84 | +import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractValue; |
78 | 85 | import static org.hamcrest.CoreMatchers.hasItem; |
| 86 | +import static org.hamcrest.Matchers.contains; |
| 87 | +import static org.hamcrest.Matchers.containsString; |
79 | 88 | import static org.hamcrest.Matchers.equalTo; |
| 89 | +import static org.hamcrest.Matchers.hasEntry; |
| 90 | +import static org.hamcrest.Matchers.hasSize; |
80 | 91 | import static org.hamcrest.Matchers.not; |
81 | 92 | import static org.hamcrest.Matchers.startsWith; |
82 | 93 |
|
@@ -710,4 +721,59 @@ public void testIndexPutSettingNonExistent() throws IOException { |
710 | 721 | + "or check the breaking changes documentation for removed settings]")); |
711 | 722 | } |
712 | 723 |
|
| 724 | + @SuppressWarnings("unchecked") |
| 725 | + public void testPutTemplate() throws Exception { |
| 726 | + PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest() |
| 727 | + .name("my-template") |
| 728 | + .patterns(Arrays.asList("pattern-1", "name-*")) |
| 729 | + .order(10) |
| 730 | + .create(randomBoolean()) |
| 731 | + .settings(Settings.builder().put("number_of_shards", "3").put("number_of_replicas", "0")) |
| 732 | + .mapping("doc", "host_name", "type=keyword", "description", "type=text") |
| 733 | + .alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("{index}-write").searchRouting("xyz")); |
| 734 | + |
| 735 | + PutIndexTemplateResponse putTemplateResponse = execute(putTemplateRequest, |
| 736 | + highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync); |
| 737 | + assertThat(putTemplateResponse.isAcknowledged(), equalTo(true)); |
| 738 | + |
| 739 | + Map<String, Object> templates = getAsMap("/_template/my-template"); |
| 740 | + assertThat(templates.keySet(), hasSize(1)); |
| 741 | + assertThat(extractValue("my-template.order", templates), equalTo(10)); |
| 742 | + assertThat(extractRawValues("my-template.index_patterns", templates), contains("pattern-1", "name-*")); |
| 743 | + assertThat(extractValue("my-template.settings.index.number_of_shards", templates), equalTo("3")); |
| 744 | + assertThat(extractValue("my-template.settings.index.number_of_replicas", templates), equalTo("0")); |
| 745 | + assertThat(extractValue("my-template.mappings.doc.properties.host_name.type", templates), equalTo("keyword")); |
| 746 | + assertThat(extractValue("my-template.mappings.doc.properties.description.type", templates), equalTo("text")); |
| 747 | + assertThat((Map<String, String>) extractValue("my-template.aliases.alias-1", templates), hasEntry("index_routing", "abc")); |
| 748 | + assertThat((Map<String, String>) extractValue("my-template.aliases.{index}-write", templates), hasEntry("search_routing", "xyz")); |
| 749 | + } |
| 750 | + |
| 751 | + public void testPutTemplateBadRequests() throws Exception { |
| 752 | + RestHighLevelClient client = highLevelClient(); |
| 753 | + |
| 754 | + // Failed to validate because index patterns are missing |
| 755 | + PutIndexTemplateRequest withoutPattern = new PutIndexTemplateRequest("t1"); |
| 756 | + ValidationException withoutPatternError = expectThrows(ValidationException.class, |
| 757 | + () -> execute(withoutPattern, client.indices()::putTemplate, client.indices()::putTemplateAsync)); |
| 758 | + assertThat(withoutPatternError.validationErrors(), contains("index patterns are missing")); |
| 759 | + |
| 760 | + // Create-only specified but an template exists already |
| 761 | + PutIndexTemplateRequest goodTemplate = new PutIndexTemplateRequest("t2").patterns(Arrays.asList("qa-*", "prod-*")); |
| 762 | + assertTrue(execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync).isAcknowledged()); |
| 763 | + goodTemplate.create(true); |
| 764 | + ElasticsearchException alreadyExistsError = expectThrows(ElasticsearchException.class, |
| 765 | + () -> execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync)); |
| 766 | + assertThat(alreadyExistsError.getDetailedMessage(), |
| 767 | + containsString("[type=illegal_argument_exception, reason=index_template [t2] already exists]")); |
| 768 | + goodTemplate.create(false); |
| 769 | + assertTrue(execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync).isAcknowledged()); |
| 770 | + |
| 771 | + // Rejected due to unknown settings |
| 772 | + PutIndexTemplateRequest unknownSettingTemplate = new PutIndexTemplateRequest("t3") |
| 773 | + .patterns(Collections.singletonList("any")) |
| 774 | + .settings(Settings.builder().put("this-setting-does-not-exist", 100)); |
| 775 | + ElasticsearchStatusException unknownSettingError = expectThrows(ElasticsearchStatusException.class, |
| 776 | + () -> execute(unknownSettingTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync)); |
| 777 | + assertThat(unknownSettingError.getDetailedMessage(), containsString("unknown setting [index.this-setting-does-not-exist]")); |
| 778 | + } |
713 | 779 | } |
0 commit comments