Example usage for com.amazonaws.services.ec2.model RunInstancesRequest getSubnetId

List of usage examples for com.amazonaws.services.ec2.model RunInstancesRequest getSubnetId

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model RunInstancesRequest getSubnetId.

Prototype


public String getSubnetId() 

Source Link

Document

[EC2-VPC] The ID of the subnet to launch the instance into.

Usage

From source file:com.rmn.qa.aws.AwsVmManager.java

License:Open Source License

/**
 * Attempts to run the {@link com.amazonaws.services.ec2.model.RunInstancesRequest RunInstancesRequest}, falling
 * back on alternative subnets if capacity is full in the current region.
 *
 * @param   request//from www.j  a  v  a  2  s.  co m
 * @param   requestNumber
 *
 * @return
 *
 * @throws  NodesCouldNotBeStartedException
 */
private RunInstancesResult getResults(final RunInstancesRequest request, int requestNumber)
        throws NodesCouldNotBeStartedException {
    RunInstancesResult runInstancesResult;
    try {
        AmazonEC2Client localClient = getClient();
        if (localClient == null) {
            throw new RuntimeException("The client is not initialized");
        }
        runInstancesResult = localClient.runInstances(request);
    } catch (AmazonServiceException e) {

        // If there is insufficient capacity in this subnet / availability zone, then we want to try other
        // configured subnets
        if ("InsufficientInstanceCapacity".equals(e.getErrorCode())
                || "VolumeTypeNotAvailableInZone".equals(e.getErrorCode())) {
            log.error(String.format("Insufficient capacity in subnet [%s]: %s", request.getSubnetId(), e));
            requestNumber = requestNumber + 1;

            Properties awsProperties = getAwsProperties();
            String fallBackSubnetId = awsProperties
                    .getProperty(region + "_subnet_fallback_id_" + requestNumber);

            // Make sure and only try to recursively loop so as long as we have a valid fallback subnet id.  Logic
            // to also
            // prevent an accidental infinite loop
            if (fallBackSubnetId != null && requestNumber < 5) {
                log.info("Setting fallback subnet: " + fallBackSubnetId);

                // Modify the original request with the new subnet ID we're trying to fallback on
                request.withSubnetId(fallBackSubnetId);
            } else {
                throw new NodesCouldNotBeStartedException(
                        "Sufficient resources were not available in any of the availability zones");
            }

            return getResults(request, requestNumber);
        } else {

            // We got an error other than insufficient capacity, and should just throw it for the caller to handle
            throw e;
        }
    }

    return runInstancesResult;
}