From 1beff0c59ef1fd890427654f2378a2d7ddb18e04 Mon Sep 17 00:00:00 2001 From: Sylvain Wallez Date: Thu, 20 Jan 2022 12:04:29 +0100 Subject: [PATCH] Add workaround for missing header in snapshot request --- .../rest_client/RestClientTransport.java | 11 +++++++ .../elasticsearch/end_to_end/RequestTest.java | 32 +++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/java-client/src/main/java/co/elastic/clients/transport/rest_client/RestClientTransport.java b/java-client/src/main/java/co/elastic/clients/transport/rest_client/RestClientTransport.java index bddd807fa..7b8682129 100644 --- a/java-client/src/main/java/co/elastic/clients/transport/rest_client/RestClientTransport.java +++ b/java-client/src/main/java/co/elastic/clients/transport/rest_client/RestClientTransport.java @@ -53,8 +53,11 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.Arrays; +import java.util.HashSet; import java.util.Iterator; import java.util.Map; +import java.util.Set; import java.util.concurrent.CompletableFuture; public class RestClientTransport implements ElasticsearchTransport { @@ -331,9 +334,17 @@ private ResponseT decodeResponse( } } + // Endpoints that (incorrectly) do not return the Elastic product header + private static final Set endpointsMissingProductHeader = new HashSet<>(Arrays.asList( + "es/snapshot.create" // #74 / elastic/elasticsearch#82358 + )); + private void checkProductHeader(Response clientResp, Endpoint endpoint) throws IOException { String header = clientResp.getHeader("X-Elastic-Product"); if (header == null) { + if (endpointsMissingProductHeader.contains(endpoint.id())) { + return; + } throw new TransportException( "Missing [X-Elastic-Product] header. Please check that you are connecting to an Elasticsearch " + "instance, and that any networking filters are preserving that header.", diff --git a/java-client/src/test/java/co/elastic/clients/elasticsearch/end_to_end/RequestTest.java b/java-client/src/test/java/co/elastic/clients/elasticsearch/end_to_end/RequestTest.java index f32f7f118..274ce3c16 100644 --- a/java-client/src/test/java/co/elastic/clients/elasticsearch/end_to_end/RequestTest.java +++ b/java-client/src/test/java/co/elastic/clients/elasticsearch/end_to_end/RequestTest.java @@ -42,6 +42,8 @@ import co.elastic.clients.elasticsearch.indices.GetMappingResponse; import co.elastic.clients.elasticsearch.indices.IndexState; import co.elastic.clients.elasticsearch.model.ModelTestCase; +import co.elastic.clients.elasticsearch.snapshot.CreateRepositoryResponse; +import co.elastic.clients.elasticsearch.snapshot.CreateSnapshotResponse; import co.elastic.clients.json.JsonpMapper; import co.elastic.clients.json.jsonb.JsonbJsonpMapper; import co.elastic.clients.transport.ElasticsearchTransport; @@ -76,17 +78,19 @@ public class RequestTest extends Assert { @BeforeClass public static void setup() { - container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:7.15.1") + container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:7.16.2") .withEnv("ES_JAVA_OPTS", "-Xms256m -Xmx256m") + .withEnv("path.repo", "/tmp") // for snapshots .withStartupTimeout(Duration.ofSeconds(30)) .withPassword("changeme"); container.start(); + int port = container.getMappedPort(9200); BasicCredentialsProvider credsProv = new BasicCredentialsProvider(); credsProv.setCredentials( AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme") ); - restClient = RestClient.builder(new HttpHost("localhost", container.getMappedPort(9200))) + restClient = RestClient.builder(new HttpHost("localhost", port)) .setHttpClientConfigCallback(hc -> hc.setDefaultCredentialsProvider(credsProv)) .build(); transport = new RestClientTransport(restClient, mapper); @@ -394,6 +398,30 @@ public void testDefaultIndexSettings() throws IOException { assertNull(settings.get(index).defaults()); } + @Test + public void testSnapshotCreation() throws IOException { + // https://github.com/elastic/elasticsearch-java/issues/74 + // https://github.com/elastic/elasticsearch/issues/82358 + + CreateRepositoryResponse repo = client.snapshot().createRepository(b1 -> b1 + .name("test") + .type("fs") + .settings(b2 -> b2 + .location("/tmp/test-repo") + ) + ); + + assertTrue(repo.acknowledged()); + + CreateSnapshotResponse snapshot = client.snapshot().create(b -> b + .repository("test") + .snapshot("1") + .waitForCompletion(true) + ); + + assertNotNull(snapshot.snapshot()); + } + @Test public void testValueBodyResponse() throws Exception { DiskUsageResponse resp = client.indices().diskUsage(b -> b