/** * 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. */ @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; }
/** * Returns a matcher proxy which matches a DescribeSpotPriceHistoryRequest with the given parameters. * * @param availabilityZone Availability zone * @param instanceTypes Instance types * @param productDescriptions Product descriptions * * @return DescribeSpotPriceHistoryRequest matcher proxy */ private DescribeSpotPriceHistoryRequest equalsDescribeSpotPriceHistoryRequest(String availabilityZone, Collection<String> instanceTypes, Collection<String> productDescriptions) { return argThat(describeSpotPriceHistoryRequest -> Objects.equal(availabilityZone, describeSpotPriceHistoryRequest.getAvailabilityZone()) && Objects.equal(instanceTypes, describeSpotPriceHistoryRequest.getInstanceTypes()) && Objects.equal(productDescriptions, describeSpotPriceHistoryRequest.getProductDescriptions()) ); }
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(); } 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; }
@Override public DescribeSpotPriceHistoryResult describeSpotPriceHistory(DescribeSpotPriceHistoryRequest describeSpotPriceHistoryRequest) throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException("Not supported in mock"); }
@Override public DescribeSpotPriceHistoryResult describeSpotPriceHistory(AmazonEC2Client ec2Client, DescribeSpotPriceHistoryRequest describeSpotPriceHistoryRequest) { return ec2Client.describeSpotPriceHistory(describeSpotPriceHistoryRequest); }
@Override public DescribeSpotPriceHistoryResult describeSpotPriceHistory(AmazonEC2Client ec2Client, DescribeSpotPriceHistoryRequest describeSpotPriceHistoryRequest) { List<SpotPrice> spotPriceHistories = new ArrayList<>(); String requestedAvailabilityZone = describeSpotPriceHistoryRequest.getAvailabilityZone(); // Get availability zones to search for Set<MockAvailabilityZone> requestedAvailabilityZones = new HashSet<>(); // If requested zone is specified, get and add if (requestedAvailabilityZone != null) { requestedAvailabilityZones.add(mockAvailabilityZones.get(requestedAvailabilityZone)); } // If requested zone is not specified, add all else { requestedAvailabilityZones.addAll(mockAvailabilityZones.values()); } // Get instance types to search for List<String> requestedInstanceTypes = describeSpotPriceHistoryRequest.getInstanceTypes(); // If not specified, add all instance types if (requestedInstanceTypes.isEmpty()) { requestedInstanceTypes.addAll(mockInstanceTypes); } // search for price for all AZ and instance types requested for (MockAvailabilityZone mockAvailabilityZone : requestedAvailabilityZones) { for (String requestedInstanceType : requestedInstanceTypes) { MockSpotPrice mockSpotPrice = mockAvailabilityZone.getSpotPrices().get(requestedInstanceType); if (mockSpotPrice != null) { spotPriceHistories.add(mockSpotPrice.toAwsObject()); } } } DescribeSpotPriceHistoryResult describeSpotPriceHistoryResult = new DescribeSpotPriceHistoryResult(); describeSpotPriceHistoryResult.setSpotPriceHistory(spotPriceHistories); return describeSpotPriceHistoryResult; }
public static double[] getPricesInRegion(String size, String os) { double[] resultInMap = pricesInRegion.get(size + "@" + os); if (resultInMap != null && resultInMap.length > 0) return resultInMap; connect(); List<String> availabilityZones = getAllAvailabilityZones(); double res[] = new double[availabilityZones.size()]; int i = 0; for (String zone : availabilityZones) { DescribeSpotPriceHistoryRequest req = new DescribeSpotPriceHistoryRequest(); List<String> instanceTypes = new ArrayList<String>(); instanceTypes.add(size); req.setInstanceTypes(instanceTypes); List<String> productDescriptions = new ArrayList<String>(); productDescriptions.add(os); req.setProductDescriptions(productDescriptions); req.setAvailabilityZone(zone); req.setMaxResults(1); DescribeSpotPriceHistoryResult priceResult = client .describeSpotPriceHistory(req); res[i] = Double.parseDouble(priceResult.getSpotPriceHistory() .get(0).getSpotPrice()); logger.debug("Zone: {}, Price: {}", zone, (float) res[i]); i++; } pricesInRegion.put(size + "@" + os, res); return res; }
/** * {@link AmazonEC2#describeSpotPriceHistory()} * * @param ec2Client {@link AmazonEC2} to use. * @param describeSpotPriceHistoryRequest The request object. * @return {@link DescribeSpotPriceHistoryResult} */ public DescribeSpotPriceHistoryResult describeSpotPriceHistory(AmazonEC2Client ec2Client, DescribeSpotPriceHistoryRequest describeSpotPriceHistoryRequest);