Example usage for com.amazonaws.services.ec2.model SpotPrice getInstanceType

List of usage examples for com.amazonaws.services.ec2.model SpotPrice getInstanceType

Introduction

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

Prototype


public String getInstanceType() 

Source Link

Document

The instance type.

Usage

From source file:org.excalibur.service.aws.ec2.EC2.java

License:Open Source License

public List<SpotPriceHistory> getSpotPriceHistory(SpotPriceHistoryRequest request) {
    List<SpotPriceHistory> history = new ArrayList<SpotPriceHistory>();

    DescribeSpotPriceHistoryRequest spotPriceRequest = new DescribeSpotPriceHistoryRequest()
            .withEndTime(request.getUntil()).withStartTime(request.getFrom())
            .withAvailabilityZone(request.getRegion() != null ? request.getRegion().getName() : null)
            .withMaxResults(request.getMaxResult()).withProductDescriptions(request.getImageTypes());

    List<InstanceType> types = request.getInstanceTypes();
    String[] instanceTypes = new String[types.size()];

    for (int i = 0; i < instanceTypes.length; i++) {
        instanceTypes[i] = types.get(i).getName();
    }// ww w.  j a  v  a2s .c  om

    spotPriceRequest.withInstanceTypes(instanceTypes);

    DescribeSpotPriceHistoryResult describeSpotPriceHistory = ec2_.describeSpotPriceHistory(spotPriceRequest);

    for (SpotPrice spot : describeSpotPriceHistory.getSpotPriceHistory()) {
        history.add(new SpotPriceHistory().withInstanceType(new InstanceType().setName(spot.getInstanceType()))
                .withPrice(spot.getSpotPrice()).withImageTypeDescription(spot.getProductDescription())
                .withRegion(new Region(spot.getAvailabilityZone())).withTime(spot.getTimestamp()));
    }

    return history;
}

From source file:org.finra.dm.dao.helper.EmrPricingHelper.java

License:Apache License

/**
 * Returns a mapping of instance types to spot prices for the given AZ and instance types. The spot prices are retrieved from EC2 API.
 * <p/>/*from w w w  .j  a v a  2 s.c  om*/
 * This method also validates that the given instance types are real instance types supported by AWS.
 *
 * @param availabilityZone The AZ of the spot instances.
 * @param instanceTypes The size of the spot instances.
 *
 * @return A mapping of instance type to spot prices.
 * @throws ObjectNotFoundException when any of the instance type does not exist in AWS
 */
private Map<String, BigDecimal> getInstanceTypeSpotPrices(AvailabilityZone availabilityZone,
        Set<String> instanceTypes) {
    List<SpotPrice> spotPrices = ec2Dao.getLatestSpotPrices(availabilityZone.getZoneName(), instanceTypes,
            getAwsParamsDto());

    Map<String, BigDecimal> instanceTypeSpotPrices = new HashMap<>();
    for (SpotPrice spotPrice : spotPrices) {
        instanceTypeSpotPrices.put(spotPrice.getInstanceType(), new BigDecimal(spotPrice.getSpotPrice()));
    }

    // Ensure that all of the specified instance types were found.
    // If not found, it probably means user tried to lookup non-existent types.
    Set<String> difference = new HashSet<>(instanceTypes);
    difference.removeAll(instanceTypeSpotPrices.keySet());

    if (!difference.isEmpty()) {
        throw new ObjectNotFoundException(
                "Spot prices for instance types " + difference + " not found in AZ " + availabilityZone + ".");
    }

    return instanceTypeSpotPrices;
}

From source file:org.finra.dm.dao.impl.Ec2DaoImpl.java

License:Apache License

/**
 * This implementation uses DescribeSpotPriceHistory API which returns the latest spot price history for the specified AZ and instance types. This method
 * then filters the returned list to only contain the latest spot price for each instance type.
 *//*from w ww . java2s.  c o m*/
