com.amazonaws.services.identitymanagement.model.ListUsersResult Java Examples

The following examples show how to use com.amazonaws.services.identitymanagement.model.ListUsersResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IAMService.java    From Serverless-Programming-Cookbook with MIT License 6 votes vote down vote up
/**
 * Check user.
 * @param userName - user name.
 * @return IAMOperationResponse
 */
public final IAMOperationResponse checkUser(final String userName) {
    boolean done = false;
    ListUsersRequest request = new ListUsersRequest();

    while (!done) {
        ListUsersResult response = iamClient.listUsers(request);

        for (User user : response.getUsers()) {
            if (user.getUserName().equals(userName)) {
                return new IAMOperationResponse("User " + userName + " exist", null);
            }
        }

        request.setMarker(response.getMarker());

        if (!response.getIsTruncated()) {
            done = true;
        }
    }
    return new IAMOperationResponse(null, "User " + userName + " does not exist");
}
 
Example #2
Source File: IAMServiceImpl.java    From Serverless-Programming-Cookbook with MIT License 6 votes vote down vote up
@Override
public final IAMOperationResponse checkUser(final String userName) {
    boolean done = false;
    ListUsersRequest request = new ListUsersRequest();

    while (!done) {
        ListUsersResult response = iamClient.listUsers(request);

        for (User user : response.getUsers()) {
            if (user.getUserName().equals(userName)) {
                return new IAMOperationResponse("User " + userName + " exist", null);
            }
        }

        request.setMarker(response.getMarker());

        if (!response.getIsTruncated()) {
            done = true;
        }
    }
    return new IAMOperationResponse(null, "User " + userName + " does not exist");
}
 
Example #3
Source File: ListUsers.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final AmazonIdentityManagement iam =
            AmazonIdentityManagementClientBuilder.defaultClient();

        boolean done = false;
        ListUsersRequest request = new ListUsersRequest();

        while(!done) {
            ListUsersResult response = iam.listUsers(request);

            for(User user : response.getUsers()) {
                System.out.format("Retrieved user %s", user.getUserName());
            }

            request.setMarker(response.getMarker());

            if(!response.getIsTruncated()) {
                done = true;
            }
        }
    }
 
Example #4
Source File: IAMServiceImplTest.java    From Serverless-Programming-Cookbook with MIT License 4 votes vote down vote up
private ListUsersResult getListUsersResult() {
    ListUsersResult result = new ListUsersResult();
    result.getUsers().add(new User().withUserName("test_user"));
    return result;
}
 
Example #5
Source File: InventoryUtilTest.java    From pacbot with Apache License 2.0 4 votes vote down vote up
/**
 * Fetch IAM users test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchIAMUsersTest() throws Exception {
    
    mockStatic(AmazonIdentityManagementClientBuilder.class);
    AmazonIdentityManagement iamClient = PowerMockito.mock(AmazonIdentityManagement.class);
    AmazonIdentityManagementClientBuilder amazonIdentityManagementClientBuilder = PowerMockito.mock(AmazonIdentityManagementClientBuilder.class);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class);
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider);
    when(amazonIdentityManagementClientBuilder.standard()).thenReturn(amazonIdentityManagementClientBuilder);
    when(amazonIdentityManagementClientBuilder.withCredentials(anyObject())).thenReturn(amazonIdentityManagementClientBuilder);
    when(amazonIdentityManagementClientBuilder.withRegion(anyString())).thenReturn(amazonIdentityManagementClientBuilder);
    when(amazonIdentityManagementClientBuilder.build()).thenReturn(iamClient);
    
    ListUsersResult listUsersResult = new ListUsersResult();
    List<User> users = new ArrayList<>();
    User user = new User();
    user.setUserName("name");
    users.add(user);
    listUsersResult.setUsers(users);
    when(iamClient.listUsers(anyObject())).thenReturn(listUsersResult);
    
    ListAccessKeysResult listAccessKeysResult = new ListAccessKeysResult();
    List<AccessKeyMetadata> accessKeyMetadataList = new ArrayList<>();
    AccessKeyMetadata accessKeyMetadata = new AccessKeyMetadata();
    accessKeyMetadata.setAccessKeyId("accessKeyId");
    accessKeyMetadataList.add(accessKeyMetadata);
    listAccessKeysResult.setAccessKeyMetadata(accessKeyMetadataList );
    when(iamClient.listAccessKeys(anyObject())).thenReturn(listAccessKeysResult);
    
    GetAccessKeyLastUsedResult getAccessKeyLastUsedResult = new GetAccessKeyLastUsedResult();
    AccessKeyLastUsed accessKeyLastUsed = new AccessKeyLastUsed();
    accessKeyLastUsed.setLastUsedDate(new Date());
    getAccessKeyLastUsedResult.setAccessKeyLastUsed(accessKeyLastUsed );
    when(iamClient.getAccessKeyLastUsed(anyObject())).thenReturn(getAccessKeyLastUsedResult);
    
    GetLoginProfileResult getLoginProfileResult = new GetLoginProfileResult();
    LoginProfile loginProfile = new LoginProfile();
    loginProfile.setCreateDate(new Date());
    loginProfile.setPasswordResetRequired(false);
    getLoginProfileResult.setLoginProfile(loginProfile );
    when(iamClient.getLoginProfile(anyObject())).thenReturn(getLoginProfileResult );
    
    ListGroupsForUserResult listGroupsForUserResult = new ListGroupsForUserResult();
    List<Group> groups = new ArrayList<>();
    Group group = new Group();
    group.setGroupName("groupName");
    groups.add(group);
    listGroupsForUserResult.setGroups(groups );
    when(iamClient.listGroupsForUser(anyObject())).thenReturn(listGroupsForUserResult );
    
    ListMFADevicesResult listMFADevicesResult = new ListMFADevicesResult();
    listMFADevicesResult.setMFADevices(new ArrayList<>());;
    when(iamClient.listMFADevices(anyObject())).thenReturn(listMFADevicesResult );
    
    assertThat(inventoryUtil.fetchIAMUsers(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"),
            "account","accountName").size(), is(1));
    
    listMFADevicesResult = new ListMFADevicesResult();
    List<MFADevice> mfaDevices = new ArrayList<>();
    mfaDevices.add(new MFADevice());
    listMFADevicesResult.setMFADevices(mfaDevices);
    when(iamClient.listMFADevices(anyObject())).thenReturn(listMFADevicesResult );
    
    assertThat(inventoryUtil.fetchIAMUsers(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"),
            "account","accountName").size(), is(1));
}