diff --git a/clouddriver-web/src/main/groovy/com/netflix/spinnaker/clouddriver/controllers/ClusterController.groovy b/clouddriver-web/src/main/groovy/com/netflix/spinnaker/clouddriver/controllers/ClusterController.groovy index 85c9c36958d..f9244bd3adb 100644 --- a/clouddriver-web/src/main/groovy/com/netflix/spinnaker/clouddriver/controllers/ClusterController.groovy +++ b/clouddriver-web/src/main/groovy/com/netflix/spinnaker/clouddriver/controllers/ClusterController.groovy @@ -79,23 +79,14 @@ class ClusterController { .sorted(Comparator.comparing({ Application it -> it.getName().toLowerCase() })) .collect(Collectors.toList()) - Map> clusterNames = Map.of() - def lastApp = null - for (app in apps) { - if (!lastApp) { - clusterNames = app.getClusterNames() - } else { - clusterNames = mergeClusters(lastApp, app) - } - lastApp = app - } + Map> clusterNames = mergeClusters(apps) return clusterNames } - private Map> mergeClusters(Application a, Application b) { + private Map> mergeClusters(List a) { Map> map = new HashMap<>() - Stream.of(a, b) + a.stream() .flatMap({ it.getClusterNames().entrySet().stream() }) .forEach({ entry -> map.computeIfAbsent(entry.getKey(), { new HashSet<>() }).addAll(entry.getValue()) diff --git a/clouddriver-web/src/test/groovy/com/netflix/spinnaker/clouddriver/controllers/ClusterControllerSpec.groovy b/clouddriver-web/src/test/groovy/com/netflix/spinnaker/clouddriver/controllers/ClusterControllerSpec.groovy index 6a76b5f42f9..bb9480f7e64 100644 --- a/clouddriver-web/src/test/groovy/com/netflix/spinnaker/clouddriver/controllers/ClusterControllerSpec.groovy +++ b/clouddriver-web/src/test/groovy/com/netflix/spinnaker/clouddriver/controllers/ClusterControllerSpec.groovy @@ -56,13 +56,21 @@ class ClusterControllerSpec extends Specification { getApplication("app") >> app2 } - clusterController.applicationProviders = [appProvider1, appProvider2] + def app3 = Stub(Application) { + getName() >> "app" + getClusterNames() >> ["stage": ["we-need-all-clusters-to-be-returned"] as Set] + } + def appProvider3 = Stub(ApplicationProvider) { + getApplication("app") >> app3 + } + + clusterController.applicationProviders = [appProvider1, appProvider2, appProvider3] when: def result = clusterController.listByAccount("app") then: - result == [test: ["foo", "bar"] as Set, prod: ["baz"] as Set] + result == [test: ["foo", "bar"] as Set, prod: ["baz"] as Set, stage: ["we-need-all-clusters-to-be-returned"] as Set] } void "should throw exception when looking for specific cluster that doesnt exist"() {