use of com.cloudera.thunderhead.service.usermanagement.UserManagementProto.AddMemberToGroupRequest 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