summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/cost/CostApiHandler.java
blob: a82d2f22e74d3d9b52ac13f5024b74a8b2e56af1 (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
package com.yahoo.vespa.hosted.controller.restapi.cost;

import com.yahoo.config.provision.CloudName;
import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.container.jdisc.LoggingRequestHandler;
import com.yahoo.restapi.Path;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.api.integration.noderepository.NodeRepositoryClientInterface;
import com.yahoo.vespa.hosted.controller.restapi.ErrorResponse;
import com.yahoo.vespa.hosted.controller.restapi.StringResponse;
import com.yahoo.vespa.hosted.controller.restapi.cost.config.SelfHostedCostConfig;

import java.time.Clock;
import java.util.Optional;

import static com.yahoo.jdisc.http.HttpRequest.Method.GET;

public class CostApiHandler extends LoggingRequestHandler {

    private final Controller controller;
    private final NodeRepositoryClientInterface nodeRepository;
    private final SelfHostedCostConfig selfHostedCostConfig;

    public CostApiHandler(Context ctx, Controller controller, NodeRepositoryClientInterface nodeRepository, SelfHostedCostConfig selfHostedCostConfig) {
        super(ctx);
        this.controller = controller;
        this.nodeRepository = nodeRepository;
        this.selfHostedCostConfig = selfHostedCostConfig;
    }

    @Override
    public HttpResponse handle(HttpRequest request) {
        if (request.getMethod() != GET) {
            return ErrorResponse.methodNotAllowed("Method '" + request.getMethod() + "' is not supported");
        }

        Path path = new Path(request.getUri().getPath());

        if (path.matches("/cost/v1/csv")) {
            Optional<String> cloudProperty = Optional.ofNullable(request.getProperty("cloud"));
            CloudName cloud = cloudProperty.map(CloudName::from).orElse(CloudName.defaultName());
            return new StringResponse(CostCalculator.resourceShareByPropertyToCsv(nodeRepository, controller, Clock.systemUTC(), selfHostedCostConfig, cloud));
        }

        return ErrorResponse.notFoundError("Nothing at " + path);
    }
}