Search in sources :

Example 6 with DescribeVolumesResult

use of com.amazonaws.services.ec2.model.DescribeVolumesResult in project photon-model by vmware.

the class AWSRemoteCleanup method deleteStaleAwsVolumes.

@Test
public void deleteStaleAwsVolumes() {
    AmazonEC2 usEastEc2Client = this.ec2Clients.get(US_EAST_1_TAG);
    DescribeVolumesResult volumesResult = usEastEc2Client.describeVolumes();
    List<Volume> volumeList = volumesResult.getVolumes();
    for (Volume volume : volumeList) {
        long volumeCreationTimeMicros = TimeUnit.MILLISECONDS.toMicros(volume.getCreateTime().getTime());
        long timeDifference = Utils.getNowMicrosUtc() - volumeCreationTimeMicros;
        if (timeDifference > TimeUnit.HOURS.toMicros(1) && volume.getState().equalsIgnoreCase("available")) {
            this.host.log("Terminating stale volume: %s", volume.getVolumeId());
            DeleteVolumeRequest deleteVolumeRequest = new DeleteVolumeRequest().withVolumeId(volume.getVolumeId());
            usEastEc2Client.deleteVolume(deleteVolumeRequest);
        }
    }
}
Also used : DeleteVolumeRequest(com.amazonaws.services.ec2.model.DeleteVolumeRequest) Volume(com.amazonaws.services.ec2.model.Volume) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) Test(org.junit.Test)

Example 7 with DescribeVolumesResult

use of com.amazonaws.services.ec2.model.DescribeVolumesResult in project photon-model by vmware.

the class TestAWSSetupUtils method getAwsDisksByIds.

/**
 * Method to get Disk details directly from Amazon
 */
public static List<Volume> getAwsDisksByIds(AmazonEC2AsyncClient client, VerificationHost host, List<String> diskIds) throws Throwable {
    try {
        host.log("Getting disks with ids " + diskIds + " from the AWS endpoint using the EC2 client.");
        DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest().withVolumeIds(diskIds);
        DescribeVolumesResult describeVolumesResult = client.describeVolumes(describeVolumesRequest);
        return describeVolumesResult.getVolumes();
    } catch (Exception e) {
        if (e instanceof AmazonEC2Exception && ((AmazonEC2Exception) e).getErrorCode().equalsIgnoreCase(AWS_INVALID_VOLUME_ID_ERROR_CODE)) {
            return null;
        }
    }
    return new ArrayList<>();
}
Also used : ArrayList(java.util.ArrayList) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest) UnknownHostException(java.net.UnknownHostException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) TimeoutException(java.util.concurrent.TimeoutException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Example 8 with DescribeVolumesResult

use of com.amazonaws.services.ec2.model.DescribeVolumesResult in project photon-model by vmware.

the class TestAWSProvisionTask method getVolume.

protected Volume getVolume(AmazonEC2AsyncClient client, Instance awsInstance, String deviceName) {
    InstanceBlockDeviceMapping bootDiskMapping = awsInstance.getBlockDeviceMappings().stream().filter(blockDeviceMapping -> blockDeviceMapping.getDeviceName().equals(deviceName)).findAny().orElse(null);
    // The ami used in this test is an ebs-backed AMI
    assertNotNull("Device type should be ebs type", bootDiskMapping.getEbs());
    String bootVolumeId = bootDiskMapping.getEbs().getVolumeId();
    DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest().withVolumeIds(bootVolumeId);
    DescribeVolumesResult describeVolumesResult = client.describeVolumes(describeVolumesRequest);
    return describeVolumesResult.getVolumes().get(0);
}
Also used : InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest)

Aggregations

DescribeVolumesResult (com.amazonaws.services.ec2.model.DescribeVolumesResult)7 DescribeVolumesRequest (com.amazonaws.services.ec2.model.DescribeVolumesRequest)5 Volume (com.amazonaws.services.ec2.model.Volume)3 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)2 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonWebServiceRequest (com.amazonaws.AmazonWebServiceRequest)1 AmazonWebServiceResult (com.amazonaws.AmazonWebServiceResult)1 AsyncHandler (com.amazonaws.handlers.AsyncHandler)1 CreateVolumeRequest (com.amazonaws.services.ec2.model.CreateVolumeRequest)1 CreateVolumeResult (com.amazonaws.services.ec2.model.CreateVolumeResult)1 DeleteVolumeRequest (com.amazonaws.services.ec2.model.DeleteVolumeRequest)1 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)1 DescribeNatGatewaysResult (com.amazonaws.services.ec2.model.DescribeNatGatewaysResult)1 Filter (com.amazonaws.services.ec2.model.Filter)1 Instance (com.amazonaws.services.ec2.model.Instance)1 InstanceBlockDeviceMapping (com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping)1 NatGateway (com.amazonaws.services.ec2.model.NatGateway)1 AWSUtils.getAWSNonTerminatedInstancesFilter (com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getAWSNonTerminatedInstancesFilter)1 UnknownHostException (java.net.UnknownHostException)1