Example usage for com.amazonaws.services.ec2.model DescribeReservedInstancesOfferingsRequest setNextToken

List of usage examples for com.amazonaws.services.ec2.model DescribeReservedInstancesOfferingsRequest setNextToken

Introduction

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

Prototype


public void setNextToken(String nextToken) 

Source Link

Document

The token to retrieve the next page of results.

Usage

From source file:com.netflix.ice.basic.BasicReservationService.java

License:Apache License

private void pollAPI() throws Exception {
    long currentTime = new DateMidnight().getMillis();

    DescribeReservedInstancesOfferingsRequest req = new DescribeReservedInstancesOfferingsRequest().withFilters(
            new com.amazonaws.services.ec2.model.Filter().withName("marketplace").withValues("false"));
    String token = null;//from   w ww. j  ava2 s .c o  m
    boolean hasNewPrice = false;
    AmazonEC2Client ec2Client = new AmazonEC2Client(AwsUtils.awsCredentialsProvider, AwsUtils.clientConfig);

    for (Region region : Region.getAllRegions()) {
        ec2Client.setEndpoint("ec2." + region.name + ".amazonaws.com");
        do {
            if (!StringUtils.isEmpty(token))
                req.setNextToken(token);
            DescribeReservedInstancesOfferingsResult offers = ec2Client.describeReservedInstancesOfferings(req);
            token = offers.getNextToken();

            for (ReservedInstancesOffering offer : offers.getReservedInstancesOfferings()) {
                if (offer.getProductDescription().indexOf("Amazon VPC") >= 0)
                    continue;
                ReservationUtilization utilization = ReservationUtilization.get(offer.getOfferingType());
                Ec2InstanceReservationPrice.ReservationPeriod term = offer.getDuration() / 24 / 3600 > 366
                        ? Ec2InstanceReservationPrice.ReservationPeriod.threeyear
                        : Ec2InstanceReservationPrice.ReservationPeriod.oneyear;
                if (term != this.term)
                    continue;

                double hourly = offer.getUsagePrice();
                if (hourly <= 0) {
                    for (RecurringCharge recurringCharge : offer.getRecurringCharges()) {
                        if (recurringCharge.getFrequency().equals("Hourly")) {
                            hourly = recurringCharge.getAmount();
                            break;
                        }
                    }
                }
                UsageType usageType = getUsageType(offer.getInstanceType(), offer.getProductDescription());
                hasNewPrice = setPrice(utilization, currentTime,
                        Zone.getZone(offer.getAvailabilityZone()).region, usageType, offer.getFixedPrice(),
                        hourly) || hasNewPrice;

                logger.info("Setting RI price for " + Zone.getZone(offer.getAvailabilityZone()).region + " "
                        + utilization + " " + usageType + " " + offer.getFixedPrice() + " " + hourly);
            }
        } while (!StringUtils.isEmpty(token));
    }

    ec2Client.shutdown();
    if (hasNewPrice) {
        for (ReservationUtilization utilization : files.keySet()) {
            File file = files.get(utilization);
            DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
            try {
                Serializer.serialize(out, this.ec2InstanceReservationPrices.get(utilization));
                AwsUtils.upload(config.workS3BucketName, config.workS3BucketPrefix, file);
            } finally {
                out.close();
            }
        }
    }
}