Search in sources :

Example 1 with DeleteKeyPairRequest

use of software.amazon.awssdk.services.ec2.model.DeleteKeyPairRequest in project cloudbreak by hortonworks.

the class AwsResourceConnector method deleteKeyPair.

private void deleteKeyPair(AuthenticatedContext ac, CloudStack stack) {
    AwsCredentialView awsCredential = new AwsCredentialView(ac.getCloudCredential());
    String region = ac.getCloudContext().getLocation().getRegion().value();
    if (!awsClient.existingKeyPairNameSpecified(stack.getInstanceAuthentication())) {
        try {
            AmazonEC2Client client = awsClient.createAccess(awsCredential, region);
            DeleteKeyPairRequest deleteKeyPairRequest = new DeleteKeyPairRequest(awsClient.getKeyPairName(ac));
            client.deleteKeyPair(deleteKeyPairRequest);
        } catch (Exception e) {
            String errorMessage = String.format("Failed to delete public key [roleArn:'%s', region: '%s'], detailed message: %s", awsCredential.getRoleArn(), region, e.getMessage());
            LOGGER.error(errorMessage, e);
        }
    }
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DeleteKeyPairRequest(com.amazonaws.services.ec2.model.DeleteKeyPairRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) IOException(java.io.IOException) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException)

Example 2 with DeleteKeyPairRequest

use of software.amazon.awssdk.services.ec2.model.DeleteKeyPairRequest in project aws-doc-sdk-examples by awsdocs.

the class DeleteKeyPair method deleteKeys.

// snippet-start:[ec2.java2.delete_key_pair.main]
public static void deleteKeys(Ec2Client ec2, String keyPair) {
    try {
        DeleteKeyPairRequest request = DeleteKeyPairRequest.builder().keyName(keyPair).build();
        DeleteKeyPairResponse response = ec2.deleteKeyPair(request);
        System.out.printf("Successfully deleted key pair named %s", keyPair);
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : DeleteKeyPairResponse(software.amazon.awssdk.services.ec2.model.DeleteKeyPairResponse) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) DeleteKeyPairRequest(software.amazon.awssdk.services.ec2.model.DeleteKeyPairRequest)

Example 3 with DeleteKeyPairRequest

use of software.amazon.awssdk.services.ec2.model.DeleteKeyPairRequest in project aws-doc-sdk-examples by awsdocs.

the class DeleteKeyPair method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply a key pair name\n" + "Ex: DeleteKeyPair <key-pair-name>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String key_name = args[0];
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    DeleteKeyPairRequest request = new DeleteKeyPairRequest().withKeyName(key_name);
    DeleteKeyPairResult response = ec2.deleteKeyPair(request);
    System.out.printf("Successfully deleted key pair named %s", key_name);
}
Also used : DeleteKeyPairResult(com.amazonaws.services.ec2.model.DeleteKeyPairResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DeleteKeyPairRequest(com.amazonaws.services.ec2.model.DeleteKeyPairRequest)

Aggregations

DeleteKeyPairRequest (com.amazonaws.services.ec2.model.DeleteKeyPairRequest)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)1 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)1 DeleteKeyPairResult (com.amazonaws.services.ec2.model.DeleteKeyPairResult)1 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)1 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 ActionWentFailException (com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException)1 IOException (java.io.IOException)1 DeleteKeyPairRequest (software.amazon.awssdk.services.ec2.model.DeleteKeyPairRequest)1 DeleteKeyPairResponse (software.amazon.awssdk.services.ec2.model.DeleteKeyPairResponse)1 Ec2Exception (software.amazon.awssdk.services.ec2.model.Ec2Exception)1