Search in sources :

Example 1 with GetComponentTemplatesRequest

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

the class ClusterRequestConverters method getComponentTemplates.

static Request getComponentTemplates(GetComponentTemplatesRequest getComponentTemplatesRequest) {
    final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_component_template").addPathPart(getComponentTemplatesRequest.name()).build();
    final Request request = new Request(HttpGet.METHOD_NAME, endpoint);
    final RequestConverters.Params params = new RequestConverters.Params();
    params.withLocal(getComponentTemplatesRequest.isLocal());
    params.withMasterTimeout(getComponentTemplatesRequest.getMasterNodeTimeout());
    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)

Example 2 with GetComponentTemplatesRequest

use of org.opensearch.client.indices.GetComponentTemplatesRequest 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 3 with GetComponentTemplatesRequest

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

Aggregations

GetComponentTemplatesRequest (org.opensearch.client.indices.GetComponentTemplatesRequest)3 PutComponentTemplateRequest (org.opensearch.client.indices.PutComponentTemplateRequest)3 ComponentTemplatesExistRequest (org.opensearch.client.indices.ComponentTemplatesExistRequest)2 DeleteComponentTemplateRequest (org.opensearch.client.indices.DeleteComponentTemplateRequest)2 GetComponentTemplatesResponse (org.opensearch.client.indices.GetComponentTemplatesResponse)2 ComponentTemplate (org.opensearch.cluster.metadata.ComponentTemplate)2 Template (org.opensearch.cluster.metadata.Template)2 IOException (java.io.IOException)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 ClusterUpdateSettingsRequest (org.opensearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest)1 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)1 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)1 RemoteInfoRequest (org.opensearch.client.cluster.RemoteInfoRequest)1 AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)1 CompressedXContent (org.opensearch.common.compress.CompressedXContent)1