Skip to content

Commit

Permalink
Add Create Repository High Level REST API (#30501)
Browse files Browse the repository at this point in the history
This commit adds Create Repository, the associated docs and tests
for the high level REST API client. A few small changes to the
PutRepository Request and Response went into the commit as well.
  • Loading branch information
hub-cap committed May 16, 2018
1 parent a0ef529 commit 4207078
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest;
import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesRequest;
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest;
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;
Expand Down Expand Up @@ -656,6 +657,19 @@ static Request getRepositories(GetRepositoriesRequest getRepositoriesRequest) {
return request;
}

static Request createRepository(PutRepositoryRequest putRepositoryRequest) throws IOException {
String endpoint = new EndpointBuilder().addPathPart("_snapshot").addPathPart(putRepositoryRequest.name()).build();
Request request = new Request(HttpPut.METHOD_NAME, endpoint);

Params parameters = new Params(request);
parameters.withMasterTimeout(putRepositoryRequest.masterNodeTimeout());
parameters.withTimeout(putRepositoryRequest.timeout());
parameters.withVerify(putRepositoryRequest.verify());

request.setEntity(createEntity(putRepositoryRequest, REQUEST_BODY_CONTENT_TYPE));
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
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 org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest;
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse;

import java.io.IOException;

Expand Down Expand Up @@ -67,4 +67,27 @@ public void getRepositoriesAsync(GetRepositoriesRequest getRepositoriesRequest,
restHighLevelClient.performRequestAsyncAndParseEntity(getRepositoriesRequest, RequestConverters::getRepositories,
GetRepositoriesResponse::fromXContent, listener, emptySet(), headers);
}

/**
* Creates a snapshot repository.
* <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 PutRepositoryResponse createRepository(PutRepositoryRequest putRepositoryRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(putRepositoryRequest, RequestConverters::createRepository,
PutRepositoryResponse::fromXContent, emptySet(), headers);
}

/**
* Asynchronously creates a snapshot repository.
* <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 createRepositoryAsync(PutRepositoryRequest putRepositoryRequest,
ActionListener<PutRepositoryResponse> listener, Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(putRepositoryRequest, RequestConverters::createRepository,
PutRepositoryResponse::fromXContent, listener, emptySet(), headers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest;
import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesRequest;
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest;
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
Expand Down Expand Up @@ -76,9 +77,11 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.lucene.uid.Versions;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand All @@ -93,6 +96,7 @@
import org.elasticsearch.index.rankeval.RankEvalSpec;
import org.elasticsearch.index.rankeval.RatedRequest;
import org.elasticsearch.index.rankeval.RestRankEvalAction;
import org.elasticsearch.repositories.fs.FsRepository;
import org.elasticsearch.rest.action.search.RestSearchAction;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
Expand All @@ -110,6 +114,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -1450,6 +1455,27 @@ public void testGetRepositories() {
assertThat(expectedParams, equalTo(request.getParameters()));
}

public void testCreateRepository() throws IOException {
String repository = "repo";
String endpoint = "/_snapshot/" + repository;
Path repositoryLocation = PathUtils.get(".");
PutRepositoryRequest putRepositoryRequest = new PutRepositoryRequest(repository);
putRepositoryRequest.type(FsRepository.TYPE);
putRepositoryRequest.verify(randomBoolean());

putRepositoryRequest.settings(
Settings.builder()
.put(FsRepository.LOCATION_SETTING.getKey(), repositoryLocation)
.put(FsRepository.COMPRESS_SETTING.getKey(), randomBoolean())
.put(FsRepository.CHUNK_SIZE_SETTING.getKey(), randomIntBetween(100, 1000), ByteSizeUnit.BYTES)
.build());

Request request = RequestConverters.createRepository(putRepositoryRequest);
assertThat(endpoint, equalTo(request.getEndpoint()));
assertThat(HttpPut.METHOD_NAME, equalTo(request.getMethod()));
assertToXContentBody(putRepositoryRequest, request.getEntity());
}

public void testPutTemplateRequest() throws Exception {
Map<String, String> names = new HashMap<>();
names.put("log", "log");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,56 @@

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.action.admin.cluster.repositories.put.PutRepositoryRequest;
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.repositories.fs.FsRepository;
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));
private PutRepositoryResponse createTestRepository(String repository, String type, String settings) throws IOException {
PutRepositoryRequest request = new PutRepositoryRequest(repository);
request.settings(settings, XContentType.JSON);
request.type(type);
return execute(request, highLevelClient().snapshot()::createRepository,
highLevelClient().snapshot()::createRepositoryAsync);

{
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();
public void testCreateRepository() throws IOException {
PutRepositoryResponse response = createTestRepository("test", FsRepository.TYPE, "{\"location\": \".\"}");
assertTrue(response.isAcknowledged());
}

highLevelClient().getLowLevelClient().performRequest("put", "_snapshot/test", Collections.emptyMap(),
new StringEntity(repositorySettings, ContentType.APPLICATION_JSON));
public void testModulesGetRepositoriesUsingParams() throws IOException {
String testRepository = "test";
assertTrue(createTestRepository(testRepository, FsRepository.TYPE, "{\"location\": \".\"}").isAcknowledged());
assertTrue(createTestRepository("other", FsRepository.TYPE, "{\"location\": \".\"}").isAcknowledged());

GetRepositoriesRequest request = new GetRepositoriesRequest();
request.repositories(new String[]{testRepository});
GetRepositoriesResponse response = execute(request, highLevelClient().snapshot()::getRepositories,
highLevelClient().snapshot()::getRepositoriesAsync);
assertThat(1, equalTo(response.repositories().size()));
}

public void testModulesGetDefaultRepositories() throws IOException {
assertTrue(createTestRepository("other", FsRepository.TYPE, "{\"location\": \".\"}").isAcknowledged());
assertTrue(createTestRepository("test", FsRepository.TYPE, "{\"location\": \".\"}").isAcknowledged());

GetRepositoriesResponse response = execute(new GetRepositoriesRequest(), highLevelClient().snapshot()::getRepositories,
highLevelClient().snapshot()::getRepositoriesAsync);
assertThat(2, equalTo(response.repositories().size()));
}

public void testModulesGetRepositoriesNonExistent() throws IOException {
String repository = "doesnotexist";
GetRepositoriesRequest request = new GetRepositoriesRequest(new String[]{repository});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,24 @@

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.action.admin.cluster.repositories.put.PutRepositoryRequest;
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse;
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.cluster.metadata.RepositoryMetaData;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.repositories.fs.FsRepository;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

Expand All @@ -58,7 +62,114 @@
*/
public class SnapshotClientDocumentationIT extends ESRestHighLevelClientTestCase {

private static final String testRepository = "test_repository";
private static final String repositoryName = "test_repository";

public void testSnapshotCreateRepository() throws IOException {
RestHighLevelClient client = highLevelClient();

// tag::create-repository-request
PutRepositoryRequest request = new PutRepositoryRequest();
// end::create-repository-request

// tag::create-repository-create-settings
String locationKey = FsRepository.LOCATION_SETTING.getKey();
String locationValue = ".";
String compressKey = FsRepository.COMPRESS_SETTING.getKey();
boolean compressValue = true;

Settings settings = Settings.builder()
.put(locationKey, locationValue)
.put(compressKey, compressValue)
.build(); // <1>
// end::create-repository-create-settings

// tag::create-repository-request-repository-settings
request.settings(settings); // <1>
// end::create-repository-request-repository-settings

{
// tag::create-repository-settings-builder
Settings.Builder settingsBuilder = Settings.builder()
.put(locationKey, locationValue)
.put(compressKey, compressValue);
request.settings(settingsBuilder); // <1>
// end::create-repository-settings-builder
}
{
// tag::create-repository-settings-map
Map<String, Object> map = new HashMap<>();
map.put(locationKey, locationValue);
map.put(compressKey, compressValue);
request.settings(map); // <1>
// end::create-repository-settings-map
}
{
// tag::create-repository-settings-source
request.settings("{\"location\": \".\", \"compress\": \"true\"}",
XContentType.JSON); // <1>
// end::create-repository-settings-source
}

// tag::create-repository-request-name
request.name(repositoryName); // <1>
// end::create-repository-request-name
// tag::create-repository-request-type
request.type(FsRepository.TYPE); // <1>
// end::create-repository-request-type

// tag::create-repository-request-masterTimeout
request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); // <1>
request.masterNodeTimeout("1m"); // <2>
// end::create-repository-request-masterTimeout
// tag::create-repository-request-timeout
request.timeout(TimeValue.timeValueMinutes(1)); // <1>
request.timeout("1m"); // <2>
// end::create-repository-request-timeout
// tag::create-repository-request-verify
request.verify(true); // <1>
// end::create-repository-request-verify

// tag::create-repository-execute
PutRepositoryResponse response = client.snapshot().createRepository(request);
// end::create-repository-execute

// tag::create-repository-response
boolean acknowledged = response.isAcknowledged(); // <1>
// end::create-repository-response
assertTrue(acknowledged);
}

public void testSnapshotCreateRepositoryAsync() throws InterruptedException {
RestHighLevelClient client = highLevelClient();
{
PutRepositoryRequest request = new PutRepositoryRequest(repositoryName);

// tag::create-repository-execute-listener
ActionListener<PutRepositoryResponse> listener =
new ActionListener<PutRepositoryResponse>() {
@Override
public void onResponse(PutRepositoryResponse putRepositoryResponse) {
// <1>
}

@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::create-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::create-repository-execute-async
client.snapshot().createRepositoryAsync(request, listener); // <1>
// end::create-repository-execute-async

assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}

public void testSnapshotGetRepository() throws IOException {
RestHighLevelClient client = highLevelClient();
Expand All @@ -70,7 +181,7 @@ public void testSnapshotGetRepository() throws IOException {
// end::get-repository-request

// tag::get-repository-request-repositories
String [] repositories = new String[] { testRepository };
String [] repositories = new String[] {repositoryName};
request.repositories(repositories); // <1>
// end::get-repository-request-repositories
// tag::get-repository-request-local
Expand All @@ -89,7 +200,7 @@ public void testSnapshotGetRepository() throws IOException {
List<RepositoryMetaData> repositoryMetaDataResponse = response.repositories();
// end::get-repository-response
assertThat(1, equalTo(repositoryMetaDataResponse.size()));
assertThat(testRepository, equalTo(repositoryMetaDataResponse.get(0).name()));
assertThat(repositoryName, equalTo(repositoryMetaDataResponse.get(0).name()));
}

public void testSnapshotGetRepositoryAsync() throws InterruptedException {
Expand Down Expand Up @@ -122,14 +233,12 @@ public void onFailure(Exception e) {

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));

PutRepositoryRequest request = new PutRepositoryRequest(repositoryName);
request.type(FsRepository.TYPE);
request.settings("{\"location\": \".\"}", XContentType.JSON);
assertTrue(highLevelClient().snapshot().createRepository(request).isAcknowledged());
}
}
Loading

0 comments on commit 4207078

Please sign in to comment.