From 759f55a2b349bbffadad59ec15cd0fdbc57f9cc3 Mon Sep 17 00:00:00 2001 From: Valerij Fredriksen Date: Thu, 24 Oct 2019 09:31:37 +0200 Subject: Add method to get snapshot of tenants, applications and instances --- .../api/integration/ApplicationIdSnapshot.java | 72 ++++++++++++++++++++++ .../api/integration/ApplicationIdSource.java | 13 +--- 2 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/ApplicationIdSnapshot.java (limited to 'controller-api') diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/ApplicationIdSnapshot.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/ApplicationIdSnapshot.java new file mode 100644 index 00000000000..e172c53163c --- /dev/null +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/ApplicationIdSnapshot.java @@ -0,0 +1,72 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.hosted.controller.api.integration; + +import com.yahoo.config.provision.ApplicationId; +import com.yahoo.config.provision.ApplicationName; +import com.yahoo.config.provision.InstanceName; +import com.yahoo.config.provision.TenantName; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; + +/** + * @author freva + */ +public class ApplicationIdSnapshot { + private final Map>> instanceByApplicationByTenantName; + + public ApplicationIdSnapshot(Map>> instanceByApplicationByTenantName) { + this.instanceByApplicationByTenantName = instanceByApplicationByTenantName.entrySet().stream() + .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, e -> e.getValue().entrySet().stream() + .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, f -> List.copyOf(f.getValue()))))); + } + + public List tenants() { + return List.copyOf(instanceByApplicationByTenantName.keySet()); + } + + public List applications(TenantName tenantName) { + return Optional.ofNullable(instanceByApplicationByTenantName.get(tenantName)) + .map(a -> List.copyOf(a.keySet())) + .orElseGet(List::of); + } + + public List instances(TenantName tenantName, ApplicationName applicationName) { + return instanceByApplicationByTenantName.getOrDefault(tenantName, Map.of()) + .getOrDefault(applicationName, List.of()); + } + + + public static class Builder { + private final Map>> instanceByApplicationByTenantName = new HashMap<>(); + + public Builder add(TenantName tenantName) { + instanceByApplicationByTenantName.computeIfAbsent(tenantName, t -> new HashMap<>()); + return this; + } + + public Builder add(TenantName tenantName, ApplicationName applicationName) { + instanceByApplicationByTenantName.computeIfAbsent(tenantName, t -> new HashMap<>()) + .computeIfAbsent(applicationName, a -> new ArrayList<>()); + return this; + } + + public Builder add(TenantName tenantName, ApplicationName applicationName, InstanceName instanceName) { + add(tenantName, applicationName); + instanceByApplicationByTenantName.get(tenantName).get(applicationName).add(instanceName); + return this; + } + + public Builder add(ApplicationId applicationId) { + return add(applicationId.tenant(), applicationId.application(), applicationId.instance()); + } + + public ApplicationIdSnapshot build() { + return new ApplicationIdSnapshot(instanceByApplicationByTenantName); + } + } +} diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/ApplicationIdSource.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/ApplicationIdSource.java index 0562ec91fb1..766ef71b25c 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/ApplicationIdSource.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/ApplicationIdSource.java @@ -1,16 +1,7 @@ package com.yahoo.vespa.hosted.controller.api.integration; -import com.yahoo.config.provision.ApplicationId; -import com.yahoo.config.provision.TenantName; - -import java.util.List; - public interface ApplicationIdSource { - /** Returns a list of all known application instance IDs. */ - List listApplications(); - - /** Returns a list of all known application instance IDs for the given tenant. */ - List listApplications(TenantName tenant); - + /** Returns a snapshot of all known tenants, applications and instances */ + ApplicationIdSnapshot applicationIdSnapshot(); } -- cgit v1.2.3