Search in sources :

Example 1 with ReservedInstances

use of software.amazon.awssdk.services.ec2.model.ReservedInstances in project aws-doc-sdk-examples by awsdocs.

the class DescribeReservedInstances method describeReservedEC2Instances.

// snippet-start:[ec2.java2.describe_reserved_instances.main]
public static void describeReservedEC2Instances(Ec2Client ec2, String instanceID) {
    try {
        DescribeReservedInstancesRequest request = DescribeReservedInstancesRequest.builder().reservedInstancesIds(instanceID).build();
        DescribeReservedInstancesResponse response = ec2.describeReservedInstances(request);
        for (ReservedInstances instance : response.reservedInstances()) {
            System.out.printf("Found a Reserved Instance with id %s, " + "in AZ %s, " + "type %s, " + "state %s " + "and monitoring state %s", instance.reservedInstancesId(), instance.availabilityZone(), instance.instanceType(), instance.state().name());
        }
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) DescribeReservedInstancesRequest(software.amazon.awssdk.services.ec2.model.DescribeReservedInstancesRequest) ReservedInstances(software.amazon.awssdk.services.ec2.model.ReservedInstances) DescribeReservedInstancesResponse(software.amazon.awssdk.services.ec2.model.DescribeReservedInstancesResponse)

Example 2 with ReservedInstances

use of software.amazon.awssdk.services.ec2.model.ReservedInstances in project photon-model by vmware.

the class MockAWSReservedInstancePlansService method createReservedInstance.

private ReservedInstances createReservedInstance(String id) {
    ReservedInstances r = new ReservedInstances();
    r.setReservedInstancesId(id);
    r.setFixedPrice(0.003f);
    r.setStart(new Date());
    r.setDuration(10000000L);
    return r;
}
Also used : ReservedInstances(com.amazonaws.services.ec2.model.ReservedInstances) Date(java.util.Date)

Example 3 with ReservedInstances

use of software.amazon.awssdk.services.ec2.model.ReservedInstances in project ice by Netflix.

the class ReservationCapacityPoller method poll.

@Override
protected void poll() throws Exception {
    ProcessorConfig config = ProcessorConfig.getInstance();
    // read from s3 if not exists
    File file = new File(config.localDir, "reservation_capacity.txt");
    if (!file.exists()) {
        logger.info("downloading " + file + "...");
        AwsUtils.downloadFileIfNotExist(config.workS3BucketName, config.workS3BucketPrefix, file);
        logger.info("downloaded " + file);
    }
    // read from file
    Map<String, ReservedInstances> reservations = Maps.newTreeMap();
    if (file.exists()) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String line;
            while ((line = reader.readLine()) != null) {
                String[] tokens = line.split(",");
                String accountId = tokens[0];
                String region = tokens[1];
                String reservationId = tokens[2];
                String zone = tokens[3];
                Long start = Long.parseLong(tokens[4]);
                long duration = Long.parseLong(tokens[5]);
                String instanceType = tokens[6];
                String productDescription = tokens[7];
                int instanceCount = Integer.parseInt(tokens[8]);
                String offeringType = tokens[9];
                String state = tokens[10];
                Long end = tokens.length > 11 ? Long.parseLong(tokens[11]) : null;
                float fixedPrice = tokens.length > 12 ? Float.parseFloat(tokens[12]) : 0;
                float usagePrice = tokens.length > 13 ? Float.parseFloat(tokens[13]) : 0;
                ReservedInstances reservation = new ReservedInstances().withAvailabilityZone(zone).withStart(new Date(start)).withDuration(duration).withInstanceType(instanceType).withProductDescription(productDescription).withInstanceCount(instanceCount).withOfferingType(offeringType).withState(state).withFixedPrice(fixedPrice).withUsagePrice(usagePrice);
                if (end != null)
                    reservation.setEnd(new Date(end));
                else
                    reservation.setEnd(new Date(start + duration * 1000));
                reservations.put(accountId + "," + region + "," + reservationId, reservation);
            }
        } catch (Exception e) {
            logger.error("error in reading " + file, e);
        } finally {
            if (reader != null)
                try {
                    reader.close();
                } catch (Exception e) {
                }
        }
    }
    logger.info("read " + reservations.size() + " reservations.");
    for (Account account : config.accountService.getReservationAccounts().keySet()) {
        try {
            AmazonEC2Client ec2Client;
            String assumeRole = config.accountService.getReservationAccessRoles().get(account);
            if (assumeRole != null) {
                String externalId = config.accountService.getReservationAccessExternalIds().get(account);
                final Credentials credentials = AwsUtils.getAssumedCredentials(account.id, assumeRole, externalId);
                ec2Client = new AmazonEC2Client(new AWSSessionCredentials() {

                    public String getAWSAccessKeyId() {
                        return credentials.getAccessKeyId();
                    }

                    public String getAWSSecretKey() {
                        return credentials.getSecretAccessKey();
                    }

                    public String getSessionToken() {
                        return credentials.getSessionToken();
                    }
                });
            } else
                ec2Client = new AmazonEC2Client(AwsUtils.awsCredentialsProvider.getCredentials(), AwsUtils.clientConfig);
            for (Region region : Region.getAllRegions()) {
                // just ignore GovCloud when polling for RIs in order to prevent AuthFailure errors.
                if (region == Region.US_GOV_WEST_1) {
                    continue;
                }
                ec2Client.setEndpoint("ec2." + region.name + ".amazonaws.com");
                try {
                    DescribeReservedInstancesResult result = ec2Client.describeReservedInstances();
                    for (ReservedInstances reservation : result.getReservedInstances()) {
                        String key = account.id + "," + region.name + "," + reservation.getReservedInstancesId();
                        reservations.put(key, reservation);
                        if (reservation.getEnd() == null)
                            reservation.setEnd(new Date(reservation.getStart().getTime() + reservation.getDuration() * 1000L));
                        if (reservation.getFixedPrice() == null)
                            reservation.setFixedPrice(0f);
                        if (reservation.getUsagePrice() == null)
                            reservation.setUsagePrice(0f);
                    }
                } catch (Exception e) {
                    logger.error("error in describeReservedInstances for " + region.name + " " + account.name, e);
                }
            }
            ec2Client.shutdown();
        } catch (Exception e) {
            logger.error("Error in describeReservedInstances for " + account.name, e);
        }
    }
    config.reservationService.updateEc2Reservations(reservations);
    updatedConfig = true;
    // archive to disk
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(file));
        for (String key : reservations.keySet()) {
            ReservedInstances reservation = reservations.get(key);
            String[] line = new String[] { key, reservation.getAvailabilityZone(), reservation.getStart().getTime() + "", reservation.getDuration().toString(), reservation.getInstanceType(), reservation.getProductDescription(), reservation.getInstanceCount().toString(), reservation.getOfferingType(), reservation.getState(), reservation.getEnd().getTime() + "", reservation.getFixedPrice() + "", reservation.getUsagePrice() + "" };
            writer.write(StringUtils.join(line, ","));
            writer.newLine();
        }
    } catch (Exception e) {
        logger.error("", e);
    } finally {
        if (writer != null)
            try {
                writer.close();
            } catch (Exception e) {
            }
    }
    logger.info("archived " + reservations.size() + " reservations.");
    // archive to s3
    logger.info("uploading " + file + "...");
    AwsUtils.upload(config.workS3BucketName, config.workS3BucketPrefix, config.localDir, file.getName());
    logger.info("uploaded " + file);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) Account(com.netflix.ice.tag.Account) ReservedInstances(com.amazonaws.services.ec2.model.ReservedInstances) Date(java.util.Date) AWSSessionCredentials(com.amazonaws.auth.AWSSessionCredentials) Region(com.netflix.ice.tag.Region) DescribeReservedInstancesResult(com.amazonaws.services.ec2.model.DescribeReservedInstancesResult) AWSSessionCredentials(com.amazonaws.auth.AWSSessionCredentials) Credentials(com.amazonaws.services.securitytoken.model.Credentials)

Aggregations

ReservedInstances (com.amazonaws.services.ec2.model.ReservedInstances)2 Date (java.util.Date)2 AWSSessionCredentials (com.amazonaws.auth.AWSSessionCredentials)1 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)1 DescribeReservedInstancesResult (com.amazonaws.services.ec2.model.DescribeReservedInstancesResult)1 Credentials (com.amazonaws.services.securitytoken.model.Credentials)1 Account (com.netflix.ice.tag.Account)1 Region (com.netflix.ice.tag.Region)1 DescribeReservedInstancesRequest (software.amazon.awssdk.services.ec2.model.DescribeReservedInstancesRequest)1 DescribeReservedInstancesResponse (software.amazon.awssdk.services.ec2.model.DescribeReservedInstancesResponse)1 Ec2Exception (software.amazon.awssdk.services.ec2.model.Ec2Exception)1 ReservedInstances (software.amazon.awssdk.services.ec2.model.ReservedInstances)1