@@ -89,30 +89,33 @@ final class CloudFoundryCleaner {
8989
9090 private final CloudFoundryClient cloudFoundryClient ;
9191
92+ private final NameFactory nameFactory ;
93+
9294 private final UaaClient uaaAdminClient ;
9395
94- CloudFoundryCleaner (CloudFoundryClient cloudFoundryClient , UaaClient uaaAdminClient ) {
96+ CloudFoundryCleaner (CloudFoundryClient cloudFoundryClient , NameFactory nameFactory , UaaClient uaaAdminClient ) {
9597 this .cloudFoundryClient = cloudFoundryClient ;
98+ this .nameFactory = nameFactory ;
9699 this .uaaAdminClient = uaaAdminClient ;
97100 }
98101
99102 void clean () {
100103 Flux .empty ()
101- .thenMany (cleanBuildpacks (this .cloudFoundryClient ))
104+ .thenMany (cleanBuildpacks (this .cloudFoundryClient , this . nameFactory ))
102105 .thenMany (cleanFeatureFlags (this .cloudFoundryClient ))
103- .thenMany (cleanRoutes (this .cloudFoundryClient ))
104- .thenMany (cleanApplicationsV2 (this .cloudFoundryClient ))
105- .thenMany (cleanApplicationsV3 (this .cloudFoundryClient ))
106+ .thenMany (cleanRoutes (this .cloudFoundryClient , this . nameFactory ))
107+ .thenMany (cleanApplicationsV2 (this .cloudFoundryClient , this . nameFactory ))
108+ .thenMany (cleanApplicationsV3 (this .cloudFoundryClient , this . nameFactory ))
106109 .thenMany (cleanPackages (this .cloudFoundryClient ))
107- .thenMany (cleanServiceInstances (this .cloudFoundryClient ))
108- .thenMany (cleanUserProvidedServiceInstances (this .cloudFoundryClient ))
109- .thenMany (cleanDomains (this .cloudFoundryClient ))
110- .thenMany (cleanPrivateDomains (this .cloudFoundryClient ))
111- .thenMany (cleanUaaGroups (this .uaaAdminClient ))
112- .thenMany (cleanUaaUsers (this .uaaAdminClient ))
113- .thenMany (cleanUaaClients (this .uaaAdminClient ))
114- .thenMany (cleanSpaces (this .cloudFoundryClient ))
115- .thenMany (cleanOrganizations (this .cloudFoundryClient ))
110+ .thenMany (cleanServiceInstances (this .cloudFoundryClient , this . nameFactory ))
111+ .thenMany (cleanUserProvidedServiceInstances (this .cloudFoundryClient , this . nameFactory ))
112+ .thenMany (cleanDomains (this .cloudFoundryClient , this . nameFactory ))
113+ .thenMany (cleanPrivateDomains (this .cloudFoundryClient , this . nameFactory ))
114+ .thenMany (cleanUaaGroups (this .uaaAdminClient , this . nameFactory ))
115+ .thenMany (cleanUaaUsers (this .uaaAdminClient , this . nameFactory ))
116+ .thenMany (cleanUaaClients (this .uaaAdminClient , this . nameFactory ))
117+ .thenMany (cleanSpaces (this .cloudFoundryClient , this . nameFactory ))
118+ .thenMany (cleanOrganizations (this .cloudFoundryClient , this . nameFactory ))
116119 .retry (5 , t -> t instanceof SSLException )
117120 .doOnSubscribe (s -> this .logger .debug (">> CLEANUP <<" ))
118121 .doOnError (Throwable ::printStackTrace )
@@ -121,13 +124,13 @@ void clean() {
121124 .block (Duration .ofMinutes (30 ));
122125 }
123126
124- private static Flux <Void > cleanApplicationsV2 (CloudFoundryClient cloudFoundryClient ) {
127+ private static Flux <Void > cleanApplicationsV2 (CloudFoundryClient cloudFoundryClient , NameFactory nameFactory ) {
125128 return PaginationUtils .
126129 requestClientV2Resources (page -> cloudFoundryClient .applicationsV2 ()
127130 .list (ListApplicationsRequest .builder ()
128131 .page (page )
129132 .build ()))
130- .filter (application -> ResourceUtils .getEntity (application ).getName (). startsWith ( "test-application-" ))
133+ .filter (application -> nameFactory . isApplicationName ( ResourceUtils .getEntity (application ).getName ()))
131134 .map (ResourceUtils ::getId )
132135 .flatMap (applicationId -> removeServiceBindings (cloudFoundryClient , applicationId )
133136 .thenMany (Flux .just (applicationId )))
@@ -137,27 +140,27 @@ private static Flux<Void> cleanApplicationsV2(CloudFoundryClient cloudFoundryCli
137140 .build ()));
138141 }
139142
140- private static Flux <Void > cleanApplicationsV3 (CloudFoundryClient cloudFoundryClient ) {
143+ private static Flux <Void > cleanApplicationsV3 (CloudFoundryClient cloudFoundryClient , NameFactory nameFactory ) {
141144 return PaginationUtils
142145 .requestClientV3Resources (page -> cloudFoundryClient .applicationsV3 ()
143146 .list (org .cloudfoundry .client .v3 .applications .ListApplicationsRequest .builder ()
144147 .page (page )
145148 .build ()))
146- .filter (application -> application .getName (). startsWith ( "test-application-" ))
149+ .filter (application -> nameFactory . isApplicationName ( application .getName ()))
147150 .map (Application ::getId )
148151 .flatMap (applicationId -> cloudFoundryClient .applicationsV3 ()
149152 .delete (org .cloudfoundry .client .v3 .applications .DeleteApplicationRequest .builder ()
150153 .applicationId (applicationId )
151154 .build ()));
152155 }
153156
154- private static Flux <Void > cleanBuildpacks (CloudFoundryClient cloudFoundryClient ) {
157+ private static Flux <Void > cleanBuildpacks (CloudFoundryClient cloudFoundryClient , NameFactory nameFactory ) {
155158 return PaginationUtils
156159 .requestClientV2Resources (page -> cloudFoundryClient .buildpacks ()
157160 .list (ListBuildpacksRequest .builder ()
158161 .page (page )
159162 .build ()))
160- .filter (buildpack -> ResourceUtils .getEntity (buildpack ).getName (). startsWith ( "test-buildpack-" ))
163+ .filter (buildpack -> nameFactory . isBuildpackName ( ResourceUtils .getEntity (buildpack ).getName ()))
161164 .map (ResourceUtils ::getId )
162165 .flatMap (buildpackId -> cloudFoundryClient .buildpacks ()
163166 .delete (DeleteBuildpackRequest .builder ()
@@ -167,13 +170,13 @@ private static Flux<Void> cleanBuildpacks(CloudFoundryClient cloudFoundryClient)
167170 .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
168171 }
169172
170- private static Flux <Void > cleanDomains (CloudFoundryClient cloudFoundryClient ) {
173+ private static Flux <Void > cleanDomains (CloudFoundryClient cloudFoundryClient , NameFactory nameFactory ) {
171174 return PaginationUtils .
172175 requestClientV2Resources (page -> cloudFoundryClient .domains ()
173176 .list (ListDomainsRequest .builder ()
174177 .page (page )
175178 .build ()))
176- .filter (domain -> ResourceUtils .getEntity (domain ).getName (). startsWith ( "test.domain." ))
179+ .filter (domain -> nameFactory . isDomainName ( ResourceUtils .getEntity (domain ).getName ()))
177180 .map (ResourceUtils ::getId )
178181 .flatMap (domainId -> cloudFoundryClient .domains ()
179182 .delete (DeleteDomainRequest .builder ()
@@ -198,13 +201,13 @@ private static Flux<Void> cleanFeatureFlags(CloudFoundryClient cloudFoundryClien
198201 .then ());
199202 }
200203
201- private static Flux <Void > cleanOrganizations (CloudFoundryClient cloudFoundryClient ) {
204+ private static Flux <Void > cleanOrganizations (CloudFoundryClient cloudFoundryClient , NameFactory nameFactory ) {
202205 return PaginationUtils .
203206 requestClientV2Resources (page -> cloudFoundryClient .organizations ()
204207 .list (ListOrganizationsRequest .builder ()
205208 .page (page )
206209 .build ()))
207- .filter (organization -> ResourceUtils .getEntity (organization ).getName (). startsWith ( "test-organization-" ))
210+ .filter (organization -> nameFactory . isOrganizationName ( ResourceUtils .getEntity (organization ).getName ()))
208211 .map (ResourceUtils ::getId )
209212 .flatMap (organizationId -> cloudFoundryClient .organizations ()
210213 .delete (DeleteOrganizationRequest .builder ()
@@ -228,13 +231,13 @@ private static Flux<Void> cleanPackages(CloudFoundryClient cloudFoundryClient) {
228231 .build ()));
229232 }
230233
231- private static Flux <Void > cleanPrivateDomains (CloudFoundryClient cloudFoundryClient ) {
234+ private static Flux <Void > cleanPrivateDomains (CloudFoundryClient cloudFoundryClient , NameFactory nameFactory ) {
232235 return PaginationUtils .
233236 requestClientV2Resources (page -> cloudFoundryClient .privateDomains ()
234237 .list (ListPrivateDomainsRequest .builder ()
235238 .page (page )
236239 .build ()))
237- .filter (domain -> ResourceUtils .getEntity (domain ).getName (). startsWith ( "test.domain." ))
240+ .filter (domain -> nameFactory . isDomainName ( ResourceUtils .getEntity (domain ).getName ()))
238241 .map (ResourceUtils ::getId )
239242 .flatMap (privateDomainId -> cloudFoundryClient .privateDomains ()
240243 .delete (DeletePrivateDomainRequest .builder ()
@@ -244,7 +247,7 @@ private static Flux<Void> cleanPrivateDomains(CloudFoundryClient cloudFoundryCli
244247 .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
245248 }
246249
247- private static Flux <Void > cleanRoutes (CloudFoundryClient cloudFoundryClient ) {
250+ private static Flux <Void > cleanRoutes (CloudFoundryClient cloudFoundryClient , NameFactory nameFactory ) {
248251 return PaginationUtils .
249252 requestClientV2Resources (page -> cloudFoundryClient .routes ()
250253 .list (ListRoutesRequest .builder ()
@@ -257,9 +260,9 @@ private static Flux<Void> cleanRoutes(CloudFoundryClient cloudFoundryClient) {
257260 .domainId (ResourceUtils .getEntity (route ).getDomainId ())
258261 .build ())
259262 ))
260- .filter (predicate ((route , domain ) -> ResourceUtils .getEntity (domain ).getName (). startsWith ( "test.domain." ) ||
261- ResourceUtils .getEntity (route ).getHost (). startsWith ( "test-application-" ) ||
262- ResourceUtils .getEntity (route ).getHost (). startsWith ( "test-host-" )))
263+ .filter (predicate ((route , domain ) -> nameFactory . isDomainName ( ResourceUtils .getEntity (domain ).getName ()) ||
264+ nameFactory . isApplicationName ( ResourceUtils .getEntity (route ).getHost ()) ||
265+ nameFactory . isHostName ( ResourceUtils .getEntity (route ).getHost ())))
263266 .map (function ((route , domain ) -> ResourceUtils .getId (route )))
264267 .flatMap (routeId -> cloudFoundryClient .routes ()
265268 .delete (DeleteRouteRequest .builder ()
@@ -269,13 +272,13 @@ private static Flux<Void> cleanRoutes(CloudFoundryClient cloudFoundryClient) {
269272 .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
270273 }
271274
272- private static Flux <Void > cleanServiceInstances (CloudFoundryClient cloudFoundryClient ) {
275+ private static Flux <Void > cleanServiceInstances (CloudFoundryClient cloudFoundryClient , NameFactory nameFactory ) {
273276 return PaginationUtils .
274277 requestClientV2Resources (page -> cloudFoundryClient .serviceInstances ()
275278 .list (ListServiceInstancesRequest .builder ()
276279 .page (page )
277280 .build ()))
278- .filter (serviceInstance -> ResourceUtils .getEntity (serviceInstance ).getName (). startsWith ( "test-service-instance-" ))
281+ .filter (serviceInstance -> nameFactory . isServiceInstanceName ( ResourceUtils .getEntity (serviceInstance ).getName ()))
279282 .map (ResourceUtils ::getId )
280283 .flatMap (serviceInstanceId -> cloudFoundryClient .serviceInstances ()
281284 .delete (DeleteServiceInstanceRequest .builder ()
@@ -285,13 +288,13 @@ private static Flux<Void> cleanServiceInstances(CloudFoundryClient cloudFoundryC
285288 .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
286289 }
287290
288- private static Flux <Void > cleanSpaces (CloudFoundryClient cloudFoundryClient ) {
291+ private static Flux <Void > cleanSpaces (CloudFoundryClient cloudFoundryClient , NameFactory nameFactory ) {
289292 return PaginationUtils .
290293 requestClientV2Resources (page -> cloudFoundryClient .spaces ()
291294 .list (ListSpacesRequest .builder ()
292295 .page (page )
293296 .build ()))
294- .filter (space -> ResourceUtils .getEntity (space ).getName (). startsWith ( "test-space-" ))
297+ .filter (space -> nameFactory . isSpaceName ( ResourceUtils .getEntity (space ).getName ()))
295298 .map (ResourceUtils ::getId )
296299 .flatMap (spaceId -> cloudFoundryClient .spaces ()
297300 .delete (DeleteSpaceRequest .builder ()
@@ -301,13 +304,13 @@ private static Flux<Void> cleanSpaces(CloudFoundryClient cloudFoundryClient) {
301304 .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
302305 }
303306
304- private static Flux <Void > cleanUaaClients (UaaClient uaaAdminClient ) {
307+ private static Flux <Void > cleanUaaClients (UaaClient uaaAdminClient , NameFactory nameFactory ) {
305308 return PaginationUtils
306309 .requestUaaResources (startIndex -> uaaAdminClient .clients ()
307310 .list (ListClientsRequest .builder ()
308311 .startIndex (startIndex )
309312 .build ()))
310- .filter (client -> client .getClientId (). startsWith ( "test-client-id-" ))
313+ .filter (client -> nameFactory . isClientId ( client .getClientId ()))
311314 .map (Client ::getClientId )
312315 .flatMap (clientId -> uaaAdminClient .clients ()
313316 .delete (DeleteClientRequest .builder ()
@@ -316,13 +319,13 @@ private static Flux<Void> cleanUaaClients(UaaClient uaaAdminClient) {
316319 .then ());
317320 }
318321
319- private static Flux <Void > cleanUaaGroups (UaaClient uaaClient ) {
322+ private static Flux <Void > cleanUaaGroups (UaaClient uaaClient , NameFactory nameFactory ) {
320323 return PaginationUtils
321324 .requestUaaResources (startIndex -> uaaClient .groups ()
322325 .list (ListGroupsRequest .builder ()
323326 .startIndex (startIndex )
324327 .build ()))
325- .filter (group -> group .getDisplayName (). startsWith ( "test-group-" ))
328+ .filter (group -> nameFactory . isGroupName ( group .getDisplayName ()))
326329 .map (Group ::getId )
327330 .flatMap (groupId -> uaaClient .groups ()
328331 .delete (DeleteGroupRequest .builder ()
@@ -332,13 +335,13 @@ private static Flux<Void> cleanUaaGroups(UaaClient uaaClient) {
332335 .then ());
333336 }
334337
335- private static Flux <Void > cleanUaaUsers (UaaClient uaaClient ) {
338+ private static Flux <Void > cleanUaaUsers (UaaClient uaaClient , NameFactory nameFactory ) {
336339 return PaginationUtils
337340 .requestUaaResources (startIndex -> uaaClient .users ()
338341 .list (ListUsersRequest .builder ()
339342 .startIndex (startIndex )
340343 .build ()))
341- .filter (user -> user .getUserName (). startsWith ( "test-user-" ))
344+ .filter (user -> nameFactory . isUserName ( user .getUserName ()))
342345 .map (User ::getId )
343346 .flatMap (userId -> uaaClient .users ()
344347 .delete (DeleteUserRequest .builder ()
@@ -348,13 +351,13 @@ private static Flux<Void> cleanUaaUsers(UaaClient uaaClient) {
348351 .then ());
349352 }
350353
351- private static Flux <Void > cleanUserProvidedServiceInstances (CloudFoundryClient cloudFoundryClient ) {
354+ private static Flux <Void > cleanUserProvidedServiceInstances (CloudFoundryClient cloudFoundryClient , NameFactory nameFactory ) {
352355 return PaginationUtils .
353356 requestClientV2Resources (page -> cloudFoundryClient .userProvidedServiceInstances ()
354357 .list (ListUserProvidedServiceInstancesRequest .builder ()
355358 .page (page )
356359 .build ()))
357- .filter (userProvidedServiceInstance -> ResourceUtils .getEntity (userProvidedServiceInstance ).getName (). startsWith ( "test-service-instance-" ))
360+ .filter (userProvidedServiceInstance -> nameFactory . isServiceInstanceName ( ResourceUtils .getEntity (userProvidedServiceInstance ).getName ()))
358361 .map (ResourceUtils ::getId )
359362 .flatMap (userProvidedServiceInstanceId -> cloudFoundryClient .userProvidedServiceInstances ()
360363 .delete (DeleteUserProvidedServiceInstanceRequest .builder ()
0 commit comments