diff options
author | Harald Musum <musum@oath.com> | 2017-11-02 15:41:46 +0100 |
---|---|---|
committer | Harald Musum <musum@oath.com> | 2017-11-02 15:41:46 +0100 |
commit | e8b61d4a613f7430a26adfca86c9d3faf6602c16 (patch) | |
tree | 247af92c68573f75f98823010876357fb1083cf9 /configserver | |
parent | 43872567307f09428c87bcd57b1962e01bec3186 (diff) |
Remove ListTenantsHandler (move functionality to TenantsHandler)
Diffstat (limited to 'configserver')
7 files changed, 59 insertions, 145 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/http/Utils.java b/configserver/src/main/java/com/yahoo/vespa/config/server/http/Utils.java index 5c26e80f9f8..873a24b5f05 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/http/Utils.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/http/Utils.java @@ -47,11 +47,6 @@ public class Utils { throw new NotFoundException("Tenant '" + tenantName + "' was not found."); } - public static TenantName getTenantNameFromRequest(HttpRequest request) { - BindingMatch<?> bm = getBindingMatch(request, "http://*/application/v2/tenant/*"); - return TenantName.from(bm.group(2)); - } - public static TenantName getTenantNameFromSessionRequest(HttpRequest request) { BindingMatch<?> bm = getBindingMatch(request, "http://*/application/v2/tenant/*/session*"); return TenantName.from(bm.group(2)); diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ListTenantsHandler.java b/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ListTenantsHandler.java deleted file mode 100644 index 137f599ac8b..00000000000 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ListTenantsHandler.java +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2017 Yahoo Holdings. 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 java.util.concurrent.Executor; - -import com.yahoo.container.jdisc.HttpRequest; -import com.yahoo.container.jdisc.HttpResponse; -import com.yahoo.container.logging.AccessLog; -import com.yahoo.vespa.config.server.http.HttpHandler; -import com.yahoo.vespa.config.server.tenant.Tenants; - -/** - * Handler to list tenants in the configserver - * - * @author vegardh - */ -public class ListTenantsHandler extends HttpHandler { - - private final Tenants tenants; - - - public ListTenantsHandler(Executor executor, AccessLog accessLog, Tenants tenants) { - super(executor, accessLog); - this.tenants = tenants; - } - - - @Override - protected HttpResponse handleGET(HttpRequest request) { - return new ListTenantsResponse(tenants.getAllTenantNames()); - } - -} diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/TenantHandler.java b/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/TenantHandler.java index 46d007126c0..5c1d8a36f6a 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/TenantHandler.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/TenantHandler.java @@ -9,6 +9,7 @@ import com.yahoo.config.provision.TenantName; import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.container.logging.AccessLog; +import com.yahoo.jdisc.application.BindingMatch; import com.yahoo.vespa.config.server.tenant.Tenant; import com.yahoo.yolean.Exceptions; import com.yahoo.vespa.config.server.application.TenantApplications; @@ -19,7 +20,7 @@ import com.yahoo.vespa.config.server.http.Utils; import com.yahoo.vespa.config.server.tenant.Tenants; /** - * Handler to create, get and delete a tenant. + * Handler to create, get and delete a tenant, and listing of tenants. * * @author vegardh */ @@ -44,35 +45,22 @@ public class TenantHandler extends HttpHandler { return new TenantCreateResponse(tenantName); } - /** - * Gets the tenant name from the request, throws if it exists already and validates its name - * - * @param request an {@link com.yahoo.container.jdisc.HttpRequest} - * @return tenant name - */ - private TenantName getAndValidateTenantFromRequest(HttpRequest request) { - final TenantName tenantName = Utils.getTenantNameFromRequest(request); - checkThatTenantDoesNotExist(tenantName); - validateTenantName(tenantName); - return tenantName; - } - - private void validateTenantName(TenantName tenant) { - if (!tenant.value().matches(TENANT_NAME_REGEXP)) { - throw new BadRequestException("Illegal tenant name: " + tenant); - } - } - @Override protected HttpResponse handleGET(HttpRequest request) { - final TenantName tenantName = Utils.getTenantNameFromRequest(request); - Utils.checkThatTenantExists(tenants, tenantName); - return new TenantGetResponse(tenantName); + if (isGetTenantRequest(request)) { + final TenantName tenantName = getTenantNameFromRequest(request); + Utils.checkThatTenantExists(tenants, tenantName); + return new TenantGetResponse(tenantName); + } else if (isListTenantsRequest(request)) { + return new ListTenantsResponse(tenants.getAllTenantNames()); + } else { + throw new BadRequestException(request.getUri().toString()); + } } @Override protected HttpResponse handleDELETE(HttpRequest request) { - final TenantName tenantName = Utils.getTenantNameFromRequest(request); + final TenantName tenantName = getTenantNameFromRequest(request); Utils.checkThatTenantExists(tenants, tenantName); Tenant tenant = tenants.getTenant(tenantName); TenantApplications applicationRepo = tenant.getApplicationRepo(); @@ -90,9 +78,48 @@ public class TenantHandler extends HttpHandler { return new TenantDeleteResponse(tenantName); } + /** + * Gets the tenant name from the request, throws if it exists already and validates its name + * + * @param request an {@link com.yahoo.container.jdisc.HttpRequest} + * @return tenant name + */ + private TenantName getAndValidateTenantFromRequest(HttpRequest request) { + final TenantName tenantName = getTenantNameFromRequest(request); + checkThatTenantDoesNotExist(tenantName); + validateTenantName(tenantName); + return tenantName; + } + + private void validateTenantName(TenantName tenant) { + if (!tenant.value().matches(TENANT_NAME_REGEXP)) { + throw new BadRequestException("Illegal tenant name: " + tenant); + } + } + private void checkThatTenantDoesNotExist(TenantName tenantName) { if (tenants.checkThatTenantExists(tenantName)) throw new BadRequestException("There already exists a tenant '" + tenantName + "'"); } + private static BindingMatch<?> getBindingMatch(HttpRequest request) { + return HttpConfigRequests.getBindingMatch(request, + "http://*/application/v2/tenant/", + "http://*/application/v2/tenant/*"); + } + + private static boolean isGetTenantRequest(HttpRequest request) { + return getBindingMatch(request).groupCount() == 3; + } + + private static boolean isListTenantsRequest(HttpRequest request) { + return getBindingMatch(request).groupCount() == 2 && + request.getUri().getPath().endsWith("/tenant/"); + } + + private static TenantName getTenantNameFromRequest(HttpRequest request) { + BindingMatch<?> bm = getBindingMatch(request); + return TenantName.from(bm.group(2)); + } + } diff --git a/configserver/src/main/resources/configserver-app/services.xml b/configserver/src/main/resources/configserver-app/services.xml index bf147b167e9..7ad1b3bbbfd 100644 --- a/configserver/src/main/resources/configserver-app/services.xml +++ b/configserver/src/main/resources/configserver-app/services.xml @@ -80,10 +80,8 @@ <binding>http://*/config/v1/*/</binding> <binding>http://*/config/v1/*/*/</binding> </handler> - <handler id='com.yahoo.vespa.config.server.http.v2.ListTenantsHandler' bundle='configserver'> - <binding>http://*/application/v2/tenant/</binding> - </handler> <handler id='com.yahoo.vespa.config.server.http.v2.TenantHandler' bundle='configserver'> + <binding>http://*/application/v2/tenant/</binding> <binding>http://*/application/v2/tenant/*</binding> </handler> <handler id='com.yahoo.vespa.config.server.http.v2.SessionCreateHandler' bundle='configserver'> diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ListTenantsResponseTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ListTenantsResponseTest.java deleted file mode 100644 index fb3c3434042..00000000000 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ListTenantsResponseTest.java +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2017 Yahoo Holdings. 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.yahoo.config.provision.TenantName; -import org.junit.Test; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; - -public class ListTenantsResponseTest extends TenantTest { - - private final TenantName a = TenantName.from("a"); - private final TenantName b = TenantName.from("b"); - private final TenantName c = TenantName.from("c"); - - @Test - public void testJsonSerialization() throws Exception { - final Collection<TenantName> tenantNames = Arrays.asList(a, b, c); - final ListTenantsResponse response = new ListTenantsResponse(tenantNames); - assertResponseEquals(response, "{\"tenants\":[\"a\",\"b\",\"c\"]}"); - } - - @Test - public void testJsonSerializationNoTenants() throws Exception { - final Collection<TenantName> tenantNames = Collections.emptyList(); - final ListTenantsResponse response = new ListTenantsResponse(tenantNames); - assertResponseEquals(response, "{\"tenants\":[]}"); - } -} diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ListTenantsTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ListTenantsTest.java deleted file mode 100644 index aa27dd0783c..00000000000 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ListTenantsTest.java +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2017 Yahoo Holdings. 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.yahoo.config.provision.TenantName; -import org.junit.Test; -import com.yahoo.container.jdisc.HttpRequest; -import com.yahoo.jdisc.http.HttpRequest.Method; - -import java.util.Arrays; -import java.util.Collection; - -import static org.junit.Assert.assertTrue; - -public class ListTenantsTest extends TenantTest { - - private final TenantName a = TenantName.from("a"); - private final TenantName b = TenantName.from("b"); - private final TenantName c = TenantName.from("c"); - - @Test - public void testListTenants() throws Exception { - tenants.addTenant(a); - tenants.addTenant(b); - tenants.addTenant(c); - - ListTenantsHandler listTenantsHandler = new ListTenantsHandler(testExecutor(), null, tenants); - - ListTenantsResponse response = (ListTenantsResponse) listTenantsHandler.handleGET(HttpRequest.createTestRequest("/blabla", Method.GET)); - final Collection<TenantName> responseTenantNames = response.getTenantNames(); - assertTrue(responseTenantNames.containsAll(Arrays.asList(a, b, c))); - assertContainsSystemTenants(responseTenantNames); - } - - private static void assertContainsSystemTenants(final Collection<TenantName> tenantNames) { - assertTrue(tenantNames.contains(TenantName.defaultName())); - } - - @Test - public void testEmptyTenants() throws Exception { - ListTenantsHandler listTenantsHandler = new ListTenantsHandler(testExecutor(), null, tenants); - - ListTenantsResponse response = (ListTenantsResponse) listTenantsHandler.handleGET(HttpRequest.createTestRequest("/blabla", Method.GET)); - final Collection<TenantName> responseTenantNames = response.getTenantNames(); - assertContainsSystemTenants(responseTenantNames); - } -} diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TenantHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TenantHandlerTest.java index ca545611e7f..ce4b25fe529 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TenantHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TenantHandlerTest.java @@ -53,11 +53,14 @@ public class TenantHandlerTest extends TenantTest { } @Test - public void testGetExisting() throws Exception { + public void testGetAndList() throws Exception { tenants.addTenant(a); - TenantGetResponse response = (TenantGetResponse) handler.handleGET( - HttpRequest.createTestRequest("http://deploy.example.yahoo.com:80/application/v2/tenant/a", Method.GET)); - assertResponseEquals(response, "{\"message\":\"Tenant 'a' exists.\"}"); + assertResponseEquals((TenantGetResponse) handler.handleGET( + HttpRequest.createTestRequest("http://deploy.example.yahoo.com:80/application/v2/tenant/a", Method.GET)), + "{\"message\":\"Tenant 'a' exists.\"}"); + assertResponseEquals((ListTenantsResponse) handler.handleGET( + HttpRequest.createTestRequest("http://deploy.example.yahoo.com:80/application/v2/tenant/", Method.GET)), + "{\"tenants\":[\"default\",\"a\"]}"); } @Test(expected=BadRequestException.class) |