From 3a35abffa2a3a8c5c49fc5cd71e0a9d736741bd0 Mon Sep 17 00:00:00 2001 From: Harald Musum Date: Fri, 1 Oct 2021 07:06:56 +0200 Subject: Add methods for getting tenants and application instances --- .../ai/vespa/hosted/api/ControllerHttpClient.java | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'hosted-api/src/main') diff --git a/hosted-api/src/main/java/ai/vespa/hosted/api/ControllerHttpClient.java b/hosted-api/src/main/java/ai/vespa/hosted/api/ControllerHttpClient.java index 614f2fe6732..907d57fffe7 100644 --- a/hosted-api/src/main/java/ai/vespa/hosted/api/ControllerHttpClient.java +++ b/hosted-api/src/main/java/ai/vespa/hosted/api/ControllerHttpClient.java @@ -4,6 +4,7 @@ package ai.vespa.hosted.api; import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.ApplicationName; import com.yahoo.config.provision.Environment; +import com.yahoo.config.provision.InstanceName; import com.yahoo.config.provision.TenantName; import com.yahoo.config.provision.zone.ZoneId; import com.yahoo.security.KeyUtils; @@ -36,9 +37,11 @@ import java.security.cert.X509Certificate; import java.time.Duration; import java.time.Instant; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Objects; import java.util.OptionalLong; +import java.util.Set; import java.util.concurrent.Callable; import java.util.function.Consumer; import java.util.function.Supplier; @@ -175,6 +178,28 @@ public abstract class ControllerHttpClient { GET)); } + /** Returns the tenants in this system. */ + public Set tenants() { + return toTenants(send(request(HttpRequest.newBuilder(tenantsPath()) + .timeout(Duration.ofSeconds(20)), + GET))); + } + + /** Returns the applications for the given tenant. */ + public Set applications(TenantName tenantName) { + return toApplications(send(request(HttpRequest.newBuilder(applicationsPath(tenantName)) + .timeout(Duration.ofSeconds(20)), + GET))); + } + + /** Returns the application instances for the given tenant. */ + public Set applicationInstances(TenantName tenantName) { + return toApplicationInstances(send(request(HttpRequest.newBuilder(applicationsPath(tenantName)) + .timeout(Duration.ofSeconds(20)), + GET)), + tenantName); + } + /** Follows the given deployment job until it is done, or this thread is interrupted, at which point the current status is returned. */ public DeploymentLog followDeploymentUntilDone(ApplicationId id, ZoneId zone, long run, Consumer out) { @@ -227,10 +252,14 @@ public abstract class ControllerHttpClient { return concatenated(endpoint, "application", "v4"); } + private URI tenantsPath() { return concatenated(applicationApiPath(), "tenant"); } + private URI tenantPath(TenantName tenant) { return concatenated(applicationApiPath(), "tenant", tenant.value()); } + private URI applicationsPath(TenantName tenant) { return concatenated(tenantPath(tenant), "application"); } + private URI applicationPath(TenantName tenant, ApplicationName application) { return concatenated(tenantPath(tenant), "application", application.value()); } @@ -415,6 +444,32 @@ public abstract class ControllerHttpClient { : OptionalLong.empty()); } + private static Set toTenants(HttpResponse response) { + Set tenants = new HashSet<>(); + toInspector(response).traverse((ArrayTraverser) (___, entryObject) -> + tenants.add(TenantName.from(entryObject.field("tenant").asString()))); + return tenants; + } + + private static Set toApplications(HttpResponse response) { + Set applications = new HashSet<>(); + toInspector(response).traverse((ArrayTraverser) (___, entryObject) -> + applications.add(ApplicationName.from(entryObject.field("application").asString()))); + return applications; + } + + private static Set toApplicationInstances(HttpResponse response, TenantName tenantName) { + Set applicationIds = new HashSet<>(); + toInspector(response).traverse((ArrayTraverser) (___, entryObject) -> { + ApplicationName applicationName = ApplicationName.from(entryObject.field("application").asString()); + entryObject.field("instances").traverse((ArrayTraverser) (____, instanceObject) -> + applicationIds.add(ApplicationId.from(tenantName, + applicationName, + InstanceName.from(instanceObject.field("instance").asString())))); + }); + return applicationIds; + } + private static Slime toSlime(byte[] data) { return SlimeUtils.jsonToSlime(data); } -- cgit v1.2.3