33import java .util .Collections ;
44import java .util .List ;
55import java .util .Map ;
6- import java .util .function .Function ;
6+ import java .util .Objects ;
7+ import java .util .UUID ;
78import java .util .stream .Collectors ;
89
910import org .cloudfoundry .client .v3 .serviceinstances .ServiceInstanceType ;
@@ -29,45 +30,87 @@ public CustomServiceKeysClient(ApplicationConfiguration configuration, WebClient
2930 super (configuration , webClientFactory , credentials , correlationId );
3031 }
3132
32- public List <DeployedMtaServiceKey > getServiceKeysByMetadataAndGuids (String spaceGuid , String mtaId , String mtaNamespace ,
33- List <DeployedMtaService > services ) {
34- String labelSelector = MtaMetadataCriteriaBuilder .builder ()
35- .label (MtaMetadataLabels .SPACE_GUID )
36- .hasValue (spaceGuid )
37- .and ()
38- .label (MtaMetadataLabels .MTA_NAMESPACE )
39- .hasValueOrIsntPresent (MtaMetadataUtil .getHashedLabel (mtaNamespace ))
40- .and ()
41- .label (MtaMetadataLabels .MTA_ID )
42- .hasValue (MtaMetadataUtil .getHashedLabel (mtaId ))
43- .build ()
44- .get ();
45-
46- return new CustomControllerClientErrorHandler ().handleErrorsOrReturnResult (
47- () -> getServiceKeysByMetadataInternal (labelSelector , services ));
33+ public List <DeployedMtaServiceKey > getServiceKeysByMetadataAndExistingGuids (
34+ String spaceGuid ,
35+ String mtaId ,
36+ String mtaNamespace ,
37+ List <String > existingServiceGuids ) {
38+
39+ String labelSelector = buildMtaMetadataLabelSelector (spaceGuid , mtaId , mtaNamespace );
40+
41+ List <String > allServiceGuids = existingServiceGuids .stream ()
42+ .filter (Objects ::nonNull )
43+ .toList ();
44+
45+ if (allServiceGuids .isEmpty ()) {
46+ return List .of ();
47+ }
48+
49+ return new CustomControllerClientErrorHandler ()
50+ .handleErrorsOrReturnResult (
51+ () -> getServiceKeysByMetadataInternal (labelSelector , allServiceGuids )
52+ );
4853 }
4954
50- private List <DeployedMtaServiceKey > getServiceKeysByMetadataInternal (String labelSelector , List <DeployedMtaService > services ) {
51- String uriSuffix = INCLUDE_SERVICE_INSTANCE_RESOURCES_PARAM ;
52- List <DeployedMtaService > managedServices = getManagedServices (services );
53- if (managedServices != null ) {
54- uriSuffix += "&service_instance_guids=" + managedServices .stream ()
55- .map (service -> service .getGuid ()
56- .toString ())
57- .collect (Collectors .joining ("," ));
55+ public List <DeployedMtaServiceKey > getServiceKeysByMetadataAndManagedServices (
56+ String spaceGuid ,
57+ String mtaId ,
58+ String mtaNamespace ,
59+ List <DeployedMtaService > services ) {
60+
61+ String labelSelector = buildMtaMetadataLabelSelector (spaceGuid , mtaId , mtaNamespace );
62+
63+ List <String > managedGuids = extractManagedServiceGuids (services );
64+
65+ if (managedGuids .isEmpty ()) {
66+ return List .of ();
5867 }
59- return getListOfResources (new ServiceKeysResponseMapper (managedServices ), SERVICE_KEYS_BY_METADATA_SELECTOR_URI + uriSuffix ,
68+
69+ return new CustomControllerClientErrorHandler ()
70+ .handleErrorsOrReturnResult (
71+ () -> getServiceKeysByMetadataInternal (labelSelector , managedGuids )
72+ );
73+ }
74+
75+ private String buildMtaMetadataLabelSelector (String spaceGuid ,
76+ String mtaId ,
77+ String mtaNamespace ) {
78+
79+ return MtaMetadataCriteriaBuilder .builder ()
80+ .label (MtaMetadataLabels .SPACE_GUID )
81+ .hasValue (spaceGuid )
82+ .and ()
83+ .label (MtaMetadataLabels .MTA_NAMESPACE )
84+ .hasValueOrIsntPresent (MtaMetadataUtil .getHashedLabel (mtaNamespace ))
85+ .and ()
86+ .label (MtaMetadataLabels .MTA_ID )
87+ .hasValue (MtaMetadataUtil .getHashedLabel (mtaId ))
88+ .build ()
89+ .get ();
90+ }
91+
92+ private List <String > extractManagedServiceGuids (List <DeployedMtaService > services ) {
93+ return getManagedServices (services ).stream ()
94+ .map (DeployedMtaService ::getGuid )
95+ .map (UUID ::toString )
96+ .filter (Objects ::nonNull )
97+ .toList ();
98+ }
99+
100+ private List <DeployedMtaServiceKey > getServiceKeysByMetadataInternal (String labelSelector , List <String > guids ) {
101+
102+ String uriSuffix = INCLUDE_SERVICE_INSTANCE_RESOURCES_PARAM
103+ + "&service_instance_guids=" + String .join ("," , guids );
104+
105+ return getListOfResources (new ServiceKeysResponseMapper (),
106+ SERVICE_KEYS_BY_METADATA_SELECTOR_URI + uriSuffix ,
60107 labelSelector );
61108 }
62109
63110 private List <DeployedMtaService > getManagedServices (List <DeployedMtaService > services ) {
64- if (services == null ) {
65- return null ;
66- }
67- List <DeployedMtaService > managedServices = services .stream ()
68- .filter (this ::serviceIsNotUserProvided )
69- .collect (Collectors .toList ());
70- return managedServices .isEmpty () ? null : managedServices ;
111+ return services .stream ()
112+ .filter (this ::serviceIsNotUserProvided )
113+ .toList ();
71114 }
72115
73116 private boolean serviceIsNotUserProvided (DeployedMtaService service ) {
@@ -76,23 +119,12 @@ private boolean serviceIsNotUserProvided(DeployedMtaService service) {
76119 }
77120
78121 protected class ServiceKeysResponseMapper extends ResourcesResponseMapper <DeployedMtaServiceKey > {
79-
80- List <DeployedMtaService > mtaServices ;
81-
82- public ServiceKeysResponseMapper (List <DeployedMtaService > mtaServices ) {
83- this .mtaServices = mtaServices ;
122+ public ServiceKeysResponseMapper () {
84123 }
85124
86125 @ Override
87126 public List <DeployedMtaServiceKey > getMappedResources () {
88- Map <String , CloudServiceInstance > serviceMapping ;
89- if (mtaServices != null ) {
90- serviceMapping = mtaServices .stream ()
91- .collect (Collectors .toMap (service -> service .getGuid ()
92- .toString (), Function .identity ()));
93- } else {
94- serviceMapping = getIncludedServiceInstancesMapping ();
95- }
127+ Map <String , CloudServiceInstance > serviceMapping = getIncludedServiceInstancesMapping ();
96128 return getQueriedResources ().stream ()
97129 .map (resource -> resourceMapper .mapServiceKeyResource (resource , serviceMapping ))
98130 .collect (Collectors .toList ());
@@ -108,4 +140,4 @@ public Map<String, CloudServiceInstance> getIncludedServiceInstancesMapping() {
108140
109141 }
110142 }
111- }
143+ }
0 commit comments