Skip to content

Commit

Permalink
Utilized the hostaddress and port from extension initialize request
Browse files Browse the repository at this point in the history
Signed-off-by: Owais Kazi <[email protected]>
  • Loading branch information
owaiskazi19 committed Aug 12, 2022
1 parent b3f1908 commit 7e44c23
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 42 deletions.
1 change: 0 additions & 1 deletion src/main/java/org/opensearch/sdk/ExtensionsRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.opensearch.transport.TransportInterceptor;
import org.opensearch.transport.TransportService;
import org.opensearch.transport.TransportSettings;
import org.opensearch.transport.TransportInterceptor;
import org.opensearch.transport.TransportResponse;

import java.io.File;
Expand Down
57 changes: 31 additions & 26 deletions src/main/java/org/opensearch/sdk/SDKClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}
}
15 changes: 0 additions & 15 deletions src/test/java/org/opensearch/sdk/TestSDKClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
));
}

}

0 comments on commit 7e44c23

Please sign in to comment.