Search in sources :

Example 16 with RepositoriesMetadata

use of org.opensearch.cluster.metadata.RepositoriesMetadata in project OpenSearch by opensearch-project.

the class TransportGetRepositoriesAction method masterOperation.

@Override
protected void masterOperation(final GetRepositoriesRequest request, ClusterState state, final ActionListener<GetRepositoriesResponse> listener) {
    Metadata metadata = state.metadata();
    RepositoriesMetadata repositories = metadata.custom(RepositoriesMetadata.TYPE);
    if (request.repositories().length == 0 || (request.repositories().length == 1 && "_all".equals(request.repositories()[0]))) {
        if (repositories != null) {
            listener.onResponse(new GetRepositoriesResponse(repositories));
        } else {
            listener.onResponse(new GetRepositoriesResponse(new RepositoriesMetadata(Collections.emptyList())));
        }
    } else {
        if (repositories != null) {
            // to keep insertion order
            Set<String> repositoriesToGet = new LinkedHashSet<>();
            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<RepositoryMetadata> repositoryListBuilder = new ArrayList<>();
            for (String repository : repositoriesToGet) {
                RepositoryMetadata repositoryMetadata = repositories.repository(repository);
                if (repositoryMetadata == null) {
                    listener.onFailure(new RepositoryMissingException(repository));
                    return;
                }
                repositoryListBuilder.add(repositoryMetadata);
            }
            listener.onResponse(new GetRepositoriesResponse(new RepositoriesMetadata(repositoryListBuilder)));
        } else {
            listener.onFailure(new RepositoryMissingException(request.repositories()[0]));
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RepositoriesMetadata(org.opensearch.cluster.metadata.RepositoriesMetadata) RepositoryMetadata(org.opensearch.cluster.metadata.RepositoryMetadata) Metadata(org.opensearch.cluster.metadata.Metadata) RepositoriesMetadata(org.opensearch.cluster.metadata.RepositoriesMetadata) RepositoryMetadata(org.opensearch.cluster.metadata.RepositoryMetadata) ArrayList(java.util.ArrayList) RepositoryMissingException(org.opensearch.repositories.RepositoryMissingException)

Aggregations

RepositoriesMetadata (org.opensearch.cluster.metadata.RepositoriesMetadata)16 RepositoryMetadata (org.opensearch.cluster.metadata.RepositoryMetadata)12 ArrayList (java.util.ArrayList)8 ClusterState (org.opensearch.cluster.ClusterState)5 Metadata (org.opensearch.cluster.metadata.Metadata)5 IOException (java.io.IOException)4 Map (java.util.Map)3 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)3 SnapshotsInProgress (org.opensearch.cluster.SnapshotsInProgress)3 Path (java.nio.file.Path)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 LogManager (org.apache.logging.log4j.LogManager)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ClusterService (org.opensearch.cluster.service.ClusterService)2