package com.airbnb.billow;

import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.Reservation;
import com.amazonaws.services.identitymanagement.AmazonIdentityManagementClient;
import com.amazonaws.services.identitymanagement.model.AccessKeyMetadata;
import com.amazonaws.services.identitymanagement.model.ListAccessKeysRequest;
import com.amazonaws.services.identitymanagement.model.User;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Data
public class AWSDatabase {
    private final ImmutableList<EC2Instance> instances;
    private final ImmutableList<AccessKeyMetadata> accessKeyMetadata;
    private final ImmutableMap<String, EC2Instance> instancesById;

    AWSDatabase(AmazonEC2Client client, AmazonIdentityManagementClient iamClient) {
        log.info("Building AWS DB");

        log.debug("Getting instances");
        final ImmutableList.Builder<EC2Instance> builder = new ImmutableList.Builder<EC2Instance>();
        for (Reservation reservation : client.describeInstances().getReservations())
            for (Instance instance : reservation.getInstances())
                builder.add(new EC2Instance(instance));

        this.instances = builder.build();
        this.instancesById = Maps.uniqueIndex(this.instances, new Function<EC2Instance, String>() {
            public String apply(EC2Instance input) {
                return input.getId();
            }
        });

        log.debug("Getting IAM keys");
        final ImmutableList.Builder<AccessKeyMetadata> keyMDBuilder = new ImmutableList.Builder<AccessKeyMetadata>();
        for (User user : iamClient.listUsers().getUsers()) {
            final ListAccessKeysRequest req = new ListAccessKeysRequest();
            req.setUserName(user.getUserName());
            keyMDBuilder.addAll(iamClient.listAccessKeys(req).getAccessKeyMetadata());
        }
        this.accessKeyMetadata = keyMDBuilder.build();

        log.info("Done building AWS DB");
    }
}

@@ -315,164 +315,9 @@
 del.
-AccessKeyMetadata;%0Aimport com.amazonaws.services.identitymanagement.model.ListAccessKeysRequest;%0Aimport com.amazonaws.services.identitymanagement.model.User
+*
 ;%0Aim
@@ -543,16 +543,40 @@
 Slf4j;%0A%0A
+import java.util.List;%0A%0A
 @Slf4j%0A@
@@ -927,37 +927,36 @@
 );%0A%0A        log.
-debug
+info
 (%22Getting instan
@@ -1546,13 +1546,12 @@
 log.
-debug
+info
 (%22Ge
@@ -1682,24 +1682,25 @@
 etadata%3E();%0A
+%0A
         for 
@@ -1700,60 +1700,429 @@
    f
-or (User user : iamClient.listUsers().getUsers()) %7B%0A
+inal ListUsersRequest listUsersRequest = new ListUsersRequest();%0A        ListUsersResult listUsersResult;%0A        do %7B%0A            log.debug(%22Performing AMI request: %7B%7D%22, listUsersRequest);%0A            listUsersResult = iamClient.listUsers(listUsersRequest);%0A            final List%3CUser%3E users = listUsersResult.getUsers();%0A            log.debug(%22Found %7B%7D users%22, users.size());%0A            for (User user : users) %7B%0A    
     
@@ -2157,19 +2157,37 @@
 Request 
-req
+listAccessKeysRequest
  = new L
@@ -2222,19 +2222,41 @@
         
-req
+    listAccessKeysRequest
 .setUser
@@ -2297,28 +2297,62 @@
     
-keyMDBuilder.addAll(
+    final List%3CAccessKeyMetadata%3E accessKeyMetadata = 
 iamC
@@ -2372,19 +2372,37 @@
 essKeys(
-req
+listAccessKeysRequest
 ).getAcc
@@ -2421,20 +2421,198 @@
 ta()
-);%0A        %7D
+;%0A                keyMDBuilder.addAll(accessKeyMetadata);%0A            %7D%0A            listUsersRequest.setMarker(listUsersResult.getMarker());%0A        %7D while (listUsersResult.isTruncated());%0A
 %0A   

support for users pagination