@@ -101,6 +101,7 @@ final class CloudFoundryCleaner {
101101
102102 CloudFoundryCleaner (CloudFoundryClient cloudFoundryClient , Mono <Optional <String >> protectedDomainId , Mono <List <String >> protectedFeatureFlags , Mono <Optional <String >> protectedOrganizationId ,
103103 Mono <List <String >> protectedSpaceIds ) {
104+
104105 this .cloudFoundryClient = cloudFoundryClient ;
105106 this .protectedDomainId = protectedDomainId ;
106107 this .protectedFeatureFlags = protectedFeatureFlags ;
@@ -164,19 +165,18 @@ void clean() {
164165 }
165166
166167 private static Flux <Void > cleanApplicationsV2 (CloudFoundryClient cloudFoundryClient , Predicate <ApplicationResource > predicate ) {
167- return
168- requestResources (page -> cloudFoundryClient .applicationsV2 ()
169- .list (ListApplicationsRequest .builder ()
170- .page (page )
171- .build ()))
172- .filter (predicate )
173- .map (ResourceUtils ::getId )
174- .flatMap (applicationId -> removeServiceBindings (cloudFoundryClient , applicationId )
175- .after (() -> Flux .just (applicationId )))
176- .flatMap (applicationId -> cloudFoundryClient .applicationsV2 ()
177- .delete (DeleteApplicationRequest .builder ()
178- .applicationId (applicationId )
179- .build ()));
168+ return requestResources (page -> cloudFoundryClient .applicationsV2 ()
169+ .list (ListApplicationsRequest .builder ()
170+ .page (page )
171+ .build ()))
172+ .filter (predicate )
173+ .map (ResourceUtils ::getId )
174+ .flatMap (applicationId -> removeServiceBindings (cloudFoundryClient , applicationId )
175+ .after (() -> Flux .just (applicationId )))
176+ .flatMap (applicationId -> cloudFoundryClient .applicationsV2 ()
177+ .delete (DeleteApplicationRequest .builder ()
178+ .applicationId (applicationId )
179+ .build ()));
180180 }
181181
182182 private static Flux <Void > cleanApplicationsV3 (CloudFoundryClient cloudFoundryClient , Predicate <ListApplicationsResponse .Resource > predicate ) {
@@ -195,19 +195,18 @@ private static Flux<Void> cleanApplicationsV3(CloudFoundryClient cloudFoundryCli
195195 }
196196
197197 private static Flux <Void > cleanDomains (CloudFoundryClient cloudFoundryClient , Predicate <DomainResource > predicate ) {
198- return
199- requestResources (page -> cloudFoundryClient .domains ()
200- .list (ListDomainsRequest .builder ()
201- .page (page )
198+ return requestResources (page -> cloudFoundryClient .domains ()
199+ .list (ListDomainsRequest .builder ()
200+ .page (page )
201+ .build ()))
202+ .filter (predicate )
203+ .map (ResourceUtils ::getId )
204+ .flatMap (domainId -> cloudFoundryClient .domains ()
205+ .delete (DeleteDomainRequest .builder ()
206+ .async (true )
207+ .domainId (domainId )
202208 .build ()))
203- .filter (predicate )
204- .map (ResourceUtils ::getId )
205- .flatMap (domainId -> cloudFoundryClient .domains ()
206- .delete (DeleteDomainRequest .builder ()
207- .async (true )
208- .domainId (domainId )
209- .build ()))
210- .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
209+ .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
211210 }
212211
213212 private static Flux <Void > cleanFeatureFlags (CloudFoundryClient cloudFoundryClient , Predicate <FeatureFlagEntity > predicate ) {
@@ -231,19 +230,18 @@ private static Flux<Void> cleanFeatureFlags(CloudFoundryClient cloudFoundryClien
231230 }
232231
233232 private static Flux <Void > cleanOrganizations (CloudFoundryClient cloudFoundryClient , Predicate <OrganizationResource > predicate ) {
234- return
235- requestResources (page -> cloudFoundryClient .organizations ()
236- .list (ListOrganizationsRequest .builder ()
237- .page (page )
233+ return requestResources (page -> cloudFoundryClient .organizations ()
234+ .list (ListOrganizationsRequest .builder ()
235+ .page (page )
236+ .build ()))
237+ .filter (predicate )
238+ .map (ResourceUtils ::getId )
239+ .flatMap (organizationId -> cloudFoundryClient .organizations ()
240+ .delete (DeleteOrganizationRequest .builder ()
241+ .async (true )
242+ .organizationId (organizationId )
238243 .build ()))
239- .filter (predicate )
240- .map (ResourceUtils ::getId )
241- .flatMap (organizationId -> cloudFoundryClient .organizations ()
242- .delete (DeleteOrganizationRequest .builder ()
243- .async (true )
244- .organizationId (organizationId )
245- .build ()))
246- .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
244+ .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
247245 }
248246
249247 private static Flux <Void > cleanPackages (CloudFoundryClient cloudFoundryClient , Predicate <ListPackagesResponse .Resource > predicate ) {
@@ -262,111 +260,105 @@ private static Flux<Void> cleanPackages(CloudFoundryClient cloudFoundryClient, P
262260 }
263261
264262 private static Flux <Void > cleanPrivateDomains (CloudFoundryClient cloudFoundryClient , Predicate <PrivateDomainResource > predicate ) {
265- return
266- requestResources (page -> cloudFoundryClient .privateDomains ()
267- .list (ListPrivateDomainsRequest .builder ()
268- .page (page )
263+ return requestResources (page -> cloudFoundryClient .privateDomains ()
264+ .list (ListPrivateDomainsRequest .builder ()
265+ .page (page )
266+ .build ()))
267+ .filter (predicate )
268+ .map (ResourceUtils ::getId )
269+ .flatMap (privateDomainId -> cloudFoundryClient .privateDomains ()
270+ .delete (DeletePrivateDomainRequest .builder ()
271+ .async (true )
272+ .privateDomainId (privateDomainId )
269273 .build ()))
270- .filter (predicate )
271- .map (ResourceUtils ::getId )
272- .flatMap (privateDomainId -> cloudFoundryClient .privateDomains ()
273- .delete (DeletePrivateDomainRequest .builder ()
274- .async (true )
275- .privateDomainId (privateDomainId )
276- .build ()))
277- .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
274+ .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
278275 }
279276
280277 private static Flux <Void > cleanRoutes (CloudFoundryClient cloudFoundryClient , Predicate <RouteResource > predicate ) {
281- return
282- requestResources (page -> cloudFoundryClient .routes ()
283- .list (ListRoutesRequest .builder ()
284- .page (page )
278+ return requestResources (page -> cloudFoundryClient .routes ()
279+ .list (ListRoutesRequest .builder ()
280+ .page (page )
281+ .build ()))
282+ .filter (predicate )
283+ .map (ResourceUtils ::getId )
284+ .flatMap (routeId -> cloudFoundryClient .routes ()
285+ .delete (DeleteRouteRequest .builder ()
286+ .async (true )
287+ .routeId (routeId )
285288 .build ()))
286- .filter (predicate )
287- .map (ResourceUtils ::getId )
288- .flatMap (routeId -> cloudFoundryClient .routes ()
289- .delete (DeleteRouteRequest .builder ()
290- .async (true )
291- .routeId (routeId )
292- .build ()))
293- .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
289+ .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
294290 }
295291
296292 private static Flux <Void > cleanServiceInstances (CloudFoundryClient cloudFoundryClient , Predicate <ServiceInstanceResource > predicate ) {
297- return
298- requestResources (page -> cloudFoundryClient .serviceInstances ()
299- .list (ListServiceInstancesRequest .builder ()
300- .page (page )
293+ return requestResources (page -> cloudFoundryClient .serviceInstances ()
294+ .list (ListServiceInstancesRequest .builder ()
295+ .page (page )
296+ .build ()))
297+ .filter (predicate )
298+ .map (ResourceUtils ::getId )
299+ .flatMap (serviceInstanceId -> cloudFoundryClient .serviceInstances ()
300+ .delete (DeleteServiceInstanceRequest .builder ()
301+ .async (true )
302+ .serviceInstanceId (serviceInstanceId )
301303 .build ()))
302- .filter (predicate )
303- .map (ResourceUtils ::getId )
304- .flatMap (serviceInstanceId -> cloudFoundryClient .serviceInstances ()
305- .delete (DeleteServiceInstanceRequest .builder ()
306- .async (true )
307- .serviceInstanceId (serviceInstanceId )
308- .build ()))
309- .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
304+ .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
310305 }
311306
312307 private static Flux <Void > cleanSpaces (CloudFoundryClient cloudFoundryClient , Predicate <SpaceResource > predicate , Logger logger ) {
313- return
314- requestResources (page -> cloudFoundryClient .spaces ()
315- .list (ListSpacesRequest .builder ()
316- .page (page )
317- .build ()))
318- .filter (predicate )
319- .map (ResourceUtils ::getId )
320- .flatMap (spaceId -> cloudFoundryClient .spaces ()
321- .delete (DeleteSpaceRequest .builder ()
322- .async (true )
323- .spaceId (spaceId )
324- .build ())
325- .doOnError (t -> { // TODO: Remove once the application deletion problem has been identified
326- PaginationUtils
327- .requestResources (page -> cloudFoundryClient .spaces ()
328- .listApplications (ListSpaceApplicationsRequest .builder ()
329- .page (page )
330- .spaceId (spaceId )
331- .build ()))
332- .map (ResourceUtils ::getId )
333- .doOnSubscribe (s -> logger .error ("Unable to delete space with id: {}" , spaceId ))
334- .doOnNext (applicationId -> logger .error ("Space {} associated with application: {}" , spaceId , applicationId ))
335- .doOnComplete (() -> logger .error ("Finished listing applications for space: {}" , spaceId ))
336- .after ()
337- .get ();
338- })
339- )
340- .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
308+ return requestResources (page -> cloudFoundryClient .spaces ()
309+ .list (ListSpacesRequest .builder ()
310+ .page (page )
311+ .build ()))
312+ .filter (predicate )
313+ .map (ResourceUtils ::getId )
314+ .flatMap (spaceId -> cloudFoundryClient .spaces ()
315+ .delete (DeleteSpaceRequest .builder ()
316+ .async (true )
317+ .spaceId (spaceId )
318+ .build ())
319+ .doOnError (t -> { // TODO: Remove once the application deletion problem has been identified
320+ PaginationUtils
321+ .requestResources (page -> cloudFoundryClient .spaces ()
322+ .listApplications (ListSpaceApplicationsRequest .builder ()
323+ .page (page )
324+ .spaceId (spaceId )
325+ .build ()))
326+ .map (ResourceUtils ::getId )
327+ .doOnSubscribe (s -> logger .error ("Unable to delete space with id: {}" , spaceId ))
328+ .doOnNext (applicationId -> logger .error ("Space {} associated with application: {}" , spaceId , applicationId ))
329+ .doOnComplete (() -> logger .error ("Finished listing applications for space: {}" , spaceId ))
330+ .after ()
331+ .get ();
332+ })
333+ )
334+ .flatMap (job -> JobUtils .waitForCompletion (cloudFoundryClient , job ));
341335 }
342336
343337 private static Flux <Void > cleanUserProvidedServiceInstances (CloudFoundryClient cloudFoundryClient , Predicate <UserProvidedServiceInstanceResource > predicate ) {
344- return
345- requestResources (page -> cloudFoundryClient .userProvidedServiceInstances ()
346- .list (ListUserProvidedServiceInstancesRequest .builder ()
347- .page (page )
348- .build ()))
349- .filter (predicate )
350- .map (ResourceUtils ::getId )
351- .flatMap (userProvidedServiceInstanceId -> cloudFoundryClient .userProvidedServiceInstances ()
352- .delete (DeleteUserProvidedServiceInstanceRequest .builder ()
353- .userProvidedServiceInstanceId (userProvidedServiceInstanceId )
354- .build ()));
338+ return requestResources (page -> cloudFoundryClient .userProvidedServiceInstances ()
339+ .list (ListUserProvidedServiceInstancesRequest .builder ()
340+ .page (page )
341+ .build ()))
342+ .filter (predicate )
343+ .map (ResourceUtils ::getId )
344+ .flatMap (userProvidedServiceInstanceId -> cloudFoundryClient .userProvidedServiceInstances ()
345+ .delete (DeleteUserProvidedServiceInstanceRequest .builder ()
346+ .userProvidedServiceInstanceId (userProvidedServiceInstanceId )
347+ .build ()));
355348 }
356349
357350 private static Flux <Void > removeServiceBindings (CloudFoundryClient cloudFoundryClient , String applicationId ) {
358- return
359- requestResources (page -> cloudFoundryClient .applicationsV2 ()
360- .listServiceBindings (ListApplicationServiceBindingsRequest .builder ()
361- .page (page )
351+ return requestResources (page -> cloudFoundryClient .applicationsV2 ()
352+ .listServiceBindings (ListApplicationServiceBindingsRequest .builder ()
353+ .page (page )
354+ .applicationId (applicationId )
355+ .build ()))
356+ .map (ResourceUtils ::getId )
357+ .flatMap (serviceBindingId -> cloudFoundryClient .applicationsV2 ()
358+ .removeServiceBinding (RemoveApplicationServiceBindingRequest .builder ()
362359 .applicationId (applicationId )
363- .build ()))
364- .map (ResourceUtils ::getId )
365- .flatMap (serviceBindingId -> cloudFoundryClient .applicationsV2 ()
366- .removeServiceBinding (RemoveApplicationServiceBindingRequest .builder ()
367- .applicationId (applicationId )
368- .serviceBindingId (serviceBindingId )
369- .build ()));
360+ .serviceBindingId (serviceBindingId )
361+ .build ()));
370362 }
371363
372364}
0 commit comments