Search in sources :

Example 1 with DescribeVolumesResult

use of com.amazonaws.services.ec2.model.DescribeVolumesResult in project SimianArmy by Netflix.

the class AWSClient method describeVolumes.

/**
     * Describe a set of specific EBS volumes.
     *
     * @param volumeIds the volume ids
     * @return the volumes
     */
public List<Volume> describeVolumes(String... volumeIds) {
    if (volumeIds == null || volumeIds.length == 0) {
        LOGGER.info(String.format("Getting all EBS volumes in region %s.", region));
    } else {
        LOGGER.info(String.format("Getting EBS volumes for %d ids in region %s.", volumeIds.length, region));
    }
    AmazonEC2 ec2Client = ec2Client();
    DescribeVolumesRequest request = new DescribeVolumesRequest();
    if (volumeIds != null) {
        request.setVolumeIds(Arrays.asList(volumeIds));
    }
    DescribeVolumesResult result = ec2Client.describeVolumes(request);
    List<Volume> volumes = result.getVolumes();
    LOGGER.info(String.format("Got %d EBS volumes in region %s.", volumes.size(), region));
    return volumes;
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2)

Example 2 with DescribeVolumesResult

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

the class AWSEBSStorageEnumerationAdapterService method createAWSRequestAndAsyncHandler.

/**
 * Initializes and saves a reference to the request object that is sent to AWS to get a page of
 * volumes. Also saves an instance to the async handler that will be used to handle the
 * responses received from AWS. It sets the nextToken value in the request object sent to AWS
 * for getting the next page of results from AWS.
 */
private void createAWSRequestAndAsyncHandler(EBSStorageEnumerationContext aws) {
    DescribeVolumesRequest request = new DescribeVolumesRequest();
    request.setMaxResults(getQueryPageSize());
    request.setNextToken(aws.nextToken);
    aws.describeVolumesRequest = request;
    AsyncHandler<DescribeVolumesRequest, DescribeVolumesResult> resultHandler = new AWSStorageEnumerationAsyncHandler(this, aws);
    aws.resultHandler = resultHandler;
}
Also used : DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest)

Example 3 with DescribeVolumesResult

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

the class TestAWSSetupUtils method createVolume.

/**
 * Creates a volume and return the volume id.
 */
public static String createVolume(VerificationHost host, AmazonEC2Client client) {
    CreateVolumeRequest req = new CreateVolumeRequest().withAvailabilityZone(zoneId + avalabilityZoneIdentifier).withSize(1);
    CreateVolumeResult res = client.createVolume(req);
    String volumeId = res.getVolume().getVolumeId();
    Filter filter = new Filter().withName(VOLUME_ID_ATTRIBUTE).withValues(volumeId);
    DescribeVolumesRequest volumesRequest = new DescribeVolumesRequest().withVolumeIds(volumeId).withFilters(filter);
    host.waitFor("Timeout waiting for creating volume", () -> {
        DescribeVolumesResult volumesResult = client.describeVolumes(volumesRequest);
        String state = volumesResult.getVolumes().get(0).getState();
        if (state.equalsIgnoreCase(VOLUME_STATUS_AVAILABLE)) {
            return true;
        }
        return false;
    });
    tagResources(client, Arrays.asList(volumeId), TAG_KEY_FOR_TEST_RESOURCES, TAG_VALUE_FOR_TEST_RESOURCES + TAG_VOLUME);
    return volumeId;
}
Also used : CreateVolumeRequest(com.amazonaws.services.ec2.model.CreateVolumeRequest) AWSUtils.getAWSNonTerminatedInstancesFilter(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getAWSNonTerminatedInstancesFilter) Filter(com.amazonaws.services.ec2.model.Filter) CreateVolumeResult(com.amazonaws.services.ec2.model.CreateVolumeResult) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest)

Example 4 with DescribeVolumesResult

use of com.amazonaws.services.ec2.model.DescribeVolumesResult in project cloudbreak by hortonworks.

the class EbsVolumeStatusCheckerTask method doCall.

@Override
protected Boolean doCall() {
    LOGGER.info("Checking if AWS EBS volume '{}' is created.", volumeId);
    DescribeVolumesResult describeVolumesResult = amazonEC2Client.describeVolumes(new DescribeVolumesRequest().withVolumeIds(volumeId));
    Optional<Volume> volume = describeVolumesResult.getVolumes().stream().findFirst();
    return volume.isPresent() && "available".equals(volume.get().getState());
}
Also used : Volume(com.amazonaws.services.ec2.model.Volume) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest)

