Skip to content

Commit

Permalink
[7.x] Deprecate legacy index template API endpoints (#71843)
Browse files Browse the repository at this point in the history
  • Loading branch information
danhermann authored Apr 19, 2021
1 parent 111f8bf commit d190d58
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@

public class IndicesClientIT extends ESRestHighLevelClientTestCase {

public static final RequestOptions LEGACY_TEMPLATE_OPTIONS = RequestOptions.DEFAULT.toBuilder()
.setWarningsHandler(warnings ->
org.elasticsearch.common.collect.List.of(RestPutIndexTemplateAction.DEPRECATION_WARNING).equals(warnings) == false).build();

public void testIndicesExists() throws IOException {
// Index present
{
Expand Down Expand Up @@ -1562,7 +1566,7 @@ public void testPutTemplateWithTypes() throws Exception {

AcknowledgedResponse putTemplateResponse = execute(putTemplateRequest,
highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync,
expectWarnings(RestPutIndexTemplateAction.TYPES_DEPRECATION_MESSAGE)
expectWarnings(RestPutIndexTemplateAction.TYPES_DEPRECATION_MESSAGE, RestPutIndexTemplateAction.DEPRECATION_WARNING)
);
assertThat(putTemplateResponse.isAcknowledged(), equalTo(true));

Expand Down Expand Up @@ -1590,7 +1594,7 @@ public void testPutTemplate() throws Exception {
.alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("{index}-write").searchRouting("xyz"));

AcknowledgedResponse putTemplateResponse = execute(putTemplateRequest,
highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync);
highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS);
assertThat(putTemplateResponse.isAcknowledged(), equalTo(true));

Map<String, Object> templates = getAsMap("/_template/my-template");
Expand Down Expand Up @@ -1619,7 +1623,10 @@ public void testPutTemplateWithDeprecatedTemplateField() throws Exception {
AcknowledgedResponse putTemplateResponse = execute(putTemplateRequest,
highLevelClient().indices()::putTemplate,
highLevelClient().indices()::putTemplateAsync,
expectWarnings("Deprecated field [template] used, replaced by [index_patterns]"));
expectWarnings(
RestPutIndexTemplateAction.DEPRECATION_WARNING,
"Deprecated field [template] used, replaced by [index_patterns]"
));
assertThat(putTemplateResponse.isAcknowledged(), equalTo(true));

Map<String, Object> templates = getAsMap("/_template/my-template");
Expand Down Expand Up @@ -1679,7 +1686,7 @@ public void testPutTemplateWithNoTypesUsingTypedApi() throws Exception {

AcknowledgedResponse putTemplateResponse = execute(putTemplateRequest,
highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync,
expectWarnings(RestPutIndexTemplateAction.TYPES_DEPRECATION_MESSAGE)
expectWarnings(RestPutIndexTemplateAction.TYPES_DEPRECATION_MESSAGE, RestPutIndexTemplateAction.DEPRECATION_WARNING)
);
assertThat(putTemplateResponse.isAcknowledged(), equalTo(true));

Expand All @@ -1705,22 +1712,26 @@ public void testPutTemplateBadRequests() throws Exception {
assertThat(withoutPatternError.validationErrors(), contains("index patterns are missing"));

// Create-only specified but an template exists already
PutIndexTemplateRequest goodTemplate = new PutIndexTemplateRequest("t2").patterns(Arrays.asList("qa-*", "prod-*"));
assertTrue(execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync).isAcknowledged());
PutIndexTemplateRequest goodTemplate = new PutIndexTemplateRequest("t2")
.patterns(org.elasticsearch.common.collect.List.of("qa-*", "prod-*"));
assertTrue(execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS)
.isAcknowledged());
goodTemplate.create(true);
ElasticsearchException alreadyExistsError = expectThrows(ElasticsearchException.class,
() -> execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync));
() -> execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS));
assertThat(alreadyExistsError.getDetailedMessage(),
containsString("[type=illegal_argument_exception, reason=index_template [t2] already exists]"));
goodTemplate.create(false);
assertTrue(execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync).isAcknowledged());
assertTrue(execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS)
.isAcknowledged());

