Browse Source

Add info method to High Level Rest client (#23350)

This commit adds support for an info() method to the High Level Rest
client that returns the cluster information usually obtained by performing a
`GET hostname:9200` request.
Christoph Büscher 8 years ago
parent
commit
3215ac11ec

+ 6 - 2
client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java

@@ -29,8 +29,8 @@ import org.apache.http.entity.ByteArrayEntity;
 import org.apache.http.entity.ContentType;
 import org.apache.lucene.util.BytesRef;
 import org.elasticsearch.action.DocWriteRequest;
-import org.elasticsearch.action.delete.DeleteRequest;
 import org.elasticsearch.action.bulk.BulkRequest;
+import org.elasticsearch.action.delete.DeleteRequest;
 import org.elasticsearch.action.get.GetRequest;
 import org.elasticsearch.action.index.IndexRequest;
 import org.elasticsearch.action.support.ActiveShardCount;
@@ -97,6 +97,10 @@ final class Request {
         return new Request(HttpDelete.METHOD_NAME, endpoint, parameters.getParams(), null);
     }
 
+    static Request info() {
+        return new Request(HttpGet.METHOD_NAME, "/", Collections.emptyMap(), null);
+    }
+
     static Request bulk(BulkRequest bulkRequest) throws IOException {
         Params parameters = Params.builder();
         parameters.withTimeout(bulkRequest.timeout());
@@ -264,7 +268,7 @@ final class Request {
     }
 
     static Request ping() {
-        return new Request("HEAD", "/", Collections.emptyMap(), null);
+        return new Request(HttpHead.METHOD_NAME, "/", Collections.emptyMap(), null);
     }
 
     static Request update(UpdateRequest updateRequest) throws IOException {

+ 9 - 0
client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java

@@ -35,6 +35,7 @@ import org.elasticsearch.action.get.GetResponse;
 import org.elasticsearch.action.index.IndexRequest;
 import org.elasticsearch.action.index.IndexResponse;
 import org.elasticsearch.action.main.MainRequest;
+import org.elasticsearch.action.main.MainResponse;
 import org.elasticsearch.action.update.UpdateRequest;
 import org.elasticsearch.action.update.UpdateResponse;
 import org.elasticsearch.common.CheckedFunction;
@@ -113,6 +114,14 @@ public class RestHighLevelClient {
                 emptySet(), headers);
     }
 
+    /**
+     * Get the cluster info otherwise provided when sending an HTTP request to port 9200
+     */
+    public MainResponse info(Header... headers) throws IOException {
+        return performRequestAndParseEntity(new MainRequest(), (request) -> Request.info(), MainResponse::fromXContent, emptySet(),
+                headers);
+    }
+
     /**
      * Retrieves a document by id using the Get API
      *

+ 1 - 1
client/rest-high-level/src/test/java/org/elasticsearch/client/ESRestHighLevelClientTestCase.java

@@ -41,7 +41,7 @@ public abstract class ESRestHighLevelClientTestCase extends ESRestTestCase {
     }
 
     @AfterClass
-    public static void cleanupClient() throws IOException {
+    public static void cleanupClient() {
         restHighLevelClient = null;
     }
 

+ 21 - 1
client/rest-high-level/src/test/java/org/elasticsearch/client/PingAndInfoIT.java

@@ -19,7 +19,10 @@
 
 package org.elasticsearch.client;
 
+import org.elasticsearch.action.main.MainResponse;
+
 import java.io.IOException;
+import java.util.Map;
 
 public class PingAndInfoIT extends ESRestHighLevelClientTestCase {
 
@@ -27,5 +30,22 @@ public class PingAndInfoIT extends ESRestHighLevelClientTestCase {
         assertTrue(highLevelClient().ping());
     }
 
-    //TODO add here integ tests for info api: "GET /" once we have parsing code for MainResponse
+    @SuppressWarnings("unchecked")
+    public void testInfo() throws IOException {
+        MainResponse info = highLevelClient().info();
+        // compare with what the low level client outputs
+        Map<String, Object> infoAsMap = entityAsMap(adminClient().performRequest("GET", "/"));
+        assertEquals(infoAsMap.get("cluster_name"), info.getClusterName().value());
+        assertEquals(infoAsMap.get("cluster_uuid"), info.getClusterUuid());
+
+        // only check node name existence, might be a different one from what was hit by low level client in multi-node cluster
+        assertNotNull(info.getNodeName());
+        Map<String, Object> versionMap = (Map<String, Object>) infoAsMap.get("version");
+        assertEquals(versionMap.get("build_hash"), info.getBuild().shortHash());
+        assertEquals(versionMap.get("build_date"), info.getBuild().date());
+        assertEquals(versionMap.get("build_snapshot"), info.getBuild().isSnapshot());
+        assertEquals(versionMap.get("number"), info.getVersion().toString());
+        assertEquals(versionMap.get("lucene_version"), info.getVersion().luceneVersion.toString());
+    }
+
 }

+ 8 - 0
client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java

@@ -66,6 +66,14 @@ public class RequestTests extends ESTestCase {
         assertEquals("HEAD", request.method);
     }
 
+    public void testInfo() {
+        Request request = Request.info();
+        assertEquals("/", request.endpoint);
+        assertEquals(0, request.params.size());
+        assertNull(request.entity);
+        assertEquals("GET", request.method);
+    }
+
     public void testGet() {
         getAndExistsTest(Request::get, "GET");
     }

+ 26 - 4
client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java

@@ -20,6 +20,7 @@
 package org.elasticsearch.client;
 
 import com.fasterxml.jackson.core.JsonParseException;
+
 import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpHost;
@@ -33,15 +34,20 @@ import org.apache.http.entity.StringEntity;
 import org.apache.http.message.BasicHttpResponse;
 import org.apache.http.message.BasicRequestLine;
 import org.apache.http.message.BasicStatusLine;
+import org.elasticsearch.Build;
 import org.elasticsearch.ElasticsearchException;
+import org.elasticsearch.Version;
 import org.elasticsearch.action.ActionListener;
 import org.elasticsearch.action.ActionRequest;
 import org.elasticsearch.action.ActionRequestValidationException;
 import org.elasticsearch.action.main.MainRequest;
+import org.elasticsearch.action.main.MainResponse;
+import org.elasticsearch.cluster.ClusterName;
 import org.elasticsearch.common.CheckedFunction;
 import org.elasticsearch.common.xcontent.NamedXContentRegistry;
 import org.elasticsearch.common.xcontent.XContentBuilder;
 import org.elasticsearch.common.xcontent.XContentParser;
+import org.elasticsearch.common.xcontent.XContentType;
 import org.elasticsearch.common.xcontent.cbor.CborXContent;
 import org.elasticsearch.common.xcontent.smile.SmileXContent;
 import org.elasticsearch.rest.RestStatus;
@@ -59,6 +65,7 @@ import java.util.List;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
 
+import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.mockito.Matchers.anyMapOf;
 import static org.mockito.Matchers.anyObject;
@@ -79,7 +86,7 @@ public class RestHighLevelClientTests extends ESTestCase {
     private RestHighLevelClient restHighLevelClient;
 
     @Before
-    public void initClient() throws IOException {
+    public void initClient() {
         restClient = mock(RestClient.class);
         restHighLevelClient = new RestHighLevelClient(restClient);
     }
@@ -115,6 +122,21 @@ public class RestHighLevelClientTests extends ESTestCase {
                 Matchers.isNull(HttpEntity.class), argThat(new HeadersVarargMatcher(headers)));
     }
 
+    public void testInfo() throws IOException {
+        Header[] headers = RestClientTestUtil.randomHeaders(random(), "Header");
+        Response response = mock(Response.class);
+        MainResponse testInfo = new MainResponse("nodeName", Version.CURRENT, new ClusterName("clusterName"), "clusterUuid",
+                Build.CURRENT, true);
+        when(response.getEntity()).thenReturn(
+                new StringEntity(toXContent(testInfo, XContentType.JSON, false).utf8ToString(), ContentType.APPLICATION_JSON));
+        when(restClient.performRequest(anyString(), anyString(), anyMapOf(String.class, String.class),
+                anyObject(), anyVararg())).thenReturn(response);
+        MainResponse receivedInfo = restHighLevelClient.info(headers);
+        assertEquals(testInfo, receivedInfo);
+        verify(restClient).performRequest(eq("GET"), eq("/"), eq(Collections.emptyMap()),
+                Matchers.isNull(HttpEntity.class), argThat(new HeadersVarargMatcher(headers)));
+    }
+
     public void testRequestValidation() {
         ActionRequestValidationException validationException = new ActionRequestValidationException();
         validationException.addValidationError("validation error");
@@ -388,7 +410,7 @@ public class RestHighLevelClientTests extends ESTestCase {
         assertEquals("Elasticsearch exception [type=exception, reason=test error message]", elasticsearchException.getMessage());
     }
 
-    public void testWrapResponseListenerOnSuccess() throws IOException {
+    public void testWrapResponseListenerOnSuccess() {
         {
             TrackingActionListener trackingActionListener = new TrackingActionListener();
             ResponseListener responseListener = restHighLevelClient.wrapResponseListener(
@@ -414,7 +436,7 @@ public class RestHighLevelClientTests extends ESTestCase {
         }
     }
 
-    public void testWrapResponseListenerOnException() throws IOException {
+    public void testWrapResponseListenerOnException() {
         TrackingActionListener trackingActionListener = new TrackingActionListener();
         ResponseListener responseListener = restHighLevelClient.wrapResponseListener(
                 response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet());
@@ -543,7 +565,7 @@ public class RestHighLevelClientTests extends ESTestCase {
         assertEquals("Elasticsearch exception [type=exception, reason=test error message]", elasticsearchException.getMessage());
     }
 
-    public void testNamedXContents() throws IOException {
+    public void testNamedXContents() {
         List<NamedXContentRegistry.Entry> namedXContents = RestHighLevelClient.getNamedXContents();
         assertEquals(0, namedXContents.size());
     }