Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created SDK Client to interact with OpenSearch #67

Merged
merged 12 commits into from
Aug 23, 2022
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
owaiskazi19 marked this conversation as resolved.
Show resolved Hide resolved
implementation "io.netty:netty-all:4.1.73.Final"
testCompileOnly ("junit:junit:4.13.2") {
exclude module : 'hamcrest'
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/opensearch/sdk/ExtensionsRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -143,6 +145,8 @@ InitializeExtensionsResponse handleExtensionInitRequest(InitializeExtensionsRequ
return initializeExtensionsResponse;
} finally {
// After sending successful response to initialization, send the REST API
port = opensearchNode.getAddress().getPort();
hostAddress = opensearchNode.getAddress().getAddress();
setOpensearchNode(opensearchNode);
extensionTransportService.connectToNode(opensearchNode);
sendRegisterRestActionsRequest(extensionTransportService);
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/org/opensearch/sdk/SDKClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.conn.ssl.NoopHostnameVerifier;
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.transport.OpenSearchTransport;
import org.opensearch.client.transport.rest_client.RestClientTransport;

/**
* This class creates SDKClient for an extension to make requests to OpenSearch
*/
public class SDKClient {
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
private final Logger logger = LogManager.getLogger(SDKClient.class);
private OpenSearchClient javaClient;
private RestClient restClient = null;

/**
* 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
* @return SDKClient which is internally an OpenSearchClient. The user is responsible for calling {@link #doCloseRestClient()} when finished with the client
*/
public OpenSearchClient initializeClient(String hostAddress, int port) throws IOException {
RestClientBuilder builder = RestClient.builder(new HttpHost(hostAddress, port));
builder.setStrictDeprecationMode(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, what does this do?
Side note: I could only find elasticsearch documentation[1].
@VachaShah

[1] https://javadoc.io/static/org.elasticsearch.client/elasticsearch-rest-client/6.8.3/org/elasticsearch/client/RestClientBuilder.html

builder.setHttpClientConfigCallback(httpClientBuilder -> {
try {
return httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
} catch (Exception e) {
throw new RuntimeException(e);
}
});

restClient = builder.build();

// Create Client
OpenSearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
javaClient = new OpenSearchClient(transport);
return javaClient;
}

/**
*
* @throws IOException if closing the restClient fails
*/
public void doCloseRestClient() throws IOException {
if (restClient != null) {
restClient.close();
}
}
}
43 changes: 43 additions & 0 deletions src/test/java/org/opensearch/sdk/TestSDKClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch.indices.Alias;
import org.opensearch.client.opensearch.indices.CreateIndexRequest;
import org.opensearch.test.OpenSearchTestCase;

import java.net.ConnectException;

import static org.junit.jupiter.api.Assertions.assertInstanceOf;

public class TestSDKClient extends OpenSearchTestCase {
SDKClient sdkClient = new SDKClient();

@Test
public void testCreateClient() throws Exception {

OpenSearchClient testClient = sdkClient.initializeClient("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()
)
);

sdkClient.doCloseRestClient();
}

}