Browse Source

[Rest Api Compatibility] Typed and xpack graph explore api (#74185)

adds back the typed and xpack endpoints for graph explore api
prevoiusly removed in #46935

relates main meta issue #51816
relates types removal issue #54160
Przemyslaw Gomulka 4 years ago
parent
commit
270eae53e9

+ 25 - 2
x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/rest/action/RestGraphAction.java

@@ -10,8 +10,10 @@ package org.elasticsearch.xpack.graph.rest.action;
 import org.elasticsearch.ElasticsearchParseException;
 import org.elasticsearch.action.support.IndicesOptions;
 import org.elasticsearch.client.node.NodeClient;
+import org.elasticsearch.common.logging.DeprecationLogger;
 import org.elasticsearch.common.xcontent.ParseField;
 import org.elasticsearch.common.Strings;
+import org.elasticsearch.core.RestApiVersion;
 import org.elasticsearch.core.TimeValue;
 import org.elasticsearch.common.xcontent.XContentParser;
 import org.elasticsearch.protocol.xpack.graph.GraphExploreRequest;
@@ -38,6 +40,11 @@ import static org.elasticsearch.xpack.core.graph.action.GraphExploreAction.INSTA
  */
 public class RestGraphAction extends BaseRestHandler {
 
+    private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestGraphAction.class);
+    public static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" +
+        " Specifying types in graph requests is deprecated.";
+    private static final String URI_BASE = "/_xpack";
+
     public static final ParseField TIMEOUT_FIELD = new ParseField("timeout");
     public static final ParseField SIGNIFICANCE_FIELD = new ParseField("use_significance");
     public static final ParseField RETURN_DETAILED_INFO = new ParseField("return_detailed_stats");
@@ -60,8 +67,19 @@ public class RestGraphAction extends BaseRestHandler {
     @Override
     public List<Route> routes() {
         return List.of(
-            new Route(GET, "/{index}/_graph/explore"),
-            new Route(POST, "/{index}/_graph/explore"));
+            Route.builder(GET, "/{index}/_graph/explore")
+                .replaces(GET, "/{index}" + URI_BASE + "/graph/_explore", RestApiVersion.V_7).build(),
+            Route.builder(POST, "/{index}/_graph/explore")
+                .replaces(POST, "/{index}" + URI_BASE + "/graph/_explore", RestApiVersion.V_7).build(),
+            Route.builder(GET, "/{index}/{type}/_graph/explore")
+                .deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7).build(),
+            Route.builder(GET, "/{index}/{type}" + URI_BASE + "/graph/_explore")
+                .deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7).build(),
+            Route.builder(POST, "/{index}/{type}/_graph/explore")
+                .deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7).build(),
+            Route.builder(POST, "/{index}/{type}" + URI_BASE + "/graph/_explore")
+                .deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7).build()
+        );
     }
 
     @Override
@@ -71,6 +89,11 @@ public class RestGraphAction extends BaseRestHandler {
 
     @Override
     public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
+        if (request.getRestApiVersion() == RestApiVersion.V_7 && request.hasParam("type")) {
+            deprecationLogger.compatibleApiWarning("graph_with_types", TYPES_DEPRECATION_MESSAGE);
+            request.param("type");
+        }
+
         GraphExploreRequest graphRequest = new GraphExploreRequest(Strings.splitStringByCommaToArray(request.param("index")));
         graphRequest.indicesOptions(IndicesOptions.fromRequest(request, graphRequest.indicesOptions()));
         graphRequest.routing(request.param("routing"));

+ 61 - 0
x-pack/plugin/graph/src/test/java/org/elasticsearch/xpack/graph/rest/action/RestGraphActionTests.java

@@ -0,0 +1,61 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0; you may not use this file except in compliance with the Elastic License
+ * 2.0.
+ */
+
+package org.elasticsearch.xpack.graph.rest.action;
+
+import org.elasticsearch.common.bytes.BytesArray;
+import org.elasticsearch.common.xcontent.XContentType;
+import org.elasticsearch.core.RestApiVersion;
+import org.elasticsearch.core.Tuple;
+import org.elasticsearch.protocol.xpack.graph.GraphExploreRequest;
+import org.elasticsearch.protocol.xpack.graph.GraphExploreResponse;
+import org.elasticsearch.rest.RestRequest;
+import org.elasticsearch.test.rest.FakeRestRequest;
+import org.elasticsearch.test.rest.RestActionTestCase;
+import org.junit.Before;
+import org.mockito.Mockito;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import static org.hamcrest.Matchers.instanceOf;
+
+public class RestGraphActionTests extends RestActionTestCase {
+    private final List<String> compatibleMediaType = Collections.singletonList(randomCompatibleMediaType(RestApiVersion.V_7));
+
+    @Before
+    public void setUpAction() {
+        controller().registerHandler(new RestGraphAction());
+        verifyingClient.setExecuteVerifier((actionType, request) -> {
+            assertThat(request, instanceOf(GraphExploreRequest.class));
+            return Mockito.mock(GraphExploreResponse.class);
+        });
+    }
+
+    public void testTypeInPath() {
+        for (Tuple<RestRequest.Method, String> methodAndPath :
+            List.of(
+                Tuple.tuple(RestRequest.Method.GET, "/some_index/some_type/_graph/explore"),
+                Tuple.tuple(RestRequest.Method.POST, "/some_index/some_type/_graph/explore"),
+                Tuple.tuple(RestRequest.Method.GET, "/some_index/some_type/_xpack/graph/_explore"),
+                Tuple.tuple(RestRequest.Method.POST, "/some_index/some_type/_xpack/graph/_explore")
+            )) {
+
+            RestRequest request = new FakeRestRequest.Builder(xContentRegistry())
+                .withHeaders(Map.of("Accept", compatibleMediaType, "Content-Type",
+                    Collections.singletonList(compatibleMediaType(XContentType.VND_JSON, RestApiVersion.V_7))))
+                .withMethod(methodAndPath.v1())
+                .withPath(methodAndPath.v2())
+                .withContent(new BytesArray("{}"), null)
+                .build();
+
+            dispatchRequest(request);
+            assertWarnings(RestGraphAction.TYPES_DEPRECATION_MESSAGE);
+        }
+    }
+}