@Override
public List<SpotPrice> getLatestSpotPrices(String availabilityZone, Collection<String> instanceTypes,
        AwsParamsDto awsParamsDto) {
    AmazonEC2Client ec2Client = getEc2Client(awsParamsDto);
    DescribeSpotPriceHistoryRequest describeSpotPriceHistoryRequest = new DescribeSpotPriceHistoryRequest();
    describeSpotPriceHistoryRequest.setAvailabilityZone(availabilityZone);
    describeSpotPriceHistoryRequest.setInstanceTypes(instanceTypes);
    DescribeSpotPriceHistoryResult describeSpotPriceHistoryResult = ec2Operations
            .describeSpotPriceHistory(ec2Client, describeSpotPriceHistoryRequest);
    List<SpotPrice> spotPrices = new ArrayList<>();
    Set<String> instanceTypesFound = new HashSet<>();
    for (SpotPrice spotPriceHistoryEntry : describeSpotPriceHistoryResult.getSpotPriceHistory()) {
        if (instanceTypesFound.add(spotPriceHistoryEntry.getInstanceType())) {
            spotPrices.add(spotPriceHistoryEntry);
        }
    }
    return spotPrices;
}

From source file:org.finra.herd.dao.helper.EmrPricingHelper.java

License:Apache License

/**
 * Returns a mapping of instance types to spot prices for the given AZ and instance types. The spot prices are retrieved from EC2 API.
 * <p/>/*from  ww  w  .j  a v a 2  s  .c  o m*/
 * This method also validates that the given instance types are real instance types supported by AWS.
 *
 * @param availabilityZone the AZ of the spot instances
 * @param instanceTypes the size of the spot instances
 * @param awsParamsDto the AWS related parameters for access/secret keys and proxy details
 *
 * @return the mapping of instance type to spot prices
 * @throws ObjectNotFoundException when any of the instance type does not exist in AWS
 */
private Map<String, BigDecimal> getInstanceTypeSpotPrices(AvailabilityZone availabilityZone,
        Set<String> instanceTypes, AwsParamsDto awsParamsDto) {
    List<String> productDescriptions = herdStringHelper
            .getDelimitedConfigurationValue(ConfigurationValue.EMR_SPOT_PRICE_HISTORY_PRODUCT_DESCRIPTIONS);
    List<SpotPrice> spotPrices = ec2Dao.getLatestSpotPrices(availabilityZone.getZoneName(), instanceTypes,
            productDescriptions, awsParamsDto);

    Map<String, BigDecimal> instanceTypeSpotPrices = new HashMap<>();
    for (SpotPrice spotPrice : spotPrices) {
        instanceTypeSpotPrices.put(spotPrice.getInstanceType(), new BigDecimal(spotPrice.getSpotPrice()));
    }

    return instanceTypeSpotPrices;
}

From source file:org.finra.herd.dao.impl.Ec2DaoImpl.java

License:Apache License

/**
 * This implementation uses DescribeSpotPriceHistory API which returns the latest spot price history for the specified AZ and instance types. This method
 * then filters the returned list to only contain the latest spot price for each instance type.
 *//*from  w  w w . ja v a  2  s .  c  o m*/
@Override
public List<SpotPrice> getLatestSpotPrices(String availabilityZone, Collection<String> instanceTypes,
        Collection<String> productDescriptions, AwsParamsDto awsParamsDto) {
    AmazonEC2Client ec2Client = getEc2Client(awsParamsDto);
    DescribeSpotPriceHistoryRequest describeSpotPriceHistoryRequest = new DescribeSpotPriceHistoryRequest();
    describeSpotPriceHistoryRequest.setAvailabilityZone(availabilityZone);
    describeSpotPriceHistoryRequest.setInstanceTypes(instanceTypes);
    describeSpotPriceHistoryRequest.setProductDescriptions(productDescriptions);
    DescribeSpotPriceHistoryResult describeSpotPriceHistoryResult = ec2Operations
            .describeSpotPriceHistory(ec2Client, describeSpotPriceHistoryRequest);
    List<SpotPrice> spotPrices = new ArrayList<>();
    Set<String> instanceTypesFound = new HashSet<>();
    for (SpotPrice spotPriceHistoryEntry : describeSpotPriceHistoryResult.getSpotPriceHistory()) {
        if (instanceTypesFound.add(spotPriceHistoryEntry.getInstanceType())) {
            spotPrices.add(spotPriceHistoryEntry);
        }
    }
    return spotPrices;
}