11package org .cloudfoundry .multiapps .controller .process .steps ;
22
3- import java .text .MessageFormat ;
4- import java .util .ArrayList ;
5- import java .util .Collections ;
6- import java .util .List ;
7- import java .util .Map ;
8- import java .util .Optional ;
9- import java .util .Set ;
10- import java .util .TreeSet ;
11- import java .util .stream .Collectors ;
12- import java .util .stream .Stream ;
13-
143import jakarta .inject .Inject ;
154import jakarta .inject .Named ;
165import org .cloudfoundry .multiapps .common .SLException ;
6655import org .springframework .beans .factory .config .BeanDefinition ;
6756import org .springframework .context .annotation .Scope ;
6857
58+ import java .text .MessageFormat ;
59+ import java .util .ArrayList ;
60+ import java .util .Collections ;
61+ import java .util .List ;
62+ import java .util .Map ;
63+ import java .util .Optional ;
64+ import java .util .Set ;
65+ import java .util .TreeSet ;
66+ import java .util .stream .Collectors ;
67+ import java .util .stream .Stream ;
68+
6969import static java .util .stream .Collectors .toList ;
7070import static java .util .stream .Collectors .toSet ;
7171
@@ -92,8 +92,6 @@ protected StepPhase executeStep(ProcessContext context) {
9292 getStepLogger ().debug (Messages .BUILDING_CLOUD_MODEL );
9393 DeploymentDescriptor deploymentDescriptor = context .getVariable (Variables .COMPLETE_DEPLOYMENT_DESCRIPTOR );
9494
95- addDetectedExistingServiceKeysToDetectedManagedKeys (context );
96-
9795 // Get module sets:
9896 DeployedMta deployedMta = context .getVariable (Variables .DEPLOYED_MTA );
9997 List <DeployedMtaApplication > deployedApplications = (deployedMta != null ) ? deployedMta .getApplications () : Collections .emptyList ();
@@ -148,6 +146,7 @@ protected StepPhase executeStep(ProcessContext context) {
148146 context .setVariable (Variables .SERVICES_TO_BIND , servicesForBindings );
149147
150148 List <Resource > resourcesForDeployment = calculateResourcesForDeployment (context , deploymentDescriptor );
149+ addDetectedExistingServiceKeysToDetectedManagedKeys (context , resourcesForDeployment );
151150
152151 SelectiveDeployChecker selectiveDeployChecker = getSelectiveDeployChecker (context , deploymentDescriptor );
153152 selectiveDeployChecker .check (resourcesForDeployment );
@@ -357,11 +356,8 @@ private List<String> getDomainsFromApps(ProcessContext context, DeploymentDescri
357356 return new ArrayList <>(domains );
358357 }
359358
360- private void addDetectedExistingServiceKeysToDetectedManagedKeys (ProcessContext context ) {
361- String mtaId = context .getVariable (Variables .MTA_ID );
362- String mtaNamespace = context .getVariable (Variables .MTA_NAMESPACE );
363-
364- List <DeployedMtaServiceKey > deployedServiceKeys = detectDeployedServiceKeys (mtaId , mtaNamespace , context );
359+ private void addDetectedExistingServiceKeysToDetectedManagedKeys (ProcessContext context , List <Resource > resourcesForDeployment ) {
360+ List <DeployedMtaServiceKey > deployedServiceKeys = detectDeployedServiceKeys (context , resourcesForDeployment );
365361 if (!deployedServiceKeys .isEmpty ()) {
366362
367363 List <DeployedMtaServiceKey > detectedServiceKeysForManagedServices = context .getVariable (Variables .DEPLOYED_MTA_SERVICE_KEYS );
@@ -375,25 +371,26 @@ private void addDetectedExistingServiceKeysToDetectedManagedKeys(ProcessContext
375371 }
376372 }
377373
378- private List <DeployedMtaServiceKey > detectDeployedServiceKeys (String mtaId , String mtaNamespace ,
379- ProcessContext context ) {
374+ private List <DeployedMtaServiceKey > detectDeployedServiceKeys (ProcessContext context , List <Resource > resourcesForDeployment ) {
375+ String mtaId = context .getVariable (Variables .MTA_ID );
376+ String mtaNamespace = context .getVariable (Variables .MTA_NAMESPACE );
380377 String spaceGuid = context .getVariable (Variables .SPACE_GUID );
381378 String userGuid = context .getVariable (Variables .USER_GUID );
382379 OAuth2AccessTokenWithAdditionalInfo token = tokenService .getToken (userGuid );
383380 CloudCredentials credentials = new CloudCredentials (token );
384381
385382 CustomServiceKeysClient serviceKeysClient = getCustomServiceKeysClient (credentials , context .getVariable (Variables .CORRELATION_ID ));
386383
387- List <String > existingInstanceGuids = getExistingServiceGuids (context );
384+ List <String > existingInstanceGuids = getExistingServiceGuids (context , resourcesForDeployment );
388385
389386 return serviceKeysClient .getServiceKeysByMetadataAndExistingGuids (
390387 spaceGuid , mtaId , mtaNamespace , existingInstanceGuids
391388 );
392389 }
393390
394- private List <String > getExistingServiceGuids (ProcessContext context ) {
391+ private List <String > getExistingServiceGuids (ProcessContext context , List < Resource > resourcesForDeployment ) {
395392 CloudControllerClient client = context .getControllerClient ();
396- List <Resource > resources = getExistingServiceResourcesFromDescriptor ( context );
393+ List <Resource > resources = getExistingResourcesOnly ( resourcesForDeployment );
397394
398395 return resources .parallelStream ()
399396 .map (resource -> resolveServiceGuid (client , resource ))
@@ -427,16 +424,11 @@ private void logIgnoredService(String message, String serviceName, Exception e)
427424 LOGGER .error (formattedMessage , e );
428425 }
429426
430- private List <Resource > getExistingServiceResourcesFromDescriptor (ProcessContext context ) {
431- DeploymentDescriptor descriptor = context .getVariable (Variables .COMPLETE_DEPLOYMENT_DESCRIPTOR );
432-
433- if (descriptor == null ) {
434- return List .of ();
435- }
436- return descriptor .getResources ()
437- .stream ()
438- .filter (resource -> CloudModelBuilderUtil .getResourceType (resource ) == ResourceType .EXISTING_SERVICE )
439- .toList ();
427+ private List <Resource > getExistingResourcesOnly (List <Resource > resourcesForDeployment ) {
428+ return resourcesForDeployment
429+ .stream ()
430+ .filter (resource -> CloudModelBuilderUtil .getResourceType (resource ) == ResourceType .EXISTING_SERVICE )
431+ .toList ();
440432 }
441433
442434 protected CustomServiceKeysClient getCustomServiceKeysClient (CloudCredentials credentials , String correlationId ) {
0 commit comments