Search in sources :

Example 1 with ComponentTemplate

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

the class ClusterClientDocumentationIT method testGetComponentTemplates.

public void testGetComponentTemplates() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        Template template = new Template(Settings.builder().put("index.number_of_replicas", 3).build(), null, null);
        ComponentTemplate componentTemplate = new ComponentTemplate(template, null, null);
        PutComponentTemplateRequest putComponentTemplateRequest = new PutComponentTemplateRequest().name("ct1").componentTemplate(componentTemplate);
        client.cluster().putComponentTemplate(putComponentTemplateRequest, RequestOptions.DEFAULT);
        assertTrue(client.cluster().putComponentTemplate(putComponentTemplateRequest, RequestOptions.DEFAULT).isAcknowledged());
    }
    // tag::get-component-templates-request
    // <1>
    GetComponentTemplatesRequest request = new GetComponentTemplatesRequest("ct1");
    // end::get-component-templates-request
    // tag::get-component-templates-request-masterTimeout
    // <1>
    request.setMasterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.setMasterNodeTimeout("1m");
    // end::get-component-templates-request-masterTimeout
    // tag::get-component-templates-execute
    GetComponentTemplatesResponse getTemplatesResponse = client.cluster().getComponentTemplate(request, RequestOptions.DEFAULT);
    // end::get-component-templates-execute
    // tag::get-component-templates-response
    // <1>
    Map<String, ComponentTemplate> templates = getTemplatesResponse.getComponentTemplates();
    // end::get-component-templates-response
    assertThat(templates.size(), is(1));
    assertThat(templates.get("ct1"), is(notNullValue()));
    // tag::get-component-templates-execute-listener
    ActionListener<GetComponentTemplatesResponse> listener = new ActionListener<GetComponentTemplatesResponse>() {

        @Override
        public void onResponse(GetComponentTemplatesResponse response) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::get-component-templates-execute-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::get-component-templates-execute-async
    // <1>
    client.cluster().getComponentTemplateAsync(request, RequestOptions.DEFAULT, listener);
    // end::get-component-templates-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : GetComponentTemplatesRequest(org.opensearch.client.indices.GetComponentTemplatesRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) Template(org.opensearch.cluster.metadata.Template) ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) PutComponentTemplateRequest(org.opensearch.client.indices.PutComponentTemplateRequest) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) GetComponentTemplatesResponse(org.opensearch.client.indices.GetComponentTemplatesResponse)

Example 2 with ComponentTemplate

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

the class GetComponentTemplatesResponseTests method randomComponentTemplate.

private static ComponentTemplate randomComponentTemplate() {
    Template template = randomTemplate();
    Map<String, Object> meta = null;
    if (randomBoolean()) {
        meta = randomMeta();
    }
    return new ComponentTemplate(template, randomBoolean() ? null : randomNonNegativeLong(), meta);
}
Also used : ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) Template(org.opensearch.cluster.metadata.Template)

Example 3 with ComponentTemplate

use of org.opensearch.cluster.metadata.ComponentTemplate 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 4 with ComponentTemplate

use of org.opensearch.cluster.metadata.ComponentTemplate 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 5 with ComponentTemplate

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

the class ClusterClientDocumentationIT method testDeleteComponentTemplate.

public void testDeleteComponentTemplate() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        PutComponentTemplateRequest request = new PutComponentTemplateRequest().name("ct1");
        Settings settings = Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1).build();
        String mappingJson = "{\n" + "  \"properties\": {\n" + "    \"message\": {\n" + "      \"type\": \"text\"\n" + "    }\n" + "  }\n" + "}";
        AliasMetadata twitterAlias = AliasMetadata.builder("twitter_alias").build();
        Map<String, AliasMetadata> aliases = new HashMap<>();
        aliases.put("twitter_alias", twitterAlias);
        Template template = new Template(settings, new CompressedXContent(mappingJson), aliases);
        request.componentTemplate(new ComponentTemplate(template, null, null));
        assertTrue(client.cluster().putComponentTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
    }
    // tag::delete-component-template-request
    // <1>
    DeleteComponentTemplateRequest deleteRequest = new DeleteComponentTemplateRequest("ct1");
    // end::delete-component-template-request
    // tag::delete-component-template-request-masterTimeout
    // <1>
    deleteRequest.setMasterTimeout(TimeValue.timeValueMinutes(1));
    // end::delete-component-template-request-masterTimeout
    // tag::delete-component-template-execute
    AcknowledgedResponse deleteTemplateAcknowledge = client.cluster().deleteComponentTemplate(deleteRequest, RequestOptions.DEFAULT);
    // end::delete-component-template-execute
    // tag::delete-component-template-response
    // <1>
    boolean acknowledged = deleteTemplateAcknowledge.isAcknowledged();
    // end::delete-component-template-response
    assertThat(acknowledged, equalTo(true));
    {
        PutComponentTemplateRequest request = new PutComponentTemplateRequest().name("ct1");
        Settings settings = Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1).build();
        Template template = new Template(settings, null, null);
        request.componentTemplate(new ComponentTemplate(template, null, null));
        assertTrue(client.cluster().putComponentTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
    }
    // tag::delete-component-template-execute-listener
    ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() {

        @Override
        public void onResponse(AcknowledgedResponse response) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::delete-component-template-execute-listener
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::delete-component-template-execute-async
    // <1>
    client.cluster().deleteComponentTemplateAsync(deleteRequest, RequestOptions.DEFAULT, listener);
    // end::delete-component-template-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) PutComponentTemplateRequest(org.opensearch.client.indices.PutComponentTemplateRequest) Template(org.opensearch.cluster.metadata.Template) ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) DeleteComponentTemplateRequest(org.opensearch.client.indices.DeleteComponentTemplateRequest) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) CompressedXContent(org.opensearch.common.compress.CompressedXContent) ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) HashMap(java.util.HashMap) Map(java.util.Map) RecoverySettings(org.opensearch.indices.recovery.RecoverySettings) Settings(org.opensearch.common.settings.Settings)

Aggregations

ComponentTemplate (org.opensearch.cluster.metadata.ComponentTemplate)9 Template (org.opensearch.cluster.metadata.Template)8 HashMap (java.util.HashMap)4 AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)4 PutComponentTemplateRequest (org.opensearch.client.indices.PutComponentTemplateRequest)3 ComposableIndexTemplate (org.opensearch.cluster.metadata.ComposableIndexTemplate)3 CompressedXContent (org.opensearch.common.compress.CompressedXContent)3 Settings (org.opensearch.common.settings.Settings)3 IOException (java.io.IOException)2 Map (java.util.Map)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ActionListener (org.opensearch.action.ActionListener)2 LatchedActionListener (org.opensearch.action.LatchedActionListener)2 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)2 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)2 DeleteComponentTemplateRequest (org.opensearch.client.indices.DeleteComponentTemplateRequest)2 GetComponentTemplatesRequest (org.opensearch.client.indices.GetComponentTemplatesRequest)2 GetComponentTemplatesResponse (org.opensearch.client.indices.GetComponentTemplatesResponse)2 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)2