|
29 | 29 | import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse;
|
30 | 30 | import org.elasticsearch.action.admin.cluster.repositories.verify.VerifyRepositoryRequest;
|
31 | 31 | import org.elasticsearch.action.admin.cluster.repositories.verify.VerifyRepositoryResponse;
|
| 32 | +import org.elasticsearch.action.admin.cluster.snapshots.delete.DeleteSnapshotRequest; |
| 33 | +import org.elasticsearch.action.admin.cluster.snapshots.delete.DeleteSnapshotResponse; |
32 | 34 | import org.elasticsearch.client.ESRestHighLevelClientTestCase;
|
| 35 | +import org.elasticsearch.client.Request; |
33 | 36 | import org.elasticsearch.client.RequestOptions;
|
| 37 | +import org.elasticsearch.client.Response; |
34 | 38 | import org.elasticsearch.client.RestHighLevelClient;
|
35 | 39 | import org.elasticsearch.cluster.metadata.RepositoryMetaData;
|
36 | 40 | import org.elasticsearch.common.settings.Settings;
|
|
41 | 45 | import java.io.IOException;
|
42 | 46 | import java.util.HashMap;
|
43 | 47 | import java.util.List;
|
| 48 | +import java.util.Locale; |
44 | 49 | import java.util.Map;
|
45 | 50 | import java.util.concurrent.CountDownLatch;
|
46 | 51 | import java.util.concurrent.TimeUnit;
|
@@ -69,6 +74,8 @@ public class SnapshotClientDocumentationIT extends ESRestHighLevelClientTestCase
|
69 | 74 |
|
70 | 75 | private static final String repositoryName = "test_repository";
|
71 | 76 |
|
| 77 | + private static final String snapshotName = "test_snapshot"; |
| 78 | + |
72 | 79 | public void testSnapshotCreateRepository() throws IOException {
|
73 | 80 | RestHighLevelClient client = highLevelClient();
|
74 | 81 |
|
@@ -360,10 +367,76 @@ public void onFailure(Exception e) {
|
360 | 367 | }
|
361 | 368 | }
|
362 | 369 |
|
| 370 | + public void testSnapshotDeleteSnapshot() throws IOException { |
| 371 | + RestHighLevelClient client = highLevelClient(); |
| 372 | + |
| 373 | + createTestRepositories(); |
| 374 | + createTestSnapshots(); |
| 375 | + |
| 376 | + // tag::delete-snapshot-request |
| 377 | + DeleteSnapshotRequest request = new DeleteSnapshotRequest(repositoryName); |
| 378 | + request.snapshot(snapshotName); |
| 379 | + // end::delete-snapshot-request |
| 380 | + |
| 381 | + // tag::delete-snapshot-request-masterTimeout |
| 382 | + request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); // <1> |
| 383 | + request.masterNodeTimeout("1m"); // <2> |
| 384 | + // end::delete-snapshot-request-masterTimeout |
| 385 | + |
| 386 | + // tag::delete-snapshot-execute |
| 387 | + DeleteSnapshotResponse response = client.snapshot().delete(request, RequestOptions.DEFAULT); |
| 388 | + // end::delete-snapshot-execute |
| 389 | + |
| 390 | + // tag::delete-snapshot-response |
| 391 | + boolean acknowledged = response.isAcknowledged(); // <1> |
| 392 | + // end::delete-snapshot-response |
| 393 | + assertTrue(acknowledged); |
| 394 | + } |
| 395 | + |
| 396 | + public void testSnapshotDeleteSnapshotAsync() throws InterruptedException { |
| 397 | + RestHighLevelClient client = highLevelClient(); |
| 398 | + { |
| 399 | + DeleteSnapshotRequest request = new DeleteSnapshotRequest(); |
| 400 | + |
| 401 | + // tag::delete-snapshot-execute-listener |
| 402 | + ActionListener<DeleteSnapshotResponse> listener = |
| 403 | + new ActionListener<DeleteSnapshotResponse>() { |
| 404 | + @Override |
| 405 | + public void onResponse(DeleteSnapshotResponse deleteSnapshotResponse) { |
| 406 | + // <1> |
| 407 | + } |
| 408 | + |
| 409 | + @Override |
| 410 | + public void onFailure(Exception e) { |
| 411 | + // <2> |
| 412 | + } |
| 413 | + }; |
| 414 | + // end::delete-snapshot-execute-listener |
| 415 | + |
| 416 | + // Replace the empty listener by a blocking listener in test |
| 417 | + final CountDownLatch latch = new CountDownLatch(1); |
| 418 | + listener = new LatchedActionListener<>(listener, latch); |
| 419 | + |
| 420 | + // tag::delete-snapshot-execute-async |
| 421 | + client.snapshot().deleteAsync(request, RequestOptions.DEFAULT, listener); // <1> |
| 422 | + // end::delete-snapshot-execute-async |
| 423 | + |
| 424 | + assertTrue(latch.await(30L, TimeUnit.SECONDS)); |
| 425 | + } |
| 426 | + } |
| 427 | + |
363 | 428 | private void createTestRepositories() throws IOException {
|
364 | 429 | PutRepositoryRequest request = new PutRepositoryRequest(repositoryName);
|
365 | 430 | request.type(FsRepository.TYPE);
|
366 | 431 | request.settings("{\"location\": \".\"}", XContentType.JSON);
|
367 | 432 | assertTrue(highLevelClient().snapshot().createRepository(request, RequestOptions.DEFAULT).isAcknowledged());
|
368 | 433 | }
|
| 434 | + |
| 435 | + private void createTestSnapshots() throws IOException { |
| 436 | + Request createSnapshot = new Request("put", String.format(Locale.ROOT, "_snapshot/%s/%s", repositoryName, snapshotName)); |
| 437 | + createSnapshot.addParameter("wait_for_completion", "true"); |
| 438 | + Response response = highLevelClient().getLowLevelClient().performRequest(createSnapshot); |
| 439 | + // check that the request went ok without parsing JSON here. When using the high level client, check acknowledgement instead. |
| 440 | + assertEquals(200, response.getStatusLine().getStatusCode()); |
| 441 | + } |
369 | 442 | }
|
0 commit comments