From 7f61167980f2345887cd51033ef6c4b7eb9ebe96 Mon Sep 17 00:00:00 2001 From: Rob Zienert Date: Wed, 7 Jun 2017 19:11:15 -0700 Subject: [PATCH] fix(web): Implementing missed clouddriver selector callsites --- .../spinnaker/gate/config/GateConfig.groovy | 14 ++++++-------- .../gate/controllers/ProjectController.groovy | 6 ++++-- .../gate/controllers/SubnetController.groovy | 10 ++++++---- .../gate/services/ClusterService.groovy | 16 ++++++---------- .../spinnaker/gate/services/ImageService.groovy | 10 +++++----- .../gate/services/InstanceService.groovy | 11 ++++++----- .../spinnaker/gate/services/JobService.groovy | 8 ++++---- .../gate/services/LoadBalancerService.groovy | 14 +++++++------- .../gate/services/InstanceServiceSpec.groovy | 11 ++++++++--- 9 files changed, 52 insertions(+), 48 deletions(-) diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/config/GateConfig.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/config/GateConfig.groovy index 22e417a9ad..4eb2a35ca6 100644 --- a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/config/GateConfig.groovy +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/config/GateConfig.groovy @@ -151,9 +151,12 @@ class GateConfig extends RedisHttpSessionConfiguration { } @Bean - ClouddriverServiceSelector clouddriverServiceSelector(OkHttpClient okHttpClient) { - ClouddriverService defaultService = createClient "clouddriver", ClouddriverService, okHttpClient + ClouddriverService clouddriverService(OkHttpClient okHttpClient) { + createClient "clouddriver", ClouddriverService, okHttpClient + } + @Bean + ClouddriverServiceSelector clouddriverServiceSelector(ClouddriverService defaultClouddriverService, OkHttpClient okHttpClient) { // support named clouddriver service clients Map dynamicServices = [:] if (serviceConfiguration.getService("clouddriver").getConfig().containsKey("dynamicEndpoints")) { @@ -163,12 +166,7 @@ class GateConfig extends RedisHttpSessionConfiguration { } } - return new ClouddriverServiceSelector(defaultService, dynamicServices) - } - - @Bean - ClouddriverService clouddriverService(OkHttpClient okHttpClient) { - createClient "clouddriver", ClouddriverService, okHttpClient + return new ClouddriverServiceSelector(defaultClouddriverService, dynamicServices) } //---- semi-optional components: diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/ProjectController.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/ProjectController.groovy index f175e1385b..8f5a58e3a4 100644 --- a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/ProjectController.groovy +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/ProjectController.groovy @@ -23,6 +23,7 @@ import groovy.util.logging.Slf4j import org.springframework.beans.factory.annotation.Autowired import org.springframework.http.HttpStatus import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.RequestHeader import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMethod import org.springframework.web.bind.annotation.RequestParam @@ -52,8 +53,9 @@ class ProjectController { } @RequestMapping(value = "/{id}/clusters", method = RequestMethod.GET) - List getClusters(@PathVariable("id") String projectId) { - def result = projectService.getClusters(projectId) + List getClusters(@PathVariable("id") String projectId, + @RequestHeader(value = "X-RateLimit-App", required = false) String sourceApp) { + def result = projectService.getClusters(projectId, sourceApp) if (!result) { log.warn("Project not found (projectId: ${projectId}") throw new ProjectNotFoundException("Project not found (projectId: ${projectId})") diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/SubnetController.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/SubnetController.groovy index e16e4ea2e0..35fc4283f2 100644 --- a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/SubnetController.groovy +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/SubnetController.groovy @@ -16,10 +16,11 @@ package com.netflix.spinnaker.gate.controllers -import com.netflix.spinnaker.gate.services.internal.ClouddriverService +import com.netflix.spinnaker.gate.services.internal.ClouddriverServiceSelector import io.swagger.annotations.ApiOperation import org.springframework.beans.factory.annotation.Autowired import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.RequestHeader import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMethod import org.springframework.web.bind.annotation.RestController @@ -29,11 +30,12 @@ import org.springframework.web.bind.annotation.RestController class SubnetController { @Autowired - ClouddriverService clouddriverService + ClouddriverServiceSelector clouddriverServiceSelector @ApiOperation(value = "Retrieve a list of subnets for a given cloud provider") @RequestMapping(value = "/{cloudProvider}", method = RequestMethod.GET) - List allByCloudProvider(@PathVariable String cloudProvider) { - clouddriverService.getSubnets(cloudProvider) + List allByCloudProvider(@PathVariable String cloudProvider, + @RequestHeader(value = "X-RateLimit-App", required = false) String sourceApp) { + clouddriverServiceSelector.select(sourceApp).getSubnets(cloudProvider) } } diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/ClusterService.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/ClusterService.groovy index b2647d33af..77198fc1f2 100644 --- a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/ClusterService.groovy +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/ClusterService.groovy @@ -18,7 +18,6 @@ package com.netflix.spinnaker.gate.services import com.netflix.hystrix.exception.HystrixBadRequestException import com.netflix.spinnaker.gate.services.commands.HystrixFactory -import com.netflix.spinnaker.gate.services.internal.ClouddriverService import com.netflix.spinnaker.gate.services.internal.ClouddriverServiceSelector import groovy.transform.CompileStatic import groovy.transform.InheritConstructors @@ -33,9 +32,6 @@ import retrofit.RetrofitError class ClusterService { private static final String GROUP = "clusters" - @Autowired - ClouddriverService clouddriverService - @Autowired ClouddriverServiceSelector clouddriverServiceSelector @@ -44,20 +40,20 @@ class ClusterService { Map getClusters(String app, String selectorKey) { HystrixFactory.newMapCommand(GROUP, "getClustersForApplication") { - clouddriverService.getClusters(app) + clouddriverServiceSelector.select(selectorKey).getClusters(app) } execute() } List getClustersForAccount(String app, String account, String selectorKey) { HystrixFactory.newListCommand(GROUP, "getClustersForApplicationInAccount-${providerLookupService.providerForAccount(account)}") { - clouddriverService.getClustersForAccount(app, account) + clouddriverServiceSelector.select(selectorKey).getClustersForAccount(app, account) } execute() } Map getCluster(String app, String account, String clusterName, String selectorKey) { HystrixFactory.newMapCommand(GROUP, "getCluster-${providerLookupService.providerForAccount(account)}") { try { - clouddriverService.getCluster(app, account, clusterName)?.getAt(0) as Map + clouddriverServiceSelector.select(selectorKey).getCluster(app, account, clusterName)?.getAt(0) as Map } catch (RetrofitError e) { if (e.response?.status == 404) { return [:] @@ -69,19 +65,19 @@ class ClusterService { } List getClusterServerGroups(String app, String account, String clusterName, String selectorKey) { - getCluster(app, account, clusterName, null).serverGroups as List + getCluster(app, account, clusterName, selectorKey).serverGroups as List } List getScalingActivities(String app, String account, String clusterName, String serverGroupName, String provider, String region, String selectorKey) { HystrixFactory.newListCommand(GROUP, "getScalingActivitiesForCluster-${providerLookupService.providerForAccount(account)}") { - clouddriverService.getScalingActivities(app, account, clusterName, provider, serverGroupName, region) + clouddriverServiceSelector.select(selectorKey).getScalingActivities(app, account, clusterName, provider, serverGroupName, region) } execute() } Map getTargetServerGroup(String app, String account, String clusterName, String cloudProviderType, String scope, String target, Boolean onlyEnabled, Boolean validateOldest, String selectorKey) { HystrixFactory.newMapCommand(GROUP, "getTargetServerGroup-${providerLookupService.providerForAccount(account)}") { try { - return clouddriverService.getTargetServerGroup(app, account, clusterName, cloudProviderType, scope, target, onlyEnabled, validateOldest) + return clouddriverServiceSelector.select(selectorKey).getTargetServerGroup(app, account, clusterName, cloudProviderType, scope, target, onlyEnabled, validateOldest) } catch (RetrofitError re) { if (re.kind == RetrofitError.Kind.HTTP && re.response?.status == 404) { throw new ServerGroupNotFound("unable to find $target in $cloudProviderType/$account/$scope/$clusterName") diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/ImageService.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/ImageService.groovy index 98e628e1fc..2669599359 100644 --- a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/ImageService.groovy +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/ImageService.groovy @@ -18,7 +18,7 @@ package com.netflix.spinnaker.gate.services import com.netflix.spinnaker.gate.services.commands.HystrixFactory -import com.netflix.spinnaker.gate.services.internal.ClouddriverService +import com.netflix.spinnaker.gate.services.internal.ClouddriverServiceSelector import groovy.transform.CompileStatic import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Component @@ -29,26 +29,26 @@ class ImageService { private static final String GROUP = "images" @Autowired - ClouddriverService clouddriverService + ClouddriverServiceSelector clouddriverServiceSelector @Autowired ProviderLookupService providerLookupService List getForAccountAndRegion(String provider, String account, String region, String imageId, String selectorKey) { HystrixFactory.newListCommand(GROUP, "getImagesForAccountAndRegion-${providerLookupService.providerForAccount(account)}") { - clouddriverService.getImageDetails(provider, account, region, imageId) + clouddriverServiceSelector.select(selectorKey).getImageDetails(provider, account, region, imageId) } execute() } List search(String provider, String query, String region, String account, Integer count, Map additionalFilters, String selectorKey) { HystrixFactory.newListCommand(GROUP, "searchImages-${providerLookupService.providerForAccount(account)}") { - clouddriverService.findImages(provider, query, region, account, count, additionalFilters) + clouddriverServiceSelector.select(selectorKey).findImages(provider, query, region, account, count, additionalFilters) } execute() } List findTags(String provider, String account, String repository, String selectorKey) { HystrixFactory.newListCommand(GROUP, "getTags-${providerLookupService.providerForAccount(account)}") { - clouddriverService.findTags(provider, account, repository) + clouddriverServiceSelector.select(selectorKey).findTags(provider, account, repository) } execute() } } diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/InstanceService.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/InstanceService.groovy index aca165a325..1c7b05262c 100644 --- a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/InstanceService.groovy +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/InstanceService.groovy @@ -19,7 +19,7 @@ package com.netflix.spinnaker.gate.services import com.netflix.spinnaker.gate.config.InsightConfiguration import com.netflix.spinnaker.gate.services.commands.HystrixFactory -import com.netflix.spinnaker.gate.services.internal.ClouddriverService +import com.netflix.spinnaker.gate.services.internal.ClouddriverServiceSelector import groovy.transform.CompileStatic import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Component @@ -30,7 +30,7 @@ class InstanceService { private static final String GROUP = "instances" @Autowired - ClouddriverService clouddriverService + ClouddriverServiceSelector clouddriverServiceSelector @Autowired InsightConfiguration insightConfiguration @@ -40,8 +40,9 @@ class InstanceService { Map getForAccountAndRegion(String account, String region, String instanceId, String selectorKey) { HystrixFactory.newMapCommand(GROUP, "getInstancesForAccountAndRegion-${providerLookupService.providerForAccount(account)}") { - def accountDetails = clouddriverService.getAccount(account) - def instanceDetails = clouddriverService.getInstanceDetails(account, region, instanceId) + def service = clouddriverServiceSelector.select(selectorKey) + def accountDetails = service.getAccount(account) + def instanceDetails = service.getInstanceDetails(account, region, instanceId) def instanceContext = instanceDetails.collectEntries { return it.value instanceof String ? [it.key, it.value] : [it.key, ""] } as Map @@ -55,7 +56,7 @@ class InstanceService { Map getConsoleOutput(String account, String region, String instanceId, String provider, String selectorKey) { HystrixFactory.newMapCommand(GROUP, "getConsoleOutput-${providerLookupService.providerForAccount(account)}") { - return clouddriverService.getConsoleOutput(account, region, instanceId, provider) + return clouddriverServiceSelector.select(selectorKey).getConsoleOutput(account, region, instanceId, provider) } execute() } diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/JobService.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/JobService.groovy index 6e43d4d093..f24de5493a 100644 --- a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/JobService.groovy +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/JobService.groovy @@ -18,7 +18,7 @@ package com.netflix.spinnaker.gate.services import com.netflix.spinnaker.gate.config.InsightConfiguration import com.netflix.spinnaker.gate.services.commands.HystrixFactory -import com.netflix.spinnaker.gate.services.internal.ClouddriverService +import com.netflix.spinnaker.gate.services.internal.ClouddriverServiceSelector import groovy.transform.CompileStatic import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Component @@ -30,7 +30,7 @@ class JobService { private static final String GROUP = "jobs" @Autowired - ClouddriverService clouddriverService + ClouddriverServiceSelector clouddriverServiceSelector @Autowired InsightConfiguration insightConfiguration @@ -41,7 +41,7 @@ class JobService { List getForApplication(String applicationName, String expand, String selectorKey) { String commandKey = Boolean.valueOf(expand) ? "getExpandedJobsForApplication" : "getJobsForApplication" HystrixFactory.newListCommand(GROUP, commandKey) { - clouddriverService.getJobs(applicationName, expand) + clouddriverServiceSelector.select(selectorKey).getJobs(applicationName, expand) } execute() } @@ -49,7 +49,7 @@ class JobService { HystrixFactory.newMapCommand(GROUP, "getJobsForApplicationAccountAndRegion-${providerLookupService.providerForAccount(account)}", { try { def context = getContext(applicationName, account, region, name) - return clouddriverService.getJobDetails(applicationName, account, region, name) + [ + return clouddriverServiceSelector.select(selectorKey).getJobDetails(applicationName, account, region, name) + [ "insightActions": insightConfiguration.job.collect { it.applyContext(context) } ] } catch (RetrofitError e) { diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/LoadBalancerService.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/LoadBalancerService.groovy index 4680ad6cbc..ff606b1ea4 100644 --- a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/LoadBalancerService.groovy +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/LoadBalancerService.groovy @@ -17,7 +17,7 @@ package com.netflix.spinnaker.gate.services import com.netflix.spinnaker.gate.services.commands.HystrixFactory -import com.netflix.spinnaker.gate.services.internal.ClouddriverService +import com.netflix.spinnaker.gate.services.internal.ClouddriverServiceSelector import groovy.transform.CompileStatic import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Component @@ -28,37 +28,37 @@ class LoadBalancerService { private static final String GROUP = "loadBalancers" @Autowired - ClouddriverService clouddriverService + ClouddriverServiceSelector clouddriverServiceSelector List getAll(String provider = "aws", String selectorKey) { HystrixFactory.newListCommand(GROUP, "getAllLoadBalancersForProvider-$provider") { - clouddriverService.getLoadBalancers(provider) + clouddriverServiceSelector.select(selectorKey).getLoadBalancers(provider) } execute() } Map get(String name, String selectorKey, String provider = "aws") { HystrixFactory.newMapCommand(GROUP, "getLoadBalancer-$provider") { - clouddriverService.getLoadBalancer(provider, name) + clouddriverServiceSelector.select(selectorKey).getLoadBalancer(provider, name) } execute() } List getDetailsForAccountAndRegion(String account, String region, String name, String selectorKey, String provider = "aws") { HystrixFactory.newListCommand(GROUP, "getLoadBalancerDetails-$provider") { - clouddriverService.getLoadBalancerDetails(provider, account, region, name) + clouddriverServiceSelector.select(selectorKey).getLoadBalancerDetails(provider, account, region, name) } execute() } List getClusterLoadBalancers(String appName, String account, String provider, String clusterName, String selectorKey) { HystrixFactory.newListCommand(GROUP, "getClusterLoadBalancers-$provider") { - clouddriverService.getClusterLoadBalancers(appName, account, clusterName, provider) + clouddriverServiceSelector.select(selectorKey).getClusterLoadBalancers(appName, account, clusterName, provider) } execute() } List getApplicationLoadBalancers(String appName, String selectorKey) { HystrixFactory.newListCommand(GROUP, "getApplicationLoadBalancers") { - clouddriverService.getApplicationLoadBalancers(appName) + clouddriverServiceSelector.select(selectorKey).getApplicationLoadBalancers(appName) } execute() } } diff --git a/gate-web/src/test/groovy/com/netflix/spinnaker/gate/services/InstanceServiceSpec.groovy b/gate-web/src/test/groovy/com/netflix/spinnaker/gate/services/InstanceServiceSpec.groovy index 0401e26960..eb1464bff2 100644 --- a/gate-web/src/test/groovy/com/netflix/spinnaker/gate/services/InstanceServiceSpec.groovy +++ b/gate-web/src/test/groovy/com/netflix/spinnaker/gate/services/InstanceServiceSpec.groovy @@ -19,15 +19,20 @@ package com.netflix.spinnaker.gate.services import com.netflix.spinnaker.gate.config.InsightConfiguration import com.netflix.spinnaker.gate.services.internal.ClouddriverService +import com.netflix.spinnaker.gate.services.internal.ClouddriverServiceSelector import spock.lang.Specification class InstanceServiceSpec extends Specification { void "should include relevant insight actions for instance"() { given: def service = new InstanceService( - clouddriverService: Mock(ClouddriverService) { - 1 * getInstanceDetails(_, _, _) >> { return [privateIpAddress: "10.0.0.1", map: [:]] } - 1 * getAccount(_) >> { return [awsAccount: "prod"] } + clouddriverServiceSelector: Mock(ClouddriverServiceSelector) { + 1 * select(_) >> { + Mock(ClouddriverService) { + 1 * getInstanceDetails(_, _, _) >> { return [privateIpAddress: "10.0.0.1", map: [:]] } + 1 * getAccount(_) >> { return [awsAccount: "prod"] } + } + } }, providerLookupService: Stub(ProviderLookupService) { providerForAccount(_) >> "test"