Search in sources :

Example 1 with Actor

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;
        }
    }
}
Also used : RemoveMemberFromGroupResponse(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.RemoveMemberFromGroupResponse) RemoveMemberFromGroupRequest(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.RemoveMemberFromGroupRequest) Actor(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.Actor) StatusRuntimeException(io.grpc.StatusRuntimeException) Crn(com.sequenceiq.cloudbreak.auth.crn.Crn)

Example 2 with Actor

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;
}
Also used : Actor(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.Actor) ArrayList(java.util.ArrayList) ListGroupsForMemberRequest(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.ListGroupsForMemberRequest) ListGroupsForMemberResponse(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.ListGroupsForMemberResponse) Crn(com.sequenceiq.cloudbreak.auth.crn.Crn)

Example 3 with Actor

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;
        }
    }
}
Also used : Actor(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.Actor) StatusRuntimeException(io.grpc.StatusRuntimeException) AddMemberToGroupResponse(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.AddMemberToGroupResponse) AddMemberToGroupRequest(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.AddMemberToGroupRequest) Crn(com.sequenceiq.cloudbreak.auth.crn.Crn)

Aggregations

Actor (com.cloudera.thunderhead.service.usermanagement.UserManagementProto.Actor)3 Crn (com.sequenceiq.cloudbreak.auth.crn.Crn)3 StatusRuntimeException (io.grpc.StatusRuntimeException)2 AddMemberToGroupRequest (com.cloudera.thunderhead.service.usermanagement.UserManagementProto.AddMemberToGroupRequest)1 AddMemberToGroupResponse (com.cloudera.thunderhead.service.usermanagement.UserManagementProto.AddMemberToGroupResponse)1 ListGroupsForMemberRequest (com.cloudera.thunderhead.service.usermanagement.UserManagementProto.ListGroupsForMemberRequest)1 ListGroupsForMemberResponse (com.cloudera.thunderhead.service.usermanagement.UserManagementProto.ListGroupsForMemberResponse)1 RemoveMemberFromGroupRequest (com.cloudera.thunderhead.service.usermanagement.UserManagementProto.RemoveMemberFromGroupRequest)1 RemoveMemberFromGroupResponse (com.cloudera.thunderhead.service.usermanagement.UserManagementProto.RemoveMemberFromGroupResponse)1 ArrayList (java.util.ArrayList)1