Package com.amazonaws.services.ec2.model

Examples of com.amazonaws.services.ec2.model.ReservedInstances


        for (SpotInstanceRequest spotInstanceRequest : result.getSpotInstanceRequests()) {
          LaunchSpecification launchSpecification = spotInstanceRequest.getLaunchSpecification();
          populateLaunchSpecificationSecurityGroupNames(launchSpecification);
        }
      } else if (response instanceof RequestSpotInstancesResult) {
        RequestSpotInstancesResult result = (RequestSpotInstancesResult)response;
        for (SpotInstanceRequest spotInstanceRequest : result.getSpotInstanceRequests()) {
          LaunchSpecification launchSpecification = spotInstanceRequest.getLaunchSpecification();
          populateLaunchSpecificationSecurityGroupNames(launchSpecification);
        }
      } else if (response instanceof DescribeInstancesResult) {
        DescribeInstancesResult result = (DescribeInstancesResult)response;
        for (Reservation reservation : result.getReservations()) {
          populateReservationSecurityGroupNames(reservation);
        }
      } else if (response instanceof RunInstancesResult) {
        RunInstancesResult result = (RunInstancesResult)response;
        populateReservationSecurityGroupNames(result.getReservation());
      }
  }
View Full Code Here


  public static List<Instance> loadInstances() {
    List<Instance> resultList = new ArrayList<Instance>();
    DescribeInstancesResult describeInstancesResult = getEC2Client().describeInstances();
    List<Reservation> reservations = describeInstancesResult.getReservations();
    for (Iterator<Reservation> iterator = reservations.iterator(); iterator.hasNext();) {
      Reservation reservation = iterator.next();
      for (Instance instance : reservation.getInstances()) {
        resultList.add(instance);
      }
    }
    return resultList;
  }
View Full Code Here

                    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()) {

                    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();
            }
        }
View Full Code Here

   * @return list of newly launched instances
   */
  public static List<Instance> launchInstance(String AMI_ID, String type, Integer number) {
    List<Instance> resultList = new ArrayList<Instance>();

    RunInstancesRequest request = new RunInstancesRequest(AMI_ID, number, number);
    request.setInstanceType(type);

    RunInstancesResult result = getEC2Client().runInstances(request);
    resultList.addAll(result.getReservation().getInstances());
    return resultList;
  }
View Full Code Here

   * @return list of newly launched instances
   */
  public static List<Instance> launchInstance(String AMI_ID, String type, Integer number, String keyname) {
    List<Instance> resultList = new ArrayList<Instance>();

    RunInstancesRequest request = new RunInstancesRequest(AMI_ID, number, number);
    request.setInstanceType(type);
    request.setKeyName(keyname);

    RunInstancesResult result = getEC2Client().runInstances(request);
    resultList.addAll(result.getReservation().getInstances());
    return resultList;
  }
View Full Code Here

  }
 

  private List<Instance> startInstances(AmazonEC2Client ec2, String AMI_ID, String INS_TYPE, NotificationService notifier) throws AmazonServiceException {
    // start only 1 instance
    RunInstancesRequest request = new RunInstancesRequest(AMI_ID, 1, 1);
    request.setKeyName(KEY_PAIR_NAME);
    request.setInstanceType(INS_TYPE);

    RunInstancesResult result = ec2.runInstances(request);

    List<Instance> instances = result.getReservation().getInstances();
View Full Code Here

    List<Instance> resultList = new ArrayList<Instance>();

    RunInstancesRequest request = new RunInstancesRequest(AMI_ID, number, number);
    request.setInstanceType(type);

    RunInstancesResult result = getEC2Client().runInstances(request);
    resultList.addAll(result.getReservation().getInstances());
    return resultList;
  }
View Full Code Here

    RunInstancesRequest request = new RunInstancesRequest(AMI_ID, number, number);
    request.setInstanceType(type);
    request.setKeyName(keyname);

    RunInstancesResult result = getEC2Client().runInstances(request);
    resultList.addAll(result.getReservation().getInstances());
    return resultList;
  }
View Full Code Here

    // start only 1 instance
    RunInstancesRequest request = new RunInstancesRequest(AMI_ID, 1, 1);
    request.setKeyName(KEY_PAIR_NAME);
    request.setInstanceType(INS_TYPE);

    RunInstancesResult result = ec2.runInstances(request);

    List<Instance> instances = result.getReservation().getInstances();

    while (!allInstancesStateEqual(instances, InstanceStateName.Running)) {

      // instance status should not be Terminated
      if (anyInstancesStateEqual(instances, InstanceStateName.Terminated)) {
View Full Code Here

        DescribeInstancesResult result = (DescribeInstancesResult)response;
        for (Reservation reservation : result.getReservations()) {
          populateReservationSecurityGroupNames(reservation);
        }
      } else if (response instanceof RunInstancesResult) {
        RunInstancesResult result = (RunInstancesResult)response;
        populateReservationSecurityGroupNames(result.getReservation());
      }
  }
View Full Code Here

TOP

Related Classes of com.amazonaws.services.ec2.model.ReservedInstances

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.