2020import org .cloudfoundry .client .v2 .ClientV2Exception ;
2121import org .cloudfoundry .client .v2 .featureflags .GetFeatureFlagRequest ;
2222import org .cloudfoundry .client .v2 .featureflags .GetFeatureFlagResponse ;
23+ import org .cloudfoundry .client .v2 .organizations .AssociateOrganizationAuditorByUsernameRequest ;
24+ import org .cloudfoundry .client .v2 .organizations .AssociateOrganizationBillingManagerByUsernameRequest ;
25+ import org .cloudfoundry .client .v2 .organizations .AssociateOrganizationManagerByUsernameRequest ;
2326import org .cloudfoundry .client .v2 .organizations .AssociateOrganizationUserByUsernameRequest ;
2427import org .cloudfoundry .client .v2 .organizations .AssociateOrganizationUserByUsernameResponse ;
28+ import org .cloudfoundry .client .v2 .organizations .ListOrganizationAuditorsRequest ;
29+ import org .cloudfoundry .client .v2 .organizations .ListOrganizationBillingManagersRequest ;
30+ import org .cloudfoundry .client .v2 .organizations .ListOrganizationManagersRequest ;
2531import org .cloudfoundry .client .v2 .organizations .ListOrganizationSpacesRequest ;
2632import org .cloudfoundry .client .v2 .organizations .ListOrganizationsRequest ;
2733import org .cloudfoundry .client .v2 .organizations .OrganizationResource ;
34+ import org .cloudfoundry .client .v2 .organizations .RemoveOrganizationAuditorByUsernameRequest ;
35+ import org .cloudfoundry .client .v2 .organizations .RemoveOrganizationBillingManagerByUsernameRequest ;
36+ import org .cloudfoundry .client .v2 .organizations .RemoveOrganizationManagerByUsernameRequest ;
2837import org .cloudfoundry .client .v2 .spaces .AssociateSpaceAuditorByUsernameRequest ;
2938import org .cloudfoundry .client .v2 .spaces .AssociateSpaceDeveloperByUsernameRequest ;
3039import org .cloudfoundry .client .v2 .spaces .AssociateSpaceManagerByUsernameRequest ;
@@ -100,6 +109,23 @@ public Mono<Void> delete(DeleteUserRequest request) {
100109 .checkpoint ();
101110 }
102111
112+ @ Override
113+ public Mono <OrganizationUsers > listOrganizationUsers (ListOrganizationUsersRequest request ) {
114+ return this .cloudFoundryClient
115+ .then (cloudFoundryClient -> Mono .when (
116+ Mono .just (cloudFoundryClient ),
117+ getOrganizationId (cloudFoundryClient , request .getOrganizationName ())
118+ ))
119+ .then (function ((cloudFoundryClient , organizationId ) -> Mono .when (
120+ listOrganizationAuditorNames (cloudFoundryClient , organizationId ),
121+ listOrganizationBillingManagerNames (cloudFoundryClient , organizationId ),
122+ listOrganizationManagerNames (cloudFoundryClient , organizationId )
123+ )))
124+ .then (function (this ::toOrganizationUsers ))
125+ .transform (OperationsLogging .log ("List Organization Users" ))
126+ .checkpoint ();
127+ }
128+
103129 @ Override
104130 public Mono <SpaceUsers > listSpaceUsers (ListSpaceUsersRequest request ) {
105131 return this .cloudFoundryClient
@@ -121,6 +147,27 @@ public Mono<SpaceUsers> listSpaceUsers(ListSpaceUsersRequest request) {
121147 .checkpoint ();
122148 }
123149
150+ @ Override
151+ public Mono <Void > setOrganizationRole (SetOrganizationRoleRequest request ) {
152+ return this .cloudFoundryClient
153+ .then (cloudFoundryClient -> Mono .when (
154+ Mono .just (cloudFoundryClient ),
155+ getFeatureFlagEnabled (cloudFoundryClient , SET_ROLES_BY_USERNAME_FEATURE_FLAG )
156+ ))
157+ .filter (predicate ((cloudFoundryClient , setRolesByUsernameEnabled ) -> setRolesByUsernameEnabled ))
158+ .switchIfEmpty (ExceptionUtils .illegalState ("Setting roles by username is not enabled" ))
159+ .then (function ((cloudFoundryClient , ignore ) -> Mono .when (
160+ Mono .just (cloudFoundryClient ),
161+ getOrganizationId (cloudFoundryClient , request .getOrganizationName ()))
162+ ))
163+ .then (function ((cloudFoundryClient , organizationId ) -> Mono .when (
164+ requestAssociateOrganizationUserByUsername (cloudFoundryClient , organizationId , request ),
165+ associateOrganizationRole (cloudFoundryClient , organizationId , request ))
166+ ))
167+ .transform (OperationsLogging .log ("Set User Organization Role" ))
168+ .then ();
169+ }
170+
124171 @ Override
125172 public Mono <Void > setSpaceRole (SetSpaceRoleRequest request ) {
126173 return this .cloudFoundryClient
@@ -149,6 +196,24 @@ public Mono<Void> setSpaceRole(SetSpaceRoleRequest request) {
149196 .then ();
150197 }
151198
199+ @ Override
200+ public Mono <Void > unsetOrganizationRole (UnsetOrganizationRoleRequest request ) {
201+ return this .cloudFoundryClient
202+ .then (cloudFoundryClient -> Mono .when (
203+ Mono .just (cloudFoundryClient ),
204+ getFeatureFlagEnabled (cloudFoundryClient , UNSET_ROLES_BY_USERNAME_FEATURE_FLAG )
205+ ))
206+ .filter (predicate ((cloudFoundryClient , setRolesByUsernameEnabled ) -> setRolesByUsernameEnabled ))
207+ .switchIfEmpty (ExceptionUtils .illegalState ("Unsetting roles by username is not enabled" ))
208+ .then (function ((cloudFoundryClient , ignore ) -> Mono .when (
209+ Mono .just (cloudFoundryClient ),
210+ getOrganizationId (cloudFoundryClient , request .getOrganizationName ()))
211+ ))
212+ .then (function ((cloudFoundryClient , organizationId ) -> removeOrganizationRole (cloudFoundryClient , organizationId , request )))
213+ .transform (OperationsLogging .log ("Unset User Organization Role" ))
214+ .then ();
215+ }
216+
152217 @ Override
153218 public Mono <Void > unsetSpaceRole (UnsetSpaceRoleRequest request ) {
154219 return this .cloudFoundryClient
@@ -171,6 +236,37 @@ public Mono<Void> unsetSpaceRole(UnsetSpaceRoleRequest request) {
171236 .then ();
172237 }
173238
239+ private static Mono <Void > associateOrganizationRole (CloudFoundryClient cloudFoundryClient , String organizationId , SetOrganizationRoleRequest request ) {
240+ if (OrganizationRole .AUDITOR == request .getOrganizationRole ()) {
241+ return cloudFoundryClient .organizations ()
242+ .associateAuditorByUsername (AssociateOrganizationAuditorByUsernameRequest .builder ()
243+ .organizationId (organizationId )
244+ .username (request .getUsername ())
245+ .build ())
246+ .then ();
247+ }
248+
249+ if (OrganizationRole .BILLING_MANAGER == request .getOrganizationRole ()) {
250+ return cloudFoundryClient .organizations ()
251+ .associateBillingManagerByUsername (AssociateOrganizationBillingManagerByUsernameRequest .builder ()
252+ .organizationId (organizationId )
253+ .username (request .getUsername ())
254+ .build ())
255+ .then ();
256+ }
257+
258+ if (OrganizationRole .MANAGER == request .getOrganizationRole ()) {
259+ return cloudFoundryClient .organizations ()
260+ .associateManagerByUsername (AssociateOrganizationManagerByUsernameRequest .builder ()
261+ .organizationId (organizationId )
262+ .username (request .getUsername ())
263+ .build ())
264+ .then ();
265+ }
266+
267+ return ExceptionUtils .illegalArgument ("Unknown organization role specified" );
268+ }
269+
174270 private static Mono <AssociateOrganizationUserByUsernameResponse > associateOrganizationRole (CloudFoundryClient cloudFoundryClient , String username , String organizationId ) {
175271 return cloudFoundryClient .organizations ()
176272 .associateUserByUsername (AssociateOrganizationUserByUsernameRequest .builder ()
@@ -253,6 +349,24 @@ private static Mono<String> getUserId(UaaClient uaaClient, String username) {
253349 .map (User ::getId );
254350 }
255351
352+ private static Mono <List <String >> listOrganizationAuditorNames (CloudFoundryClient cloudFoundryClient , String organizationId ) {
353+ return requestListOrganizationAuditors (cloudFoundryClient , organizationId )
354+ .map (resource -> ResourceUtils .getEntity (resource ).getUsername ())
355+ .collectList ();
356+ }
357+
358+ private static Mono <List <String >> listOrganizationBillingManagerNames (CloudFoundryClient cloudFoundryClient , String organizationId ) {
359+ return requestListOrganizationBillingManagers (cloudFoundryClient , organizationId )
360+ .map (resource -> ResourceUtils .getEntity (resource ).getUsername ())
361+ .collectList ();
362+ }
363+
364+ private static Mono <List <String >> listOrganizationManagerNames (CloudFoundryClient cloudFoundryClient , String organizationId ) {
365+ return requestListOrganizationManagers (cloudFoundryClient , organizationId )
366+ .map (resource -> ResourceUtils .getEntity (resource ).getUsername ())
367+ .collectList ();
368+ }
369+
256370 private static Mono <List <String >> listSpaceAuditorNames (CloudFoundryClient cloudFoundryClient , String spaceId ) {
257371 return requestListSpaceAuditors (cloudFoundryClient , spaceId )
258372 .map (resource -> ResourceUtils .getEntity (resource ).getUsername ())
@@ -271,6 +385,37 @@ private static Mono<List<String>> listSpaceManagerNames(CloudFoundryClient cloud
271385 .collectList ();
272386 }
273387
388+ private static Mono <Void > removeOrganizationRole (CloudFoundryClient cloudFoundryClient , String organizationId , UnsetOrganizationRoleRequest request ) {
389+ if (OrganizationRole .AUDITOR == request .getOrganizationRole ()) {
390+ return cloudFoundryClient .organizations ()
391+ .removeAuditorByUsername (RemoveOrganizationAuditorByUsernameRequest .builder ()
392+ .organizationId (organizationId )
393+ .username (request .getUsername ())
394+ .build ())
395+ .then ();
396+ }
397+
398+ if (OrganizationRole .BILLING_MANAGER == request .getOrganizationRole ()) {
399+ return cloudFoundryClient .organizations ()
400+ .removeBillingManagerByUsername (RemoveOrganizationBillingManagerByUsernameRequest .builder ()
401+ .organizationId (organizationId )
402+ .username (request .getUsername ())
403+ .build ())
404+ .then ();
405+ }
406+
407+ if (OrganizationRole .MANAGER == request .getOrganizationRole ()) {
408+ return cloudFoundryClient .organizations ()
409+ .removeManagerByUsername (RemoveOrganizationManagerByUsernameRequest .builder ()
410+ .organizationId (organizationId )
411+ .username (request .getUsername ())
412+ .build ())
413+ .then ();
414+ }
415+
416+ return ExceptionUtils .illegalArgument ("Unknown organization role specified" );
417+ }
418+
274419 private static Mono <Void > removeSpaceRole (CloudFoundryClient cloudFoundryClient , UnsetSpaceRoleRequest request , String spaceId ) {
275420 if (SpaceRole .AUDITOR == request .getSpaceRole ()) {
276421 return cloudFoundryClient .spaces ()
@@ -302,6 +447,15 @@ private static Mono<Void> removeSpaceRole(CloudFoundryClient cloudFoundryClient,
302447 return ExceptionUtils .illegalArgument ("Unknown space role specified" );
303448 }
304449
450+ private static Mono <AssociateOrganizationUserByUsernameResponse > requestAssociateOrganizationUserByUsername (CloudFoundryClient cloudFoundryClient , String organizationId ,
451+ SetOrganizationRoleRequest request ) {
452+ return cloudFoundryClient .organizations ()
453+ .associateUserByUsername (AssociateOrganizationUserByUsernameRequest .builder ()
454+ .organizationId (organizationId )
455+ .username (request .getUsername ())
456+ .build ());
457+ }
458+
305459 private static Mono <CreateUserResponse > requestCreateUaaUser (UaaClient uaaClient , CreateUserRequest request ) {
306460 return uaaClient .users ()
307461 .create (org .cloudfoundry .uaa .users .CreateUserRequest .builder ()
@@ -347,6 +501,30 @@ private static Mono<GetFeatureFlagResponse> requestGetFeatureFlag(CloudFoundryCl
347501 .build ());
348502 }
349503
504+ private static Flux <UserResource > requestListOrganizationAuditors (CloudFoundryClient cloudFoundryClient , String organizationId ) {
505+ return PaginationUtils .requestClientV2Resources (page -> cloudFoundryClient .organizations ()
506+ .listAuditors (ListOrganizationAuditorsRequest .builder ()
507+ .organizationId (organizationId )
508+ .page (page )
509+ .build ()));
510+ }
511+
512+ private static Flux <UserResource > requestListOrganizationBillingManagers (CloudFoundryClient cloudFoundryClient , String organizationId ) {
513+ return PaginationUtils .requestClientV2Resources (page -> cloudFoundryClient .organizations ()
514+ .listBillingManagers (ListOrganizationBillingManagersRequest .builder ()
515+ .organizationId (organizationId )
516+ .page (page )
517+ .build ()));
518+ }
519+
520+ private static Flux <UserResource > requestListOrganizationManagers (CloudFoundryClient cloudFoundryClient , String organizationId ) {
521+ return PaginationUtils .requestClientV2Resources (page -> cloudFoundryClient .organizations ()
522+ .listManagers (ListOrganizationManagersRequest .builder ()
523+ .organizationId (organizationId )
524+ .page (page )
525+ .build ()));
526+ }
527+
350528 private static Flux <OrganizationResource > requestListOrganizations (CloudFoundryClient cloudFoundryClient , String organizationName ) {
351529 return PaginationUtils .requestClientV2Resources (page -> cloudFoundryClient .organizations ()
352530 .list (ListOrganizationsRequest .builder ()
@@ -388,6 +566,14 @@ private static Flux<SpaceResource> requestListSpaces(CloudFoundryClient cloudFou
388566 .build ()));
389567 }
390568
569+ private Mono <OrganizationUsers > toOrganizationUsers (List <String > auditors , List <String > billingManagers , List <String > managers ) {
570+ return Mono .just (OrganizationUsers .builder ()
571+ .addAllAuditors (auditors )
572+ .addAllBillingManagers (billingManagers )
573+ .addAllManagers (managers )
574+ .build ());
575+ }
576+
391577 private Mono <SpaceUsers > toSpaceUsers (List <String > auditors , List <String > developers , List <String > managers ) {
392578 return Mono .just (SpaceUsers .builder ()
393579 .addAllAuditors (auditors )
0 commit comments