diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/TransportGetRepositoriesAction.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/TransportGetRepositoriesAction.java index d09c73e17b2a0..39d9cacbda39d 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/TransportGetRepositoriesAction.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/TransportGetRepositoriesAction.java @@ -31,6 +31,7 @@ import org.elasticsearch.cluster.metadata.RepositoriesMetaData; import org.elasticsearch.cluster.metadata.RepositoryMetaData; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.repositories.RepositoryMissingException; import org.elasticsearch.threadpool.ThreadPool; @@ -38,7 +39,9 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; /** * Transport action for get repositories operation @@ -78,8 +81,20 @@ protected void masterOperation(final GetRepositoriesRequest request, ClusterStat } } else { if (repositories != null) { + Set repositoriesToGet = new LinkedHashSet<>(); // to keep insertion order + for (String repositoryOrPattern : request.repositories()) { + if (Regex.isSimpleMatchPattern(repositoryOrPattern) == false) { + repositoriesToGet.add(repositoryOrPattern); + } else { + for (RepositoryMetaData repository : repositories.repositories()) { + if (Regex.simpleMatch(repositoryOrPattern, repository.name())) { + repositoriesToGet.add(repository.name()); + } + } + } + } List repositoryListBuilder = new ArrayList<>(); - for (String repository : request.repositories()) { + for (String repository : repositoriesToGet) { RepositoryMetaData repositoryMetaData = repositories.repository(repository); if (repositoryMetaData == null) { listener.onFailure(new RepositoryMissingException(repository)); diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java index 7bfb0e8dc263c..478146de357b6 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java @@ -29,6 +29,7 @@ import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.SnapshotId; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.snapshots.Snapshot; import org.elasticsearch.snapshots.SnapshotInfo; @@ -38,7 +39,9 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; /** * Transport Action for get snapshots operation @@ -84,8 +87,24 @@ protected void masterOperation(final GetSnapshotsRequest request, ClusterState s snapshotInfoBuilder.add(new SnapshotInfo(snapshot)); } } else { - for (int i = 0; i < request.snapshots().length; i++) { - SnapshotId snapshotId = new SnapshotId(request.repository(), request.snapshots()[i]); + Set snapshotsToGet = new LinkedHashSet<>(); // to keep insertion order + List snapshots = null; + for (String snapshotOrPattern : request.snapshots()) { + if (Regex.isSimpleMatchPattern(snapshotOrPattern) == false) { + snapshotsToGet.add(snapshotOrPattern); + } else { + if (snapshots == null) { // lazily load snapshots + snapshots = snapshotsService.snapshots(request.repository(), request.ignoreUnavailable()); + } + for (Snapshot snapshot : snapshots) { + if (Regex.simpleMatch(snapshotOrPattern, snapshot.name())) { + snapshotsToGet.add(snapshot.name()); + } + } + } + } + for (String snapshot : snapshotsToGet) { + SnapshotId snapshotId = new SnapshotId(request.repository(), snapshot); snapshotInfoBuilder.add(new SnapshotInfo(snapshotsService.snapshot(snapshotId))); } } diff --git a/core/src/test/java/org/elasticsearch/snapshots/RepositoriesIT.java b/core/src/test/java/org/elasticsearch/snapshots/RepositoriesIT.java index 0bfc76f4d90ab..b0de06138a817 100644 --- a/core/src/test/java/org/elasticsearch/snapshots/RepositoriesIT.java +++ b/core/src/test/java/org/elasticsearch/snapshots/RepositoriesIT.java @@ -94,7 +94,8 @@ public void testRepositoryCreation() throws Exception { assertThat(repositoriesMetaData.repository("test-repo-2").type(), equalTo("fs")); logger.info("--> check that both repositories can be retrieved by getRepositories query"); - GetRepositoriesResponse repositoriesResponse = client.admin().cluster().prepareGetRepositories().get(); + GetRepositoriesResponse repositoriesResponse = client.admin().cluster() + .prepareGetRepositories(randomFrom("_all", "*", "test-repo-*")).get(); assertThat(repositoriesResponse.repositories().size(), equalTo(2)); assertThat(findRepository(repositoriesResponse.repositories(), "test-repo-1"), notNullValue()); assertThat(findRepository(repositoriesResponse.repositories(), "test-repo-2"), notNullValue()); diff --git a/core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java b/core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java index afbdf9d48d5d0..57a22c0dd15e3 100644 --- a/core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java +++ b/core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java @@ -149,7 +149,10 @@ public void testBasicWorkFlow() throws Exception { assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0)); assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards())); - SnapshotInfo snapshotInfo = client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").get().getSnapshots().get(0); + List snapshotInfos = client.admin().cluster().prepareGetSnapshots("test-repo") + .setSnapshots(randomFrom("test-snap", "_all", "*", "*-snap", "test*")).get().getSnapshots(); + assertThat(snapshotInfos.size(), equalTo(1)); + SnapshotInfo snapshotInfo = snapshotInfos.get(0); assertThat(snapshotInfo.state(), equalTo(SnapshotState.SUCCESS)); assertThat(snapshotInfo.version(), equalTo(Version.CURRENT)); diff --git a/docs/reference/modules/snapshots.asciidoc b/docs/reference/modules/snapshots.asciidoc index b6371dc36a781..dbb9f395402c7 100644 --- a/docs/reference/modules/snapshots.asciidoc +++ b/docs/reference/modules/snapshots.asciidoc @@ -45,6 +45,15 @@ which returns: } ----------------------------------- +Information about multiple repositories can be fetched in one go by using a comma-delimited list of repository names. +Star wildcards are supported as well. For example, information about repositories that start with `repo` or that contain `backup` +can be obtained using the following command: + +[source,js] +----------------------------------- +GET /_snapshot/repo*,*backup* +----------------------------------- + If a repository name is not specified, or `_all` is used as repository name Elasticsearch will return information about all repositories currently registered in the cluster: @@ -251,6 +260,14 @@ GET /_snapshot/my_backup/snapshot_1 ----------------------------------- // AUTOSENSE +Similar as for repositories, information about multiple snapshots can be queried in one go, supporting wildcards as well: + +[source,sh] +----------------------------------- +GET /_snapshot/my_backup/snapshot_*,some_other_snapshot +----------------------------------- +// AUTOSENSE + All snapshots currently stored in the repository can be listed using the following command: [source,sh]