aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/CliApiHandler.java
blob: 150acd297c253a3d5e0d46ab35f51d61bbba8cd3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright Vespa.ai. 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.vespa.hosted.controller.restapi.ErrorResponses;
import com.yahoo.yolean.Exceptions;

/**
 * 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 {
            return switch (request.getMethod()) {
                case GET -> get(request);
                default -> ErrorResponse.methodNotAllowed("Method '" + request.getMethod() + "' is not supported");
            };
        }
        catch (IllegalArgumentException e) {
            return ErrorResponse.badRequest(Exceptions.toMessageString(e));
        }
        catch (RuntimeException e) {
            return ErrorResponses.logThrowing(request, log, 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);
    }

}