-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GET Repository High Level REST API (#30362)
This commit adds the Snapshot Client with a first API call within it, the get repositories call in snapshot/restore module. This also creates a snapshot namespace for the docs, as well as get repositories docs. Relates #27205
- Loading branch information
Showing
9 changed files
with
435 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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; | ||
|
||
import org.apache.http.Header; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesRequest; | ||
import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesResponse; | ||
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; | ||
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse; | ||
|
||
import java.io.IOException; | ||
|
||
import static java.util.Collections.emptySet; | ||
|
||
/** | ||
* A wrapper for the {@link RestHighLevelClient} that provides methods for accessing the Snapshot API. | ||
* <p> | ||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-snapshots.html">Snapshot API on elastic.co</a> | ||
*/ | ||
public final class SnapshotClient { | ||
private final RestHighLevelClient restHighLevelClient; | ||
|
||
SnapshotClient(RestHighLevelClient restHighLevelClient) { | ||
this.restHighLevelClient = restHighLevelClient; | ||
} | ||
|
||
/** | ||
* Gets a list of snapshot repositories. If the list of repositories is empty or it contains a single element "_all", all | ||
* registered repositories are returned. | ||
* <p> | ||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-snapshots.html"> Snapshot and Restore | ||
* API on elastic.co</a> | ||
*/ | ||
public GetRepositoriesResponse getRepositories(GetRepositoriesRequest getRepositoriesRequest, Header... headers) | ||
throws IOException { | ||
return restHighLevelClient.performRequestAndParseEntity(getRepositoriesRequest, RequestConverters::getRepositories, | ||
GetRepositoriesResponse::fromXContent, emptySet(), headers); | ||
} | ||
|
||
/** | ||
* Asynchronously gets a list of snapshot repositories. If the list of repositories is empty or it contains a single element "_all", all | ||
* registered repositories are returned. | ||
* <p> | ||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-snapshots.html"> Snapshot and Restore | ||
* API on elastic.co</a> | ||
*/ | ||
public void getRepositoriesAsync(GetRepositoriesRequest getRepositoriesRequest, | ||
ActionListener<GetRepositoriesResponse> listener, Header... headers) { | ||
restHighLevelClient.performRequestAsyncAndParseEntity(getRepositoriesRequest, RequestConverters::getRepositories, | ||
GetRepositoriesResponse::fromXContent, listener, emptySet(), headers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* 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; | ||
|
||
import org.apache.http.entity.ContentType; | ||
import org.apache.http.entity.StringEntity; | ||
import org.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesRequest; | ||
import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesResponse; | ||
import org.elasticsearch.rest.RestStatus; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class SnapshotIT extends ESRestHighLevelClientTestCase { | ||
|
||
public void testModulesGetRepositoriesUsingParams() 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)); | ||
|
||
highLevelClient().getLowLevelClient().performRequest("put", "_snapshot/" + repository + "_other", Collections.emptyMap(), | ||
new StringEntity(repositorySettings, ContentType.APPLICATION_JSON)); | ||
|
||
{ | ||
GetRepositoriesRequest request = new GetRepositoriesRequest(); | ||
request.repositories(new String[]{repository}); | ||
GetRepositoriesResponse response = execute(request, highLevelClient().snapshot()::getRepositories, | ||
highLevelClient().snapshot()::getRepositoriesAsync); | ||
assertThat(1, equalTo(response.repositories().size())); | ||
} | ||
{ | ||
GetRepositoriesRequest request = new GetRepositoriesRequest(); | ||
GetRepositoriesResponse response = execute(request, highLevelClient().snapshot()::getRepositories, | ||
highLevelClient().snapshot()::getRepositoriesAsync); | ||
assertThat(2, equalTo(response.repositories().size())); | ||
} | ||
} | ||
|
||
public void testModulesGetDefaultRepositories() throws IOException { | ||
String repositorySettings = "{\"type\":\"fs\", \"settings\":{\"location\": \".\"}}"; | ||
GetRepositoriesRequest request = new GetRepositoriesRequest(); | ||
|
||
highLevelClient().getLowLevelClient().performRequest("put", "_snapshot/test", Collections.emptyMap(), | ||
new StringEntity(repositorySettings, ContentType.APPLICATION_JSON)); | ||
|
||
GetRepositoriesResponse response = execute(request, highLevelClient().snapshot()::getRepositories, | ||
highLevelClient().snapshot()::getRepositoriesAsync); | ||
assertThat(1, equalTo(response.repositories().size())); | ||
} | ||
|
||
public void testModulesGetRepositoriesNonExistent() throws IOException { | ||
String repository = "doesnotexist"; | ||
GetRepositoriesRequest request = new GetRepositoriesRequest(new String[]{repository}); | ||
ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(request, | ||
highLevelClient().snapshot()::getRepositories, highLevelClient().snapshot()::getRepositoriesAsync)); | ||
|
||
assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND)); | ||
assertThat(exception.getMessage(), equalTo( | ||
"Elasticsearch exception [type=repository_missing_exception, reason=[" + repository + "] missing]")); | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
...l/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* 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.documentation; | ||
|
||
import org.apache.http.entity.ContentType; | ||
import org.apache.http.entity.StringEntity; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.LatchedActionListener; | ||
import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesRequest; | ||
import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesResponse; | ||
import org.elasticsearch.client.ESRestHighLevelClientTestCase; | ||
import org.elasticsearch.client.RestHighLevelClient; | ||
import org.elasticsearch.cluster.metadata.RepositoryMetaData; | ||
import org.elasticsearch.common.unit.TimeValue; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
/** | ||
* This class is used to generate the Java Cluster API documentation. | ||
* You need to wrap your code between two tags like: | ||
* // tag::example | ||
* // end::example | ||
* | ||
* Where example is your tag name. | ||
* | ||
* Then in the documentation, you can extract what is between tag and end tags with | ||
* ["source","java",subs="attributes,callouts,macros"] | ||
* -------------------------------------------------- | ||
* include-tagged::{doc-tests}/SnapshotClientDocumentationIT.java[example] | ||
* -------------------------------------------------- | ||
* | ||
* The column width of the code block is 84. If the code contains a line longer | ||
* than 84, the line will be cut and a horizontal scroll bar will be displayed. | ||
* (the code indentation of the tag is not included in the width) | ||
*/ | ||
public class SnapshotClientDocumentationIT extends ESRestHighLevelClientTestCase { | ||
|
||
private static final String testRepository = "test_repository"; | ||
|
||
public void testSnapshotGetRepository() throws IOException { | ||
RestHighLevelClient client = highLevelClient(); | ||
|
||
createTestRepositories(); | ||
|
||
// tag::get-repository-request | ||
GetRepositoriesRequest request = new GetRepositoriesRequest(); | ||
// end::get-repository-request | ||
|
||
// tag::get-repository-request-repositories | ||
String [] repositories = new String[] { testRepository }; | ||
request.repositories(repositories); // <1> | ||
// end::get-repository-request-repositories | ||
// tag::get-repository-request-local | ||
request.local(true); // <1> | ||
// end::get-repository-request-local | ||
// tag::get-repository-request-masterTimeout | ||
request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); // <1> | ||
request.masterNodeTimeout("1m"); // <2> | ||
// end::get-repository-request-masterTimeout | ||
|
||
// tag::get-repository-execute | ||
GetRepositoriesResponse response = client.snapshot().getRepositories(request); | ||
// end::get-repository-execute | ||
|
||
// tag::get-repository-response | ||
List<RepositoryMetaData> repositoryMetaDataResponse = response.repositories(); | ||
// end::get-repository-response | ||
assertThat(1, equalTo(repositoryMetaDataResponse.size())); | ||
assertThat(testRepository, equalTo(repositoryMetaDataResponse.get(0).name())); | ||
} | ||
|
||
public void testSnapshotGetRepositoryAsync() throws InterruptedException { | ||
RestHighLevelClient client = highLevelClient(); | ||
{ | ||
GetRepositoriesRequest request = new GetRepositoriesRequest(); | ||
|
||
// tag::get-repository-execute-listener | ||
ActionListener<GetRepositoriesResponse> listener = | ||
new ActionListener<GetRepositoriesResponse>() { | ||
@Override | ||
public void onResponse(GetRepositoriesResponse getRepositoriesResponse) { | ||
// <1> | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
// <2> | ||
} | ||
}; | ||
// end::get-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::get-repository-execute-async | ||
client.snapshot().getRepositoriesAsync(request, listener); // <1> | ||
// end::get-repository-execute-async | ||
|
||
assertTrue(latch.await(30L, TimeUnit.SECONDS)); | ||
} | ||
|
||
} | ||
|
||
private void createTestRepositories() throws IOException { | ||
RestHighLevelClient client = highLevelClient(); | ||
String repositorySettings = "{\"type\":\"fs\", \"settings\":{\"location\": \".\"}}"; | ||
highLevelClient().getLowLevelClient().performRequest("put", "_snapshot/" + testRepository, Collections.emptyMap(), | ||
new StringEntity(repositorySettings, ContentType.APPLICATION_JSON)); | ||
|
||
} | ||
} |
Oops, something went wrong.