Search in sources :

Example 1 with DeleteComponentTemplateRequest

use of org.opensearch.client.indices.DeleteComponentTemplateRequest 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)

Example 2 with DeleteComponentTemplateRequest

use of org.opensearch.client.indices.DeleteComponentTemplateRequest in project OpenSearch by opensearch-project.

the class ClusterClientIT method testComponentTemplates.

public void testComponentTemplates() 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));
    ComponentTemplate componentTemplate = new ComponentTemplate(template, 1L, new HashMap<>());
    PutComponentTemplateRequest putComponentTemplateRequest = new PutComponentTemplateRequest().name(templateName).create(true).componentTemplate(componentTemplate);
    AcknowledgedResponse response = execute(putComponentTemplateRequest, highLevelClient().cluster()::putComponentTemplate, highLevelClient().cluster()::putComponentTemplateAsync);
    assertThat(response.isAcknowledged(), equalTo(true));
    ComponentTemplatesExistRequest componentTemplatesExistRequest = new ComponentTemplatesExistRequest(templateName);
    boolean exist = execute(componentTemplatesExistRequest, highLevelClient().cluster()::existsComponentTemplate, highLevelClient().cluster()::existsComponentTemplateAsync);
    assertTrue(exist);
    GetComponentTemplatesRequest getComponentTemplatesRequest = new GetComponentTemplatesRequest(templateName);
    GetComponentTemplatesResponse getResponse = execute(getComponentTemplatesRequest, highLevelClient().cluster()::getComponentTemplate, highLevelClient().cluster()::getComponentTemplateAsync);
    assertThat(getResponse.getComponentTemplates().size(), equalTo(1));
    assertThat(getResponse.getComponentTemplates().containsKey(templateName), equalTo(true));
    assertThat(getResponse.getComponentTemplates().get(templateName), equalTo(componentTemplate));
    DeleteComponentTemplateRequest deleteComponentTemplateRequest = new DeleteComponentTemplateRequest(templateName);
    response = execute(deleteComponentTemplateRequest, highLevelClient().cluster()::deleteComponentTemplate, highLevelClient().cluster()::deleteComponentTemplateAsync);
    assertThat(response.isAcknowledged(), equalTo(true));
    OpenSearchStatusException statusException = expectThrows(OpenSearchStatusException.class, () -> execute(getComponentTemplatesRequest, highLevelClient().cluster()::getComponentTemplate, highLevelClient().cluster()::getComponentTemplateAsync));
    assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND));
    exist = execute(componentTemplatesExistRequest, highLevelClient().cluster()::existsComponentTemplate, highLevelClient().cluster()::existsComponentTemplateAsync);
    assertFalse(exist);
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) ComponentTemplatesExistRequest(org.opensearch.client.indices.ComponentTemplatesExistRequest) GetComponentTemplatesRequest(org.opensearch.client.indices.GetComponentTemplatesRequest) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) Template(org.opensearch.cluster.metadata.Template) ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) PutComponentTemplateRequest(org.opensearch.client.indices.PutComponentTemplateRequest) DeleteComponentTemplateRequest(org.opensearch.client.indices.DeleteComponentTemplateRequest) CompressedXContent(org.opensearch.common.compress.CompressedXContent) ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) GetComponentTemplatesResponse(org.opensearch.client.indices.GetComponentTemplatesResponse) RecoverySettings(org.opensearch.indices.recovery.RecoverySettings) Settings(org.opensearch.common.settings.Settings) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 3 with DeleteComponentTemplateRequest

use of org.opensearch.client.indices.DeleteComponentTemplateRequest in project OpenSearch by opensearch-project.

the class ClusterRequestConverters method deleteComponentTemplate.

static Request deleteComponentTemplate(DeleteComponentTemplateRequest deleteComponentTemplateRequest) {
    String name = deleteComponentTemplateRequest.getName();
    String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_component_template").addPathPart(name).build();
    Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
    RequestConverters.Params params = new RequestConverters.Params();
    params.withMasterTimeout(deleteComponentTemplateRequest.masterNodeTimeout());
    request.addParameters(params.asMap());
    return request;
}
Also used : ClusterUpdateSettingsRequest(org.opensearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest) PutComponentTemplateRequest(org.opensearch.client.indices.PutComponentTemplateRequest) ComponentTemplatesExistRequest(org.opensearch.client.indices.ComponentTemplatesExistRequest) GetComponentTemplatesRequest(org.opensearch.client.indices.GetComponentTemplatesRequest) ClusterGetSettingsRequest(org.opensearch.action.admin.cluster.settings.ClusterGetSettingsRequest) DeleteComponentTemplateRequest(org.opensearch.client.indices.DeleteComponentTemplateRequest) RemoteInfoRequest(org.opensearch.client.cluster.RemoteInfoRequest) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest)

Aggregations

DeleteComponentTemplateRequest (org.opensearch.client.indices.DeleteComponentTemplateRequest)3 PutComponentTemplateRequest (org.opensearch.client.indices.PutComponentTemplateRequest)3 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)2 ComponentTemplatesExistRequest (org.opensearch.client.indices.ComponentTemplatesExistRequest)2 GetComponentTemplatesRequest (org.opensearch.client.indices.GetComponentTemplatesRequest)2 AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)2 ComponentTemplate (org.opensearch.cluster.metadata.ComponentTemplate)2 Template (org.opensearch.cluster.metadata.Template)2 CompressedXContent (org.opensearch.common.compress.CompressedXContent)2 Settings (org.opensearch.common.settings.Settings)2 RecoverySettings (org.opensearch.indices.recovery.RecoverySettings)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 OpenSearchStatusException (org.opensearch.OpenSearchStatusException)1 ActionListener (org.opensearch.action.ActionListener)1 LatchedActionListener (org.opensearch.action.LatchedActionListener)1 ClusterHealthRequest (org.opensearch.action.admin.cluster.health.ClusterHealthRequest)1 ClusterGetSettingsRequest (org.opensearch.action.admin.cluster.settings.ClusterGetSettingsRequest)1