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

Improve docs for Elasticsearch 8 #8870

Merged
merged 11 commits into from
Aug 7, 2024
Merged
Next Next commit
Improve docs for Elasticsearch 8
  • Loading branch information
philipp94831 committed Jul 10, 2024
commit 0a77fa2e59567f7a36449d94b0fbc0c56a354cd8
2 changes: 2 additions & 0 deletions docs/modules/elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ You can start an elasticsearch container instance from any Java application by u

<!--codeinclude-->
[HttpClient](../../modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java) inside_block:httpClientContainer
[HttpClient with Elasticsearch 8](../../modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java) inside_block:httpClientContainer8
[HttpClient with Elasticsearch 8 and SSL disabled](../../modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java) inside_block:httpClientContainer8NoSSL
[TransportClient](../../modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java) inside_block:transportClientContainer
<!--/codeinclude-->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,85 @@ public void restClientClusterHealth() throws IOException {
// }
}

@Test
public void restClientClusterHealthElasticsearch8() throws IOException {
// httpClientContainer8 {
// Create the elasticsearch container.
try (
ElasticsearchContainer container = new ElasticsearchContainer(
"docker.elastic.co/elasticsearch/elasticsearch:8.1.2"
)
) {
// Start the container. This step might take some time...
container.start();

// Do whatever you want with the rest client ...
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD)
);

client =
RestClient
// use HTTPS for Elasticsearch 8
.builder(HttpHost.create("https://" + container.getHttpHostAddress()))
.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
// SSL is activated by default in Elasticseach 8
httpClientBuilder.setSSLContext(container.createSslContextFromCa());
return httpClientBuilder;
})
.build();

Response response = client.performRequest(new Request("GET", "/_cluster/health"));
// }}
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(EntityUtils.toString(response.getEntity())).contains("cluster_name");
// httpClientContainer8 {{
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// httpClientContainer8 {{
// httpClientContainer8 {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In line 211 it is also {{. Which one is correct?

Copy link
Member

Choose a reason for hiding this comment

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

It should be a single curly brace.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tested it and it only works with a single curly brace here. Docs preview looks good to me now

}
// }
}

@Test
public void restClientClusterHealthElasticsearch8WithoutSSL() throws IOException {
// httpClientContainer8NoSSL {
// Create the elasticsearch container.
try (
ElasticsearchContainer container = new ElasticsearchContainer(
"docker.elastic.co/elasticsearch/elasticsearch:8.1.2"
)
// disable SSL
.withEnv("xpack.security.transport.ssl.enabled", "false")
.withEnv("xpack.security.http.ssl.enabled", "false")
) {
// Start the container. This step might take some time...
container.start();

// Do whatever you want with the rest client ...
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD)
);

client =
RestClient
.builder(HttpHost.create(container.getHttpHostAddress()))
.setHttpClientConfigCallback(httpClientBuilder -> {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
})
.build();

Response response = client.performRequest(new Request("GET", "/_cluster/health"));
// }}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// }}
// }

assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(EntityUtils.toString(response.getEntity())).contains("cluster_name");
// httpClientContainer8NoSSL {{
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// httpClientContainer8NoSSL {{
// httpClientContainer8NoSSL {

}
// }
}

@Test
public void restClientSecuredClusterHealth() throws IOException {
// httpClientSecuredContainer {
Expand Down
Loading