Search in sources :

Example 21 with AliasMetadata

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

the class IndicesClientIT method testIndexTemplates.

public void testIndexTemplates() throws Exception {
    String templateName = "my-template";
    Settings settings = Settings.builder().put("index.number_of_shards", 1).build();
    CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"host_name\":{\"type\":\"keyword\"}}}");
    AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build();
    Template template = new Template(settings, mappings, Collections.singletonMap("alias", alias));
    List<String> pattern = Collections.singletonList("pattern");
    ComposableIndexTemplate indexTemplate = new ComposableIndexTemplate(pattern, template, Collections.emptyList(), 1L, 1L, new HashMap<>(), null);
    PutComposableIndexTemplateRequest putComposableIndexTemplateRequest = new PutComposableIndexTemplateRequest().name(templateName).create(true).indexTemplate(indexTemplate);
    AcknowledgedResponse response = execute(putComposableIndexTemplateRequest, highLevelClient().indices()::putIndexTemplate, highLevelClient().indices()::putIndexTemplateAsync);
    assertThat(response.isAcknowledged(), equalTo(true));
    ComposableIndexTemplateExistRequest composableIndexTemplateExistRequest = new ComposableIndexTemplateExistRequest(templateName);
    boolean exist = execute(composableIndexTemplateExistRequest, highLevelClient().indices()::existsIndexTemplate, highLevelClient().indices()::existsIndexTemplateAsync);
    assertTrue(exist);
    GetComposableIndexTemplateRequest getComposableIndexTemplateRequest = new GetComposableIndexTemplateRequest(templateName);
    GetComposableIndexTemplatesResponse getResponse = execute(getComposableIndexTemplateRequest, highLevelClient().indices()::getIndexTemplate, highLevelClient().indices()::getIndexTemplateAsync);
    assertThat(getResponse.getIndexTemplates().size(), equalTo(1));
    assertThat(getResponse.getIndexTemplates().containsKey(templateName), equalTo(true));
    assertThat(getResponse.getIndexTemplates().get(templateName), equalTo(indexTemplate));
    DeleteComposableIndexTemplateRequest deleteComposableIndexTemplateRequest = new DeleteComposableIndexTemplateRequest(templateName);
    response = execute(deleteComposableIndexTemplateRequest, highLevelClient().indices()::deleteIndexTemplate, highLevelClient().indices()::deleteIndexTemplateAsync);
    assertThat(response.isAcknowledged(), equalTo(true));
    OpenSearchStatusException statusException = expectThrows(OpenSearchStatusException.class, () -> execute(getComposableIndexTemplateRequest, highLevelClient().indices()::getIndexTemplate, highLevelClient().indices()::getIndexTemplateAsync));
    assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND));
    exist = execute(composableIndexTemplateExistRequest, highLevelClient().indices()::existsIndexTemplate, highLevelClient().indices()::existsIndexTemplateAsync);
    assertFalse(exist);
}
Also used : ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) PutComposableIndexTemplateRequest(org.opensearch.client.indices.PutComposableIndexTemplateRequest) ComposableIndexTemplateExistRequest(org.opensearch.client.indices.ComposableIndexTemplateExistRequest) ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) Template(org.opensearch.cluster.metadata.Template) CompressedXContent(org.opensearch.common.compress.CompressedXContent) GetComposableIndexTemplatesResponse(org.opensearch.client.indices.GetComposableIndexTemplatesResponse) DeleteComposableIndexTemplateRequest(org.opensearch.client.indices.DeleteComposableIndexTemplateRequest) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) GetComposableIndexTemplateRequest(org.opensearch.client.indices.GetComposableIndexTemplateRequest) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 22 with AliasMetadata

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

the class GetAliasesResponseTests method testFromXContentWithMissingAndFoundAlias.

