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; } } }
/** * This method returns all Iam users. * @return List<User> returns all Iam users. * @throws AmazonClientException */ public List<User> getAllIamUsers() throws AmazonClientException { String marker = null; List<User> allIamUsers = new ArrayList<>(); try { // Fetch all iam users. while(true) { ListUsersRequest listUsersRequest = new ListUsersRequest(); listUsersRequest.setMarker(marker); ListUsersResult listUsersResult = this.iamClient.listUsers(listUsersRequest); allIamUsers.addAll(listUsersResult.getUsers()); // Check the listUsersResult is truncated. This method returns users in batches of 100. if ( listUsersResult.isTruncated() ) { marker = listUsersResult.getMarker(); } else { break; } } } catch(AmazonClientException e) { System.out.println("ERROR : fetching all iam users"); e.printStackTrace(); throw e; } List<String> allIamUsersName = allIamUsers.stream().map(e -> e.getUserName()).collect(Collectors.toList()); System.out.println("INFO : Number of Iam users : " + allIamUsers.size()); System.out.println("INFO : Iam users : " + allIamUsersName); return allIamUsers; }
@Override public List<AbstractResource<?>> listUsers(Account account, DateTime dt) { AmazonIdentityManagement iam = findClient(account); ListUsersRequest req = new ListUsersRequest(); log.debug("start list users for account:{} via api", account.getId() + "=>" + account.getName()); ListUsersResult res = iam.listUsers(req); return converter.toIamUsers(res.getUsers(), account.getId(), dt); }