// Rejected due to unknown settings
PutIndexTemplateRequest unknownSettingTemplate = new PutIndexTemplateRequest("t3")
.patterns(Collections.singletonList("any"))
.settings(Settings.builder().put("this-setting-does-not-exist", 100));
ElasticsearchStatusException unknownSettingError = expectThrows(ElasticsearchStatusException.class,
() -> execute(unknownSettingTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync));
() -> execute(unknownSettingTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync,
LEGACY_TEMPLATE_OPTIONS));
assertThat(unknownSettingError.getDetailedMessage(), containsString("unknown setting [index.this-setting-does-not-exist]"));
}

Expand Down Expand Up @@ -1773,16 +1784,16 @@ public void testCRUDIndexTemplateWithTypes() throws Exception {
org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest putTemplate1 =
new org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest().name("template-1")
.patterns(Arrays.asList("pattern-1", "name-1")).alias(new Alias("alias-1"));
assertThat(execute(putTemplate1, client.indices()::putTemplate, client.indices()::putTemplateAsync
, expectWarnings(RestPutIndexTemplateAction.TYPES_DEPRECATION_MESSAGE))
assertThat(execute(putTemplate1, client.indices()::putTemplate, client.indices()::putTemplateAsync,
expectWarnings(RestPutIndexTemplateAction.TYPES_DEPRECATION_MESSAGE, RestPutIndexTemplateAction.DEPRECATION_WARNING))
.isAcknowledged(), equalTo(true));
org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest putTemplate2 =
new org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest().name("template-2")
.patterns(Arrays.asList("pattern-2", "name-2"))
.mapping("custom_doc_type", "name", "type=text")
.settings(Settings.builder().put("number_of_shards", "2").put("number_of_replicas", "0"));
assertThat(execute(putTemplate2, client.indices()::putTemplate, client.indices()::putTemplateAsync,
expectWarnings(RestPutIndexTemplateAction.TYPES_DEPRECATION_MESSAGE))
expectWarnings(RestPutIndexTemplateAction.TYPES_DEPRECATION_MESSAGE, RestPutIndexTemplateAction.DEPRECATION_WARNING))
.isAcknowledged(), equalTo(true));

org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse getTemplate1 = execute(
Expand Down Expand Up @@ -1839,32 +1850,36 @@ public void testCRUDIndexTemplateWithTypes() throws Exception {

assertThat(execute(new GetIndexTemplatesRequest("template-*"),
client.indices()::getTemplate, client.indices()::getTemplateAsync,
expectWarnings(RestGetIndexTemplateAction.TYPES_DEPRECATION_MESSAGE)).getIndexTemplates(), hasSize(1));
expectWarnings(RestGetIndexTemplateAction.TYPES_DEPRECATION_MESSAGE))
.getIndexTemplates(), hasSize(1));
assertThat(execute(new GetIndexTemplatesRequest("template-*"),
client.indices()::getTemplate, client.indices()::getTemplateAsync,
expectWarnings(RestGetIndexTemplateAction.TYPES_DEPRECATION_MESSAGE)).getIndexTemplates()
.get(0).name(), equalTo("template-2"));
expectWarnings(RestGetIndexTemplateAction.TYPES_DEPRECATION_MESSAGE))
.getIndexTemplates().get(0).name(), equalTo("template-2"));

assertTrue(execute(new DeleteIndexTemplateRequest("template-*"),
client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync).isAcknowledged());
assertThat(expectThrows(ElasticsearchException.class, () -> execute(new GetIndexTemplatesRequest("template-*"),
client.indices()::getTemplate, client.indices()::getTemplateAsync,
expectWarnings(RestGetIndexTemplateAction.TYPES_DEPRECATION_MESSAGE))).status(), equalTo(RestStatus.NOT_FOUND));
expectWarnings(RestGetIndexTemplateAction.TYPES_DEPRECATION_MESSAGE)))
.status(), equalTo(RestStatus.NOT_FOUND));
}


