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

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

Introduction

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

Prototype

DescribeKeyPairsResult describeKeyPairs(DescribeKeyPairsRequest describeKeyPairsRequest);

Source Link

Document

Describes the specified key pairs or all of your key pairs.

Usage

From source file:com.ec2box.manage.action.EC2KeyAction.java

License:Apache License

/**
 * returns keypairs as a json string//www .  j  a va2  s  .  c om
 */
@Action(value = "/manage/getKeyPairJSON")
public String getKeyPairJSON() {

    AWSCred awsCred = AWSCredDB.getAWSCred(ec2Key.getAwsCredId());

    //set  AWS credentials for service
    BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(),
            awsCred.getSecretKey());
    AmazonEC2 service = new AmazonEC2Client(awsCredentials, AWSClientConfig.getClientConfig());

    service.setEndpoint(ec2Key.getEc2Region());

    DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();

    DescribeKeyPairsResult describeKeyPairsResult = service.describeKeyPairs(describeKeyPairsRequest);

    List<KeyPairInfo> keyPairInfoList = describeKeyPairsResult.getKeyPairs();
    String json = new Gson().toJson(keyPairInfoList);
    try {
        servletResponse.getOutputStream().write(json.getBytes());
    } catch (Exception ex) {
        log.error(ex.toString(), ex);
    }
    return null;
}

From source file:com.ec2box.manage.action.EC2KeyAction.java

License:Apache License

@Action(value = "/manage/importEC2Key", results = {
        @Result(name = "input", location = "/manage/view_ec2_keys.jsp"),
        @Result(name = "success", location = "/manage/viewEC2Keys.action", type = "redirect") })
public String importEC2Key() {

    String retVal = SUCCESS;/*from w  w  w.  ja  va  2  s  .co m*/

    try {
        //get AWS credentials from DB
        AWSCred awsCred = AWSCredDB.getAWSCred(ec2Key.getAwsCredId());

        //set  AWS credentials for service
        BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(),
                awsCred.getSecretKey());

        //create service
        AmazonEC2 service = new AmazonEC2Client(awsCredentials, AWSClientConfig.getClientConfig());
        service.setEndpoint(ec2Key.getEc2Region());

        //describe key pair request
        DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();
        describeKeyPairsRequest.setKeyNames(Arrays.asList(ec2Key.getKeyNm()));

        //call service
        DescribeKeyPairsResult describeKeyPairsResult = service.describeKeyPairs(describeKeyPairsRequest);

        if (describeKeyPairsResult != null && describeKeyPairsResult.getKeyPairs().size() > 0) {
            //add to db
            EC2KeyDB.saveEC2Key(ec2Key);
        } else {
            addActionError("Imported key does not exist on AWS");
            retVal = INPUT;
        }

    } catch (AmazonServiceException ex) {
        addActionError(ex.getMessage());
        retVal = INPUT;

    }

    return retVal;

}

From source file:com.keybox.manage.action.ApplicationKeysAction.java

License:Apache License

/**
  * returns keypairs as a json string//  ww w  . j  a v  a  2 s . com
  */
@Action(value = "/manage/getKeyPairJSON")
public String getKeyPairJSON() {

    AWSCred awsCred = AWSCredDB.getAWSCred(ec2Key.getAwsCredentials().getId());

    //set  AWS credentials for service
    BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(),
            awsCred.getSecretKey());
    AmazonEC2 service = new AmazonEC2Client(awsCredentials, AWSClientConfig.getClientConfig());

    service.setEndpoint(ec2Key.getEc2Region());

    DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();

    DescribeKeyPairsResult describeKeyPairsResult = service.describeKeyPairs(describeKeyPairsRequest);

    List<KeyPairInfo> keyPairInfoList = describeKeyPairsResult.getKeyPairs();
    String json = new Gson().toJson(keyPairInfoList);
    try {
        servletResponse.getOutputStream().write(json.getBytes());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:jp.classmethod.aws.gradle.ec2.AmazonEC2ImportKeyTask.java

License:Apache License

private boolean exists(AmazonEC2 ec2) {
    // to enable conventionMappings feature
    String keyName = getKeyName();

    try {/*  www .j  ava2 s  . co m*/
        DescribeKeyPairsResult describeKeyPairsResult = ec2
                .describeKeyPairs(new DescribeKeyPairsRequest().withKeyNames(keyName));
        return describeKeyPairsResult.getKeyPairs().isEmpty() == false;
    } catch (AmazonClientException e) {
        return false;
    }
}