Skip to content

Commit 483ba7a

Browse files
committed
Merge branch '840-user-admin-orgs' into 2.x
2 parents d8010b4 + 8534b24 commit 483ba7a

File tree

12 files changed

+926
-11
lines changed

12 files changed

+926
-11
lines changed

cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/useradmin/DefaultUserAdmin.java

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,20 @@
2020
import org.cloudfoundry.client.v2.ClientV2Exception;
2121
import org.cloudfoundry.client.v2.featureflags.GetFeatureFlagRequest;
2222
import 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;
2326
import org.cloudfoundry.client.v2.organizations.AssociateOrganizationUserByUsernameRequest;
2427
import 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;
2531
import org.cloudfoundry.client.v2.organizations.ListOrganizationSpacesRequest;
2632
import org.cloudfoundry.client.v2.organizations.ListOrganizationsRequest;
2733
import 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;
2837
import org.cloudfoundry.client.v2.spaces.AssociateSpaceAuditorByUsernameRequest;
2938
import org.cloudfoundry.client.v2.spaces.AssociateSpaceDeveloperByUsernameRequest;
3039
import 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)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2013-2018 the original author or authors.
3+
*
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
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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.
15+
*/
16+
17+
package org.cloudfoundry.operations.useradmin;
18+
19+
/**
20+
* The organization roles of users
21+
*/
22+
public enum OrganizationRole {
23+
24+
/**
25+
* User is an Auditor for the organization
26+
*/
27+
AUDITOR,
28+
29+
/**
30+
* User is a Billing Manager for the organization
31+
*/
32+
BILLING_MANAGER,
33+
34+
/**
35+
* User is a Manager for the organization
36+
*/
37+
MANAGER
38+
39+
}

cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/useradmin/UserAdmin.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public interface UserAdmin {
3939
*/
4040
Mono<Void> delete(DeleteUserRequest request);
4141

42+
/**
43+
* List organization users
44+
*
45+
* @param request the list organization users request
46+
* @return the Organization Users
47+
*/
48+
Mono<OrganizationUsers> listOrganizationUsers(ListOrganizationUsersRequest request);
49+
4250
/**
4351
* List space users
4452
*
@@ -47,6 +55,14 @@ public interface UserAdmin {
4755
*/
4856
Mono<SpaceUsers> listSpaceUsers(ListSpaceUsersRequest request);
4957

58+
/**
59+
* Assign an organization role to a user
60+
*
61+
* @param request the set organization user request
62+
* @return completion indicator
63+
*/
64+
Mono<Void> setOrganizationRole(SetOrganizationRoleRequest request);
65+
5066
/**
5167
* Assign a space role to a user
5268
*
@@ -55,6 +71,14 @@ public interface UserAdmin {
5571
*/
5672
Mono<Void> setSpaceRole(SetSpaceRoleRequest request);
5773

74+
/**
75+
* Remove an organization role from a user
76+
*
77+
* @param request the unset organization user request
78+
* @return completion indicator
79+
*/
80+
Mono<Void> unsetOrganizationRole(UnsetOrganizationRoleRequest request);
81+
5882
/**
5983
* Remove a space role from a user
6084
*
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2013-2018 the original author or authors.
3+
*
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
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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.
15+
*/
16+
17+
package org.cloudfoundry.operations.useradmin;
18+
19+
import org.immutables.value.Value;
20+
21+
/**
22+
* The request options for the list organization users operation
23+
*/
24+
@Value.Immutable
25+
abstract class _ListOrganizationUsersRequest {
26+
27+
/**
28+
* Organization name to list
29+
*/
30+
abstract String getOrganizationName();
31+
32+
}

0 commit comments

Comments
 (0)