From a8d5d3952898669a7d336c9c51e169293acc3d3a Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Thu, 28 Jul 2022 18:02:00 -0700 Subject: [PATCH 01/12] Integrated SDK Client to make request to OpenSearch Signed-off-by: Owais Kazi --- build.gradle | 2 + .../java/org/opensearch/sdk/SDKClient.java | 86 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/main/java/org/opensearch/sdk/SDKClient.java diff --git a/build.gradle b/build.gradle index 03a08d5c..450c1ce5 100644 --- a/build.gradle +++ b/build.gradle @@ -59,6 +59,8 @@ dependencies { implementation "org.opensearch.plugin:transport-netty4-client:3.0.0-SNAPSHOT" implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.17.1' implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.17.1' + implementation 'org.opensearch.client:opensearch-rest-client:2.0.0' + implementation 'org.opensearch.client:opensearch-java:2.0.0' implementation "io.netty:netty-all:4.1.73.Final" testCompileOnly ("junit:junit:4.13.2") { exclude module : 'hamcrest' diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java new file mode 100644 index 00000000..9ed3ce82 --- /dev/null +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -0,0 +1,86 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ +package org.opensearch.sdk; + +import java.io.IOException; + +import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.ssl.SSLContextBuilder; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.opensearch.client.RestClient; +import org.opensearch.client.RestClientBuilder; +import org.opensearch.client.json.jackson.JacksonJsonpMapper; +import org.opensearch.client.opensearch.OpenSearchClient; +import org.opensearch.client.opensearch.indices.CreateIndexRequest; +import org.opensearch.client.opensearch.indices.CreateIndexResponse; +import org.opensearch.client.transport.OpenSearchTransport; +import org.opensearch.client.transport.rest_client.RestClientTransport; + +/** + * This class creates a Client for SDK to make requests to OpenSearch + */ +public class SDKClient { + private final Logger logger = LogManager.getLogger(SDKClient.class); + private OpenSearchClient client; + + /** + * Creates client for SDK + * @throws IOException if client failed + */ + public void createClient() throws IOException { + String endpoint = "localhost"; + String username = "admin"; + String password = "admin"; + String protocol = "http"; + int port = 9200; + RestClient restClient = null; + try { + RestClientBuilder builder = RestClient.builder(new HttpHost(endpoint, port, protocol)); + builder.setStrictDeprecationMode(true); + builder.setHttpClientConfigCallback(httpClientBuilder -> { + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); + try { + return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider) + .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) + .setSSLContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build()); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + + restClient = builder.build(); + + // Create Client + OpenSearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper()); + OpenSearchClient client = new OpenSearchClient(transport); + } finally { + if (restClient != null) { + restClient.close(); + } + } + } + + /** + * Creates index on OpenSearch + * @throws IOException if request failed + */ + public void createIndex(String index) throws IOException { + logger.info("Creating Index on OpenSearch"); + // Create Index + CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(index).build(); + CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest); + logger.info("Created Index on OpenSearch", createIndexResponse); + } +} From f148523bd0ecbf9e23c9d8f96cb69c5aeef55134 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Thu, 28 Jul 2022 18:37:15 -0700 Subject: [PATCH 02/12] Added test and restructure SDK Signed-off-by: Owais Kazi --- .../java/org/opensearch/sdk/SDKClient.java | 17 +-- .../org/opensearch/sdk/FailingTransport.java | 112 ++++++++++++++++++ .../org/opensearch/sdk/TestSDKClient.java | 31 +++++ 3 files changed, 152 insertions(+), 8 deletions(-) create mode 100644 src/test/java/org/opensearch/sdk/FailingTransport.java create mode 100644 src/test/java/org/opensearch/sdk/TestSDKClient.java diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index 9ed3ce82..3b07f598 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -53,8 +53,8 @@ public void createClient() throws IOException { credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); try { return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider) - .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) - .setSSLContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build()); + .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) + .setSSLContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build()); } catch (Exception e) { throw new RuntimeException(e); } @@ -76,11 +76,12 @@ public void createClient() throws IOException { * Creates index on OpenSearch * @throws IOException if request failed */ - public void createIndex(String index) throws IOException { - logger.info("Creating Index on OpenSearch"); - // Create Index - CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(index).build(); - CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest); - logger.info("Created Index on OpenSearch", createIndexResponse); + public CreateIndexResponse createIndex(String index) throws IOException { + logger.info("Creating Index on OpenSearch"); + // Create Index + CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(index).build(); + CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest); + logger.info("Created Index on OpenSearch", createIndexResponse); + return createIndexResponse; } } diff --git a/src/test/java/org/opensearch/sdk/FailingTransport.java b/src/test/java/org/opensearch/sdk/FailingTransport.java new file mode 100644 index 00000000..72cbde16 --- /dev/null +++ b/src/test/java/org/opensearch/sdk/FailingTransport.java @@ -0,0 +1,112 @@ +package org.opensearch.sdk; + +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.jsonb.JsonbJsonpMapper; +import org.opensearch.client.transport.OpenSearchTransport; +import org.opensearch.client.transport.Endpoint; +import org.opensearch.client.transport.TransportException; +import org.opensearch.client.transport.TransportOptions; + +import javax.annotation.Nullable; +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; + +/** + * A transport implementation that always fails. Used for simple doc sections where we just want to check compilation. + */ +public class FailingTransport implements OpenSearchTransport { + + private JsonpMapper mapper = new JsonbJsonpMapper(); + + private TransportOptions options = new TransportOptions() { + @Override + public Collection> headers() { + return Collections.emptyList(); + } + + @Override + public Map queryParameters() { + return Collections.emptyMap(); + } + + @Override + public Function, Boolean> onWarnings() { + return null; + } + + @Override + public Builder toBuilder() { + return null; + } + }; + + @Override + public ResponseT performRequest( + RequestT request, + Endpoint endpoint, + @Nullable TransportOptions options + ) throws IOException { + throw new TransportException("Not implemented"); + } + + @Override + public CompletableFuture performRequestAsync( + RequestT request, + Endpoint endpoint, + @Nullable TransportOptions options + ) { + CompletableFuture future = new CompletableFuture<>(); + future.completeExceptionally(new TransportException("Not implemented")); + return future; + } + + @Override + public JsonpMapper jsonpMapper() { + return mapper; + } + + @Override + public TransportOptions options() { + return options; + } + + @Override + public void close() throws IOException {} +} diff --git a/src/test/java/org/opensearch/sdk/TestSDKClient.java b/src/test/java/org/opensearch/sdk/TestSDKClient.java new file mode 100644 index 00000000..9f026960 --- /dev/null +++ b/src/test/java/org/opensearch/sdk/TestSDKClient.java @@ -0,0 +1,31 @@ +package org.opensearch.sdk; + +import org.junit.jupiter.api.Test; +import org.opensearch.client.opensearch.OpenSearchClient; +import org.opensearch.client.opensearch.indices.Alias; +import org.opensearch.client.opensearch.indices.CreateIndexRequest; +import org.opensearch.client.transport.OpenSearchTransport; +import org.opensearch.client.transport.TransportException; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class TestSDKClient { + private OpenSearchTransport transport = new FailingTransport(); + + @Test + public void testCreateIndex() throws Exception { + OpenSearchClient client = new OpenSearchClient(transport); + + //tag::builders + assertThrows(TransportException.class, () -> client.indices().create( + new CreateIndexRequest.Builder() + .index("my-index") + .aliases("foo", + new Alias.Builder().isWriteIndex(true).build() + ) + .build() + )); + //end::builders + } + +} From a77764499c2bf4724af19d6a174527e90b51e056 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Thu, 28 Jul 2022 18:39:01 -0700 Subject: [PATCH 03/12] Added license Signed-off-by: Owais Kazi --- src/test/java/org/opensearch/sdk/TestSDKClient.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/org/opensearch/sdk/TestSDKClient.java b/src/test/java/org/opensearch/sdk/TestSDKClient.java index 9f026960..5f7e638b 100644 --- a/src/test/java/org/opensearch/sdk/TestSDKClient.java +++ b/src/test/java/org/opensearch/sdk/TestSDKClient.java @@ -1,3 +1,11 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + package org.opensearch.sdk; import org.junit.jupiter.api.Test; From 009fbbe472fc9bbd068b1a46a7997270182dc767 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Thu, 11 Aug 2022 10:02:10 -0700 Subject: [PATCH 04/12] Send config from initial request Signed-off-by: Owais Kazi --- .../org/opensearch/sdk/ExtensionsRunner.java | 4 +++ .../java/org/opensearch/sdk/SDKClient.java | 28 +++---------------- .../org/opensearch/sdk/TestSDKClient.java | 17 ++++++++++- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java index fadc8b6e..d4b24cd6 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java +++ b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java @@ -71,6 +71,8 @@ public class ExtensionsRunner { private String uniqueId; private DiscoveryNode opensearchNode; private TransportService extensionTransportService = null; + private int port; + private String hostAddress; private final Settings settings = Settings.builder() .put("node.name", extensionSettings.getExtensionName()) @@ -143,6 +145,8 @@ InitializeExtensionsResponse handleExtensionInitRequest(InitializeExtensionsRequ return initializeExtensionsResponse; } finally { // After sending successful response to initialization, send the REST API + port = extensionInitRequest.getPort(); + hostAddress = opensearchNode.getHostAddress(); setOpensearchNode(opensearchNode); extensionTransportService.connectToNode(opensearchNode); sendRegisterRestActionsRequest(extensionTransportService); diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index 3b07f598..5568b4c9 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -22,8 +22,6 @@ import org.opensearch.client.RestClientBuilder; import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.opensearch.OpenSearchClient; -import org.opensearch.client.opensearch.indices.CreateIndexRequest; -import org.opensearch.client.opensearch.indices.CreateIndexResponse; import org.opensearch.client.transport.OpenSearchTransport; import org.opensearch.client.transport.rest_client.RestClientTransport; @@ -38,21 +36,16 @@ public class SDKClient { * Creates client for SDK * @throws IOException if client failed */ - public void createClient() throws IOException { - String endpoint = "localhost"; - String username = "admin"; - String password = "admin"; - String protocol = "http"; - int port = 9200; + public void createClient(String hostAddress, int port) throws IOException { RestClient restClient = null; try { - RestClientBuilder builder = RestClient.builder(new HttpHost(endpoint, port, protocol)); + RestClientBuilder builder = RestClient.builder(new HttpHost(hostAddress, port)); builder.setStrictDeprecationMode(true); builder.setHttpClientConfigCallback(httpClientBuilder -> { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); + //credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); try { - return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider) + return httpClientBuilder .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) .setSSLContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build()); } catch (Exception e) { @@ -71,17 +64,4 @@ public void createClient() throws IOException { } } } - - /** - * Creates index on OpenSearch - * @throws IOException if request failed - */ - public CreateIndexResponse createIndex(String index) throws IOException { - logger.info("Creating Index on OpenSearch"); - // Create Index - CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(index).build(); - CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest); - logger.info("Created Index on OpenSearch", createIndexResponse); - return createIndexResponse; - } } diff --git a/src/test/java/org/opensearch/sdk/TestSDKClient.java b/src/test/java/org/opensearch/sdk/TestSDKClient.java index 5f7e638b..74697de6 100644 --- a/src/test/java/org/opensearch/sdk/TestSDKClient.java +++ b/src/test/java/org/opensearch/sdk/TestSDKClient.java @@ -15,13 +15,14 @@ import org.opensearch.client.transport.OpenSearchTransport; import org.opensearch.client.transport.TransportException; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; public class TestSDKClient { private OpenSearchTransport transport = new FailingTransport(); @Test - public void testCreateIndex() throws Exception { + public void testCreateIndexException() throws Exception { OpenSearchClient client = new OpenSearchClient(transport); //tag::builders @@ -36,4 +37,18 @@ public void testCreateIndex() throws Exception { //end::builders } + @Test + public void testCreateIndex() { + OpenSearchClient client = new OpenSearchClient(transport); + + assertEquals(, () -> client.indices().create( + new CreateIndexRequest.Builder() + .index("my-index") + .aliases("foo", + new Alias.Builder().isWriteIndex(true).build() + ) + .build() + )); + } + } From 84749761d0a4c37eb8fa9149428f411b25b6ff1c Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Thu, 11 Aug 2022 17:13:01 -0700 Subject: [PATCH 05/12] Utilized the hostaddress and port from extension initialize request Signed-off-by: Owais Kazi --- .../java/org/opensearch/sdk/SDKClient.java | 57 ++++++++++--------- .../org/opensearch/sdk/TestSDKClient.java | 15 ----- 2 files changed, 31 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index 5568b4c9..aaaf563c 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -10,8 +10,6 @@ import java.io.IOException; import org.apache.http.HttpHost; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.impl.client.BasicCredentialsProvider; @@ -31,37 +29,44 @@ public class SDKClient { private final Logger logger = LogManager.getLogger(SDKClient.class); private OpenSearchClient client; + private RestClient restClient = null; /** * Creates client for SDK + * @param hostAddress The address client can connect to + * @param port The port of the address * @throws IOException if client failed + * @return SDKClient */ - public void createClient(String hostAddress, int port) throws IOException { - RestClient restClient = null; - try { - RestClientBuilder builder = RestClient.builder(new HttpHost(hostAddress, port)); - builder.setStrictDeprecationMode(true); - builder.setHttpClientConfigCallback(httpClientBuilder -> { - final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - //credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); - try { - return httpClientBuilder - .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) - .setSSLContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build()); - } catch (Exception e) { - throw new RuntimeException(e); - } - }); + public OpenSearchClient createClient(String hostAddress, int port) throws IOException { + RestClientBuilder builder = RestClient.builder(new HttpHost(hostAddress, port)); + builder.setStrictDeprecationMode(true); + builder.setHttpClientConfigCallback(httpClientBuilder -> { + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + // credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); + try { + return httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) + .setSSLContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build()); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); - restClient = builder.build(); + restClient = builder.build(); - // Create Client - OpenSearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper()); - OpenSearchClient client = new OpenSearchClient(transport); - } finally { - if (restClient != null) { - restClient.close(); - } + // Create Client + OpenSearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper()); + client = new OpenSearchClient(transport); + return client; + } + + /** + * + * @throws IOException if closing the client fails + */ + public void doClose() throws IOException { + if (restClient != null) { + restClient.close(); } } } diff --git a/src/test/java/org/opensearch/sdk/TestSDKClient.java b/src/test/java/org/opensearch/sdk/TestSDKClient.java index 74697de6..4702bbbc 100644 --- a/src/test/java/org/opensearch/sdk/TestSDKClient.java +++ b/src/test/java/org/opensearch/sdk/TestSDKClient.java @@ -15,7 +15,6 @@ import org.opensearch.client.transport.OpenSearchTransport; import org.opensearch.client.transport.TransportException; -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; public class TestSDKClient { @@ -37,18 +36,4 @@ public void testCreateIndexException() throws Exception { //end::builders } - @Test - public void testCreateIndex() { - OpenSearchClient client = new OpenSearchClient(transport); - - assertEquals(, () -> client.indices().create( - new CreateIndexRequest.Builder() - .index("my-index") - .aliases("foo", - new Alias.Builder().isWriteIndex(true).build() - ) - .build() - )); - } - } From bd0f00c6a69c71a07190968af19d41371e4d6179 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Thu, 28 Jul 2022 18:02:00 -0700 Subject: [PATCH 06/12] Integrated SDK Client to make request to OpenSearch Signed-off-by: Owais Kazi --- .../java/org/opensearch/sdk/SDKClient.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index aaaf563c..ca556053 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -10,6 +10,11 @@ import java.io.IOException; import org.apache.http.HttpHost; +<<<<<<< HEAD +======= +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +>>>>>>> 1c06e87 (Integrated SDK Client to make request to OpenSearch) import org.apache.http.client.CredentialsProvider; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.impl.client.BasicCredentialsProvider; @@ -20,6 +25,11 @@ import org.opensearch.client.RestClientBuilder; import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.opensearch.OpenSearchClient; +<<<<<<< HEAD +======= +import org.opensearch.client.opensearch.indices.CreateIndexRequest; +import org.opensearch.client.opensearch.indices.CreateIndexResponse; +>>>>>>> 1c06e87 (Integrated SDK Client to make request to OpenSearch) import org.opensearch.client.transport.OpenSearchTransport; import org.opensearch.client.transport.rest_client.RestClientTransport; @@ -29,6 +39,7 @@ public class SDKClient { private final Logger logger = LogManager.getLogger(SDKClient.class); private OpenSearchClient client; +<<<<<<< HEAD private RestClient restClient = null; /** @@ -68,5 +79,56 @@ public void doClose() throws IOException { if (restClient != null) { restClient.close(); } +======= + + /** + * Creates client for SDK + * @throws IOException if client failed + */ + public void createClient() throws IOException { + String endpoint = "localhost"; + String username = "admin"; + String password = "admin"; + String protocol = "http"; + int port = 9200; + RestClient restClient = null; + try { + RestClientBuilder builder = RestClient.builder(new HttpHost(endpoint, port, protocol)); + builder.setStrictDeprecationMode(true); + builder.setHttpClientConfigCallback(httpClientBuilder -> { + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); + try { + return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider) + .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) + .setSSLContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build()); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + + restClient = builder.build(); + + // Create Client + OpenSearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper()); + OpenSearchClient client = new OpenSearchClient(transport); + } finally { + if (restClient != null) { + restClient.close(); + } + } + } + + /** + * Creates index on OpenSearch + * @throws IOException if request failed + */ + public void createIndex(String index) throws IOException { + logger.info("Creating Index on OpenSearch"); + // Create Index + CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(index).build(); + CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest); + logger.info("Created Index on OpenSearch", createIndexResponse); +>>>>>>> 1c06e87 (Integrated SDK Client to make request to OpenSearch) } } From d822c59b652e9377eaef00b87af41a35518cfe29 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Thu, 28 Jul 2022 18:37:15 -0700 Subject: [PATCH 07/12] Added test and restructure SDK Signed-off-by: Owais Kazi --- .../java/org/opensearch/sdk/SDKClient.java | 63 +------------------ 1 file changed, 1 insertion(+), 62 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index ca556053..95241f4e 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -10,11 +10,7 @@ import java.io.IOException; import org.apache.http.HttpHost; -<<<<<<< HEAD -======= -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; ->>>>>>> 1c06e87 (Integrated SDK Client to make request to OpenSearch) + import org.apache.http.client.CredentialsProvider; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.impl.client.BasicCredentialsProvider; @@ -25,11 +21,6 @@ import org.opensearch.client.RestClientBuilder; import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.opensearch.OpenSearchClient; -<<<<<<< HEAD -======= -import org.opensearch.client.opensearch.indices.CreateIndexRequest; -import org.opensearch.client.opensearch.indices.CreateIndexResponse; ->>>>>>> 1c06e87 (Integrated SDK Client to make request to OpenSearch) import org.opensearch.client.transport.OpenSearchTransport; import org.opensearch.client.transport.rest_client.RestClientTransport; @@ -39,7 +30,6 @@ public class SDKClient { private final Logger logger = LogManager.getLogger(SDKClient.class); private OpenSearchClient client; -<<<<<<< HEAD private RestClient restClient = null; /** @@ -79,56 +69,5 @@ public void doClose() throws IOException { if (restClient != null) { restClient.close(); } -======= - - /** - * Creates client for SDK - * @throws IOException if client failed - */ - public void createClient() throws IOException { - String endpoint = "localhost"; - String username = "admin"; - String password = "admin"; - String protocol = "http"; - int port = 9200; - RestClient restClient = null; - try { - RestClientBuilder builder = RestClient.builder(new HttpHost(endpoint, port, protocol)); - builder.setStrictDeprecationMode(true); - builder.setHttpClientConfigCallback(httpClientBuilder -> { - final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); - try { - return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider) - .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) - .setSSLContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build()); - } catch (Exception e) { - throw new RuntimeException(e); - } - }); - - restClient = builder.build(); - - // Create Client - OpenSearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper()); - OpenSearchClient client = new OpenSearchClient(transport); - } finally { - if (restClient != null) { - restClient.close(); - } - } - } - - /** - * Creates index on OpenSearch - * @throws IOException if request failed - */ - public void createIndex(String index) throws IOException { - logger.info("Creating Index on OpenSearch"); - // Create Index - CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(index).build(); - CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest); - logger.info("Created Index on OpenSearch", createIndexResponse); ->>>>>>> 1c06e87 (Integrated SDK Client to make request to OpenSearch) } } From b25bccf453df3c767d909abd882ed0db145a0fcc Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Fri, 19 Aug 2022 12:30:38 -0700 Subject: [PATCH 08/12] Removed security related attributes Signed-off-by: Owais Kazi --- src/main/java/org/opensearch/sdk/SDKClient.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index 95241f4e..232f570d 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -43,11 +43,9 @@ public OpenSearchClient createClient(String hostAddress, int port) throws IOExce RestClientBuilder builder = RestClient.builder(new HttpHost(hostAddress, port)); builder.setStrictDeprecationMode(true); builder.setHttpClientConfigCallback(httpClientBuilder -> { - final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - // credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); try { - return httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) - .setSSLContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build()); + return httpClientBuilder + .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); } catch (Exception e) { throw new RuntimeException(e); } From e0a24cfa321e1fba8a2d31bf567a195c6487ad79 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Mon, 22 Aug 2022 11:09:18 -0700 Subject: [PATCH 09/12] Updated branch Signed-off-by: Owais Kazi --- src/main/java/org/opensearch/sdk/SDKClient.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index 232f570d..9ba8f35d 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -11,10 +11,7 @@ import org.apache.http.HttpHost; -import org.apache.http.client.CredentialsProvider; import org.apache.http.conn.ssl.NoopHostnameVerifier; -import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.ssl.SSLContextBuilder; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensearch.client.RestClient; @@ -44,8 +41,7 @@ public OpenSearchClient createClient(String hostAddress, int port) throws IOExce builder.setStrictDeprecationMode(true); builder.setHttpClientConfigCallback(httpClientBuilder -> { try { - return httpClientBuilder - .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); + return httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); } catch (Exception e) { throw new RuntimeException(e); } From 29f07cf50a7b6bf2312433e0b6eb323b2112386e Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Mon, 22 Aug 2022 13:01:35 -0700 Subject: [PATCH 10/12] PR comments Signed-off-by: Owais Kazi --- .../org/opensearch/sdk/ExtensionsRunner.java | 4 +- .../java/org/opensearch/sdk/SDKClient.java | 14 +-- .../org/opensearch/sdk/FailingTransport.java | 112 ------------------ .../org/opensearch/sdk/TestSDKClient.java | 38 +++--- 4 files changed, 30 insertions(+), 138 deletions(-) delete mode 100644 src/test/java/org/opensearch/sdk/FailingTransport.java diff --git a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java index d4b24cd6..7c6cf402 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java +++ b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java @@ -145,8 +145,8 @@ InitializeExtensionsResponse handleExtensionInitRequest(InitializeExtensionsRequ return initializeExtensionsResponse; } finally { // After sending successful response to initialization, send the REST API - port = extensionInitRequest.getPort(); - hostAddress = opensearchNode.getHostAddress(); + port = opensearchNode.getAddress().getPort(); + hostAddress = opensearchNode.getAddress().getAddress(); setOpensearchNode(opensearchNode); extensionTransportService.connectToNode(opensearchNode); sendRegisterRestActionsRequest(extensionTransportService); diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index 9ba8f35d..7ecee919 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -22,7 +22,7 @@ import org.opensearch.client.transport.rest_client.RestClientTransport; /** - * This class creates a Client for SDK to make requests to OpenSearch + * This class creates SDKClient for an extension to make requests to OpenSearch */ public class SDKClient { private final Logger logger = LogManager.getLogger(SDKClient.class); @@ -30,11 +30,11 @@ public class SDKClient { private RestClient restClient = null; /** - * Creates client for SDK - * @param hostAddress The address client can connect to - * @param port The port of the address + * Creates OpenSearchClient for SDK + * @param hostAddress The address of OpenSearch cluster, client can connect to + * @param port The port of OpenSearch cluster * @throws IOException if client failed - * @return SDKClient + * @return SDKClient which is internally an OpenSearchClient. The user is responsible for calling {@link #doCloseRestClient()} when finished with the client */ public OpenSearchClient createClient(String hostAddress, int port) throws IOException { RestClientBuilder builder = RestClient.builder(new HttpHost(hostAddress, port)); @@ -57,9 +57,9 @@ public OpenSearchClient createClient(String hostAddress, int port) throws IOExce /** * - * @throws IOException if closing the client fails + * @throws IOException if closing the restClient fails */ - public void doClose() throws IOException { + public void doCloseRestClient() throws IOException { if (restClient != null) { restClient.close(); } diff --git a/src/test/java/org/opensearch/sdk/FailingTransport.java b/src/test/java/org/opensearch/sdk/FailingTransport.java deleted file mode 100644 index 72cbde16..00000000 --- a/src/test/java/org/opensearch/sdk/FailingTransport.java +++ /dev/null @@ -1,112 +0,0 @@ -package org.opensearch.sdk; - -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -import org.opensearch.client.json.JsonpMapper; -import org.opensearch.client.json.jsonb.JsonbJsonpMapper; -import org.opensearch.client.transport.OpenSearchTransport; -import org.opensearch.client.transport.Endpoint; -import org.opensearch.client.transport.TransportException; -import org.opensearch.client.transport.TransportOptions; - -import javax.annotation.Nullable; -import java.io.IOException; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CompletableFuture; -import java.util.function.Function; - -/** - * A transport implementation that always fails. Used for simple doc sections where we just want to check compilation. - */ -public class FailingTransport implements OpenSearchTransport { - - private JsonpMapper mapper = new JsonbJsonpMapper(); - - private TransportOptions options = new TransportOptions() { - @Override - public Collection> headers() { - return Collections.emptyList(); - } - - @Override - public Map queryParameters() { - return Collections.emptyMap(); - } - - @Override - public Function, Boolean> onWarnings() { - return null; - } - - @Override - public Builder toBuilder() { - return null; - } - }; - - @Override - public ResponseT performRequest( - RequestT request, - Endpoint endpoint, - @Nullable TransportOptions options - ) throws IOException { - throw new TransportException("Not implemented"); - } - - @Override - public CompletableFuture performRequestAsync( - RequestT request, - Endpoint endpoint, - @Nullable TransportOptions options - ) { - CompletableFuture future = new CompletableFuture<>(); - future.completeExceptionally(new TransportException("Not implemented")); - return future; - } - - @Override - public JsonpMapper jsonpMapper() { - return mapper; - } - - @Override - public TransportOptions options() { - return options; - } - - @Override - public void close() throws IOException {} -} diff --git a/src/test/java/org/opensearch/sdk/TestSDKClient.java b/src/test/java/org/opensearch/sdk/TestSDKClient.java index 4702bbbc..1ca5fb87 100644 --- a/src/test/java/org/opensearch/sdk/TestSDKClient.java +++ b/src/test/java/org/opensearch/sdk/TestSDKClient.java @@ -12,28 +12,32 @@ import org.opensearch.client.opensearch.OpenSearchClient; import org.opensearch.client.opensearch.indices.Alias; import org.opensearch.client.opensearch.indices.CreateIndexRequest; -import org.opensearch.client.transport.OpenSearchTransport; -import org.opensearch.client.transport.TransportException; +import org.opensearch.test.OpenSearchTestCase; -import static org.junit.jupiter.api.Assertions.assertThrows; +import java.net.ConnectException; -public class TestSDKClient { - private OpenSearchTransport transport = new FailingTransport(); +import static org.junit.jupiter.api.Assertions.assertInstanceOf; + +public class TestSDKClient extends OpenSearchTestCase { + SDKClient sdkClient = new SDKClient(); @Test - public void testCreateIndexException() throws Exception { - OpenSearchClient client = new OpenSearchClient(transport); - - //tag::builders - assertThrows(TransportException.class, () -> client.indices().create( - new CreateIndexRequest.Builder() - .index("my-index") - .aliases("foo", - new Alias.Builder().isWriteIndex(true).build() - ) + public void testCreateClient() throws Exception { + + OpenSearchClient testClient = sdkClient.createClient("localhost", 9200); + assertInstanceOf(OpenSearchClient.class, testClient); + + assertThrows( + ConnectException.class, + () -> testClient.indices() + .create( + new CreateIndexRequest.Builder().index("my-index") + .aliases("foo", new Alias.Builder().isWriteIndex(true).build()) .build() - )); - //end::builders + ) + ); + + sdkClient.doCloseRestClient(); } } From f2588693da6f66e7921d7133b6ad3ade09a3a187 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Mon, 22 Aug 2022 13:08:24 -0700 Subject: [PATCH 11/12] Changed the method name Signed-off-by: Owais Kazi --- src/main/java/org/opensearch/sdk/SDKClient.java | 4 ++-- src/test/java/org/opensearch/sdk/TestSDKClient.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index 7ecee919..d0c71381 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -30,13 +30,13 @@ public class SDKClient { private RestClient restClient = null; /** - * Creates OpenSearchClient for SDK + * Creates OpenSearchClient for SDK. It also creates a restClient as a wrapper around OpenSearchClient * @param hostAddress The address of OpenSearch cluster, client can connect to * @param port The port of OpenSearch cluster * @throws IOException if client failed * @return SDKClient which is internally an OpenSearchClient. The user is responsible for calling {@link #doCloseRestClient()} when finished with the client */ - public OpenSearchClient createClient(String hostAddress, int port) throws IOException { + public OpenSearchClient initializeClient(String hostAddress, int port) throws IOException { RestClientBuilder builder = RestClient.builder(new HttpHost(hostAddress, port)); builder.setStrictDeprecationMode(true); builder.setHttpClientConfigCallback(httpClientBuilder -> { diff --git a/src/test/java/org/opensearch/sdk/TestSDKClient.java b/src/test/java/org/opensearch/sdk/TestSDKClient.java index 1ca5fb87..824dbb97 100644 --- a/src/test/java/org/opensearch/sdk/TestSDKClient.java +++ b/src/test/java/org/opensearch/sdk/TestSDKClient.java @@ -24,7 +24,7 @@ public class TestSDKClient extends OpenSearchTestCase { @Test public void testCreateClient() throws Exception { - OpenSearchClient testClient = sdkClient.createClient("localhost", 9200); + OpenSearchClient testClient = sdkClient.initializeClient("localhost", 9200); assertInstanceOf(OpenSearchClient.class, testClient); assertThrows( From 997ed6dcbffeab7a2ce1b77b549daa0c706ae761 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Mon, 22 Aug 2022 15:15:01 -0700 Subject: [PATCH 12/12] Small changes Signed-off-by: Owais Kazi --- src/main/java/org/opensearch/sdk/SDKClient.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index d0c71381..5e960b18 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -26,11 +26,11 @@ */ public class SDKClient { private final Logger logger = LogManager.getLogger(SDKClient.class); - private OpenSearchClient client; + private OpenSearchClient javaClient; private RestClient restClient = null; /** - * Creates OpenSearchClient for SDK. It also creates a restClient as a wrapper around OpenSearchClient + * Creates OpenSearchClient for SDK. It also creates a restClient as a wrapper around Java OpenSearchClient * @param hostAddress The address of OpenSearch cluster, client can connect to * @param port The port of OpenSearch cluster * @throws IOException if client failed @@ -51,8 +51,8 @@ public OpenSearchClient initializeClient(String hostAddress, int port) throws IO // Create Client OpenSearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper()); - client = new OpenSearchClient(transport); - return client; + javaClient = new OpenSearchClient(transport); + return javaClient; } /**