public void testCRUDIndexTemplate() throws Exception {
RestHighLevelClient client = highLevelClient();

PutIndexTemplateRequest putTemplate1 = new PutIndexTemplateRequest("template-1")
.patterns(Arrays.asList("pattern-1", "name-1")).alias(new Alias("alias-1"));
assertThat(execute(putTemplate1, client.indices()::putTemplate, client.indices()::putTemplateAsync).isAcknowledged(),
.patterns(org.elasticsearch.common.collect.List.of("pattern-1", "name-1"))
.alias(new Alias("alias-1"));
assertThat(execute(putTemplate1, client.indices()::putTemplate, client.indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS)
.isAcknowledged(),
equalTo(true));
PutIndexTemplateRequest putTemplate2 = new PutIndexTemplateRequest("template-2")
.patterns(Arrays.asList("pattern-2", "name-2"))
.mapping("{\"properties\": { \"name\": { \"type\": \"text\" }}}", XContentType.JSON)
.settings(Settings.builder().put("number_of_shards", "2").put("number_of_replicas", "0"));
assertThat(execute(putTemplate2, client.indices()::putTemplate, client.indices()::putTemplateAsync)
assertThat(execute(putTemplate2, client.indices()::putTemplate, client.indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS)
.isAcknowledged(), equalTo(true));

GetIndexTemplatesResponse getTemplate1 = execute(
Expand Down Expand Up @@ -1939,7 +1954,14 @@ public void testIndexTemplatesExist() throws Exception {
final PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("template-" + suffix)
.patterns(Arrays.asList("pattern-" + suffix, "name-" + suffix))
.alias(new Alias("alias-" + suffix));
assertTrue(execute(putRequest, client.indices()::putTemplate, client.indices()::putTemplateAsync).isAcknowledged());
assertTrue(
execute(
putRequest,
client.indices()::putTemplate,
client.indices()::putTemplateAsync,
LEGACY_TEMPLATE_OPTIONS
).isAcknowledged()
);

final IndexTemplatesExistRequest existsRequest = new IndexTemplatesExistRequest("template-" + suffix);
assertTrue(execute(existsRequest, client.indices()::existsTemplate, client.indices()::existsTemplateAsync));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@
import org.elasticsearch.client.indices.DeleteComposableIndexTemplateRequest;
import org.elasticsearch.client.indices.DetailAnalyzeResponse;
import org.elasticsearch.client.indices.FreezeIndexRequest;
import org.elasticsearch.client.indices.GetComposableIndexTemplateRequest;
import org.elasticsearch.client.indices.GetComposableIndexTemplatesResponse;
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.client.indices.GetFieldMappingsResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.client.indices.GetComposableIndexTemplateRequest;
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
import org.elasticsearch.client.indices.GetIndexTemplatesResponse;
import org.elasticsearch.client.indices.GetComposableIndexTemplatesResponse;
import org.elasticsearch.client.indices.GetMappingsRequest;
import org.elasticsearch.client.indices.GetMappingsResponse;
import org.elasticsearch.client.indices.IndexTemplateMetadata;
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutComponentTemplateRequest;
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
import org.elasticsearch.client.indices.PutComposableIndexTemplateRequest;
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.client.indices.ReloadAnalyzersRequest;
import org.elasticsearch.client.indices.ReloadAnalyzersResponse;
Expand Down Expand Up @@ -110,6 +110,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.elasticsearch.client.IndicesClientIT.LEGACY_TEMPLATE_OPTIONS;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
Expand Down Expand Up @@ -2183,7 +2184,7 @@ public void testPutTemplate() throws Exception {
"}",
XContentType.JSON);
// end::put-template-request-mappings-json
assertTrue(client.indices().putTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
assertTrue(client.indices().putTemplate(request, LEGACY_TEMPLATE_OPTIONS).isAcknowledged());
}
{
//tag::put-template-request-mappings-map
Expand All @@ -2199,7 +2200,7 @@ public void testPutTemplate() throws Exception {
}
request.mapping(jsonMap); // <1>
//end::put-template-request-mappings-map
assertTrue(client.indices().putTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
assertTrue(client.indices().putTemplate(request, LEGACY_TEMPLATE_OPTIONS).isAcknowledged());
}
{
//tag::put-template-request-mappings-xcontent
Expand All @@ -2219,7 +2220,7 @@ public void testPutTemplate() throws Exception {
builder.endObject();
request.mapping(builder); // <1>
//end::put-template-request-mappings-xcontent
assertTrue(client.indices().putTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
assertTrue(client.indices().putTemplate(request, LEGACY_TEMPLATE_OPTIONS).isAcknowledged());
}

// tag::put-template-request-aliases
Expand Down Expand Up @@ -2271,7 +2272,7 @@ public void testPutTemplate() throws Exception {
request.create(false); // make test happy

// tag::put-template-execute
AcknowledgedResponse putTemplateResponse = client.indices().putTemplate(request, RequestOptions.DEFAULT);
AcknowledgedResponse putTemplateResponse = client.indices().putTemplate(request, LEGACY_TEMPLATE_OPTIONS);
// end::put-template-execute

// tag::put-template-response
Expand Down Expand Up @@ -2299,7 +2300,7 @@ public void onFailure(Exception e) {
listener = new LatchedActionListener<>(listener, latch);

// tag::put-template-execute-async
client.indices().putTemplateAsync(request, RequestOptions.DEFAULT, listener); // <1>
client.indices().putTemplateAsync(request, LEGACY_TEMPLATE_OPTIONS, listener); // <1>
// end::put-template-execute-async

assertTrue(latch.await(30L, TimeUnit.SECONDS));
Expand All @@ -2314,7 +2315,7 @@ public void testGetTemplates() throws Exception {
putRequest.mapping("{ \"properties\": { \"message\": { \"type\": \"text\" } } }",
XContentType.JSON
);
assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
assertTrue(client.indices().putTemplate(putRequest, LEGACY_TEMPLATE_OPTIONS).isAcknowledged());
}

// tag::get-templates-request
Expand Down Expand Up @@ -2705,9 +2706,9 @@ public void onFailure(Exception e) {
public void testTemplatesExist() throws Exception {
final RestHighLevelClient client = highLevelClient();
{
final PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template");
putRequest.patterns(Collections.singletonList("foo"));
assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
final PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template")
.patterns(org.elasticsearch.common.collect.List.of("foo"));
assertTrue(client.indices().putTemplate(putRequest, LEGACY_TEMPLATE_OPTIONS).isAcknowledged());
}

{
Expand Down Expand Up @@ -3118,7 +3119,7 @@ public void testDeleteTemplate() throws Exception {
PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template");
putRequest.patterns(Arrays.asList("pattern-1", "log-*"));
putRequest.settings(Settings.builder().put("index.number_of_shards", 3));
assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
assertTrue(client.indices().putTemplate(putRequest, LEGACY_TEMPLATE_OPTIONS).isAcknowledged());
}

// tag::delete-template-request
Expand All @@ -3144,7 +3145,7 @@ public void testDeleteTemplate() throws Exception {
PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template");
putRequest.patterns(Arrays.asList("pattern-1", "log-*"));
putRequest.settings(Settings.builder().put("index.number_of_shards", 3));
assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
assertTrue(client.indices().putTemplate(putRequest, LEGACY_TEMPLATE_OPTIONS).isAcknowledged());
}
// tag::delete-template-execute-listener
ActionListener<AcknowledgedResponse> listener =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.client.Response;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.action.admin.indices.RestPutIndexTemplateAction;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.hamcrest.Matcher;

Expand Down Expand Up @@ -138,7 +139,10 @@ public void testTemplateExists() throws IOException {
".slm-history => [.slm-history-5*]," +
".watch-history-13 => [.watcher-history-13*],ilm-history => [ilm-history-5*]," +
"logs => [logs-*-*],metrics => [metrics-*-*],synthetics => [synthetics-*-*]" +
"); this template [template] may be ignored in favor of a composable template at index creation time"));
"); this template [template] may be ignored in favor of a composable template at index creation time",
RestPutIndexTemplateAction.DEPRECATION_WARNING));
} else {
request.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING));
}
request.setJsonEntity(Strings.toString(builder));
client().performRequest(request);
Expand Down
Loading

0 comments on commit d190d58

Please sign in to comment.