blob: 0f8d17969834862bd66b57da12dfed24b7aec99c (
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.http.v2;
import com.google.common.collect.ImmutableSet;
import com.yahoo.component.annotation.Inject;
import com.yahoo.config.provision.TenantName;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.restapi.ErrorResponse;
import com.yahoo.restapi.RestApi;
import com.yahoo.restapi.RestApiException;
import com.yahoo.restapi.RestApiRequestHandler;
import com.yahoo.vespa.config.server.ApplicationRepository;
import com.yahoo.vespa.config.server.http.v2.response.ListTenantsResponse;
import com.yahoo.vespa.config.server.http.v2.response.TenantCreateResponse;
import com.yahoo.vespa.config.server.http.v2.response.TenantDeleteResponse;
import com.yahoo.vespa.config.server.http.v2.response.TenantGetResponse;
import com.yahoo.vespa.config.server.tenant.TenantRepository;
import com.yahoo.yolean.Exceptions;
/**
* Handler to create, get and delete a tenant, and listing of tenants.
*
* @author jonmv
*/
public class TenantHandler extends RestApiRequestHandler<TenantHandler> {
private static final String TENANT_NAME_REGEXP = "[\\w-]+";
private final TenantRepository tenantRepository;
private final ApplicationRepository applicationRepository;
@Inject
public TenantHandler(Context ctx, ApplicationRepository applicationRepository) {
super(ctx, TenantHandler::defineApi);
this.tenantRepository = applicationRepository.tenantRepository();
this.applicationRepository = applicationRepository;
}
private RestApi defineApi() {
return RestApi.builder()
.addRoute(RestApi.route("/application/v2/tenant")
.get(this::getTenants))
.addRoute(RestApi.route("/application/v2/tenant/{tenant}")
.get(this::getTenant)
.put(this::putTenant)
.delete(this::deleteTenant))
.addExceptionMapper(IllegalArgumentException.class, (c, e) -> ErrorResponse.badRequest(Exceptions.toMessageString(e)))
.addExceptionMapper(RuntimeException.class, (c, e) -> ErrorResponse.internalServerError(Exceptions.toMessageString(e)))
.build();
}
private HttpResponse getTenants(RestApi.RequestContext context) {
return new ListTenantsResponse(ImmutableSet.copyOf(tenantRepository.getAllTenantNames()));
}
private HttpResponse getTenant(RestApi.RequestContext context) {
TenantName name = TenantName.from(context.pathParameters().getStringOrThrow("tenant"));
if ( ! tenantRepository.checkThatTenantExists(name))
throw new RestApiException.NotFound("Tenant '" + name + "' was not found.");
return new TenantGetResponse(name);
}
private HttpResponse putTenant(RestApi.RequestContext context) {
TenantName name = TenantName.from(context.pathParameters().getStringOrThrow("tenant"));
if (tenantRepository.checkThatTenantExists(name))
throw new RestApiException.BadRequest("There already exists a tenant '" + name + "'");
if ( ! name.value().matches(TENANT_NAME_REGEXP))
throw new RestApiException.BadRequest("Illegal tenant name: " + name);
tenantRepository.addTenant(name);
return new TenantCreateResponse(name);
}
private HttpResponse deleteTenant(RestApi.RequestContext context) {
TenantName name = TenantName.from(context.pathParameters().getStringOrThrow("tenant"));
if ( ! tenantRepository.checkThatTenantExists(name))
throw new RestApiException.NotFound("Tenant '" + name + "' was not found.");
applicationRepository.deleteTenant(name);
return new TenantDeleteResponse(name);
}
}
|