11/*
22 * Copyright 2013-2021 the original author or authors.
33 *
4- * Licensed under the Apache License, Version 2.0 (the "License");
5- * you may not use this file except in compliance with the License.
6- * You may obtain a copy of the License at
4+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+ * in compliance with the License. You may obtain a copy of the License at
76 *
8- * http://www.apache.org/licenses/LICENSE-2.0
7+ * http://www.apache.org/licenses/LICENSE-2.0
98 *
10- * Unless required by applicable law or agreed to in writing, software
11- * distributed under the License is distributed on an "AS IS" BASIS,
12- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- * See the License for the specific language governing permissions and
14- * limitations under the License.
9+ * Unless required by applicable law or agreed to in writing, software distributed under the License
10+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+ * or implied. See the License for the specific language governing permissions and limitations under
12+ * the License.
1513 */
1614
1715package org .cloudfoundry .operations .routes ;
1816
1917import static org .cloudfoundry .util .tuple .TupleUtils .function ;
20- import static org .cloudfoundry .util .tuple .TupleUtils .predicate ;
2118
2219import java .time .Duration ;
2320import java .util .Collections ;
3835import org .cloudfoundry .client .v2 .privatedomains .PrivateDomainResource ;
3936import org .cloudfoundry .client .v2 .routes .AbstractRouteResource ;
4037import org .cloudfoundry .client .v2 .routes .CreateRouteResponse ;
41- import org .cloudfoundry .client .v2 .routes .DeleteRouteResponse ;
4238import org .cloudfoundry .client .v2 .routes .ListRouteApplicationsRequest ;
4339import org .cloudfoundry .client .v2 .routes .RouteEntity ;
44- import org .cloudfoundry .client .v2 .routes .RouteExistsRequest ;
4540import org .cloudfoundry .client .v2 .routes .RouteResource ;
4641import org .cloudfoundry .client .v2 .serviceinstances .UnionServiceInstanceResource ;
4742import org .cloudfoundry .client .v2 .shareddomains .ListSharedDomainsRequest ;
5045import org .cloudfoundry .client .v2 .spaces .ListSpaceRoutesRequest ;
5146import org .cloudfoundry .client .v2 .spaces .ListSpaceServiceInstancesRequest ;
5247import org .cloudfoundry .client .v2 .spaces .SpaceResource ;
48+ import org .cloudfoundry .client .v3 .domains .CheckReservedRoutesRequest ;
49+ import org .cloudfoundry .client .v3 .domains .DomainResource ;
50+ import org .cloudfoundry .client .v3 .organizations .ListOrganizationDomainsRequest ;
51+ import org .cloudfoundry .client .v3 .spaces .DeleteUnmappedRoutesRequest ;
5352import org .cloudfoundry .operations .util .OperationsLogging ;
5453import org .cloudfoundry .util .ExceptionUtils ;
5554import org .cloudfoundry .util .JobUtils ;
@@ -81,18 +80,18 @@ public Mono<Boolean> check(CheckRouteRequest request) {
8180 return Mono .zip (this .cloudFoundryClient , this .organizationId )
8281 .flatMap (
8382 function (
84- (cloudFoundryClient , organizationId ) ->
83+ (client , organizationId ) ->
8584 Mono .zip (
86- Mono . just ( cloudFoundryClient ) ,
87- getOptionalDomainId (
88- cloudFoundryClient ,
85+ this . cloudFoundryClient ,
86+ getOptionalDomainIdByName (
87+ client ,
8988 organizationId ,
9089 request .getDomain ()))))
9190 .flatMap (
9291 function (
93- (cloudFoundryClient , domainId ) ->
94- requestRouteExists (
95- cloudFoundryClient ,
92+ (client , domainId ) ->
93+ routeExists (
94+ client ,
9695 domainId ,
9796 request .getHost (),
9897 request .getPath ())))
@@ -101,6 +100,42 @@ public Mono<Boolean> check(CheckRouteRequest request) {
101100 .checkpoint ();
102101 }
103102
103+ private static Mono <Boolean > routeExists (
104+ CloudFoundryClient cloudFoundryClient , String domainId , String host , String path ) {
105+ return cloudFoundryClient
106+ .domainsV3 ()
107+ .checkReservedRoutes (
108+ CheckReservedRoutesRequest .builder ()
109+ .domainId (domainId )
110+ .host (host )
111+ .path (path )
112+ .build ())
113+ .flatMap (response -> Mono .just (response .getMatchingRoute ()));
114+ }
115+
116+ private static Mono <String > getOptionalDomainIdByName (
117+ CloudFoundryClient cloudFoundryClient , String organizationId , String domain ) {
118+ return listDomains (cloudFoundryClient , organizationId , new String [] {domain })
119+ .singleOrEmpty ()
120+ .map (resource -> resource .getId ());
121+ }
122+
123+ private static Flux <DomainResource > listDomains (
124+ CloudFoundryClient cloudFoundryClient ,
125+ String organizationId ,
126+ String [] domainNamesFilter ) {
127+ return PaginationUtils .requestClientV3Resources (
128+ page ->
129+ cloudFoundryClient
130+ .organizationsV3 ()
131+ .listDomains (
132+ ListOrganizationDomainsRequest .builder ()
133+ .names (domainNamesFilter )
134+ .page (page )
135+ .organizationId (organizationId )
136+ .build ()));
137+ }
138+
104139 @ Override
105140 public Mono <Integer > create (CreateRouteRequest request ) {
106141 return Mono .zip (this .cloudFoundryClient , this .organizationId )
@@ -170,43 +205,22 @@ public Mono<Void> delete(DeleteRouteRequest request) {
170205 @ Override
171206 public Mono <Void > deleteOrphanedRoutes (DeleteOrphanedRoutesRequest request ) {
172207 return Mono .zip (this .cloudFoundryClient , this .spaceId )
173- .flatMapMany (
174- function (
175- (cloudFoundryClient , spaceId ) ->
176- requestSpaceRoutes (cloudFoundryClient , spaceId )
177- .filter (
178- route ->
179- isRouteOrphan (
180- ResourceUtils .getEntity (
181- route )))
182- .map (ResourceUtils ::getId )
183- .map (
184- routeId ->
185- Tuples .of (
186- cloudFoundryClient ,
187- routeId ))))
188208 .flatMap (
189209 function (
190- (cloudFoundryClient , routeId ) ->
191- getApplications (cloudFoundryClient , routeId )
192- .map (
193- applicationResources ->
194- Tuples .of (
195- cloudFoundryClient ,
196- applicationResources ,
197- routeId ))))
198- .filter (
199- predicate (
200- (cloudFoundryClient , applicationResources , routeId ) ->
201- isApplicationOrphan (applicationResources )))
210+ (client , spaceId ) ->
211+ Mono .zip (
212+ this .cloudFoundryClient ,
213+ client .spacesV3 ()
214+ .deleteUnmappedRoutes (
215+ DeleteUnmappedRoutesRequest
216+ .builder ()
217+ .spaceId (spaceId )
218+ .build ()))))
202219 .flatMap (
203220 function (
204- (cloudFoundryClient , applicationResources , routeId ) ->
205- deleteRoute (
206- cloudFoundryClient ,
207- request .getCompletionTimeout (),
208- routeId )))
209- .then ()
221+ (client , job ) ->
222+ JobUtils .waitForCompletion (
223+ client , request .getCompletionTimeout (), job )))
210224 .transform (OperationsLogging .log ("Delete Orphaned Routes" ))
211225 .checkpoint ();
212226 }
@@ -324,15 +338,6 @@ public Mono<Void> unmap(UnmapRouteRequest request) {
324338 .checkpoint ();
325339 }
326340
327- private static Mono <Void > deleteRoute (
328- CloudFoundryClient cloudFoundryClient , Duration completionTimeout , String routeId ) {
329- return requestDeleteRoute (cloudFoundryClient , routeId )
330- .flatMap (
331- job ->
332- JobUtils .waitForCompletion (
333- cloudFoundryClient , completionTimeout , job ));
334- }
335-
336341 private static Mono <Map <String , String >> getAllDomains (
337342 CloudFoundryClient cloudFoundryClient , String organizationId ) {
338343 return requestAllPrivateDomains (cloudFoundryClient , organizationId )
@@ -386,11 +391,6 @@ private static Mono<List<String>> getApplicationNames(
386391 .collectList ();
387392 }
388393
389- private static Mono <List <ApplicationResource >> getApplications (
390- CloudFoundryClient cloudFoundryClient , String routeId ) {
391- return requestApplications (cloudFoundryClient , routeId ).collectList ();
392- }
393-
394394 private static Mono <Resource <?>> getDomain (
395395 CloudFoundryClient cloudFoundryClient , String organizationId , String domain ) {
396396 return getDomains (cloudFoundryClient , organizationId , domain )
@@ -416,13 +416,6 @@ private static Flux<Resource<?>> getDomains(
416416 .switchIfEmpty (requestSharedDomains (cloudFoundryClient , domain ));
417417 }
418418
419- private static Mono <String > getOptionalDomainId (
420- CloudFoundryClient cloudFoundryClient , String organizationId , String domain ) {
421- return getDomains (cloudFoundryClient , organizationId , domain )
422- .singleOrEmpty ()
423- .map (ResourceUtils ::getId );
424- }
425-
426419 private static Mono <AbstractRouteResource > getOrCreateRoute (
427420 CloudFoundryClient cloudFoundryClient ,
428421 String organizationId ,
@@ -562,10 +555,6 @@ private static Mono<String> getSpaceName(Map<String, String> spaces, String spac
562555 return Mono .just (spaces .get (spaceId ));
563556 }
564557
565- private static boolean isApplicationOrphan (List <ApplicationResource > applications ) {
566- return applications .isEmpty ();
567- }
568-
569558 private static boolean isIdentical (String s , String t ) {
570559 return s == null ? t == null : s .equals (t );
571560 }
@@ -673,13 +662,21 @@ private static Mono<CreateRouteResponse> requestCreateRoute(
673662 .create (builder .domainId (domainId ).spaceId (spaceId ).build ());
674663 }
675664
676- private static Mono <DeleteRouteResponse > requestDeleteRoute (
665+ private static Mono <Void > deleteRoute (
666+ CloudFoundryClient cloudFoundryClient , Duration completionTimeout , String routeId ) {
667+ return requestDeleteRoute (cloudFoundryClient , routeId )
668+ .flatMap (
669+ job ->
670+ JobUtils .waitForCompletion (
671+ cloudFoundryClient , completionTimeout , job ));
672+ }
673+
674+ private static Mono <String > requestDeleteRoute (
677675 CloudFoundryClient cloudFoundryClient , String routeId ) {
678676 return cloudFoundryClient
679- .routes ()
677+ .routesV3 ()
680678 .delete (
681- org .cloudfoundry .client .v2 .routes .DeleteRouteRequest .builder ()
682- .async (true )
679+ org .cloudfoundry .client .v3 .routes .DeleteRouteRequest .builder ()
683680 .routeId (routeId )
684681 .build ());
685682 }
@@ -723,18 +720,6 @@ private static Mono<Void> requestRemoveRouteFromApplication(
723720 .build ());
724721 }
725722
726- private static Mono <Boolean > requestRouteExists (
727- CloudFoundryClient cloudFoundryClient , String domainId , String host , String path ) {
728- return cloudFoundryClient
729- .routes ()
730- .exists (
731- RouteExistsRequest .builder ()
732- .domainId (domainId )
733- .host (host )
734- .path (path )
735- .build ());
736- }
737-
738723 private static Flux <RouteResource > requestRoutes (
739724 CloudFoundryClient cloudFoundryClient ,
740725 UnaryOperator <org .cloudfoundry .client .v2 .routes .ListRoutesRequest .Builder > modifier ) {
@@ -827,8 +812,4 @@ private static Route toRoute(
827812
828813 return builder .build ();
829814 }
830-
831- private boolean isRouteOrphan (RouteEntity entity ) {
832- return entity .getServiceInstanceId () == null || entity .getServiceInstanceId ().isEmpty ();
833- }
834815}
0 commit comments