aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/zone/v1/ZoneApiHandler.java
blob: 7978e64482b068d2b80bc42638febf9433e244dc (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// 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.zone.v1;

import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.zone.ZoneApi;
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.api.integration.ServiceRegistry;
import com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneRegistry;
import com.yahoo.vespa.hosted.controller.restapi.ErrorResponses;
import com.yahoo.yolean.Exceptions;

import java.util.Comparator;
import java.util.List;

/**
 * Read-only REST API that provides information about zones in hosted Vespa (version 1)
 *
 * @author mpolden
 */
@SuppressWarnings("unused")
public class ZoneApiHandler extends ThreadedHttpRequestHandler {

    private final ZoneRegistry zoneRegistry;

    public ZoneApiHandler(ThreadedHttpRequestHandler.Context parentCtx, ServiceRegistry serviceRegistry) {
        super(parentCtx);
        this.zoneRegistry = serviceRegistry.zoneRegistry();
    }

    @Override
    public HttpResponse handle(HttpRequest request) {
        try {
            return switch (request.getMethod()) {
                case GET -> get(request);
                default -> ErrorResponse.methodNotAllowed("Method '" + request.getMethod() + "' is unsupported");
            };
        } 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("/zone/v1")) {
            return root(request);
        }
        if (path.matches("/zone/v1/environment/{environment}")) {
            return environment(request, Environment.from(path.get("environment")));
        }
        if (path.matches("/zone/v1/environment/{environment}/default")) {
            return defaultRegion(request, Environment.from(path.get("environment")));
        }
        return notFound(path);
    }

    private HttpResponse root(HttpRequest request) {
        List<Environment> environments = zoneRegistry.zones().publiclyVisible().zones().stream()
                                                     .map(ZoneApi::getEnvironment)
                                                     .distinct()
                                                     .sorted(Comparator.comparing(Environment::value))
                                                     .toList();
        Slime slime = new Slime();
        Cursor root = slime.setArray();
        environments.forEach(environment -> {
            Cursor object = root.addObject();
            object.setString("name", environment.value());
            object.setString("url", request.getUri()
                                           .resolve("/zone/v1/environment/")
                                           .resolve(environment.value())
                                           .toString());
        });
        return new SlimeJsonResponse(slime);
    }

    private HttpResponse environment(HttpRequest request, Environment environment) {
        Slime slime = new Slime();
        Cursor root = slime.setArray();
        zoneRegistry.zones().publiclyVisible().all().in(environment).zones().forEach(zone -> {
            Cursor object = root.addObject();
            object.setString("name", zone.getRegionName().value());
            object.setString("url", request.getUri()
                                           .resolve("/zone/v2/")
                                           .resolve(environment.value() + "/")
                                           .resolve(zone.getRegionName().value())
                                           .toString());
        });
        return new SlimeJsonResponse(slime);
    }

    private HttpResponse defaultRegion(HttpRequest request, Environment environment) {
        RegionName region = zoneRegistry.getDefaultRegion(environment)
                .orElseThrow(() -> new IllegalArgumentException("No default region for environment: " + environment));
        Slime slime = new Slime();
        Cursor root = slime.setObject();
        root.setString("name", region.value());
        root.setString("url", request.getUri()
                                     .resolve("/zone/v2/")
                                     .resolve(environment.value() + "/")
                                     .resolve(region.value())
                                     .toString());
        return new SlimeJsonResponse(slime);
    }

    private HttpResponse notFound(Path path) {
        return ErrorResponse.notFoundError("Nothing at " + path);
    }

}