Search in sources :

Example 1 with AliasMetadata

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

the class GetAliasesResponse method parseAliases.

private static Set<AliasMetadata> parseAliases(XContentParser parser) throws IOException {
    Set<AliasMetadata> aliases = new HashSet<>();
    Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != Token.END_OBJECT) {
        if (token == Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == Token.START_OBJECT) {
            if ("aliases".equals(currentFieldName)) {
                while (parser.nextToken() != Token.END_OBJECT) {
                    AliasMetadata fromXContent = AliasMetadata.Builder.fromXContent(parser);
                    aliases.add(fromXContent);
                }
            } else {
                parser.skipChildren();
            }
        } else if (token == Token.START_ARRAY) {
            parser.skipChildren();
        }
    }
    return aliases;
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) XContentParserUtils.ensureExpectedToken(org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken) Token(org.opensearch.common.xcontent.XContentParser.Token) HashSet(java.util.HashSet)

Example 2 with AliasMetadata

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

the class IndicesClientIT method testSimulateIndexTemplate.

public void testSimulateIndexTemplate() 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, org.opensearch.common.collect.Map.of("alias", alias));
    List<String> pattern = org.opensearch.common.collect.List.of("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));
    SimulateIndexTemplateRequest simulateIndexTemplateRequest = new SimulateIndexTemplateRequest("pattern");
    AliasMetadata simulationAlias = AliasMetadata.builder("simulation-alias").writeIndex(true).build();
    ComposableIndexTemplate simulationTemplate = new ComposableIndexTemplate(pattern, new Template(null, null, org.opensearch.common.collect.Map.of("simulation-alias", simulationAlias)), Collections.emptyList(), 2L, 1L, new HashMap<>(), null);
    PutComposableIndexTemplateRequest newIndexTemplateReq = new PutComposableIndexTemplateRequest().name("used-for-simulation").create(true).indexTemplate(indexTemplate);
    newIndexTemplateReq.indexTemplate(simulationTemplate);
    simulateIndexTemplateRequest.indexTemplateV2Request(newIndexTemplateReq);
    SimulateIndexTemplateResponse simulateResponse = execute(simulateIndexTemplateRequest, highLevelClient().indices()::simulateIndexTemplate, highLevelClient().indices()::simulateIndexTemplateAsync);
    Map<String, AliasMetadata> aliases = simulateResponse.resolvedTemplate().aliases();
    assertThat(aliases, is(notNullValue()));
    assertThat("the template we provided for the simulation has a higher priority than the one in the system", aliases.get("simulation-alias"), is(notNullValue()));
    assertThat(aliases.get("simulation-alias").getAlias(), is("simulation-alias"));
    assertThat("existing template overlaps the higher priority template we provided for the simulation", simulateResponse.overlappingTemplates().get("my-template").get(0), is("pattern"));
}
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) ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) Template(org.opensearch.cluster.metadata.Template) SimulateIndexTemplateRequest(org.opensearch.client.indices.SimulateIndexTemplateRequest) CompressedXContent(org.opensearch.common.compress.CompressedXContent) SimulateIndexTemplateResponse(org.opensearch.client.indices.SimulateIndexTemplateResponse) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Example 3 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 4 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 5 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)

Aggregations

AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)45 List (java.util.List)17 Settings (org.opensearch.common.settings.Settings)15 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)10 Template (org.opensearch.cluster.metadata.Template)10 ImmutableOpenMap (org.opensearch.common.collect.ImmutableOpenMap)10 Matchers.containsString (org.hamcrest.Matchers.containsString)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 ComponentTemplate (org.opensearch.cluster.metadata.ComponentTemplate)7 ComposableIndexTemplate (org.opensearch.cluster.metadata.ComposableIndexTemplate)7 IndexTemplateMetadata (org.opensearch.cluster.metadata.IndexTemplateMetadata)7 MappingMetadata (org.opensearch.cluster.metadata.MappingMetadata)7 Metadata (org.opensearch.cluster.metadata.Metadata)7 CompressedXContent (org.opensearch.common.compress.CompressedXContent)7 ClusterState (org.opensearch.cluster.ClusterState)6 IOException (java.io.IOException)5 Map (java.util.Map)5 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)5 ActionListener (org.opensearch.action.ActionListener)4