diff options
author | Jon Bratseth <bratseth@gmail.com> | 2022-02-24 11:27:28 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-24 11:27:28 +0100 |
commit | 79b80263090de0519791175782ac192725238d35 (patch) | |
tree | 1170f503826b381121c62b4db10b824733f35e4b /controller-server | |
parent | 122aa5b6f4f7c61b8662df6e2115a91af640de33 (diff) | |
parent | 392edc58a07748b7d0acabbf5585329bf3f7ed9d (diff) |
Merge pull request #21362 from vespa-engine/mpolden/cli-compat
Warn on API incompatibility in Vespa CLI
Diffstat (limited to 'controller-server')
3 files changed, 94 insertions, 0 deletions
diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/CliApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/CliApiHandler.java new file mode 100644 index 00000000000..ab8b6a1d26f --- /dev/null +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/CliApiHandler.java @@ -0,0 +1,65 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.hosted.controller.restapi.deployment; + +import com.yahoo.component.Version; +import com.yahoo.container.jdisc.HttpRequest; +import com.yahoo.container.jdisc.HttpResponse; +import com.yahoo.container.jdisc.ThreadedHttpRequestHandler; +import com.yahoo.restapi.ErrorResponse; +import com.yahoo.restapi.Path; +import com.yahoo.restapi.SlimeJsonResponse; +import com.yahoo.slime.Cursor; +import com.yahoo.slime.Slime; +import com.yahoo.yolean.Exceptions; + +import java.util.logging.Level; + +/** + * This handler implements the /cli/v1/ API. The API allows Vespa CLI to retrieve information about the system, without + * authorization. One example of such information is the minimum Vespa CLI version supported by our APIs. + * + * @author mpolden + */ +public class CliApiHandler extends ThreadedHttpRequestHandler { + + /** + * The minimum version of Vespa CLI which is considered compatible with our APIs. If a version of Vespa CLI below + * this version tries to use our APIs, Vespa CLI will print a warning instructing the user to upgrade. + */ + private static final Version MIN_CLI_VERSION = Version.fromString("7.547.18"); + + public CliApiHandler(Context context) { + super(context); + } + + @Override + public HttpResponse handle(HttpRequest request) { + try { + switch (request.getMethod()) { + case GET: return get(request); + default: return ErrorResponse.methodNotAllowed("Method '" + request.getMethod() + "' is not supported"); + } + } + catch (IllegalArgumentException e) { + return ErrorResponse.badRequest(Exceptions.toMessageString(e)); + } + catch (RuntimeException e) { + log.log(Level.WARNING, "Unexpected error handling '" + request.getUri() + "'", e); + return ErrorResponse.internalServerError(Exceptions.toMessageString(e)); + } + } + + private HttpResponse get(HttpRequest request) { + Path path = new Path(request.getUri()); + if (path.matches("/cli/v1/")) return root(); + return ErrorResponse.notFoundError("Nothing at " + path); + } + + private HttpResponse root() { + Slime slime = new Slime(); + Cursor root = slime.setObject(); + root.setString("minVersion", MIN_CLI_VERSION.toFullString()); + return new SlimeJsonResponse(slime); + } + +} diff --git a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/ControllerContainerTest.java b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/ControllerContainerTest.java index 621a3272918..6b13e6c951a 100644 --- a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/ControllerContainerTest.java +++ b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/ControllerContainerTest.java @@ -78,6 +78,9 @@ public class ControllerContainerTest { " <handler id='com.yahoo.vespa.hosted.controller.restapi.deployment.BadgeApiHandler'>\n" + " <binding>http://*/badge/v1/*</binding>\n" + " </handler>\n" + + " <handler id='com.yahoo.vespa.hosted.controller.restapi.deployment.CliApiHandler'>\n" + + " <binding>http://*/cli/v1/*</binding>\n" + + " </handler>\n" + " <handler id='com.yahoo.vespa.hosted.controller.restapi.controller.ControllerApiHandler'>\n" + " <binding>http://*/controller/v1/*</binding>\n" + " </handler>\n" + diff --git a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/application/CliApiHandlerTest.java b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/application/CliApiHandlerTest.java new file mode 100644 index 00000000000..5a4d47e0db5 --- /dev/null +++ b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/application/CliApiHandlerTest.java @@ -0,0 +1,26 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.hosted.controller.restapi.application; + +import com.yahoo.vespa.hosted.controller.restapi.ContainerTester; +import com.yahoo.vespa.hosted.controller.restapi.ControllerContainerTest; +import org.junit.Before; +import org.junit.Test; + +/** + * @author mpolden + */ +public class CliApiHandlerTest extends ControllerContainerTest { + + private ContainerTester tester; + + @Before + public void before() { + tester = new ContainerTester(container, "src/test/java/com/yahoo/vespa/hosted/controller/restapi/application/responses/"); + } + + @Test + public void root() { + tester.assertResponse(authenticatedRequest("http://localhost:8080/cli/v1/"), "{\"minVersion\":\"7.547.18\"}"); + } + +} |