Search in sources :

Example 11 with AliasMetadata

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

the class RestGetAliasesAction method buildRestResponse.

static RestResponse buildRestResponse(boolean aliasesExplicitlyRequested, String[] requestedAliases, ImmutableOpenMap<String, List<AliasMetadata>> responseAliasMap, XContentBuilder builder) throws Exception {
    final Set<String> indicesToDisplay = new HashSet<>();
    final Set<String> returnedAliasNames = new HashSet<>();
    for (final ObjectObjectCursor<String, List<AliasMetadata>> cursor : responseAliasMap) {
        for (final AliasMetadata aliasMetadata : cursor.value) {
            if (aliasesExplicitlyRequested) {
                // only display indices that have aliases
                indicesToDisplay.add(cursor.key);
            }
            returnedAliasNames.add(aliasMetadata.alias());
        }
    }
    // compute explicitly requested aliases that have are not returned in the result
    final SortedSet<String> missingAliases = new TreeSet<>();
    // first wildcard index, leading "-" as an alias name after this index means
    // that it is an exclusion
    int firstWildcardIndex = requestedAliases.length;
    for (int i = 0; i < requestedAliases.length; i++) {
        if (Regex.isSimpleMatchPattern(requestedAliases[i])) {
            firstWildcardIndex = i;
            break;
        }
    }
    for (int i = 0; i < requestedAliases.length; i++) {
        if (Metadata.ALL.equals(requestedAliases[i]) || Regex.isSimpleMatchPattern(requestedAliases[i]) || (i > firstWildcardIndex && requestedAliases[i].charAt(0) == '-')) {
            // only explicitly requested aliases will be called out as missing (404)
            continue;
        }
        // check if aliases[i] is subsequently excluded
        int j = Math.max(i + 1, firstWildcardIndex);
        for (; j < requestedAliases.length; j++) {
            if (requestedAliases[j].charAt(0) == '-') {
                // this is an exclude pattern
                if (Regex.simpleMatch(requestedAliases[j].substring(1), requestedAliases[i]) || Metadata.ALL.equals(requestedAliases[j].substring(1))) {
                    // aliases[i] is excluded by aliases[j]
                    break;
                }
            }
        }
        if (j == requestedAliases.length) {
            // explicitly requested aliases[i] is not excluded by any subsequent "-" wildcard in expression
            if (false == returnedAliasNames.contains(requestedAliases[i])) {
                // aliases[i] is not in the result set
                missingAliases.add(requestedAliases[i]);
            }
        }
    }
    final RestStatus status;
    builder.startObject();
    {
        if (missingAliases.isEmpty()) {
            status = RestStatus.OK;
        } else {
            status = RestStatus.NOT_FOUND;
            final String message;
            if (missingAliases.size() == 1) {
                message = String.format(Locale.ROOT, "alias [%s] missing", Strings.collectionToCommaDelimitedString(missingAliases));
            } else {
                message = String.format(Locale.ROOT, "aliases [%s] missing", Strings.collectionToCommaDelimitedString(missingAliases));
            }
            builder.field("error", message);
            builder.field("status", status.getStatus());
        }
        for (final ObjectObjectCursor<String, List<AliasMetadata>> entry : responseAliasMap) {
            if (aliasesExplicitlyRequested == false || (aliasesExplicitlyRequested && indicesToDisplay.contains(entry.key))) {
                builder.startObject(entry.key);
                {
                    builder.startObject("aliases");
                    {
                        for (final AliasMetadata alias : entry.value) {
                            AliasMetadata.Builder.toXContent(alias, builder, ToXContent.EMPTY_PARAMS);
                        }
                    }
                    builder.endObject();
                }
                builder.endObject();
            }
        }
    }
    builder.endObject();
    return new BytesRestResponse(status, builder);
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) RestStatus(org.opensearch.rest.RestStatus) TreeSet(java.util.TreeSet) BytesRestResponse(org.opensearch.rest.BytesRestResponse) Collections.unmodifiableList(java.util.Collections.unmodifiableList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) HashSet(java.util.HashSet)

Example 12 with AliasMetadata

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

the class PutMappingRequestTests method addAliases.

/**
 * Adds aliases to the supplied ClusterState instance. The aliases parameter takes of list of tuples of aliasName
 * to the alias's indices. The alias's indices are a tuple of index name and a flag indicating whether the alias
 * is a write alias for that index. See usage examples above.
 */
private static ClusterState addAliases(ClusterState cs, List<Tuple<String, List<Tuple<String, Boolean>>>> aliases) {
    Metadata.Builder builder = Metadata.builder(cs.metadata());
    for (Tuple<String, List<Tuple<String, Boolean>>> alias : aliases) {
        for (Tuple<String, Boolean> index : alias.v2()) {
            IndexMetadata im = builder.get(index.v1());
            AliasMetadata newAliasMd = AliasMetadata.newAliasMetadataBuilder(alias.v1()).writeIndex(index.v2()).build();
            builder.put(IndexMetadata.builder(im).putAlias(newAliasMd));
        }
    }
    return ClusterState.builder(cs).metadata(builder.build()).build();
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) List(java.util.List) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 13 with AliasMetadata

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

