use of com.cloudera.thunderhead.service.usermanagement.UserManagementProto.Actor in project cloudbreak by hortonworks.
the class UmsClient method removeMemberFromGroup.
/**
* Remove member from the selected user group if it is exist.
*
* @param requestId the request ID for the request
* @param accountId the account ID
* @param groupName the group where user is going to be assigned
* @param memberCrn member (e.g., user) CRN
*/
public void removeMemberFromGroup(String requestId, String accountId, String groupName, String memberCrn) {
checkNotNull(requestId, "requestId should not be null.");
checkNotNull(groupName, "groupName should not be null.");
checkNotNull(memberCrn, "memberCrn should not be null.");
validateAccountIdWithWarning(accountId);
Crn crn = Crn.safeFromString(memberCrn);
Actor.Builder actor = Actor.newBuilder().setAccountId(accountId);
switch(crn.getResourceType()) {
case USER:
actor.setUserIdOrCrn(memberCrn);
LOGGER.info("Found user for member Crn: '{}'.", memberCrn);
break;
case MACHINE_USER:
actor.setMachineUserNameOrCrn(memberCrn);
LOGGER.info("Found machine user for member Crn: '{}'.", memberCrn);
break;
default:
throw new IllegalArgumentException(String.format("memberCrn %s is not a USER or MACHINE_USER", memberCrn));
}
try {
RemoveMemberFromGroupRequest.Builder removeMemberFromGroupRequest = RemoveMemberFromGroupRequest.newBuilder().setMember(actor.build()).setGroupNameOrCrn(groupName);
RemoveMemberFromGroupResponse removeMemberFromGroupResponse = newStub(requestId).removeMemberFromGroup(removeMemberFromGroupRequest.build());
LOGGER.info("User '{}' has been removed from the '{}' group successfully.", removeMemberFromGroupResponse.getMemberCrn(), groupName);
} catch (StatusRuntimeException e) {
if (e.getStatus().getCode().equals(Status.NOT_FOUND.getCode())) {
LOGGER.info("User group '{}' or user '{}' not found or has already been deleted.", groupName, memberCrn);
} else {
throw e;
}
}
}
use of com.cloudera.thunderhead.service.usermanagement.UserManagementProto.Actor in project cloudbreak by hortonworks.
the class UmsClient method listGroupsForMembers.
/**
* Wraps calls to ListGroupsForMember with an Account ID and member CRN.
*
* @param requestId the request ID for the request
* @param accountId the account ID
* @param memberCrn member (e.g., user) CRN for which groups are fetched.
* @return the list of group CRNs
*/
public List<String> listGroupsForMembers(String requestId, String accountId, String memberCrn) {
validateAccountIdWithWarning(accountId);
checkNotNull(memberCrn, "memberCrn should not be null.");
Crn crn = Crn.safeFromString(memberCrn);
Actor.Builder actor = Actor.newBuilder().setAccountId(accountId);
switch(crn.getResourceType()) {
case USER:
actor.setUserIdOrCrn(memberCrn);
break;
case MACHINE_USER:
actor.setMachineUserNameOrCrn(memberCrn);
break;
default:
throw new IllegalArgumentException(String.format("memberCrn %s is not a USER or MACHINE_USER", memberCrn));
}
ListGroupsForMemberRequest.Builder request = ListGroupsForMemberRequest.newBuilder().setMember(actor.build());
ListGroupsForMemberResponse response;
List<String> groups = new ArrayList<>();
do {
response = newStub(requestId).listGroupsForMember(request.build());
for (int i = 0; i < response.getGroupCrnCount(); i++) {
String grpCRN = response.getGroupCrn(i);
groups.add(grpCRN);
}
request.setPageToken(response.getNextPageToken());
} while (response.hasNextPageToken());
return groups;
}
use of com.cloudera.thunderhead.service.usermanagement.UserManagementProto.Actor in project cloudbreak by hortonworks.
the class UmsClient method addMemberToGroup.
/**
* Add member to the selected user group if it exist.
*
* @param requestId the request ID for the request
* @param accountId the account ID
* @param groupName the group where user is going to be assigned
* @param memberCrn member (e.g., user) CRN
*/
public void addMemberToGroup(String requestId, String accountId, String groupName, String memberCrn) {
checkNotNull(requestId, "requestId should not be null.");
checkNotNull(groupName, "groupName should not be null.");
checkNotNull(memberCrn, "memberCrn should not be null.");
validateAccountIdWithWarning(accountId);
Crn crn = Crn.safeFromString(memberCrn);
Actor.Builder actor = Actor.newBuilder().setAccountId(accountId);
switch(crn.getResourceType()) {
case USER:
actor.setUserIdOrCrn(memberCrn);
LOGGER.info("Found user for member Crn: '{}'.", memberCrn);
break;
case MACHINE_USER:
actor.setMachineUserNameOrCrn(memberCrn);
LOGGER.info("Found machine user for member Crn: '{}'.", memberCrn);
break;
default:
throw new IllegalArgumentException(String.format("memberCrn %s is not a USER or MACHINE_USER", memberCrn));
}
try {
AddMemberToGroupRequest.Builder addMemberToGroupRequest = AddMemberToGroupRequest.newBuilder().setMember(actor.build()).setGroupNameOrCrn(groupName);
AddMemberToGroupResponse addMemberToGroupResponse = newStub(requestId).addMemberToGroup(addMemberToGroupRequest.build());
LOGGER.info("User '{}' has been assigned to the '{}' group successfully.", addMemberToGroupResponse.getMemberCrn(), groupName);
} catch (StatusRuntimeException e) {
if (e.getStatus().getCode().equals(Status.NOT_FOUND.getCode())) {
LOGGER.info("User group '{}' not found or has already been deleted.", groupName);
} else if (e.getStatus().getCode().equals(Status.ALREADY_EXISTS.getCode())) {
LOGGER.info("User '{}' for group '{}' has already assigned.", memberCrn, groupName);
} else {
throw e;
}
}
}
Aggregations