From f55f2d6d2ac7f56e35f42e1fb1ab97aa472fef8b Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Thu, 26 Mar 2020 15:21:28 +0100 Subject: [PATCH 01/17] HLRC support for Component Templates --- .../indices/GetComponentTemplatesRequest.java | 99 +++++++++++++ .../GetComponentTemplatesResponse.java | 109 +++++++++++++++ .../GetComponentTemplatesResponseTests.java | 132 ++++++++++++++++++ .../cluster/metadata/ComponentTemplate.java | 2 +- 4 files changed, 341 insertions(+), 1 deletion(-) create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesResponse.java create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComponentTemplatesResponseTests.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java new file mode 100644 index 0000000000000..bb4b65175bb92 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java @@ -0,0 +1,99 @@ +/* + * 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.client.indices; + +import org.elasticsearch.client.TimedRequest; +import org.elasticsearch.client.Validatable; +import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.unit.TimeValue; + +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +import static java.util.Collections.unmodifiableList; + +/** + * A request to read the content of component templates + */ +public class GetComponentTemplatesRequest implements Validatable { + + private final List names; + + private TimeValue masterNodeTimeout = TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT; + private boolean local = false; + + /** + * Create a request to read the content of one or more component templates. If no template names are provided, all templates will be read + * + * @param names the names of templates to read + */ + public GetComponentTemplatesRequest(String... names) { + this(Arrays.asList(names)); + } + + /** + * Create a request to read the content of one or more index templates. If no template names are provided, all templates will be read + * + * @param names the names of templates to read + */ + public GetComponentTemplatesRequest(List names) { + Objects.requireNonNull(names); + if (names.stream().anyMatch(name -> name == null || Strings.hasText(name) == false)) { + throw new IllegalArgumentException("all index template names must be non null and non empty"); + } + this.names = unmodifiableList(names); + } + + /** + * @return the names of index templates this request is requesting + */ + public List names() { + return names; + } + + /** + * @return the timeout for waiting for the master node to respond + */ + public TimeValue getMasterNodeTimeout() { + return masterNodeTimeout; + } + + public void setMasterNodeTimeout(@Nullable TimeValue masterNodeTimeout) { + this.masterNodeTimeout = masterNodeTimeout; + } + + public void setMasterNodeTimeout(String masterNodeTimeout) { + final TimeValue timeValue = TimeValue.parseTimeValue(masterNodeTimeout, getClass().getSimpleName() + ".masterNodeTimeout"); + setMasterNodeTimeout(timeValue); + } + + /** + * @return true if this request is to read from the local cluster state, rather than the master node - false otherwise + */ + public boolean isLocal() { + return local; + } + + public void setLocal(boolean local) { + this.local = local; + } +} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesResponse.java new file mode 100644 index 0000000000000..82b6c64d6a795 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesResponse.java @@ -0,0 +1,109 @@ +/* + * 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.client.indices; + +import org.elasticsearch.cluster.metadata.ComponentTemplate; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.io.IOException; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + + +public class GetComponentTemplatesResponse { + + public static final ParseField NAME = new ParseField("name"); + public static final ParseField COMPONENT_TEMPLATES = new ParseField("component_templates"); + public static final ParseField COMPONENT_TEMPLATE = new ParseField("component_template"); + + @SuppressWarnings("unchecked") + private static final ConstructingObjectParser, Void> PARSER = + new ConstructingObjectParser<>("component_templates", false, + a -> ((List) a[0]).stream().collect(Collectors.toMap(n -> n.name, n -> n.componentTemplate, + (n1, n2) -> n1, LinkedHashMap::new))); + + private static final ConstructingObjectParser INNER_PARSER = + new ConstructingObjectParser<>("named_component_template", false, + a -> new NamedComponentTemplate((String) a[0], (ComponentTemplate) a[1])); + + static { + INNER_PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME); + INNER_PARSER.declareObject(ConstructingObjectParser.constructorArg(), ComponentTemplate.PARSER, COMPONENT_TEMPLATE); + PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), INNER_PARSER, COMPONENT_TEMPLATES); + } + + private static class NamedComponentTemplate { + String name; + ComponentTemplate componentTemplate; + + public NamedComponentTemplate(String name, ComponentTemplate componentTemplate) { + this.name = name; + this.componentTemplate = componentTemplate; + } + } + + @Override + public String toString() { + return "GetIndexTemplatesResponse [indexTemplates=" + componentTemplates + "]"; + } + + private final Map componentTemplates; + + GetComponentTemplatesResponse() { + componentTemplates = Collections.emptyMap(); + } + + GetComponentTemplatesResponse(Map componentTemplates) { + this.componentTemplates = Collections.unmodifiableMap(new LinkedHashMap<>(componentTemplates)); + } + + public Map getComponentTemplates() { + return componentTemplates; + } + + + public static GetComponentTemplatesResponse fromXContent(XContentParser parser) throws IOException { + return new GetComponentTemplatesResponse(PARSER.apply(parser, null)); + } + + @Override + public int hashCode() { + return Objects.hash(componentTemplates); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + GetComponentTemplatesResponse other = (GetComponentTemplatesResponse) obj; + return Objects.equals(componentTemplates, other.componentTemplates); + } + + +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComponentTemplatesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComponentTemplatesResponseTests.java new file mode 100644 index 0000000000000..413e2bb7c6065 --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComponentTemplatesResponseTests.java @@ -0,0 +1,132 @@ +/* + * 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.client.indices; + +import org.elasticsearch.cluster.metadata.AliasMetaData; +import org.elasticsearch.cluster.metadata.ComponentTemplate; +import org.elasticsearch.cluster.metadata.Template; +import org.elasticsearch.common.compress.CompressedXContent; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.test.ESTestCase; + +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester; + +public class GetComponentTemplatesResponseTests extends ESTestCase { + + public void testFromXContent() throws Exception { + xContentTester( + this::createParser, + GetComponentTemplatesResponseTests::createTestInstance, + GetComponentTemplatesResponseTests::toXContent, + GetComponentTemplatesResponse::fromXContent) + .supportsUnknownFields(true) + .randomFieldsExcludeFilter(a -> true) + .test(); + } + + private static GetComponentTemplatesResponse createTestInstance() { + Map templates = new HashMap<>(); + if (randomBoolean()) { + int count = randomInt(10); + for (int i = 0; i < count; i++) { + templates.put(randomAlphaOfLength(10), randomTemplate()); + } + } + return new GetComponentTemplatesResponse(templates); + } + + private static void toXContent(GetComponentTemplatesResponse response, XContentBuilder builder) throws IOException { + builder.startObject(); + builder.startArray("component_templates"); + for (Map.Entry e : response.getComponentTemplates().entrySet()) { + builder.startObject(); + builder.field("name", e.getKey()); + builder.field("component_template"); + e.getValue().toXContent(builder, null); + builder.endObject(); + builder.endObject(); + } + builder.endArray(); + builder.endObject(); + } + + private static ComponentTemplate randomTemplate() { + Settings settings = null; + CompressedXContent mappings = null; + Map aliases = null; + if (randomBoolean()) { + settings = randomSettings(); + } + if (randomBoolean()) { + mappings = randomMappings(); + } + if (randomBoolean()) { + aliases = randomAliases(); + } + Template template = new Template(settings, mappings, aliases); + + Map meta = null; + if (randomBoolean()) { + meta = randomMeta(); + } + return new ComponentTemplate(template, randomBoolean() ? null : randomNonNegativeLong(), meta); + } + + private static Map randomAliases() { + String aliasName = randomAlphaOfLength(5); + AliasMetaData aliasMeta = AliasMetaData.builder(aliasName) + .filter(Collections.singletonMap(randomAlphaOfLength(2), randomAlphaOfLength(2))) + .routing(randomBoolean() ? null : randomAlphaOfLength(3)) + .isHidden(randomBoolean() ? null : randomBoolean()) + .writeIndex(randomBoolean() ? null : randomBoolean()) + .build(); + return Collections.singletonMap(aliasName, aliasMeta); + } + + private static CompressedXContent randomMappings() { + try { + return new CompressedXContent("{\"" + randomAlphaOfLength(3) + "\":\"" + randomAlphaOfLength(7) + "\"}"); + } catch (IOException e) { + fail("got an IO exception creating fake mappings: " + e); + return null; + } + } + + private static Settings randomSettings() { + return Settings.builder() + .put(randomAlphaOfLength(4), randomAlphaOfLength(10)) + .build(); + } + + private static Map randomMeta() { + if (randomBoolean()) { + return Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(4)); + } else { + return Collections.singletonMap(randomAlphaOfLength(5), + Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(4))); + } + } +} diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/ComponentTemplate.java b/server/src/main/java/org/elasticsearch/cluster/metadata/ComponentTemplate.java index 0cc8c8f8a940d..24638caddc0ab 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/ComponentTemplate.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/ComponentTemplate.java @@ -47,7 +47,7 @@ public class ComponentTemplate extends AbstractDiffable imple private static final ParseField METADATA = new ParseField("_meta"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("component_template", false, a -> new ComponentTemplate((Template) a[0], (Long) a[1], (Map) a[2])); From 6e904d03b3f38373fe06d195118339e38f4b6481 Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Fri, 27 Mar 2020 12:02:48 +0100 Subject: [PATCH 02/17] hlrc --- .../elasticsearch/client/IndicesClient.java | 30 +++++++++++++++++++ .../client/IndicesRequestConverters.java | 14 +++++++++ .../indices/GetComponentTemplatesRequest.java | 4 +-- .../GetComponentTemplatesResponseTests.java | 1 - 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java index 96494238e75e0..39ab20c5539d1 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java @@ -49,6 +49,8 @@ import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.DeleteAliasRequest; import org.elasticsearch.client.indices.FreezeIndexRequest; +import org.elasticsearch.client.indices.GetComponentTemplatesRequest; +import org.elasticsearch.client.indices.GetComponentTemplatesResponse; import org.elasticsearch.client.indices.GetFieldMappingsRequest; import org.elasticsearch.client.indices.GetFieldMappingsResponse; import org.elasticsearch.client.indices.GetIndexRequest; @@ -943,6 +945,34 @@ public Cancellable validateQueryAsync(ValidateQueryRequest validateQueryRequest, ValidateQueryResponse::fromXContent, listener, emptySet()); } + /** + * Gets component templates using the Components Templates API + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param getComponentTemplatesRequest the request + * @return the response + * @throws IOException in case there is a problem sending the request or parsing back the response + */ + public GetComponentTemplatesResponse getComponentTemplate(GetComponentTemplatesRequest getComponentTemplatesRequest, + RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(getComponentTemplatesRequest, + IndicesRequestConverters::getComponentTemplates, + options, GetComponentTemplatesResponse::fromXContent, emptySet()); + } + + /** + * Asynchronously gets component templates using the Components Templates API + * @param getComponentTemplatesRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion + * @return cancellable that may be used to cancel the request + */ + public Cancellable getComponentTemplateAsync(GetComponentTemplatesRequest getComponentTemplatesRequest, RequestOptions options, + ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity(getComponentTemplatesRequest, + IndicesRequestConverters::getComponentTemplates, + options, GetComponentTemplatesResponse::fromXContent, listener, emptySet()); + } + /** * Gets index templates using the Index Templates API * See Index Templates API diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java index b0afa84a05e38..b8711ee3f5fcb 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java @@ -42,6 +42,7 @@ import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.DeleteAliasRequest; import org.elasticsearch.client.indices.FreezeIndexRequest; +import org.elasticsearch.client.indices.GetComponentTemplatesRequest; import org.elasticsearch.client.indices.GetFieldMappingsRequest; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.client.indices.GetIndexTemplatesRequest; @@ -429,6 +430,19 @@ static Request getAlias(GetAliasesRequest getAliasesRequest) { return request; } + static Request getComponentTemplates(GetComponentTemplatesRequest getComponentTemplatesRequest){ + final String endpoint = new RequestConverters.EndpointBuilder() + .addPathPartAsIs("_component_template") + .addCommaSeparatedPathParts(getComponentTemplatesRequest.names()) + .build(); + final Request request = new Request(HttpGet.METHOD_NAME, endpoint); + final RequestConverters.Params params = new RequestConverters.Params(); + params.withLocal(getComponentTemplatesRequest.isLocal()); + params.withMasterTimeout(getComponentTemplatesRequest.getMasterNodeTimeout()); + request.addParameters(params.asMap()); + return request; + } + static Request getTemplates(GetIndexTemplatesRequest getIndexTemplatesRequest) { final String endpoint = new RequestConverters.EndpointBuilder() .addPathPartAsIs("_template") diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java index bb4b65175bb92..1d7e6b3cbb17d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java @@ -51,7 +51,7 @@ public GetComponentTemplatesRequest(String... names) { } /** - * Create a request to read the content of one or more index templates. If no template names are provided, all templates will be read + * Create a request to read the content of one or more component templates. If no template names are provided, all templates will be read * * @param names the names of templates to read */ @@ -64,7 +64,7 @@ public GetComponentTemplatesRequest(List names) { } /** - * @return the names of index templates this request is requesting + * @return the names of component templates this request is requesting */ public List names() { return names; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComponentTemplatesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComponentTemplatesResponseTests.java index 413e2bb7c6065..1c33895b029e8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComponentTemplatesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComponentTemplatesResponseTests.java @@ -67,7 +67,6 @@ private static void toXContent(GetComponentTemplatesResponse response, XContentB builder.field("component_template"); e.getValue().toXContent(builder, null); builder.endObject(); - builder.endObject(); } builder.endArray(); builder.endObject(); From 71ea5f164de1e689ce112a00b15330436b984c5f Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Thu, 2 Apr 2020 13:13:24 +0200 Subject: [PATCH 03/17] hlrc --- .../elasticsearch/client/IndicesClient.java | 91 ++++++++++ .../client/IndicesRequestConverters.java | 44 +++++ .../ComponentTemplatesExistRequest.java | 50 ++++++ .../DeleteComponentTemplateRequest.java | 35 ++++ .../indices/PutComponentTemplateRequest.java | 161 ++++++++++++++++++ .../elasticsearch/client/IndicesClientIT.java | 51 ++++++ .../cluster/metadata/Template.java | 1 + 7 files changed, 433 insertions(+) create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ComponentTemplatesExistRequest.java create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/indices/DeleteComponentTemplateRequest.java create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutComponentTemplateRequest.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java index 39ab20c5539d1..7d4f1426572d2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java @@ -36,6 +36,7 @@ import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest; import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; +import org.elasticsearch.action.admin.indices.template.delete.DeleteComponentTemplateAction; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse; @@ -45,9 +46,11 @@ import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.client.indices.CloseIndexRequest; import org.elasticsearch.client.indices.CloseIndexResponse; +import org.elasticsearch.client.indices.ComponentTemplatesExistRequest; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.DeleteAliasRequest; +import org.elasticsearch.client.indices.DeleteComponentTemplateRequest; import org.elasticsearch.client.indices.FreezeIndexRequest; import org.elasticsearch.client.indices.GetComponentTemplatesRequest; import org.elasticsearch.client.indices.GetComponentTemplatesResponse; @@ -60,6 +63,7 @@ import org.elasticsearch.client.indices.GetMappingsRequest; import org.elasticsearch.client.indices.GetMappingsResponse; import org.elasticsearch.client.indices.IndexTemplatesExistRequest; +import org.elasticsearch.client.indices.PutComponentTemplateRequest; import org.elasticsearch.client.indices.PutIndexTemplateRequest; import org.elasticsearch.client.indices.PutMappingRequest; import org.elasticsearch.client.indices.ReloadAnalyzersRequest; @@ -911,6 +915,36 @@ public Cancellable putTemplateAsync(PutIndexTemplateRequest putIndexTemplateRequ AcknowledgedResponse::fromXContent, listener, emptySet()); } + /** + * Puts a component template using the Component Templates API. + * + * @param putComponentTemplateRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @return the response + * @throws IOException in case there is a problem sending the request or parsing back the response + */ + public AcknowledgedResponse putComponentTemplate( + PutComponentTemplateRequest putComponentTemplateRequest, + RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(putComponentTemplateRequest, IndicesRequestConverters::putComponentTemplate, + options, AcknowledgedResponse::fromXContent, emptySet()); + } + + /** + * Asynchronously puts a component template using the Component Templates API. + * + * @param putComponentTemplateRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion + * @return cancellable that may be used to cancel the request + */ + public Cancellable putComponentTemplateAsync(PutComponentTemplateRequest putComponentTemplateRequest, + RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity(putComponentTemplateRequest, + IndicesRequestConverters::putComponentTemplate, options, + AcknowledgedResponse::fromXContent, listener, emptySet()); + } + /** * Validate a potentially expensive query without executing it. *

@@ -973,6 +1007,37 @@ public Cancellable getComponentTemplateAsync(GetComponentTemplatesRequest getCom options, GetComponentTemplatesResponse::fromXContent, listener, emptySet()); } + /** + * Uses the Component Templates API to determine if component templates exist + * + * @param componentTemplatesRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @return true if any index templates in the request exist, false otherwise + * @throws IOException in case there is a problem sending the request or parsing back the response + */ + public boolean existsComponentTemplate(ComponentTemplatesExistRequest componentTemplatesRequest, + RequestOptions options) throws IOException { + return restHighLevelClient.performRequest(componentTemplatesRequest, + IndicesRequestConverters::componentTemplatesExist, options, + RestHighLevelClient::convertExistsResponse, emptySet()); + } + + /** + * Uses the Index Templates API to determine if index templates exist + * @param componentTemplatesRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion. The listener will be called with the value {@code true} + * @return cancellable that may be used to cancel the request + */ + public Cancellable existsComponentTemplateAsync(ComponentTemplatesExistRequest componentTemplatesRequest, + RequestOptions options, + ActionListener listener) { + + return restHighLevelClient.performRequestAsync(componentTemplatesRequest, + IndicesRequestConverters::componentTemplatesExist, options, + RestHighLevelClient::convertExistsResponse, listener, emptySet()); + } + /** * Gets index templates using the Index Templates API * See Index Templates API @@ -1142,6 +1207,32 @@ public Cancellable deleteTemplateAsync(DeleteIndexTemplateRequest request, Reque options, AcknowledgedResponse::fromXContent, listener, emptySet()); } + /** + * Delete a component template using the Component Templates API + * + * @param req the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @throws IOException in case there is a problem sending the request or parsing back the response + */ + public AcknowledgedResponse deleteComponentTemplate(DeleteComponentTemplateRequest req, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(req, IndicesRequestConverters::deleteComponentTemplate, + options, AcknowledgedResponse::fromXContent, emptySet()); + } + + /** + * Asynchronously delete a component template using the Component Templates API + * + * @param request the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion + * @return cancellable that may be used to cancel the request + */ + public Cancellable deleteComponentTemplateAsync(DeleteComponentTemplateRequest request, RequestOptions options, + ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::deleteComponentTemplate, + options, AcknowledgedResponse::fromXContent, listener, emptySet()); + } + /** * Synchronously calls the _reload_search_analyzers API * diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java index b8711ee3f5fcb..b6a4f888bde77 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java @@ -35,12 +35,15 @@ import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; import org.elasticsearch.action.admin.indices.shrink.ResizeType; +import org.elasticsearch.action.admin.indices.template.delete.DeleteComponentTemplateAction; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.client.indices.CloseIndexRequest; +import org.elasticsearch.client.indices.ComponentTemplatesExistRequest; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.DeleteAliasRequest; +import org.elasticsearch.client.indices.DeleteComponentTemplateRequest; import org.elasticsearch.client.indices.FreezeIndexRequest; import org.elasticsearch.client.indices.GetComponentTemplatesRequest; import org.elasticsearch.client.indices.GetFieldMappingsRequest; @@ -48,6 +51,7 @@ import org.elasticsearch.client.indices.GetIndexTemplatesRequest; import org.elasticsearch.client.indices.GetMappingsRequest; import org.elasticsearch.client.indices.IndexTemplatesExistRequest; +import org.elasticsearch.client.indices.PutComponentTemplateRequest; import org.elasticsearch.client.indices.PutIndexTemplateRequest; import org.elasticsearch.client.indices.PutMappingRequest; import org.elasticsearch.client.indices.ReloadAnalyzersRequest; @@ -404,6 +408,23 @@ static Request putTemplate(PutIndexTemplateRequest putIndexTemplateRequest) thro return request; } + static Request putComponentTemplate(PutComponentTemplateRequest putComponentTemplateRequest) throws IOException { + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_component_template") + .addPathPart(putComponentTemplateRequest.name()).build(); + Request request = new Request(HttpPut.METHOD_NAME, endpoint); + RequestConverters.Params params = new RequestConverters.Params(); + params.withMasterTimeout(putComponentTemplateRequest.masterNodeTimeout()); + if (putComponentTemplateRequest.create()) { + params.putParam("create", Boolean.TRUE.toString()); + } + if (Strings.hasText(putComponentTemplateRequest.cause())) { + params.putParam("cause", putComponentTemplateRequest.cause()); + } + request.addParameters(params.asMap()); + request.setEntity(RequestConverters.createEntity(putComponentTemplateRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE)); + return request; + } + static Request validateQuery(ValidateQueryRequest validateQueryRequest) throws IOException { String[] indices = validateQueryRequest.indices() == null ? Strings.EMPTY_ARRAY : validateQueryRequest.indices(); String endpoint = RequestConverters.endpoint(indices, "_validate/query"); @@ -443,6 +464,19 @@ static Request getComponentTemplates(GetComponentTemplatesRequest getComponentTe return request; } + static Request componentTemplatesExist(ComponentTemplatesExistRequest componentTemplatesRequest) { + final String endpoint = new RequestConverters.EndpointBuilder() + .addPathPartAsIs("_component_template") + .addCommaSeparatedPathParts(componentTemplatesRequest.names()) + .build(); + final Request request = new Request(HttpHead.METHOD_NAME, endpoint); + final RequestConverters.Params params = new RequestConverters.Params(); + params.withLocal(componentTemplatesRequest.isLocal()); + params.withMasterTimeout(componentTemplatesRequest.getMasterNodeTimeout()); + request.addParameters(params.asMap()); + return request; + } + static Request getTemplates(GetIndexTemplatesRequest getIndexTemplatesRequest) { final String endpoint = new RequestConverters.EndpointBuilder() .addPathPartAsIs("_template") @@ -515,6 +549,16 @@ static Request deleteTemplate(DeleteIndexTemplateRequest deleteIndexTemplateRequ return request; } + static Request deleteComponentTemplate(DeleteComponentTemplateRequest deleteComponentTemplateRequest) { + String name = deleteComponentTemplateRequest.getName(); + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_component_template").addPathPart(name).build(); + Request request = new Request(HttpDelete.METHOD_NAME, endpoint); + RequestConverters.Params params = new RequestConverters.Params(); + params.withMasterTimeout(deleteComponentTemplateRequest.masterNodeTimeout()); + request.addParameters(params.asMap()); + return request; + } + static Request reloadAnalyzers(ReloadAnalyzersRequest reloadAnalyzersRequest) { String endpoint = RequestConverters.endpoint(reloadAnalyzersRequest.getIndices(), "_reload_search_analyzers"); Request request = new Request(HttpPost.METHOD_NAME, endpoint); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ComponentTemplatesExistRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ComponentTemplatesExistRequest.java new file mode 100644 index 0000000000000..8a42e9dbb4f4a --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ComponentTemplatesExistRequest.java @@ -0,0 +1,50 @@ +/* + * 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.client.indices; + +import java.util.Arrays; +import java.util.List; + +/** + * A request to check for the existence of component templates + */ +public class ComponentTemplatesExistRequest extends GetComponentTemplatesRequest { + + /** + * Create a request to check for the existence of component templates. At least one template index name must be provided + * + * @param names the names of templates to check for the existence of + */ + public ComponentTemplatesExistRequest(String... names) { + this(Arrays.asList(names)); + } + + /** + * Create a request to check for the existence of component templates. At least one template index name must be provided + * + * @param names the names of templates to check for the existence of + */ + public ComponentTemplatesExistRequest(List names) { + super(names); + if (names().isEmpty()) { + throw new IllegalArgumentException("must provide at least one component template name"); + } + } +} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/DeleteComponentTemplateRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/DeleteComponentTemplateRequest.java new file mode 100644 index 0000000000000..e96c2e3545921 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/DeleteComponentTemplateRequest.java @@ -0,0 +1,35 @@ +/* + * 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.client.indices; + +import org.elasticsearch.client.TimedRequest; + +public class DeleteComponentTemplateRequest extends TimedRequest { + + private final String name; + + public DeleteComponentTemplateRequest(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutComponentTemplateRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutComponentTemplateRequest.java new file mode 100644 index 0000000000000..08227d5a27d08 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutComponentTemplateRequest.java @@ -0,0 +1,161 @@ +/* + * 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.client.indices; + +import org.apache.lucene.util.BytesRef; +import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.client.TimedRequest; +import org.elasticsearch.cluster.metadata.ComponentTemplate; +import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.DeprecationHandler; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.ToXContentFragment; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; + +import java.io.IOException; + +/** + * A request to create an component template. + */ +public class PutComponentTemplateRequest extends TimedRequest implements ToXContentFragment { + + private String name; + + private String cause = ""; + + private boolean create; + + private ComponentTemplate componentTemplate; + + /** + * Sets the name of the component template. + */ + public PutComponentTemplateRequest name(String name) { + if (name == null) { + throw new IllegalArgumentException("Name cannot be null"); + } + this.name = name; + return this; + } + + /** + * The name of the component template. + */ + public String name() { + return this.name; + } + + /** + * Set to {@code true} to force only creation, not an update of an component template. If it already + * exists, it will fail with an {@link IllegalArgumentException}. + */ + public PutComponentTemplateRequest create(boolean create) { + this.create = create; + return this; + } + + public boolean create() { + return create; + } + + /** + * The component template to create. + */ + public PutComponentTemplateRequest componentTemplate(ComponentTemplate componentTemplate) { + this.componentTemplate = componentTemplate; + return this; + } + + /** + * The cause for this component template creation. + */ + public PutComponentTemplateRequest cause(String cause) { + this.cause = cause; + return this; + } + + public String cause() { + return this.cause; + } + + /** + * The template source definition. + */ + public PutComponentTemplateRequest source(XContentBuilder templateBuilder) { + try { + return source(BytesReference.bytes(templateBuilder), templateBuilder.contentType()); + } catch (Exception e) { + throw new IllegalArgumentException("Failed to build json for template request", e); + } + } + + /** + * The template source definition. + */ + public PutComponentTemplateRequest source(String templateSource, XContentType xContentType) { + try (XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, templateSource)) { + return componentTemplate(ComponentTemplate.parse(parser)); + } catch (IOException e) { + throw new ElasticsearchParseException("Failed to parse content to map", e); + } + } + + /** + * The template source definition. + */ + public PutComponentTemplateRequest source(byte[] source, XContentType xContentType) { + return source(source, 0, source.length, xContentType); + } + + /** + * The template source definition. + */ + public PutComponentTemplateRequest source(byte[] source, int offset, int length, XContentType xContentType) { + try (XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, source, offset, length)) { + return componentTemplate(ComponentTemplate.parse(parser)); + } catch (IOException e) { + throw new ElasticsearchParseException("Failed to parse content to map", e); + } + } + + /** + * The template source definition. + */ + public PutComponentTemplateRequest source(BytesReference source, XContentType xContentType) { + BytesRef ref = source.toBytesRef(); + return source(ref.bytes, ref.offset, ref.length, xContentType); + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + if (componentTemplate != null) { + componentTemplate.toXContent(builder, params); + } + return builder; + } + + @Override + public boolean isFragment() { + return false; + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index cbe0b683b9726..2797529fd0c6d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -58,10 +58,14 @@ import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.client.indices.CloseIndexRequest; import org.elasticsearch.client.indices.CloseIndexResponse; +import org.elasticsearch.client.indices.ComponentTemplatesExistRequest; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.DeleteAliasRequest; +import org.elasticsearch.client.indices.DeleteComponentTemplateRequest; import org.elasticsearch.client.indices.FreezeIndexRequest; +import org.elasticsearch.client.indices.GetComponentTemplatesRequest; +import org.elasticsearch.client.indices.GetComponentTemplatesResponse; import org.elasticsearch.client.indices.GetFieldMappingsRequest; import org.elasticsearch.client.indices.GetFieldMappingsResponse; import org.elasticsearch.client.indices.GetIndexRequest; @@ -72,6 +76,7 @@ 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.PutMappingRequest; import org.elasticsearch.client.indices.ReloadAnalyzersRequest; @@ -84,6 +89,7 @@ import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; +import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; @@ -1265,6 +1271,51 @@ public void testIndexPutSettingNonExistent() throws IOException { + "or check the breaking changes documentation for removed settings]")); } + public void testComponentTemplates() throws Exception { + String templateName = "my-template"; + Settings settings = Settings.builder().put("index.number_of_shards", 1).build(); + CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"host_name\":{\"type\":\"keyword\"}}}"); + AliasMetaData alias = AliasMetaData.builder("alias").writeIndex(true).build(); + Template template = new Template(settings, mappings, Map.of("alias", alias)); + ComponentTemplate componentTemplate = new ComponentTemplate(template, 1L, new HashMap<>()); + PutComponentTemplateRequest putComponentTemplateRequest = + new PutComponentTemplateRequest().name(templateName).create(true).componentTemplate(componentTemplate); + + AcknowledgedResponse response = execute(putComponentTemplateRequest, + highLevelClient().indices()::putComponentTemplate, highLevelClient().indices()::putComponentTemplateAsync); + assertThat(response.isAcknowledged(), equalTo(true)); + + ComponentTemplatesExistRequest componentTemplatesExistRequest = new ComponentTemplatesExistRequest(templateName); + boolean exist = execute(componentTemplatesExistRequest, + highLevelClient().indices()::existsComponentTemplate, highLevelClient().indices()::existsComponentTemplateAsync); + + assertTrue(exist); + + GetComponentTemplatesRequest getComponentTemplatesRequest = new GetComponentTemplatesRequest(templateName); + GetComponentTemplatesResponse getResponse = execute(getComponentTemplatesRequest, + highLevelClient().indices()::getComponentTemplate, highLevelClient().indices()::getComponentTemplateAsync); + + assertThat(getResponse.getComponentTemplates().size(), equalTo(1)); + assertThat(getResponse.getComponentTemplates().containsKey(templateName), equalTo(true)); + assertThat(getResponse.getComponentTemplates().get(templateName), equalTo(componentTemplate)); + + DeleteComponentTemplateRequest deleteComponentTemplateRequest = new DeleteComponentTemplateRequest(templateName); + response = execute(deleteComponentTemplateRequest, highLevelClient().indices()::deleteComponentTemplate, + highLevelClient().indices()::deleteComponentTemplateAsync); + assertThat(response.isAcknowledged(), equalTo(true)); + + ElasticsearchStatusException statusException = expectThrows(ElasticsearchStatusException.class, + () -> execute(getComponentTemplatesRequest, + highLevelClient().indices()::getComponentTemplate, highLevelClient().indices()::getComponentTemplateAsync)); + + assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND)); + + exist = execute(componentTemplatesExistRequest, + highLevelClient().indices()::existsComponentTemplate, highLevelClient().indices()::existsComponentTemplateAsync); + + assertFalse(exist); + } + @SuppressWarnings("unchecked") public void testPutTemplate() throws Exception { PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template", List.of("pattern-1", "name-*")) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/Template.java b/server/src/main/java/org/elasticsearch/cluster/metadata/Template.java index eb819bb8f3993..6138f3804765a 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/Template.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/Template.java @@ -148,6 +148,7 @@ public boolean equals(Object obj) { return false; } Template other = (Template) obj; + return Objects.equals(settings, other.settings) && Objects.equals(mappings, other.mappings) && Objects.equals(aliases, other.aliases); From 7f5173feb20349feb97ca0d51f4531233dd862ef Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Thu, 2 Apr 2020 13:15:49 +0200 Subject: [PATCH 04/17] merge fix --- .../test/java/org/elasticsearch/client/IndicesClientIT.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index 2797529fd0c6d..90171b011a4e7 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -85,8 +85,10 @@ import org.elasticsearch.client.indices.rollover.RolloverRequest; import org.elasticsearch.client.indices.rollover.RolloverResponse; import org.elasticsearch.cluster.metadata.AliasMetadata; +import org.elasticsearch.cluster.metadata.ComponentTemplate; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.MappingMetadata; +import org.elasticsearch.cluster.metadata.Template; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.compress.CompressedXContent; @@ -1275,7 +1277,7 @@ public void testComponentTemplates() throws Exception { String templateName = "my-template"; Settings settings = Settings.builder().put("index.number_of_shards", 1).build(); CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"host_name\":{\"type\":\"keyword\"}}}"); - AliasMetaData alias = AliasMetaData.builder("alias").writeIndex(true).build(); + AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build(); Template template = new Template(settings, mappings, Map.of("alias", alias)); ComponentTemplate componentTemplate = new ComponentTemplate(template, 1L, new HashMap<>()); PutComponentTemplateRequest putComponentTemplateRequest = From d433c1da3364dd63dd21aa9f79125ff7ab4fcf11 Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Thu, 2 Apr 2020 15:05:46 +0200 Subject: [PATCH 05/17] removed unused import --- .../src/main/java/org/elasticsearch/client/IndicesClient.java | 1 - 1 file changed, 1 deletion(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java index 7d4f1426572d2..70092e6ed16fe 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java @@ -36,7 +36,6 @@ import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest; import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; -import org.elasticsearch.action.admin.indices.template.delete.DeleteComponentTemplateAction; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse; From 0b48a5268a0687937d0b6e96f2f3b1ef6d5ef783 Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Thu, 2 Apr 2020 15:13:01 +0200 Subject: [PATCH 06/17] checkstyle fixes --- .../org/elasticsearch/client/IndicesRequestConverters.java | 1 - .../client/indices/GetComponentTemplatesRequest.java | 6 ++++-- .../client/indices/GetComponentTemplatesResponse.java | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java index b6a4f888bde77..e57a7a94355df 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java @@ -35,7 +35,6 @@ import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; import org.elasticsearch.action.admin.indices.shrink.ResizeType; -import org.elasticsearch.action.admin.indices.template.delete.DeleteComponentTemplateAction; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; import org.elasticsearch.client.indices.AnalyzeRequest; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java index 1d7e6b3cbb17d..f6f1ee1707480 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java @@ -42,7 +42,8 @@ public class GetComponentTemplatesRequest implements Validatable { private boolean local = false; /** - * Create a request to read the content of one or more component templates. If no template names are provided, all templates will be read + * Create a request to read the content of one or more component templates. If no template names are provided, all templates will + * be read * * @param names the names of templates to read */ @@ -51,7 +52,8 @@ public GetComponentTemplatesRequest(String... names) { } /** - * Create a request to read the content of one or more component templates. If no template names are provided, all templates will be read + * Create a request to read the content of one or more component templates. If no template names are provided, all templates will + * be read * * @param names the names of templates to read */ diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesResponse.java index 82b6c64d6a795..abe37e95fbca9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesResponse.java @@ -58,7 +58,7 @@ private static class NamedComponentTemplate { String name; ComponentTemplate componentTemplate; - public NamedComponentTemplate(String name, ComponentTemplate componentTemplate) { + private NamedComponentTemplate(String name, ComponentTemplate componentTemplate) { this.name = name; this.componentTemplate = componentTemplate; } From 10ae2320638201b56c17626d974c58a70f7ce1ad Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Thu, 2 Apr 2020 16:05:22 +0200 Subject: [PATCH 07/17] metaData -> metadata --- .../indices/GetComponentTemplatesResponseTests.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComponentTemplatesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComponentTemplatesResponseTests.java index 1c33895b029e8..1f4acce23a61e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComponentTemplatesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComponentTemplatesResponseTests.java @@ -19,7 +19,7 @@ package org.elasticsearch.client.indices; -import org.elasticsearch.cluster.metadata.AliasMetaData; +import org.elasticsearch.cluster.metadata.AliasMetadata; import org.elasticsearch.cluster.metadata.ComponentTemplate; import org.elasticsearch.cluster.metadata.Template; import org.elasticsearch.common.compress.CompressedXContent; @@ -75,7 +75,7 @@ private static void toXContent(GetComponentTemplatesResponse response, XContentB private static ComponentTemplate randomTemplate() { Settings settings = null; CompressedXContent mappings = null; - Map aliases = null; + Map aliases = null; if (randomBoolean()) { settings = randomSettings(); } @@ -94,9 +94,9 @@ private static ComponentTemplate randomTemplate() { return new ComponentTemplate(template, randomBoolean() ? null : randomNonNegativeLong(), meta); } - private static Map randomAliases() { + private static Map randomAliases() { String aliasName = randomAlphaOfLength(5); - AliasMetaData aliasMeta = AliasMetaData.builder(aliasName) + AliasMetadata aliasMeta = AliasMetadata.builder(aliasName) .filter(Collections.singletonMap(randomAlphaOfLength(2), randomAlphaOfLength(2))) .routing(randomBoolean() ? null : randomAlphaOfLength(3)) .isHidden(randomBoolean() ? null : randomBoolean()) From e12c371ce9242e2a6f6eb8fd27438e167b10093d Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Thu, 2 Apr 2020 18:00:58 +0200 Subject: [PATCH 08/17] move to ClusterClient --- .../elasticsearch/client/ClusterClient.java | 121 ++++++++++++++++++ .../client/ClusterRequestConverters.java | 59 +++++++++ .../elasticsearch/client/IndicesClient.java | 120 ----------------- .../client/IndicesRequestConverters.java | 53 -------- .../elasticsearch/client/ClusterClientIT.java | 55 ++++++++ .../elasticsearch/client/IndicesClientIT.java | 46 ------- .../client/RestHighLevelClientTests.java | 3 - .../cluster.exists_component_template.json | 35 +++++ 8 files changed, 270 insertions(+), 222 deletions(-) create mode 100644 rest-api-spec/src/main/resources/rest-api-spec/api/cluster.exists_component_template.json diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.java index 14fbf3e1f6d0f..cc8a49e925d28 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.java @@ -26,8 +26,14 @@ import org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsResponse; import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse; +import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.cluster.RemoteInfoRequest; import org.elasticsearch.client.cluster.RemoteInfoResponse; +import org.elasticsearch.client.indices.ComponentTemplatesExistRequest; +import org.elasticsearch.client.indices.DeleteComponentTemplateRequest; +import org.elasticsearch.client.indices.GetComponentTemplatesRequest; +import org.elasticsearch.client.indices.GetComponentTemplatesResponse; +import org.elasticsearch.client.indices.PutComponentTemplateRequest; import org.elasticsearch.rest.RestStatus; import java.io.IOException; @@ -169,4 +175,119 @@ public Cancellable remoteInfoAsync(RemoteInfoRequest request, RequestOptions opt return restHighLevelClient.performRequestAsyncAndParseEntity(request, ClusterRequestConverters::remoteInfo, options, RemoteInfoResponse::fromXContent, listener, singleton(RestStatus.REQUEST_TIMEOUT.getStatus())); } + + /** + * Delete a component template using the Component Templates API + * + * @param req the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @throws IOException in case there is a problem sending the request or parsing back the response + */ + public AcknowledgedResponse deleteComponentTemplate(DeleteComponentTemplateRequest req, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(req, ClusterRequestConverters::deleteComponentTemplate, + options, AcknowledgedResponse::fromXContent, emptySet()); + } + + /** + * Asynchronously delete a component template using the Component Templates API + * + * @param request the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion + * @return cancellable that may be used to cancel the request + */ + public Cancellable deleteComponentTemplateAsync(DeleteComponentTemplateRequest request, RequestOptions options, + ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity(request, ClusterRequestConverters::deleteComponentTemplate, + options, AcknowledgedResponse::fromXContent, listener, emptySet()); + } + + /** + * Puts a component template using the Component Templates API. + * + * @param putComponentTemplateRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @return the response + * @throws IOException in case there is a problem sending the request or parsing back the response + */ + public AcknowledgedResponse putComponentTemplate( + PutComponentTemplateRequest putComponentTemplateRequest, + RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(putComponentTemplateRequest, ClusterRequestConverters::putComponentTemplate, + options, AcknowledgedResponse::fromXContent, emptySet()); + } + + /** + * Asynchronously puts a component template using the Component Templates API. + * + * @param putComponentTemplateRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion + * @return cancellable that may be used to cancel the request + */ + public Cancellable putComponentTemplateAsync(PutComponentTemplateRequest putComponentTemplateRequest, + RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity(putComponentTemplateRequest, + ClusterRequestConverters::putComponentTemplate, options, + AcknowledgedResponse::fromXContent, listener, emptySet()); + } + + /** + * Gets component templates using the Components Templates API + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param getComponentTemplatesRequest the request + * @return the response + * @throws IOException in case there is a problem sending the request or parsing back the response + */ + public GetComponentTemplatesResponse getComponentTemplate(GetComponentTemplatesRequest getComponentTemplatesRequest, + RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(getComponentTemplatesRequest, + ClusterRequestConverters::getComponentTemplates, + options, GetComponentTemplatesResponse::fromXContent, emptySet()); + } + + /** + * Asynchronously gets component templates using the Components Templates API + * @param getComponentTemplatesRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion + * @return cancellable that may be used to cancel the request + */ + public Cancellable getComponentTemplateAsync(GetComponentTemplatesRequest getComponentTemplatesRequest, RequestOptions options, + ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity(getComponentTemplatesRequest, + ClusterRequestConverters::getComponentTemplates, + options, GetComponentTemplatesResponse::fromXContent, listener, emptySet()); + } + + /** + * Uses the Component Templates API to determine if component templates exist + * + * @param componentTemplatesRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @return true if any index templates in the request exist, false otherwise + * @throws IOException in case there is a problem sending the request or parsing back the response + */ + public boolean existsComponentTemplate(ComponentTemplatesExistRequest componentTemplatesRequest, + RequestOptions options) throws IOException { + return restHighLevelClient.performRequest(componentTemplatesRequest, + ClusterRequestConverters::componentTemplatesExist, options, + RestHighLevelClient::convertExistsResponse, emptySet()); + } + + /** + * Uses the Index Templates API to determine if index templates exist + * @param componentTemplatesRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion. The listener will be called with the value {@code true} + * @return cancellable that may be used to cancel the request + */ + public Cancellable existsComponentTemplateAsync(ComponentTemplatesExistRequest componentTemplatesRequest, + RequestOptions options, + ActionListener listener) { + + return restHighLevelClient.performRequestAsync(componentTemplatesRequest, + ClusterRequestConverters::componentTemplatesExist, options, + RestHighLevelClient::convertExistsResponse, listener, emptySet()); + } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterRequestConverters.java index 74b2c3b7c6aae..1c1bd99c2a803 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterRequestConverters.java @@ -19,13 +19,19 @@ package org.elasticsearch.client; +import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpPut; import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; import org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsRequest; import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.client.cluster.RemoteInfoRequest; +import org.elasticsearch.client.indices.ComponentTemplatesExistRequest; +import org.elasticsearch.client.indices.DeleteComponentTemplateRequest; +import org.elasticsearch.client.indices.GetComponentTemplatesRequest; +import org.elasticsearch.client.indices.PutComponentTemplateRequest; import org.elasticsearch.common.Strings; import java.io.IOException; @@ -81,4 +87,57 @@ static Request clusterHealth(ClusterHealthRequest healthRequest) { static Request remoteInfo(RemoteInfoRequest remoteInfoRequest) { return new Request(HttpGet.METHOD_NAME, "/_remote/info"); } + + static Request putComponentTemplate(PutComponentTemplateRequest putComponentTemplateRequest) throws IOException { + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_component_template") + .addPathPart(putComponentTemplateRequest.name()).build(); + Request request = new Request(HttpPut.METHOD_NAME, endpoint); + RequestConverters.Params params = new RequestConverters.Params(); + params.withMasterTimeout(putComponentTemplateRequest.masterNodeTimeout()); + if (putComponentTemplateRequest.create()) { + params.putParam("create", Boolean.TRUE.toString()); + } + if (Strings.hasText(putComponentTemplateRequest.cause())) { + params.putParam("cause", putComponentTemplateRequest.cause()); + } + request.addParameters(params.asMap()); + request.setEntity(RequestConverters.createEntity(putComponentTemplateRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE)); + return request; + } + + static Request getComponentTemplates(GetComponentTemplatesRequest getComponentTemplatesRequest){ + final String endpoint = new RequestConverters.EndpointBuilder() + .addPathPartAsIs("_component_template") + .addCommaSeparatedPathParts(getComponentTemplatesRequest.names()) + .build(); + final Request request = new Request(HttpGet.METHOD_NAME, endpoint); + final RequestConverters.Params params = new RequestConverters.Params(); + params.withLocal(getComponentTemplatesRequest.isLocal()); + params.withMasterTimeout(getComponentTemplatesRequest.getMasterNodeTimeout()); + request.addParameters(params.asMap()); + return request; + } + + static Request componentTemplatesExist(ComponentTemplatesExistRequest componentTemplatesRequest) { + final String endpoint = new RequestConverters.EndpointBuilder() + .addPathPartAsIs("_component_template") + .addCommaSeparatedPathParts(componentTemplatesRequest.names()) + .build(); + final Request request = new Request(HttpHead.METHOD_NAME, endpoint); + final RequestConverters.Params params = new RequestConverters.Params(); + params.withLocal(componentTemplatesRequest.isLocal()); + params.withMasterTimeout(componentTemplatesRequest.getMasterNodeTimeout()); + request.addParameters(params.asMap()); + return request; + } + + static Request deleteComponentTemplate(DeleteComponentTemplateRequest deleteComponentTemplateRequest) { + String name = deleteComponentTemplateRequest.getName(); + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_component_template").addPathPart(name).build(); + Request request = new Request(HttpDelete.METHOD_NAME, endpoint); + RequestConverters.Params params = new RequestConverters.Params(); + params.withMasterTimeout(deleteComponentTemplateRequest.masterNodeTimeout()); + request.addParameters(params.asMap()); + return request; + } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java index 70092e6ed16fe..96494238e75e0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java @@ -45,14 +45,10 @@ import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.client.indices.CloseIndexRequest; import org.elasticsearch.client.indices.CloseIndexResponse; -import org.elasticsearch.client.indices.ComponentTemplatesExistRequest; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.DeleteAliasRequest; -import org.elasticsearch.client.indices.DeleteComponentTemplateRequest; import org.elasticsearch.client.indices.FreezeIndexRequest; -import org.elasticsearch.client.indices.GetComponentTemplatesRequest; -import org.elasticsearch.client.indices.GetComponentTemplatesResponse; import org.elasticsearch.client.indices.GetFieldMappingsRequest; import org.elasticsearch.client.indices.GetFieldMappingsResponse; import org.elasticsearch.client.indices.GetIndexRequest; @@ -62,7 +58,6 @@ import org.elasticsearch.client.indices.GetMappingsRequest; import org.elasticsearch.client.indices.GetMappingsResponse; import org.elasticsearch.client.indices.IndexTemplatesExistRequest; -import org.elasticsearch.client.indices.PutComponentTemplateRequest; import org.elasticsearch.client.indices.PutIndexTemplateRequest; import org.elasticsearch.client.indices.PutMappingRequest; import org.elasticsearch.client.indices.ReloadAnalyzersRequest; @@ -914,36 +909,6 @@ public Cancellable putTemplateAsync(PutIndexTemplateRequest putIndexTemplateRequ AcknowledgedResponse::fromXContent, listener, emptySet()); } - /** - * Puts a component template using the Component Templates API. - * - * @param putComponentTemplateRequest the request - * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized - * @return the response - * @throws IOException in case there is a problem sending the request or parsing back the response - */ - public AcknowledgedResponse putComponentTemplate( - PutComponentTemplateRequest putComponentTemplateRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(putComponentTemplateRequest, IndicesRequestConverters::putComponentTemplate, - options, AcknowledgedResponse::fromXContent, emptySet()); - } - - /** - * Asynchronously puts a component template using the Component Templates API. - * - * @param putComponentTemplateRequest the request - * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized - * @param listener the listener to be notified upon request completion - * @return cancellable that may be used to cancel the request - */ - public Cancellable putComponentTemplateAsync(PutComponentTemplateRequest putComponentTemplateRequest, - RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(putComponentTemplateRequest, - IndicesRequestConverters::putComponentTemplate, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); - } - /** * Validate a potentially expensive query without executing it. *

@@ -978,65 +943,6 @@ public Cancellable validateQueryAsync(ValidateQueryRequest validateQueryRequest, ValidateQueryResponse::fromXContent, listener, emptySet()); } - /** - * Gets component templates using the Components Templates API - * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized - * @param getComponentTemplatesRequest the request - * @return the response - * @throws IOException in case there is a problem sending the request or parsing back the response - */ - public GetComponentTemplatesResponse getComponentTemplate(GetComponentTemplatesRequest getComponentTemplatesRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(getComponentTemplatesRequest, - IndicesRequestConverters::getComponentTemplates, - options, GetComponentTemplatesResponse::fromXContent, emptySet()); - } - - /** - * Asynchronously gets component templates using the Components Templates API - * @param getComponentTemplatesRequest the request - * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized - * @param listener the listener to be notified upon request completion - * @return cancellable that may be used to cancel the request - */ - public Cancellable getComponentTemplateAsync(GetComponentTemplatesRequest getComponentTemplatesRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(getComponentTemplatesRequest, - IndicesRequestConverters::getComponentTemplates, - options, GetComponentTemplatesResponse::fromXContent, listener, emptySet()); - } - - /** - * Uses the Component Templates API to determine if component templates exist - * - * @param componentTemplatesRequest the request - * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized - * @return true if any index templates in the request exist, false otherwise - * @throws IOException in case there is a problem sending the request or parsing back the response - */ - public boolean existsComponentTemplate(ComponentTemplatesExistRequest componentTemplatesRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequest(componentTemplatesRequest, - IndicesRequestConverters::componentTemplatesExist, options, - RestHighLevelClient::convertExistsResponse, emptySet()); - } - - /** - * Uses the Index Templates API to determine if index templates exist - * @param componentTemplatesRequest the request - * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized - * @param listener the listener to be notified upon request completion. The listener will be called with the value {@code true} - * @return cancellable that may be used to cancel the request - */ - public Cancellable existsComponentTemplateAsync(ComponentTemplatesExistRequest componentTemplatesRequest, - RequestOptions options, - ActionListener listener) { - - return restHighLevelClient.performRequestAsync(componentTemplatesRequest, - IndicesRequestConverters::componentTemplatesExist, options, - RestHighLevelClient::convertExistsResponse, listener, emptySet()); - } - /** * Gets index templates using the Index Templates API * See Index Templates API @@ -1206,32 +1112,6 @@ public Cancellable deleteTemplateAsync(DeleteIndexTemplateRequest request, Reque options, AcknowledgedResponse::fromXContent, listener, emptySet()); } - /** - * Delete a component template using the Component Templates API - * - * @param req the request - * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized - * @throws IOException in case there is a problem sending the request or parsing back the response - */ - public AcknowledgedResponse deleteComponentTemplate(DeleteComponentTemplateRequest req, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(req, IndicesRequestConverters::deleteComponentTemplate, - options, AcknowledgedResponse::fromXContent, emptySet()); - } - - /** - * Asynchronously delete a component template using the Component Templates API - * - * @param request the request - * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized - * @param listener the listener to be notified upon request completion - * @return cancellable that may be used to cancel the request - */ - public Cancellable deleteComponentTemplateAsync(DeleteComponentTemplateRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::deleteComponentTemplate, - options, AcknowledgedResponse::fromXContent, listener, emptySet()); - } - /** * Synchronously calls the _reload_search_analyzers API * diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java index e57a7a94355df..eae5c355f99ab 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java @@ -407,23 +407,6 @@ static Request putTemplate(PutIndexTemplateRequest putIndexTemplateRequest) thro return request; } - static Request putComponentTemplate(PutComponentTemplateRequest putComponentTemplateRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_component_template") - .addPathPart(putComponentTemplateRequest.name()).build(); - Request request = new Request(HttpPut.METHOD_NAME, endpoint); - RequestConverters.Params params = new RequestConverters.Params(); - params.withMasterTimeout(putComponentTemplateRequest.masterNodeTimeout()); - if (putComponentTemplateRequest.create()) { - params.putParam("create", Boolean.TRUE.toString()); - } - if (Strings.hasText(putComponentTemplateRequest.cause())) { - params.putParam("cause", putComponentTemplateRequest.cause()); - } - request.addParameters(params.asMap()); - request.setEntity(RequestConverters.createEntity(putComponentTemplateRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE)); - return request; - } - static Request validateQuery(ValidateQueryRequest validateQueryRequest) throws IOException { String[] indices = validateQueryRequest.indices() == null ? Strings.EMPTY_ARRAY : validateQueryRequest.indices(); String endpoint = RequestConverters.endpoint(indices, "_validate/query"); @@ -450,32 +433,6 @@ static Request getAlias(GetAliasesRequest getAliasesRequest) { return request; } - static Request getComponentTemplates(GetComponentTemplatesRequest getComponentTemplatesRequest){ - final String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_component_template") - .addCommaSeparatedPathParts(getComponentTemplatesRequest.names()) - .build(); - final Request request = new Request(HttpGet.METHOD_NAME, endpoint); - final RequestConverters.Params params = new RequestConverters.Params(); - params.withLocal(getComponentTemplatesRequest.isLocal()); - params.withMasterTimeout(getComponentTemplatesRequest.getMasterNodeTimeout()); - request.addParameters(params.asMap()); - return request; - } - - static Request componentTemplatesExist(ComponentTemplatesExistRequest componentTemplatesRequest) { - final String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_component_template") - .addCommaSeparatedPathParts(componentTemplatesRequest.names()) - .build(); - final Request request = new Request(HttpHead.METHOD_NAME, endpoint); - final RequestConverters.Params params = new RequestConverters.Params(); - params.withLocal(componentTemplatesRequest.isLocal()); - params.withMasterTimeout(componentTemplatesRequest.getMasterNodeTimeout()); - request.addParameters(params.asMap()); - return request; - } - static Request getTemplates(GetIndexTemplatesRequest getIndexTemplatesRequest) { final String endpoint = new RequestConverters.EndpointBuilder() .addPathPartAsIs("_template") @@ -548,16 +505,6 @@ static Request deleteTemplate(DeleteIndexTemplateRequest deleteIndexTemplateRequ return request; } - static Request deleteComponentTemplate(DeleteComponentTemplateRequest deleteComponentTemplateRequest) { - String name = deleteComponentTemplateRequest.getName(); - String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_component_template").addPathPart(name).build(); - Request request = new Request(HttpDelete.METHOD_NAME, endpoint); - RequestConverters.Params params = new RequestConverters.Params(); - params.withMasterTimeout(deleteComponentTemplateRequest.masterNodeTimeout()); - request.addParameters(params.asMap()); - return request; - } - static Request reloadAnalyzers(ReloadAnalyzersRequest reloadAnalyzersRequest) { String endpoint = RequestConverters.endpoint(reloadAnalyzersRequest.getIndices(), "_reload_search_analyzers"); Request request = new Request(HttpPost.METHOD_NAME, endpoint); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ClusterClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ClusterClientIT.java index 09c8549d7258c..f6edb86eca1eb 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ClusterClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ClusterClientIT.java @@ -21,20 +21,31 @@ import org.apache.http.util.EntityUtils; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; import org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsRequest; import org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsResponse; import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse; +import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.cluster.RemoteConnectionInfo; import org.elasticsearch.client.cluster.RemoteInfoRequest; import org.elasticsearch.client.cluster.RemoteInfoResponse; import org.elasticsearch.client.cluster.SniffModeInfo; +import org.elasticsearch.client.indices.ComponentTemplatesExistRequest; +import org.elasticsearch.client.indices.DeleteComponentTemplateRequest; +import org.elasticsearch.client.indices.GetComponentTemplatesRequest; +import org.elasticsearch.client.indices.GetComponentTemplatesResponse; +import org.elasticsearch.client.indices.PutComponentTemplateRequest; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.cluster.health.ClusterIndexHealth; import org.elasticsearch.cluster.health.ClusterShardHealth; +import org.elasticsearch.cluster.metadata.AliasMetadata; +import org.elasticsearch.cluster.metadata.ComponentTemplate; +import org.elasticsearch.cluster.metadata.Template; import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider; +import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.TimeValue; @@ -342,4 +353,48 @@ public void testRemoteInfo() throws Exception { assertThat(sniffModeInfo.getSeedNodes(), equalTo(seeds)); } + public void testComponentTemplates() throws Exception { + String templateName = "my-template"; + Settings settings = Settings.builder().put("index.number_of_shards", 1).build(); + CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"host_name\":{\"type\":\"keyword\"}}}"); + AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build(); + Template template = new Template(settings, mappings, Map.of("alias", alias)); + ComponentTemplate componentTemplate = new ComponentTemplate(template, 1L, new HashMap<>()); + PutComponentTemplateRequest putComponentTemplateRequest = + new PutComponentTemplateRequest().name(templateName).create(true).componentTemplate(componentTemplate); + + AcknowledgedResponse response = execute(putComponentTemplateRequest, + highLevelClient().cluster()::putComponentTemplate, highLevelClient().cluster()::putComponentTemplateAsync); + assertThat(response.isAcknowledged(), equalTo(true)); + + ComponentTemplatesExistRequest componentTemplatesExistRequest = new ComponentTemplatesExistRequest(templateName); + boolean exist = execute(componentTemplatesExistRequest, + highLevelClient().cluster()::existsComponentTemplate, highLevelClient().cluster()::existsComponentTemplateAsync); + + assertTrue(exist); + + GetComponentTemplatesRequest getComponentTemplatesRequest = new GetComponentTemplatesRequest(templateName); + GetComponentTemplatesResponse getResponse = execute(getComponentTemplatesRequest, + highLevelClient().cluster()::getComponentTemplate, highLevelClient().cluster()::getComponentTemplateAsync); + + assertThat(getResponse.getComponentTemplates().size(), equalTo(1)); + assertThat(getResponse.getComponentTemplates().containsKey(templateName), equalTo(true)); + assertThat(getResponse.getComponentTemplates().get(templateName), equalTo(componentTemplate)); + + DeleteComponentTemplateRequest deleteComponentTemplateRequest = new DeleteComponentTemplateRequest(templateName); + response = execute(deleteComponentTemplateRequest, highLevelClient().cluster()::deleteComponentTemplate, + highLevelClient().cluster()::deleteComponentTemplateAsync); + assertThat(response.isAcknowledged(), equalTo(true)); + + ElasticsearchStatusException statusException = expectThrows(ElasticsearchStatusException.class, + () -> execute(getComponentTemplatesRequest, + highLevelClient().cluster()::getComponentTemplate, highLevelClient().cluster()::getComponentTemplateAsync)); + + assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND)); + + exist = execute(componentTemplatesExistRequest, + highLevelClient().cluster()::existsComponentTemplate, highLevelClient().cluster()::existsComponentTemplateAsync); + + assertFalse(exist); + } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index 90171b011a4e7..2d1f9b1598dcf 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -85,7 +85,6 @@ import org.elasticsearch.client.indices.rollover.RolloverRequest; import org.elasticsearch.client.indices.rollover.RolloverResponse; import org.elasticsearch.cluster.metadata.AliasMetadata; -import org.elasticsearch.cluster.metadata.ComponentTemplate; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.cluster.metadata.Template; @@ -1273,51 +1272,6 @@ public void testIndexPutSettingNonExistent() throws IOException { + "or check the breaking changes documentation for removed settings]")); } - public void testComponentTemplates() throws Exception { - String templateName = "my-template"; - Settings settings = Settings.builder().put("index.number_of_shards", 1).build(); - CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"host_name\":{\"type\":\"keyword\"}}}"); - AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build(); - Template template = new Template(settings, mappings, Map.of("alias", alias)); - ComponentTemplate componentTemplate = new ComponentTemplate(template, 1L, new HashMap<>()); - PutComponentTemplateRequest putComponentTemplateRequest = - new PutComponentTemplateRequest().name(templateName).create(true).componentTemplate(componentTemplate); - - AcknowledgedResponse response = execute(putComponentTemplateRequest, - highLevelClient().indices()::putComponentTemplate, highLevelClient().indices()::putComponentTemplateAsync); - assertThat(response.isAcknowledged(), equalTo(true)); - - ComponentTemplatesExistRequest componentTemplatesExistRequest = new ComponentTemplatesExistRequest(templateName); - boolean exist = execute(componentTemplatesExistRequest, - highLevelClient().indices()::existsComponentTemplate, highLevelClient().indices()::existsComponentTemplateAsync); - - assertTrue(exist); - - GetComponentTemplatesRequest getComponentTemplatesRequest = new GetComponentTemplatesRequest(templateName); - GetComponentTemplatesResponse getResponse = execute(getComponentTemplatesRequest, - highLevelClient().indices()::getComponentTemplate, highLevelClient().indices()::getComponentTemplateAsync); - - assertThat(getResponse.getComponentTemplates().size(), equalTo(1)); - assertThat(getResponse.getComponentTemplates().containsKey(templateName), equalTo(true)); - assertThat(getResponse.getComponentTemplates().get(templateName), equalTo(componentTemplate)); - - DeleteComponentTemplateRequest deleteComponentTemplateRequest = new DeleteComponentTemplateRequest(templateName); - response = execute(deleteComponentTemplateRequest, highLevelClient().indices()::deleteComponentTemplate, - highLevelClient().indices()::deleteComponentTemplateAsync); - assertThat(response.isAcknowledged(), equalTo(true)); - - ElasticsearchStatusException statusException = expectThrows(ElasticsearchStatusException.class, - () -> execute(getComponentTemplatesRequest, - highLevelClient().indices()::getComponentTemplate, highLevelClient().indices()::getComponentTemplateAsync)); - - assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND)); - - exist = execute(componentTemplatesExistRequest, - highLevelClient().indices()::existsComponentTemplate, highLevelClient().indices()::existsComponentTemplateAsync); - - assertFalse(exist); - } - @SuppressWarnings("unchecked") public void testPutTemplate() throws Exception { PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template", List.of("pattern-1", "name-*")) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java index 6a173c164d646..8a740a78a7609 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java @@ -796,9 +796,6 @@ public void testApiNamingConventions() throws Exception { "indices.put_alias", "render_search_template", "scripts_painless_execute", - "cluster.put_component_template", - "cluster.get_component_template", - "cluster.delete_component_template", "indices.create_data_stream", "indices.get_data_streams", "indices.delete_data_stream", diff --git a/rest-api-spec/src/main/resources/rest-api-spec/api/cluster.exists_component_template.json b/rest-api-spec/src/main/resources/rest-api-spec/api/cluster.exists_component_template.json new file mode 100644 index 0000000000000..cf000e50951b1 --- /dev/null +++ b/rest-api-spec/src/main/resources/rest-api-spec/api/cluster.exists_component_template.json @@ -0,0 +1,35 @@ +{ + "cluster.exists_component_template":{ + "documentation":{ + "url":"https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-component-templates.html", + "description":"Returns information about whether a particular component template exist" + }, + "stability":"stable", + "url":{ + "paths":[ + { + "path":"/_component_template/{name}", + "methods":[ + "GET" + ], + "parts":{ + "name":{ + "type":"list", + "description":"The comma separated names of the component templates" + } + } + } + ] + }, + "params":{ + "master_timeout":{ + "type":"time", + "description":"Explicit operation timeout for connection to master node" + }, + "local":{ + "type":"boolean", + "description":"Return local information, do not retrieve the state from master node (default: false)" + } + } + } +} From 23578ebeeba6a05149e9133c53c5a5e613beed7a Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Thu, 2 Apr 2020 18:10:45 +0200 Subject: [PATCH 09/17] checkstyle fixes --- .../org/elasticsearch/client/IndicesRequestConverters.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java index eae5c355f99ab..b0afa84a05e38 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java @@ -39,18 +39,14 @@ import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.client.indices.CloseIndexRequest; -import org.elasticsearch.client.indices.ComponentTemplatesExistRequest; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.DeleteAliasRequest; -import org.elasticsearch.client.indices.DeleteComponentTemplateRequest; import org.elasticsearch.client.indices.FreezeIndexRequest; -import org.elasticsearch.client.indices.GetComponentTemplatesRequest; import org.elasticsearch.client.indices.GetFieldMappingsRequest; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.client.indices.GetIndexTemplatesRequest; import org.elasticsearch.client.indices.GetMappingsRequest; import org.elasticsearch.client.indices.IndexTemplatesExistRequest; -import org.elasticsearch.client.indices.PutComponentTemplateRequest; import org.elasticsearch.client.indices.PutIndexTemplateRequest; import org.elasticsearch.client.indices.PutMappingRequest; import org.elasticsearch.client.indices.ReloadAnalyzersRequest; From 03c86328b6d162c0cc846b3c8c7474b7109f76e8 Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Thu, 2 Apr 2020 18:11:41 +0200 Subject: [PATCH 10/17] checkstyle fixes --- .../java/org/elasticsearch/client/IndicesClientIT.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index 2d1f9b1598dcf..cbe0b683b9726 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -58,14 +58,10 @@ import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.client.indices.CloseIndexRequest; import org.elasticsearch.client.indices.CloseIndexResponse; -import org.elasticsearch.client.indices.ComponentTemplatesExistRequest; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.DeleteAliasRequest; -import org.elasticsearch.client.indices.DeleteComponentTemplateRequest; import org.elasticsearch.client.indices.FreezeIndexRequest; -import org.elasticsearch.client.indices.GetComponentTemplatesRequest; -import org.elasticsearch.client.indices.GetComponentTemplatesResponse; import org.elasticsearch.client.indices.GetFieldMappingsRequest; import org.elasticsearch.client.indices.GetFieldMappingsResponse; import org.elasticsearch.client.indices.GetIndexRequest; @@ -76,7 +72,6 @@ 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.PutMappingRequest; import org.elasticsearch.client.indices.ReloadAnalyzersRequest; @@ -87,10 +82,8 @@ import org.elasticsearch.cluster.metadata.AliasMetadata; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.MappingMetadata; -import org.elasticsearch.cluster.metadata.Template; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; From 4f32ee49784d007870fb048e16317911c410f175 Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Thu, 2 Apr 2020 18:12:25 +0200 Subject: [PATCH 11/17] checkstyle fixes --- .../main/java/org/elasticsearch/cluster/metadata/Template.java | 1 - 1 file changed, 1 deletion(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/Template.java b/server/src/main/java/org/elasticsearch/cluster/metadata/Template.java index 6138f3804765a..eb819bb8f3993 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/Template.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/Template.java @@ -148,7 +148,6 @@ public boolean equals(Object obj) { return false; } Template other = (Template) obj; - return Objects.equals(settings, other.settings) && Objects.equals(mappings, other.mappings) && Objects.equals(aliases, other.aliases); From ce7cbc5c06ad162c9f63ffdd6a4d06b65674f4df Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Thu, 2 Apr 2020 18:13:18 +0200 Subject: [PATCH 12/17] method in spec fixed --- .../rest-api-spec/api/cluster.exists_component_template.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest-api-spec/src/main/resources/rest-api-spec/api/cluster.exists_component_template.json b/rest-api-spec/src/main/resources/rest-api-spec/api/cluster.exists_component_template.json index cf000e50951b1..d842dbef9f1a7 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/api/cluster.exists_component_template.json +++ b/rest-api-spec/src/main/resources/rest-api-spec/api/cluster.exists_component_template.json @@ -10,7 +10,7 @@ { "path":"/_component_template/{name}", "methods":[ - "GET" + "HEAD" ], "parts":{ "name":{ From 6cffe8a2c23ffd0e535e622863503a41acc7c28e Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Fri, 3 Apr 2020 19:28:33 +0200 Subject: [PATCH 13/17] PR comments --- .../elasticsearch/client/ClusterClient.java | 15 ++--- .../client/ClusterRequestConverters.java | 4 +- .../ComponentTemplatesExistRequest.java | 24 +++---- .../indices/GetComponentTemplatesRequest.java | 30 +++------ .../GetComponentTemplatesResponse.java | 13 ++-- .../indices/PutComponentTemplateRequest.java | 62 +------------------ .../cluster.exists_component_template.json | 4 +- .../cluster/metadata/Template.java | 4 ++ 8 files changed, 37 insertions(+), 119 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.java index cc8a49e925d28..da4805581c02a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.java @@ -228,8 +228,7 @@ public AcknowledgedResponse putComponentTemplate( public Cancellable putComponentTemplateAsync(PutComponentTemplateRequest putComponentTemplateRequest, RequestOptions options, ActionListener listener) { return restHighLevelClient.performRequestAsyncAndParseEntity(putComponentTemplateRequest, - ClusterRequestConverters::putComponentTemplate, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + ClusterRequestConverters::putComponentTemplate, options, AcknowledgedResponse::fromXContent, listener, emptySet()); } /** @@ -242,8 +241,7 @@ public Cancellable putComponentTemplateAsync(PutComponentTemplateRequest putComp public GetComponentTemplatesResponse getComponentTemplate(GetComponentTemplatesRequest getComponentTemplatesRequest, RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity(getComponentTemplatesRequest, - ClusterRequestConverters::getComponentTemplates, - options, GetComponentTemplatesResponse::fromXContent, emptySet()); + ClusterRequestConverters::getComponentTemplates, options, GetComponentTemplatesResponse::fromXContent, emptySet()); } /** @@ -256,8 +254,7 @@ public GetComponentTemplatesResponse getComponentTemplate(GetComponentTemplatesR public Cancellable getComponentTemplateAsync(GetComponentTemplatesRequest getComponentTemplatesRequest, RequestOptions options, ActionListener listener) { return restHighLevelClient.performRequestAsyncAndParseEntity(getComponentTemplatesRequest, - ClusterRequestConverters::getComponentTemplates, - options, GetComponentTemplatesResponse::fromXContent, listener, emptySet()); + ClusterRequestConverters::getComponentTemplates, options, GetComponentTemplatesResponse::fromXContent, listener, emptySet()); } /** @@ -271,8 +268,7 @@ public Cancellable getComponentTemplateAsync(GetComponentTemplatesRequest getCom public boolean existsComponentTemplate(ComponentTemplatesExistRequest componentTemplatesRequest, RequestOptions options) throws IOException { return restHighLevelClient.performRequest(componentTemplatesRequest, - ClusterRequestConverters::componentTemplatesExist, options, - RestHighLevelClient::convertExistsResponse, emptySet()); + ClusterRequestConverters::componentTemplatesExist, options, RestHighLevelClient::convertExistsResponse, emptySet()); } /** @@ -287,7 +283,6 @@ public Cancellable existsComponentTemplateAsync(ComponentTemplatesExistRequest c ActionListener listener) { return restHighLevelClient.performRequestAsync(componentTemplatesRequest, - ClusterRequestConverters::componentTemplatesExist, options, - RestHighLevelClient::convertExistsResponse, listener, emptySet()); + ClusterRequestConverters::componentTemplatesExist, options, RestHighLevelClient::convertExistsResponse, listener, emptySet()); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterRequestConverters.java index 1c1bd99c2a803..e4f8f541caad9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterRequestConverters.java @@ -108,7 +108,7 @@ static Request putComponentTemplate(PutComponentTemplateRequest putComponentTemp static Request getComponentTemplates(GetComponentTemplatesRequest getComponentTemplatesRequest){ final String endpoint = new RequestConverters.EndpointBuilder() .addPathPartAsIs("_component_template") - .addCommaSeparatedPathParts(getComponentTemplatesRequest.names()) + .addPathPart(getComponentTemplatesRequest.name()) .build(); final Request request = new Request(HttpGet.METHOD_NAME, endpoint); final RequestConverters.Params params = new RequestConverters.Params(); @@ -121,7 +121,7 @@ static Request getComponentTemplates(GetComponentTemplatesRequest getComponentTe static Request componentTemplatesExist(ComponentTemplatesExistRequest componentTemplatesRequest) { final String endpoint = new RequestConverters.EndpointBuilder() .addPathPartAsIs("_component_template") - .addCommaSeparatedPathParts(componentTemplatesRequest.names()) + .addPathPart(componentTemplatesRequest.name()) .build(); final Request request = new Request(HttpHead.METHOD_NAME, endpoint); final RequestConverters.Params params = new RequestConverters.Params(); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ComponentTemplatesExistRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ComponentTemplatesExistRequest.java index 8a42e9dbb4f4a..ae2bbed7d04c4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ComponentTemplatesExistRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ComponentTemplatesExistRequest.java @@ -19,8 +19,7 @@ package org.elasticsearch.client.indices; -import java.util.Arrays; -import java.util.List; +import org.elasticsearch.common.Strings; /** * A request to check for the existence of component templates @@ -28,23 +27,14 @@ public class ComponentTemplatesExistRequest extends GetComponentTemplatesRequest { /** - * Create a request to check for the existence of component templates. At least one template index name must be provided + * Create a request to check for the existence of component template. Name must be provided * - * @param names the names of templates to check for the existence of + * @param name the name of template to check for the existence of */ - public ComponentTemplatesExistRequest(String... names) { - this(Arrays.asList(names)); - } - - /** - * Create a request to check for the existence of component templates. At least one template index name must be provided - * - * @param names the names of templates to check for the existence of - */ - public ComponentTemplatesExistRequest(List names) { - super(names); - if (names().isEmpty()) { - throw new IllegalArgumentException("must provide at least one component template name"); + public ComponentTemplatesExistRequest(String name) { + super(name); + if (Strings.isNullOrEmpty(name)) { + throw new IllegalArgumentException("must provide component template name"); } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java index f6f1ee1707480..91b1c8c7518c8 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java @@ -36,40 +36,26 @@ */ public class GetComponentTemplatesRequest implements Validatable { - private final List names; + private final String name; private TimeValue masterNodeTimeout = TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT; private boolean local = false; /** - * Create a request to read the content of one or more component templates. If no template names are provided, all templates will + * Create a request to read the content of component template. If no template name is provided, all templates will * be read * - * @param names the names of templates to read + * @param name the name of template to read */ - public GetComponentTemplatesRequest(String... names) { - this(Arrays.asList(names)); + public GetComponentTemplatesRequest(String name) { + this.name = name; } /** - * Create a request to read the content of one or more component templates. If no template names are provided, all templates will - * be read - * - * @param names the names of templates to read - */ - public GetComponentTemplatesRequest(List names) { - Objects.requireNonNull(names); - if (names.stream().anyMatch(name -> name == null || Strings.hasText(name) == false)) { - throw new IllegalArgumentException("all index template names must be non null and non empty"); - } - this.names = unmodifiableList(names); - } - - /** - * @return the names of component templates this request is requesting + * @return the name of component template this request is requesting */ - public List names() { - return names; + public String name() { + return name; } /** diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesResponse.java index abe37e95fbca9..6d870c7eaa1b6 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesResponse.java @@ -71,10 +71,6 @@ public String toString() { private final Map componentTemplates; - GetComponentTemplatesResponse() { - componentTemplates = Collections.emptyMap(); - } - GetComponentTemplatesResponse(Map componentTemplates) { this.componentTemplates = Collections.unmodifiableMap(new LinkedHashMap<>(componentTemplates)); } @@ -95,12 +91,15 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (obj == null) + } + if (obj == null) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; + } GetComponentTemplatesResponse other = (GetComponentTemplatesResponse) obj; return Objects.equals(componentTemplates, other.componentTemplates); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutComponentTemplateRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutComponentTemplateRequest.java index 08227d5a27d08..036d433479dd8 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutComponentTemplateRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutComponentTemplateRequest.java @@ -18,17 +18,11 @@ */ package org.elasticsearch.client.indices; -import org.apache.lucene.util.BytesRef; -import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.client.TimedRequest; import org.elasticsearch.cluster.metadata.ComponentTemplate; -import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.common.xcontent.DeprecationHandler; -import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; @@ -49,8 +43,8 @@ public class PutComponentTemplateRequest extends TimedRequest implements ToXCont * Sets the name of the component template. */ public PutComponentTemplateRequest name(String name) { - if (name == null) { - throw new IllegalArgumentException("Name cannot be null"); + if (Strings.isNullOrEmpty(name)) { + throw new IllegalArgumentException("name cannot be null or empty"); } this.name = name; return this; @@ -96,56 +90,6 @@ public String cause() { return this.cause; } - /** - * The template source definition. - */ - public PutComponentTemplateRequest source(XContentBuilder templateBuilder) { - try { - return source(BytesReference.bytes(templateBuilder), templateBuilder.contentType()); - } catch (Exception e) { - throw new IllegalArgumentException("Failed to build json for template request", e); - } - } - - /** - * The template source definition. - */ - public PutComponentTemplateRequest source(String templateSource, XContentType xContentType) { - try (XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, templateSource)) { - return componentTemplate(ComponentTemplate.parse(parser)); - } catch (IOException e) { - throw new ElasticsearchParseException("Failed to parse content to map", e); - } - } - - /** - * The template source definition. - */ - public PutComponentTemplateRequest source(byte[] source, XContentType xContentType) { - return source(source, 0, source.length, xContentType); - } - - /** - * The template source definition. - */ - public PutComponentTemplateRequest source(byte[] source, int offset, int length, XContentType xContentType) { - try (XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, source, offset, length)) { - return componentTemplate(ComponentTemplate.parse(parser)); - } catch (IOException e) { - throw new ElasticsearchParseException("Failed to parse content to map", e); - } - } - - /** - * The template source definition. - */ - public PutComponentTemplateRequest source(BytesReference source, XContentType xContentType) { - BytesRef ref = source.toBytesRef(); - return source(ref.bytes, ref.offset, ref.length, xContentType); - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { if (componentTemplate != null) { diff --git a/rest-api-spec/src/main/resources/rest-api-spec/api/cluster.exists_component_template.json b/rest-api-spec/src/main/resources/rest-api-spec/api/cluster.exists_component_template.json index d842dbef9f1a7..23c918d605606 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/api/cluster.exists_component_template.json +++ b/rest-api-spec/src/main/resources/rest-api-spec/api/cluster.exists_component_template.json @@ -14,8 +14,8 @@ ], "parts":{ "name":{ - "type":"list", - "description":"The comma separated names of the component templates" + "type":"string", + "description":"The name of the template" } } } diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/Template.java b/server/src/main/java/org/elasticsearch/cluster/metadata/Template.java index eb819bb8f3993..46d159cc23da5 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/Template.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/Template.java @@ -184,4 +184,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.endObject(); return builder; } + + public class Builder { + + } } From 25d7134b2d2627ed8fbdc861e1a6f3328050ee0e Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Fri, 3 Apr 2020 19:33:43 +0200 Subject: [PATCH 14/17] PR comments --- .../main/java/org/elasticsearch/client/ClusterClient.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.java index da4805581c02a..bebb71d9b99d3 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.java @@ -210,9 +210,8 @@ public Cancellable deleteComponentTemplateAsync(DeleteComponentTemplateRequest r * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public AcknowledgedResponse putComponentTemplate( - PutComponentTemplateRequest putComponentTemplateRequest, - RequestOptions options) throws IOException { + public AcknowledgedResponse putComponentTemplate(PutComponentTemplateRequest putComponentTemplateRequest, + RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity(putComponentTemplateRequest, ClusterRequestConverters::putComponentTemplate, options, AcknowledgedResponse::fromXContent, emptySet()); } From 43562eeaca662234c35d18ab1344f87cc60de281 Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Fri, 3 Apr 2020 19:35:06 +0200 Subject: [PATCH 15/17] PR comments --- .../java/org/elasticsearch/cluster/metadata/Template.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/Template.java b/server/src/main/java/org/elasticsearch/cluster/metadata/Template.java index 46d159cc23da5..eb819bb8f3993 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/Template.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/Template.java @@ -184,8 +184,4 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.endObject(); return builder; } - - public class Builder { - - } } From 15ee13633bcea317fbdcab3862d4df4006c72cd5 Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Fri, 3 Apr 2020 19:45:56 +0200 Subject: [PATCH 16/17] unused imports fixed --- .../client/indices/GetComponentTemplatesRequest.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java index 91b1c8c7518c8..c7a537d4018fa 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesRequest.java @@ -22,15 +22,8 @@ import org.elasticsearch.client.TimedRequest; import org.elasticsearch.client.Validatable; import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.unit.TimeValue; -import java.util.Arrays; -import java.util.List; -import java.util.Objects; - -import static java.util.Collections.unmodifiableList; - /** * A request to read the content of component templates */ From 579bea4d6a0091531f3fd9027d4e90dbb6e40caf Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Fri, 3 Apr 2020 23:19:37 +0200 Subject: [PATCH 17/17] review comment --- .../client/indices/PutComponentTemplateRequest.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutComponentTemplateRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutComponentTemplateRequest.java index 036d433479dd8..e37d44baee8d8 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutComponentTemplateRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutComponentTemplateRequest.java @@ -21,7 +21,7 @@ import org.elasticsearch.client.TimedRequest; import org.elasticsearch.cluster.metadata.ComponentTemplate; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.xcontent.ToXContentFragment; +import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; @@ -29,7 +29,7 @@ /** * A request to create an component template. */ -public class PutComponentTemplateRequest extends TimedRequest implements ToXContentFragment { +public class PutComponentTemplateRequest extends TimedRequest implements ToXContentObject { private String name; @@ -97,9 +97,4 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } return builder; } - - @Override - public boolean isFragment() { - return false; - } }