Example usage for com.amazonaws.services.ec2 AmazonEC2Client describeAddresses

List of usage examples for com.amazonaws.services.ec2 AmazonEC2Client describeAddresses

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2 AmazonEC2Client describeAddresses.

Prototype

@Override
    public DescribeAddressesResult describeAddresses() 

Source Link

Usage

From source file:org.elasticdroid.model.EC2DashboardModel.java

License:Open Source License

/**
 * Gets the data to populate the EC2 Dashboard with in the background thread, and loads it into
 * a Hashtable<String, Integer>. // www.  j  av a2  s .c  o  m
 * 
 * @param This method accepts *ONE* Hashtable<String, String> of LoginDetails arguments. The
 * required keys are as follows (anything else is ignored):
 * <ul>
 * <li> accessKey: The accesskey for the AWS/AWS IAM account used.</li> 
 * <li> secretAccessKey: The secretAccessKey for the AWS/AWS IAM account used.</li> 
 * <li> endpoint: AWS Endpoint for the selected region (@see {@link AWSConstants.EndPoints}</li>
 * </ul>
 * If you're missing any of these keys, AmazonServiceExceptions will be thrown. This shouldn't
 * be visible to the end-user as this is a programmer fault!!! :P
 * 
 * @return This method can return:
 * <ul>
 * <li>{@link IllegalArgumentException}: If there are too many/few arguments, or the keys are  
 * incorrect. Only one Hashtable<String, String> accepted.</li>
 * <li>{@link Hashtable<String, Integer}: data to populate dashboard with.
 *       <ul>
 *       <li><i>runningInstances:</i> The number of running instances for the user in the current 
 *       region</li>
 *       <li><i>stoppedInstances:</i> The number of stopped instances for the user in the current 
 *       region</li>
 *       <li><i>elasticIp:</i> The number of elastic IPs owned by the user (in the current region)
 *      </li>
 *       <li><i>securityGroups:</i> The number of security groups avail 2 the user (in the current
 *       region)</li>
 *       <li><i>keyPairs:</i> The number of keypairs avail 2 the user (in the current
 *       region)</li>
 *       </ul> 
 * </li>
 * </ul>
 */
@SuppressWarnings("unchecked")
@Override
protected Object doInBackground(HashMap<?, ?>... params) {
    HashMap<String, String> connectionData;
    HashMap<String, Integer> dashboardData;

    //we accept only one param, but AsyncTask forces us to potentially accept
    //a whole bloody lot of them. :P
    if (params.length != 1) {
        return new IllegalArgumentException(
                "Only one Hashtable<String,String> parameter " + "should be passed.");
    }
    connectionData = (HashMap<String, String>) params[0]; //convenience variable, so that
    //i dont have to keep typing params[0] everywhere in this method.;)

    Log.v(this.getClass().getName(), "Getting EC2 dashboard data...");

    //prepare to get the dashboard data!
    //create credentials using the BasicAWSCredentials class
    BasicAWSCredentials credentials = new BasicAWSCredentials(connectionData.get("accessKey"),
            connectionData.get("secretAccessKey"));
    //create Amazon EC2 Client object, and set tye end point to the region. params[3]
    //contains endpoint
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);
    amazonEC2Client.setEndpoint(connectionData.get("endpoint"));
    //initialise result holder variable
    dashboardData = new HashMap<String, Integer>();

    try {
        //get the number of running and stopped instances
        DescribeInstancesResult instances = amazonEC2Client.describeInstances();

        int numOfRunningInstances = 0;
        int numOfStoppedInstances = 0;
        //get the list of reservations in the results
        for (Reservation reservation : instances.getReservations()) {
            //for each reservation, get the list of instances associated
            for (Instance instance : reservation.getInstances()) {
                if (instance.getState().getCode().byteValue() == InstanceStateConstants.RUNNING) {
                    numOfRunningInstances++;
                } else if (instance.getState().getCode().byteValue() == InstanceStateConstants.STOPPED) {
                    numOfStoppedInstances++;
                }
            }
        }
        dashboardData.put("runningInstances", numOfRunningInstances);
        dashboardData.put("stoppedInstances", numOfStoppedInstances);

        //get the list of elastic Ips.
        dashboardData.put("elasticIp", amazonEC2Client.describeAddresses().getAddresses().size());

        //get the list of security groups
        dashboardData.put("securityGroups",
                amazonEC2Client.describeSecurityGroups().getSecurityGroups().size());

        //get the list of keypairs
        dashboardData.put("keyPairs", amazonEC2Client.describeKeyPairs().getKeyPairs().size());
    } catch (AmazonServiceException amazonServiceException) {
        return amazonServiceException;
    } catch (AmazonClientException amazonClientException) {
        return amazonClientException;
    }

    return dashboardData;
}