From b716b0819717076518dad7ee9a15716dcdb50347 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Wed, 30 May 2018 11:10:00 -0500 Subject: [PATCH] Add Verify Repository High Level REST API (#30934) This commit adds Verify Repository, the associated docs and tests for the high level REST API client. A few small changes to the Verify Repository Response went into the commit as well. Relates #27205 --- .../client/RequestConverters.java | 14 ++++ .../elasticsearch/client/SnapshotClient.java | 26 ++++++ .../client/RequestConvertersTests.java | 16 ++++ .../org/elasticsearch/client/SnapshotIT.java | 17 +++- .../SnapshotClientDocumentationIT.java | 62 ++++++++++++++ .../snapshot/verify_repository.asciidoc | 81 +++++++++++++++++++ .../high-level/supported-apis.asciidoc | 2 + .../verify/VerifyRepositoryResponse.java | 32 ++++++++ .../verify/VerifyRepositoryResponseTests.java | 47 +++++++++++ 9 files changed, 293 insertions(+), 4 deletions(-) create mode 100644 docs/java-rest/high-level/snapshot/verify_repository.asciidoc create mode 100644 server/src/test/java/org/elasticsearch/action/admin/cluster/repositories/verify/VerifyRepositoryResponseTests.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java index 1f542736d7dc0..435381774b0c3 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java @@ -33,6 +33,7 @@ import org.elasticsearch.action.admin.cluster.repositories.delete.DeleteRepositoryRequest; import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesRequest; import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest; +import org.elasticsearch.action.admin.cluster.repositories.verify.VerifyRepositoryRequest; import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; @@ -747,6 +748,19 @@ static Request deleteRepository(DeleteRepositoryRequest deleteRepositoryRequest) return request; } + static Request verifyRepository(VerifyRepositoryRequest verifyRepositoryRequest) { + String endpoint = new EndpointBuilder().addPathPartAsIs("_snapshot") + .addPathPart(verifyRepositoryRequest.name()) + .addPathPartAsIs("_verify") + .build(); + Request request = new Request(HttpPost.METHOD_NAME, endpoint); + + Params parameters = new Params(request); + parameters.withMasterTimeout(verifyRepositoryRequest.masterNodeTimeout()); + parameters.withTimeout(verifyRepositoryRequest.timeout()); + return request; + } + static Request putTemplate(PutIndexTemplateRequest putIndexTemplateRequest) throws IOException { String endpoint = new EndpointBuilder().addPathPartAsIs("_template").addPathPart(putIndexTemplateRequest.name()).build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotClient.java index d969232f0d70f..104bc91271148 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotClient.java @@ -27,6 +27,8 @@ import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesResponse; import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest; import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse; +import org.elasticsearch.action.admin.cluster.repositories.verify.VerifyRepositoryRequest; +import org.elasticsearch.action.admin.cluster.repositories.verify.VerifyRepositoryResponse; import java.io.IOException; @@ -116,4 +118,28 @@ public void deleteRepositoryAsync(DeleteRepositoryRequest deleteRepositoryReques restHighLevelClient.performRequestAsyncAndParseEntity(deleteRepositoryRequest, RequestConverters::deleteRepository, DeleteRepositoryResponse::fromXContent, listener, emptySet(), headers); } + + /** + * Verifies a snapshot repository. + *

+ * See Snapshot and Restore + * API on elastic.co + */ + public VerifyRepositoryResponse verifyRepository(VerifyRepositoryRequest verifyRepositoryRequest, Header... headers) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity(verifyRepositoryRequest, RequestConverters::verifyRepository, + VerifyRepositoryResponse::fromXContent, emptySet(), headers); + } + + /** + * Asynchronously verifies a snapshot repository. + *

+ * See Snapshot and Restore + * API on elastic.co + */ + public void verifyRepositoryAsync(VerifyRepositoryRequest verifyRepositoryRequest, + ActionListener listener, Header... headers) { + restHighLevelClient.performRequestAsyncAndParseEntity(verifyRepositoryRequest, RequestConverters::verifyRepository, + VerifyRepositoryResponse::fromXContent, listener, emptySet(), headers); + } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java index 0f0c1f275881b..5388b5ba82e6f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java @@ -33,6 +33,7 @@ import org.elasticsearch.action.admin.cluster.repositories.delete.DeleteRepositoryRequest; import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesRequest; import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest; +import org.elasticsearch.action.admin.cluster.repositories.verify.VerifyRepositoryRequest; import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; @@ -1632,6 +1633,21 @@ public void testDeleteRepository() { assertNull(request.getEntity()); } + public void testVerifyRepository() { + Map expectedParams = new HashMap<>(); + String repository = randomIndicesNames(1, 1)[0]; + String endpoint = "/_snapshot/" + repository + "/_verify"; + + VerifyRepositoryRequest verifyRepositoryRequest = new VerifyRepositoryRequest(repository); + setRandomMasterTimeout(verifyRepositoryRequest, expectedParams); + setRandomTimeout(verifyRepositoryRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); + + Request request = RequestConverters.verifyRepository(verifyRepositoryRequest); + assertThat(endpoint, equalTo(request.getEndpoint())); + assertThat(HttpPost.METHOD_NAME, equalTo(request.getMethod())); + assertThat(expectedParams, equalTo(request.getParameters())); + } + public void testPutTemplateRequest() throws Exception { Map names = new HashMap<>(); names.put("log", "log"); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotIT.java index 02e03bfec764e..aaba5da820613 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotIT.java @@ -28,6 +28,8 @@ import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesResponse; import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest; import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse; +import org.elasticsearch.action.admin.cluster.repositories.verify.VerifyRepositoryRequest; +import org.elasticsearch.action.admin.cluster.repositories.verify.VerifyRepositoryResponse; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.repositories.fs.FsRepository; import org.elasticsearch.rest.RestStatus; @@ -86,10 +88,7 @@ public void testSnapshotGetRepositoriesNonExistent() { public void testSnapshotDeleteRepository() throws IOException { String repository = "test"; - String repositorySettings = "{\"type\":\"fs\", \"settings\":{\"location\": \".\"}}"; - - highLevelClient().getLowLevelClient().performRequest("put", "_snapshot/" + repository, - Collections.emptyMap(), new StringEntity(repositorySettings, ContentType.APPLICATION_JSON)); + assertTrue(createTestRepository(repository, FsRepository.TYPE, "{\"location\": \".\"}").isAcknowledged()); GetRepositoriesRequest request = new GetRepositoriesRequest(); GetRepositoriesResponse response = execute(request, highLevelClient().snapshot()::getRepositories, @@ -102,4 +101,14 @@ public void testSnapshotDeleteRepository() throws IOException { assertTrue(deleteResponse.isAcknowledged()); } + + public void testVerifyRepository() throws IOException { + PutRepositoryResponse putRepositoryResponse = createTestRepository("test", FsRepository.TYPE, "{\"location\": \".\"}"); + assertTrue(putRepositoryResponse.isAcknowledged()); + + VerifyRepositoryRequest request = new VerifyRepositoryRequest("test"); + VerifyRepositoryResponse response = execute(request, highLevelClient().snapshot()::verifyRepository, + highLevelClient().snapshot()::verifyRepositoryAsync); + assertThat(response.getNodes().size(), equalTo(1)); + } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java index dc749a96bb85c..2890ad50c2666 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java @@ -27,6 +27,8 @@ import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesResponse; import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest; import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse; +import org.elasticsearch.action.admin.cluster.repositories.verify.VerifyRepositoryRequest; +import org.elasticsearch.action.admin.cluster.repositories.verify.VerifyRepositoryResponse; import org.elasticsearch.client.ESRestHighLevelClientTestCase; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.cluster.metadata.RepositoryMetaData; @@ -297,6 +299,66 @@ public void onFailure(Exception e) { } } + public void testSnapshotVerifyRepository() throws IOException { + RestHighLevelClient client = highLevelClient(); + createTestRepositories(); + + // tag::verify-repository-request + VerifyRepositoryRequest request = new VerifyRepositoryRequest(repositoryName); + // end::verify-repository-request + + // tag::verify-repository-request-masterTimeout + request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); // <1> + request.masterNodeTimeout("1m"); // <2> + // end::verify-repository-request-masterTimeout + // tag::verify-repository-request-timeout + request.timeout(TimeValue.timeValueMinutes(1)); // <1> + request.timeout("1m"); // <2> + // end::verify-repository-request-timeout + + // tag::verify-repository-execute + VerifyRepositoryResponse response = client.snapshot().verifyRepository(request); + // end::verify-repository-execute + + // tag::verify-repository-response + List repositoryMetaDataResponse = response.getNodes(); + // end::verify-repository-response + assertThat(1, equalTo(repositoryMetaDataResponse.size())); + assertThat("node-0", equalTo(repositoryMetaDataResponse.get(0).getName())); + } + + public void testSnapshotVerifyRepositoryAsync() throws InterruptedException { + RestHighLevelClient client = highLevelClient(); + { + VerifyRepositoryRequest request = new VerifyRepositoryRequest(repositoryName); + + // tag::verify-repository-execute-listener + ActionListener listener = + new ActionListener() { + @Override + public void onResponse(VerifyRepositoryResponse verifyRepositoryRestResponse) { + // <1> + } + + @Override + public void onFailure(Exception e) { + // <2> + } + }; + // end::verify-repository-execute-listener + + // Replace the empty listener by a blocking listener in test + final CountDownLatch latch = new CountDownLatch(1); + listener = new LatchedActionListener<>(listener, latch); + + // tag::verify-repository-execute-async + client.snapshot().verifyRepositoryAsync(request, listener); // <1> + // end::verify-repository-execute-async + + assertTrue(latch.await(30L, TimeUnit.SECONDS)); + } + } + private void createTestRepositories() throws IOException { PutRepositoryRequest request = new PutRepositoryRequest(repositoryName); request.type(FsRepository.TYPE); diff --git a/docs/java-rest/high-level/snapshot/verify_repository.asciidoc b/docs/java-rest/high-level/snapshot/verify_repository.asciidoc new file mode 100644 index 0000000000000..4f03d1e5fe382 --- /dev/null +++ b/docs/java-rest/high-level/snapshot/verify_repository.asciidoc @@ -0,0 +1,81 @@ +[[java-rest-high-snapshot-verify-repository]] +=== Snapshot Verify Repository API + +The Snapshot Verify Repository API allows to verify a registered repository. + +[[java-rest-high-snapshot-verify-repository-request]] +==== Snapshot Verify Repository Request + +A `VerifyRepositoryRequest`: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/SnapshotClientDocumentationIT.java[verify-repository-request] +-------------------------------------------------- + +==== Optional Arguments +The following arguments can optionally be provided: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/SnapshotClientDocumentationIT.java[create-repository-request-timeout] +-------------------------------------------------- +<1> Timeout to wait for the all the nodes to acknowledge the settings were applied +as a `TimeValue` +<2> Timeout to wait for the all the nodes to acknowledge the settings were applied +as a `String` + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/SnapshotClientDocumentationIT.java[verify-repository-request-masterTimeout] +-------------------------------------------------- +<1> Timeout to connect to the master node as a `TimeValue` +<2> Timeout to connect to the master node as a `String` + +[[java-rest-high-snapshot-verify-repository-sync]] +==== Synchronous Execution + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/SnapshotClientDocumentationIT.java[verify-repository-execute] +-------------------------------------------------- + +[[java-rest-high-snapshot-verify-repository-async]] +==== Asynchronous Execution + +The asynchronous execution of a snapshot verify repository requires both the +`VerifyRepositoryRequest` instance and an `ActionListener` instance to be +passed to the asynchronous method: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/SnapshotClientDocumentationIT.java[verify-repository-execute-async] +-------------------------------------------------- +<1> The `VerifyRepositoryRequest` to execute and the `ActionListener` +to use when the execution completes + +The asynchronous method does not block and returns immediately. Once it is +completed the `ActionListener` is called back using the `onResponse` method +if the execution successfully completed or using the `onFailure` method if +it failed. + +A typical listener for `VerifyRepositoryResponse` looks like: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/SnapshotClientDocumentationIT.java[verify-repository-execute-listener] +-------------------------------------------------- +<1> Called when the execution is successfully completed. The response is +provided as an argument +<2> Called in case of a failure. The raised exception is provided as an argument + +[[java-rest-high-cluster-verify-repository-response]] +==== Snapshot Verify Repository Response + +The returned `VerifyRepositoryResponse` allows to retrieve information about the +executed operation as follows: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/SnapshotClientDocumentationIT.java[verify-repository-response] +-------------------------------------------------- \ No newline at end of file diff --git a/docs/java-rest/high-level/supported-apis.asciidoc b/docs/java-rest/high-level/supported-apis.asciidoc index 41202e963a4e5..ff1e03afe3ec3 100644 --- a/docs/java-rest/high-level/supported-apis.asciidoc +++ b/docs/java-rest/high-level/supported-apis.asciidoc @@ -118,10 +118,12 @@ The Java High Level REST Client supports the following Snapshot APIs: * <> * <> * <> +* <> include::snapshot/get_repository.asciidoc[] include::snapshot/create_repository.asciidoc[] include::snapshot/delete_repository.asciidoc[] +include::snapshot/verify_repository.asciidoc[] == Tasks APIs diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/verify/VerifyRepositoryResponse.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/verify/VerifyRepositoryResponse.java index d5cc0b6f95728..f8f1c055c7beb 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/verify/VerifyRepositoryResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/verify/VerifyRepositoryResponse.java @@ -32,6 +32,7 @@ import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; import java.util.Arrays; @@ -129,6 +130,12 @@ public int hashCode() { private ClusterName clusterName; + private static final ObjectParser PARSER = + new ObjectParser<>(VerifyRepositoryResponse.class.getName(), VerifyRepositoryResponse::new); + static { + PARSER.declareNamedObjects(VerifyRepositoryResponse::setNodes, NodeView.PARSER, new ParseField("nodes")); + } + VerifyRepositoryResponse() { } @@ -167,6 +174,10 @@ public ClusterName getClusterName() { return clusterName; } + protected void setNodes(List nodes) { + this.nodes = nodes.stream().map(n -> n.convertToDiscoveryNode()).collect(Collectors.toList()); + } + @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); @@ -187,8 +198,29 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } + public static VerifyRepositoryResponse fromXContent(XContentParser parser) { + return PARSER.apply(parser, null); + } + @Override public String toString() { return Strings.toString(this); } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + VerifyRepositoryResponse other = (VerifyRepositoryResponse) obj; + return nodes.equals(other.nodes); + } + + @Override + public int hashCode() { + return nodes.hashCode(); + } } diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/repositories/verify/VerifyRepositoryResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/repositories/verify/VerifyRepositoryResponseTests.java new file mode 100644 index 0000000000000..ad72e29ee5d5c --- /dev/null +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/repositories/verify/VerifyRepositoryResponseTests.java @@ -0,0 +1,47 @@ +/* + * 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.action.admin.cluster.repositories.verify; + +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractXContentTestCase; + +import java.util.ArrayList; +import java.util.List; + +public class VerifyRepositoryResponseTests extends AbstractXContentTestCase { + + @Override + protected VerifyRepositoryResponse doParseInstance(XContentParser parser) { + return VerifyRepositoryResponse.fromXContent(parser); + } + + @Override + protected boolean supportsUnknownFields() { + return false; + } + + @Override + protected VerifyRepositoryResponse createTestInstance() { + VerifyRepositoryResponse response = new VerifyRepositoryResponse(); + List nodes = new ArrayList<>(); + nodes.add(new VerifyRepositoryResponse.NodeView("node-id", "node-name")); + response.setNodes(nodes); + return response; + } +}