Skip to content

Commit 0aecc5a

Browse files
authored
Add workaround for missing header in snapshot request (#116)
1 parent fb484d1 commit 0aecc5a

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

java-client/src/main/java/co/elastic/clients/transport/rest_client/RestClientTransport.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@
5353
import java.io.ByteArrayOutputStream;
5454
import java.io.IOException;
5555
import java.io.InputStream;
56+
import java.util.Arrays;
57+
import java.util.HashSet;
5658
import java.util.Iterator;
5759
import java.util.Map;
60+
import java.util.Set;
5861
import java.util.concurrent.CompletableFuture;
5962

6063
public class RestClientTransport implements ElasticsearchTransport {
@@ -331,9 +334,17 @@ private <ResponseT> ResponseT decodeResponse(
331334
}
332335
}
333336

337+
// Endpoints that (incorrectly) do not return the Elastic product header
338+
private static final Set<String> endpointsMissingProductHeader = new HashSet<>(Arrays.asList(
339+
"es/snapshot.create" // #74 / elastic/elasticsearch#82358
340+
));
341+
334342
private void checkProductHeader(Response clientResp, Endpoint<?, ?, ?> endpoint) throws IOException {
335343
String header = clientResp.getHeader("X-Elastic-Product");
336344
if (header == null) {
345+
if (endpointsMissingProductHeader.contains(endpoint.id())) {
346+
return;
347+
}
337348
throw new TransportException(
338349
"Missing [X-Elastic-Product] header. Please check that you are connecting to an Elasticsearch "
339350
+ "instance, and that any networking filters are preserving that header.",

java-client/src/test/java/co/elastic/clients/elasticsearch/end_to_end/RequestTest.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import co.elastic.clients.elasticsearch.indices.GetMappingResponse;
4343
import co.elastic.clients.elasticsearch.indices.IndexState;
4444
import co.elastic.clients.elasticsearch.model.ModelTestCase;
45+
import co.elastic.clients.elasticsearch.snapshot.CreateRepositoryResponse;
46+
import co.elastic.clients.elasticsearch.snapshot.CreateSnapshotResponse;
4547
import co.elastic.clients.json.JsonpMapper;
4648
import co.elastic.clients.json.jsonb.JsonbJsonpMapper;
4749
import co.elastic.clients.transport.ElasticsearchTransport;
@@ -76,17 +78,19 @@ public class RequestTest extends Assert {
7678

7779
@BeforeClass
7880
public static void setup() {
79-
container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:7.15.1")
81+
container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:7.16.2")
8082
.withEnv("ES_JAVA_OPTS", "-Xms256m -Xmx256m")
83+
.withEnv("path.repo", "/tmp") // for snapshots
8184
.withStartupTimeout(Duration.ofSeconds(30))
8285
.withPassword("changeme");
8386
container.start();
87+
int port = container.getMappedPort(9200);
8488

8589
BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
8690
credsProv.setCredentials(
8791
AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme")
8892
);
89-
restClient = RestClient.builder(new HttpHost("localhost", container.getMappedPort(9200)))
93+
restClient = RestClient.builder(new HttpHost("localhost", port))
9094
.setHttpClientConfigCallback(hc -> hc.setDefaultCredentialsProvider(credsProv))
9195
.build();
9296
transport = new RestClientTransport(restClient, mapper);
@@ -394,6 +398,30 @@ public void testDefaultIndexSettings() throws IOException {
394398
assertNull(settings.get(index).defaults());
395399
}
396400

401+
@Test
402+
public void testSnapshotCreation() throws IOException {
403+
// https://github.com/elastic/elasticsearch-java/issues/74
404+
// https://github.com/elastic/elasticsearch/issues/82358
405+
406+
CreateRepositoryResponse repo = client.snapshot().createRepository(b1 -> b1
407+
.name("test")
408+
.type("fs")
409+
.settings(b2 -> b2
410+
.location("/tmp/test-repo")
411+
)
412+
);
413+
414+
assertTrue(repo.acknowledged());
415+
416+
CreateSnapshotResponse snapshot = client.snapshot().create(b -> b
417+
.repository("test")
418+
.snapshot("1")
419+
.waitForCompletion(true)
420+
);
421+
422+
assertNotNull(snapshot.snapshot());
423+
}
424+
397425
@Test
398426
public void testValueBodyResponse() throws Exception {
399427
DiskUsageResponse resp = client.indices().diskUsage(b -> b

0 commit comments

Comments
 (0)