Java Code Examples for com.amazonaws.services.ec2.AmazonEC2Client#deleteSnapshot()

The following examples show how to use com.amazonaws.services.ec2.AmazonEC2Client#deleteSnapshot() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EncryptedImageCopyService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void deleteSnapshot(AmazonEC2Client client, BlockDeviceMapping blockDeviceMapping, CloudResource encryptedImage, String regionName) {
    String snapshotId = blockDeviceMapping.getEbs().getSnapshotId();
    String imageName = encryptedImage.getName();
    try {
        LOGGER.debug("Delete encrypted AMI's ['{}'] snapshot ['{}'], in region: '{}'", imageName, snapshotId, regionName);
        DeleteSnapshotRequest deleteSnapshotRequest = new DeleteSnapshotRequest().withSnapshotId(snapshotId);
        client.deleteSnapshot(deleteSnapshotRequest);
    } catch (Exception e) {
        String errorMessage = format("Failed to delete snapshot [id:'%s'] of AMI [id:'%s'], in region: '%s', detailed message: %s",
                snapshotId, imageName, regionName, e.getMessage());
        LOGGER.warn(errorMessage, e);
        throw new CloudConnectorException(errorMessage, e);
    }
}
 
Example 2
Source File: EncryptedSnapshotService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void deleteSnapshotResources(AmazonEC2Client client, List<CloudResource> resources) {
    Set<CloudResource> encryptedSnapshots = filterResourcesByType(resources, ResourceType.AWS_SNAPSHOT);
    for (CloudResource snapshot : encryptedSnapshots) {
        try {
            DeleteSnapshotRequest deleteSnapshotRequest = new DeleteSnapshotRequest().withSnapshotId(snapshot.getName());
            client.deleteSnapshot(deleteSnapshotRequest);
        } catch (Exception e) {
            String errorMessage = String.format("Failed to delete snapshot [id:'%s'], detailed message: %s", snapshot.getName(), e.getMessage());
            LOGGER.warn(errorMessage, e);
            if (!e.getMessage().contains(SNAPSHOT_NOT_FOUND_MSG_CODE)) {
                throw new CloudConnectorException(errorMessage, e);
            }
        }
    }
}