Example 5 with DescribeVolumesResult

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

the class AWSTaskStatusChecker method buildHandler.

private AsyncHandler buildHandler(T type) {
    return new AsyncHandler<AmazonWebServiceRequest, AmazonWebServiceResult>() {

        @Override
        public void onError(Exception exception) {
            // particular instanceId.
            if (exception instanceof AmazonServiceException && ((AmazonServiceException) exception).getErrorCode().equalsIgnoreCase(AWS_INVALID_INSTANCE_ID_ERROR_CODE)) {
                AWSTaskStatusChecker.this.service.logWarning("Could not retrieve status for instance %s. Retrying... Exception on AWS is %s", AWSTaskStatusChecker.this.instanceId, exception);
                AWSTaskStatusChecker.create(AWSTaskStatusChecker.this.amazonEC2Client, AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, AWSTaskStatusChecker.this.failureStates, AWSTaskStatusChecker.this.consumer, AWSTaskStatusChecker.this.taskManager, AWSTaskStatusChecker.this.service, AWSTaskStatusChecker.this.expirationTimeMicros).start(type);
                return;
            } else if (exception instanceof AmazonEC2Exception && ((AmazonEC2Exception) exception).getErrorCode().equalsIgnoreCase(AWS_INVALID_VOLUME_ID_ERROR_CODE)) {
                AWSTaskStatusChecker.this.consumer.accept(null);
                return;
            }
            AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(exception);
            return;
        }

        @Override
        public void onSuccess(AmazonWebServiceRequest request, AmazonWebServiceResult result) {
            String status;
            Object instance;
            String failureMessage = null;
            String stateReason = null;
            if (result instanceof DescribeInstancesResult) {
                instance = ((DescribeInstancesResult) result).getReservations().get(0).getInstances().get(0);
                Instance vm = (Instance) instance;
                status = vm.getState().getName();
                stateReason = vm.getStateReason() != null ? vm.getStateReason().getMessage() : null;
            } else if (result instanceof DescribeNatGatewaysResult) {
                instance = ((DescribeNatGatewaysResult) result).getNatGateways().get(0);
                status = ((NatGateway) instance).getState();
                // if NAT gateway creation fails, the status is still "pending";
                // rather than keep checking for status and eventually time out, get the
                // failure message and fail the task
                failureMessage = ((NatGateway) instance).getFailureMessage();
            } else if (result instanceof DescribeVolumesResult) {
                instance = ((DescribeVolumesResult) result).getVolumes().get(0);
                status = ((Volume) instance).getState().toLowerCase();
            } else {
                AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalArgumentException("Invalid type " + result));
                return;
            }
            if (failureMessage != null) {
                // operation failed; no need to keep checking for desired status
                AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalStateException(failureMessage));
                return;
            } else if (AWSTaskStatusChecker.this.failureStates.contains(status)) {
                // operation failed; no need to keep checking for desired status
                AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalStateException("Resource is state:[" + status + "]," + "reason:" + stateReason));
                return;
            } else if (!status.equals(AWSTaskStatusChecker.this.desiredState)) {
                AWSTaskStatusChecker.this.service.logInfo("Instance %s not yet in desired state %s. Current state %s, failure states %s, waiting 5s", AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, status, AWSTaskStatusChecker.this.failureStates);
                // if the instance is not in the desired state, schedule thread
                // to run again in 5 seconds
                AWSTaskStatusChecker.this.service.getHost().schedule(() -> {
                    AWSTaskStatusChecker.create(AWSTaskStatusChecker.this.amazonEC2Client, AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, AWSTaskStatusChecker.this.failureStates, AWSTaskStatusChecker.this.consumer, AWSTaskStatusChecker.this.taskManager, AWSTaskStatusChecker.this.service, AWSTaskStatusChecker.this.expirationTimeMicros).start(type);
                }, 5, TimeUnit.SECONDS);
                return;
            }
            AWSTaskStatusChecker.this.consumer.accept(instance);
            return;
        }
    };
}
Also used : AsyncHandler(com.amazonaws.handlers.AsyncHandler) Instance(com.amazonaws.services.ec2.model.Instance) DescribeNatGatewaysResult(com.amazonaws.services.ec2.model.DescribeNatGatewaysResult) NatGateway(com.amazonaws.services.ec2.model.NatGateway) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AmazonWebServiceResult(com.amazonaws.AmazonWebServiceResult) Volume(com.amazonaws.services.ec2.model.Volume) AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

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