Skip to content

Commit

Permalink
HLRC support for Component Templates APIs (elastic#54635)
Browse files Browse the repository at this point in the history
* HLRC support for Component Templates

* hlrc

* hlrc

* merge fix

* removed unused import

* checkstyle fixes

* metaData -> metadata

* move to ClusterClient

* checkstyle fixes

* checkstyle fixes

* checkstyle fixes

* method in spec fixed

* PR comments

* PR comments

* PR comments

* unused imports fixed

* review comment

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
probakowski and elasticmachine committed Apr 6, 2020
1 parent 2794572 commit c27022d
Show file tree
Hide file tree
Showing 12 changed files with 759 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -169,4 +175,113 @@ 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<AcknowledgedResponse> 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<AcknowledgedResponse> 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<GetComponentTemplatesResponse> 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<Boolean> listener) {

return restHighLevelClient.performRequestAsync(componentTemplatesRequest,
ClusterRequestConverters::componentTemplatesExist, options, RestHighLevelClient::convertExistsResponse, listener, emptySet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
.addPathPart(getComponentTemplatesRequest.name())
.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")
.addPathPart(componentTemplatesRequest.name())
.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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.common.Strings;

/**
* 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 template. Name must be provided
*
* @param name the name of template to check for the existence of
*/
public ComponentTemplatesExistRequest(String name) {
super(name);
if (Strings.isNullOrEmpty(name)) {
throw new IllegalArgumentException("must provide component template name");
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.unit.TimeValue;

/**
* A request to read the content of component templates
*/
public class GetComponentTemplatesRequest implements Validatable {

private final String name;

private TimeValue masterNodeTimeout = TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT;
private boolean local = false;

/**
* Create a request to read the content of component template. If no template name is provided, all templates will
* be read
*
* @param name the name of template to read
*/
public GetComponentTemplatesRequest(String name) {
this.name = name;
}

/**
* @return the name of component template this request is requesting
*/
public String name() {
return name;
}

/**
* @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;
}
}
Loading

0 comments on commit c27022d

Please sign in to comment.