public void testFromXContentWithMissingAndFoundAlias() throws IOException {
    String xContent = "{" + "  \"error\": \"alias [something] missing\"," + "  \"status\": 404," + "  \"index\": {" + "    \"aliases\": {" + "      \"alias\": {}" + "    }" + "  }" + "}";
    final String index = "index";
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) {
        GetAliasesResponse response = GetAliasesResponse.fromXContent(parser);
        assertThat(response.status(), equalTo(RestStatus.NOT_FOUND));
        assertThat(response.getError(), equalTo("alias [something] missing"));
        assertThat(response.getAliases().size(), equalTo(1));
        assertThat(response.getAliases().get(index).size(), equalTo(1));
        AliasMetadata aliasMetadata = response.getAliases().get(index).iterator().next();
        assertThat(aliasMetadata.alias(), equalTo("alias"));
        assertThat(response.getException(), nullValue());
    }
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 23 with AliasMetadata

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

the class GetIndexResponseTests method createServerTestInstance.

@Override
protected org.opensearch.action.admin.indices.get.GetIndexResponse createServerTestInstance(XContentType xContentType) {
    String[] indices = generateRandomStringArray(5, 5, false, false);
    ImmutableOpenMap.Builder<String, MappingMetadata> mappings = ImmutableOpenMap.builder();
    ImmutableOpenMap.Builder<String, List<AliasMetadata>> aliases = ImmutableOpenMap.builder();
    ImmutableOpenMap.Builder<String, Settings> settings = ImmutableOpenMap.builder();
    ImmutableOpenMap.Builder<String, Settings> defaultSettings = ImmutableOpenMap.builder();
    ImmutableOpenMap.Builder<String, String> dataStreams = ImmutableOpenMap.builder();
    IndexScopedSettings indexScopedSettings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS;
    boolean includeDefaults = randomBoolean();
    for (String index : indices) {
        mappings.put(index, createMappingsForIndex());
        List<AliasMetadata> aliasMetadataList = new ArrayList<>();
        int aliasesNum = randomIntBetween(0, 3);
        for (int i = 0; i < aliasesNum; i++) {
            aliasMetadataList.add(GetAliasesResponseTests.createAliasMetadata());
        }
        CollectionUtil.timSort(aliasMetadataList, Comparator.comparing(AliasMetadata::alias));
        aliases.put(index, Collections.unmodifiableList(aliasMetadataList));
        Settings.Builder builder = Settings.builder();
        builder.put(RandomCreateIndexGenerator.randomIndexSettings());
        settings.put(index, builder.build());
        if (includeDefaults) {
            defaultSettings.put(index, indexScopedSettings.diff(settings.get(index), Settings.EMPTY));
        }
        if (randomBoolean()) {
            dataStreams.put(index, randomAlphaOfLength(5).toLowerCase(Locale.ROOT));
        }
    }
    return new org.opensearch.action.admin.indices.get.GetIndexResponse(indices, mappings.build(), aliases.build(), settings.build(), defaultSettings.build(), dataStreams.build());
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) ArrayList(java.util.ArrayList) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) ArrayList(java.util.ArrayList) List(java.util.List) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) Settings(org.opensearch.common.settings.Settings)

Example 24 with AliasMetadata

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

the class GetComponentTemplatesResponseTests method randomAliases.

private static Map<String, AliasMetadata> randomAliases() {
    String aliasName = randomAlphaOfLength(5);
    AliasMetadata aliasMeta = AliasMetadata.builder(aliasName).filter(Collections.singletonMap(randomAlphaOfLength(2), randomAlphaOfLength(2))).routing(randomBoolean() ? null : randomAlphaOfLength(3)).isHidden(randomBoolean() ? null : randomBoolean()).writeIndex(randomBoolean() ? null : randomBoolean()).build();
    return Collections.singletonMap(aliasName, aliasMeta);
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata)

Example 25 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)

Aggregations

AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)51 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 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 CompressedXContent (org.opensearch.common.compress.CompressedXContent)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