Search in sources :

Example 6 with AliasMetadata

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

the class GetIndexTemplatesResponseTests method testParsingFromEsResponse.

public void testParsingFromEsResponse() throws IOException {
    for (int runs = 0; runs < 20; runs++) {
        org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse esResponse = new org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse(new ArrayList<>());
        XContentType xContentType = randomFrom(XContentType.values());
        int numTemplates = randomIntBetween(0, 32);
        for (int i = 0; i < numTemplates; i++) {
            org.opensearch.cluster.metadata.IndexTemplateMetadata.Builder esIMD = new org.opensearch.cluster.metadata.IndexTemplateMetadata.Builder(String.format(Locale.ROOT, "%02d ", i) + randomAlphaOfLength(4));
            esIMD.patterns(Arrays.asList(generateRandomStringArray(32, 4, false, false)));
            esIMD.settings(randomIndexSettings());
            esIMD.putMapping("_doc", new CompressedXContent(BytesReference.bytes(randomMapping("_doc", xContentType))));
            int numAliases = randomIntBetween(0, 8);
            for (int j = 0; j < numAliases; j++) {
                esIMD.putAlias(randomAliasMetadata(String.format(Locale.ROOT, "%02d ", j) + randomAlphaOfLength(4)));
            }
            esIMD.order(randomIntBetween(0, Integer.MAX_VALUE));
            esIMD.version(randomIntBetween(0, Integer.MAX_VALUE));
            esResponse.getIndexTemplates().add(esIMD.build());
        }
        XContentBuilder xContentBuilder = XContentBuilder.builder(xContentType.xContent());
        esResponse.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
        try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(xContentBuilder), xContentType)) {
            GetIndexTemplatesResponse response = GetIndexTemplatesResponse.fromXContent(parser);
            assertThat(response.getIndexTemplates().size(), equalTo(numTemplates));
            response.getIndexTemplates().sort(Comparator.comparing(IndexTemplateMetadata::name));
            for (int i = 0; i < numTemplates; i++) {
                org.opensearch.cluster.metadata.IndexTemplateMetadata esIMD = esResponse.getIndexTemplates().get(i);
                IndexTemplateMetadata result = response.getIndexTemplates().get(i);
                assertThat(result.patterns(), equalTo(esIMD.patterns()));
                assertThat(result.settings(), equalTo(esIMD.settings()));
                assertThat(result.order(), equalTo(esIMD.order()));
                assertThat(result.version(), equalTo(esIMD.version()));
                assertThat(esIMD.mappings().size(), equalTo(1));
                BytesReference mappingSource = esIMD.mappings().valuesIt().next().uncompressed();
                Map<String, Object> expectedMapping = XContentHelper.convertToMap(mappingSource, true, xContentBuilder.contentType()).v2();
                assertThat(result.mappings().sourceAsMap(), equalTo(expectedMapping.get("_doc")));
                assertThat(result.aliases().size(), equalTo(esIMD.aliases().size()));
                List<AliasMetadata> expectedAliases = Arrays.stream(esIMD.aliases().values().toArray(AliasMetadata.class)).sorted(Comparator.comparing(AliasMetadata::alias)).collect(Collectors.toList());
                List<AliasMetadata> actualAliases = Arrays.stream(result.aliases().values().toArray(AliasMetadata.class)).sorted(Comparator.comparing(AliasMetadata::alias)).collect(Collectors.toList());
                for (int j = 0; j < result.aliases().size(); j++) {
                    assertThat(actualAliases.get(j), equalTo(expectedAliases.get(j)));
                }
            }
        }
    }
}
Also used : XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentType(org.opensearch.common.xcontent.XContentType) CompressedXContent(org.opensearch.common.compress.CompressedXContent) BytesReference(org.opensearch.common.bytes.BytesReference) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 7 with AliasMetadata

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

the class GetAliasesResponseTests method mutateAliases.

private static ImmutableOpenMap<String, List<AliasMetadata>> mutateAliases(ImmutableOpenMap<String, List<AliasMetadata>> aliases) {
    if (aliases.isEmpty()) {
        return createIndicesAliasesMap(1, 3).build();
    }
    if (randomBoolean()) {
        ImmutableOpenMap.Builder<String, List<AliasMetadata>> builder = ImmutableOpenMap.builder(aliases);
        ImmutableOpenMap<String, List<AliasMetadata>> list = createIndicesAliasesMap(1, 2).build();
        list.forEach(e -> builder.put(e.key, e.value));
        return builder.build();
    }
    Set<String> indices = new HashSet<>();
    Iterator<String> keys = aliases.keysIt();
    while (keys.hasNext()) {
        indices.add(keys.next());
    }
    List<String> indicesToBeModified = randomSubsetOf(randomIntBetween(1, indices.size()), indices);
    ImmutableOpenMap.Builder<String, List<AliasMetadata>> builder = ImmutableOpenMap.builder();
    for (String index : indices) {
        List<AliasMetadata> list = new ArrayList<>(aliases.get(index));
        if (indicesToBeModified.contains(index)) {
            if (randomBoolean() || list.isEmpty()) {
                list.add(createAliasMetadata());
            } else {
                int aliasIndex = randomInt(list.size() - 1);
                AliasMetadata aliasMetadata = list.get(aliasIndex);
                list.add(aliasIndex, mutateAliasMetadata(aliasMetadata));
            }
        }
        builder.put(index, list);
    }
    return builder.build();
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) ArrayList(java.util.ArrayList) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 8 with AliasMetadata

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