the class MetadataRolloverServiceTests method testHiddenAffectsResolvedV2ComponentTemplates.

public void testHiddenAffectsResolvedV2ComponentTemplates() {
    Map<String, AliasMetadata> aliases = new HashMap<>();
    aliases.put("foo-write", AliasMetadata.builder("foo-write").build());
    aliases.put("bar-write", AliasMetadata.builder("bar-write").writeIndex(randomBoolean()).build());
    final ComponentTemplate ct = new ComponentTemplate(new Template(null, null, aliases), null, null);
    final ComposableIndexTemplate template = new ComposableIndexTemplate(Collections.singletonList("*"), null, Collections.singletonList("ct"), null, null, null, null);
    final Metadata metadata = Metadata.builder().put(createMetadata(randomAlphaOfLengthBetween(5, 7)), false).put("ct", ct).put("test-template", template).build();
    String indexName = randomFrom("foo-123", "bar-xyz");
    String aliasName = randomFrom("foo-write", "bar-write");
    // hidden shouldn't throw
    MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, Boolean.TRUE);
    // not hidden will throw
    final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, randomFrom(Boolean.FALSE, null)));
    assertThat(ex.getMessage(), containsString("index template [test-template]"));
}
Also used : ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) HashMap(java.util.HashMap) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) Matchers.containsString(org.hamcrest.Matchers.containsString) ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) Template(org.opensearch.cluster.metadata.Template)

Example 14 with AliasMetadata

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

the class MetadataRolloverServiceTests method testRejectDuplicateAliasV2UsingComponentTemplates.

public void testRejectDuplicateAliasV2UsingComponentTemplates() {
    Map<String, AliasMetadata> aliases = new HashMap<>();
    aliases.put("foo-write", AliasMetadata.builder("foo-write").build());
    aliases.put("bar-write", AliasMetadata.builder("bar-write").writeIndex(randomBoolean()).build());
    final ComponentTemplate ct = new ComponentTemplate(new Template(null, null, aliases), null, null);
    final ComposableIndexTemplate template = new ComposableIndexTemplate(Arrays.asList("foo-*", "bar-*"), null, Collections.singletonList("ct"), null, null, null, null);
    final Metadata metadata = Metadata.builder().put(createMetadata(randomAlphaOfLengthBetween(5, 7)), false).put("ct", ct).put("test-template", template).build();
    String indexName = randomFrom("foo-123", "bar-xyz");
    String aliasName = randomFrom("foo-write", "bar-write");
    final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, randomBoolean()));
    assertThat(ex.getMessage(), containsString("index template [test-template]"));
}
Also used : ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) HashMap(java.util.HashMap) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) Matchers.containsString(org.hamcrest.Matchers.containsString) ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) Template(org.opensearch.cluster.metadata.Template)

Example 15 with AliasMetadata

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

the class ValidateIndicesAliasesRequestIT method testAllowed.

public void testAllowed() {
    final Settings settings = Settings.builder().putList(IndicesAliasesPlugin.ALLOWED_ORIGINS_SETTING.getKey(), Collections.singletonList("allowed")).build();
    createIndex("index", settings);
    final IndicesAliasesRequest request = new IndicesAliasesRequest().origin("allowed");
    request.addAliasAction(IndicesAliasesRequest.AliasActions.add().index("index").alias("alias"));
    assertAcked(client().admin().indices().aliases(request).actionGet());
    final GetAliasesResponse response = client().admin().indices().getAliases(new GetAliasesRequest("alias")).actionGet();
    assertThat(response.getAliases().keys().size(), equalTo(1));
    assertThat(response.getAliases().keys().iterator().next().value, equalTo("index"));
    final List<AliasMetadata> aliasMetadata = response.getAliases().get("index");
    assertThat(aliasMetadata, hasSize(1));
    assertThat(aliasMetadata.get(0).alias(), equalTo("alias"));
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) GetAliasesResponse(org.opensearch.action.admin.indices.alias.get.GetAliasesResponse) GetAliasesRequest(org.opensearch.action.admin.indices.alias.get.GetAliasesRequest) Settings(org.opensearch.common.settings.Settings)

Aggregations

AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)52 List (java.util.List)21 Settings (org.opensearch.common.settings.Settings)16 ArrayList (java.util.ArrayList)13 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)13 ImmutableOpenMap (org.opensearch.common.collect.ImmutableOpenMap)12 Template (org.opensearch.cluster.metadata.Template)10 HashMap (java.util.HashMap)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 MappingMetadata (org.opensearch.cluster.metadata.MappingMetadata)8 Metadata (org.opensearch.cluster.metadata.Metadata)8 CompressedXContent (org.opensearch.common.compress.CompressedXContent)8 IOException (java.io.IOException)7 ComponentTemplate (org.opensearch.cluster.metadata.ComponentTemplate)7 ComposableIndexTemplate (org.opensearch.cluster.metadata.ComposableIndexTemplate)7 IndexTemplateMetadata (org.opensearch.cluster.metadata.IndexTemplateMetadata)7 HashSet (java.util.HashSet)6 Map (java.util.Map)6 ActionListener (org.opensearch.action.ActionListener)6 GetAliasesRequest (org.opensearch.action.admin.indices.alias.get.GetAliasesRequest)6