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

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

Introduction

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

Prototype

@Override
    public DescribeKeyPairsResult describeKeyPairs() 

Source Link

Usage

From source file:com.boxupp.dao.AwsProjectDAOManager.java

License:Apache License

private StatusBean validateKeyPair(AmazonEC2Client ec2Client, String keyPair) throws AmazonServiceException {
    StatusBean statusBean = new StatusBean();
    List<com.amazonaws.services.ec2.model.Region> regions = ec2Client.describeRegions().getRegions();
    for (com.amazonaws.services.ec2.model.Region region : regions) {
        ec2Client.setRegion(com.amazonaws.regions.Region
                .getRegion(com.amazonaws.regions.Regions.fromName(region.getRegionName())));
        List<KeyPairInfo> keyPairs = ec2Client.describeKeyPairs().getKeyPairs();
        for (KeyPairInfo keyPairInfo : keyPairs) {
            if (keyPairInfo.getKeyName().equals(keyPair)) {
                statusBean.setStatusCode(0);
                statusBean.setStatusMessage("Key Pair validated with region " + region.getRegionName());
                return statusBean;
            }//from   w ww  .j  a  v a 2  s .c om
        }
    }
    statusBean.setStatusCode(1);
    statusBean.setStatusMessage("Key Pair not found Please enter a valid key pair");
    return statusBean;
}

From source file:n3phele.factory.ec2.VirtualServerResource.java

License:Open Source License

@GET
@RolesAllowed("authenticated")
@Path("dump")
public String dump(@QueryParam("id") String id, @QueryParam("key") String key,
        @DefaultValue("https://ec2.amazonaws.com") @QueryParam("location") String location) {
    log.info("Id=" + id + " key=" + key);
    ClientConfiguration clientConfiguration = new ClientConfiguration();

    try {/*from  w w w.  j  av a 2  s  .  c o  m*/
        clientConfiguration
                .setProtocol(Protocol.valueOf(URI.create(location).toURL().getProtocol().toUpperCase()));
    } catch (MalformedURLException e) {
        throw new WebApplicationException();
    }
    AmazonEC2Client client = new AmazonEC2Client(new BasicAWSCredentials(id, key), clientConfiguration);
    client.setEndpoint(location.toString());

    DescribeKeyPairsResult result = client.describeKeyPairs();
    log.info("Key pairs " + result.getKeyPairs());

    return (result.getKeyPairs().size() + " key pairs ");
}

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>. /*w ww . j  a v  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;
}