the class TransportGetAliasesActionTests method testDeprecationWarningEmittedForTotalWildcard.

public void testDeprecationWarningEmittedForTotalWildcard() {
    ClusterState state = systemIndexTestClusterState();
    GetAliasesRequest request = new GetAliasesRequest();
    ImmutableOpenMap<String, List<AliasMetadata>> aliases = ImmutableOpenMap.<String, List<AliasMetadata>>builder().fPut(".b", Collections.singletonList(new AliasMetadata.Builder(".y").build())).fPut("c", Collections.singletonList(new AliasMetadata.Builder("d").build())).build();
    final String[] concreteIndices = { "a", ".b", "c" };
    assertEquals(state.metadata().findAliases(request, concreteIndices), aliases);
    ImmutableOpenMap<String, List<AliasMetadata>> result = TransportGetAliasesAction.postProcess(request, concreteIndices, aliases, state, false, EMPTY_SYSTEM_INDICES);
    assertThat(result.size(), equalTo(3));
    assertThat(result.get("a").size(), equalTo(0));
    assertThat(result.get(".b").size(), equalTo(1));
    assertThat(result.get("c").size(), equalTo(1));
    assertWarnings("this request accesses system indices: [.b], but in a future major version, direct access to system " + "indices will be prevented by default");
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) List(java.util.List)

Example 9 with AliasMetadata

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

the class TransportGetAliasesActionTests method testDeprecationWarningEmittedWhenRequestingNonExistingAliasInSystemPattern.

public void testDeprecationWarningEmittedWhenRequestingNonExistingAliasInSystemPattern() {
    ClusterState state = systemIndexTestClusterState();
    SystemIndices systemIndices = new SystemIndices(Collections.singletonMap(this.getTestName(), Collections.singletonList(new SystemIndexDescriptor(".y", "an index that doesn't exist"))));
    GetAliasesRequest request = new GetAliasesRequest(".y");
    ImmutableOpenMap<String, List<AliasMetadata>> aliases = ImmutableOpenMap.<String, List<AliasMetadata>>builder().build();
    final String[] concreteIndices = {};
    assertEquals(state.metadata().findAliases(request, concreteIndices), aliases);
    ImmutableOpenMap<String, List<AliasMetadata>> result = TransportGetAliasesAction.postProcess(request, concreteIndices, aliases, state, false, systemIndices);
    assertThat(result.size(), equalTo(0));
    assertWarnings("this request accesses aliases with names reserved for system indices: [.y], but in a future major version, direct" + "access to system indices and their aliases will not be allowed");
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) SystemIndexDescriptor(org.opensearch.indices.SystemIndexDescriptor) List(java.util.List) SystemIndices(org.opensearch.indices.SystemIndices)

Example 10 with AliasMetadata

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

the class RestAliasAction method buildTable.

private Table buildTable(RestRequest request, GetAliasesResponse response) {
    Table table = getTableWithHeader(request);
    for (ObjectObjectCursor<String, List<AliasMetadata>> cursor : response.getAliases()) {
        String indexName = cursor.key;
        for (AliasMetadata aliasMetadata : cursor.value) {
            table.startRow();
            table.addCell(aliasMetadata.alias());
            table.addCell(indexName);
            table.addCell(aliasMetadata.filteringRequired() ? "*" : "-");
            String indexRouting = Strings.hasLength(aliasMetadata.indexRouting()) ? aliasMetadata.indexRouting() : "-";
            table.addCell(indexRouting);
            String searchRouting = Strings.hasLength(aliasMetadata.searchRouting()) ? aliasMetadata.searchRouting() : "-";
            table.addCell(searchRouting);
            String isWriteIndex = aliasMetadata.writeIndex() == null ? "-" : aliasMetadata.writeIndex().toString();
            table.addCell(isWriteIndex);
            table.endRow();
        }
    }
    return table;
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) Table(org.opensearch.common.Table) Collections.unmodifiableList(java.util.Collections.unmodifiableList) List(java.util.List) Arrays.asList(java.util.Arrays.asList)

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