Package com.amazonaws.services.ec2

Examples of com.amazonaws.services.ec2.AmazonEC2Client


        DescribeReservedInstancesOfferingsRequest req =  new DescribeReservedInstancesOfferingsRequest()
                .withFilters(new com.amazonaws.services.ec2.model.Filter().withName("marketplace").withValues("false"));
        String token = null;
        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 {
View Full Code Here


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

  }

  private AmazonEC2 newClient() {

    final AmazonEC2 amazonClient = new AmazonEC2Client(credentials);

    amazonClient.setEndpoint(endpoint);

    return amazonClient;

  }
View Full Code Here

     */
    private static void init() throws Exception {
        AWSCredentials credentials = new PropertiesCredentials(
                AwsConsoleApp.class.getResourceAsStream("AwsCredentials.properties"));

        ec2 = new AmazonEC2Client(credentials);
        s3  = new AmazonS3Client(credentials);
        sdb = new AmazonSimpleDBClient(credentials);
    }
View Full Code Here

        public AmazonEC2 load(Provider provider) throws Exception {
            String region = Optional.fromNullable(provider.getOptions().get(ProviderOptions.REGION))
                .or(ProviderOptions.DEFAULT_REGION);

            AWSCredentials credentials = new BasicAWSCredentials(provider.getAccessKey(), provider.getSecretKey());
            AmazonEC2 client = new AmazonEC2Client(credentials, new ClientConfiguration()
                .withUserAgent(AXEMBLR_USER_AGENT));

            if (provider.getEndpoint().isPresent()) {
                LOG.info("Using endpoint {} as configured", provider.getEndpoint().get());
                client.setEndpoint(provider.getEndpoint().get());

            } else {
                LOG.info(">> Searching endpoint for region {}", region);
                DescribeRegionsRequest request = new DescribeRegionsRequest().withRegionNames(region);

                DescribeRegionsResult result = client.describeRegions(request);
                checkArgument(result.getRegions().size() == 1, "Invalid region name %s. Expected one result found %s",
                    region, result.getRegions());

                LOG.info("<< Using endpoint {} for region {}", result.getRegions().get(0).getEndpoint(), region);
                client.setEndpoint(result.getRegions().get(0).getEndpoint());
            }

            return client;

        }
View Full Code Here

      for (GroupIdentifier g : this.instance.getSecurityGroups()) {
        IpPermission ip = new IpPermission();
        ip.setIpProtocol("tcp");
        ip.setFromPort(22);
        ip.setToPort(22);
        AuthorizeSecurityGroupIngressRequest r = new AuthorizeSecurityGroupIngressRequest();
        r = r.withIpPermissions(ip.withIpRanges("0.0.0.0/0"));
        r.setGroupId(g.getGroupId());
        try {
          ec2client.authorizeSecurityGroupIngress(r);
        } catch (AmazonServiceException as) {
          /*
           * If exception is from duplicate room, ignore it.
View Full Code Here

       * If key exists and new key is created, delete old key and replace
       * with new one. Else, do nothing
       */

      if (newKey) {
        DeleteKeyPairRequest deleteKeyPairRequest = new DeleteKeyPairRequest(KEY_PAIR_NAME);
        ec2.deleteKeyPair(deleteKeyPairRequest);
        ImportKeyPairRequest importKeyPairRequest = new ImportKeyPairRequest(KEY_PAIR_NAME, encodedPublicKey);
        ec2.importKeyPair(importKeyPairRequest);
      }

View Full Code Here

            ec2.describeKeyPairs(describeKeyPairsRequest.withKeyNames(keyPairName));

            /* If key exists and new key is created, delete old key and replace
             * with new one. Else, do nothing */
            if (newKey) {
                DeleteKeyPairRequest deleteKeyPairRequest = new DeleteKeyPairRequest(keyPairName);
                ec2.deleteKeyPair(deleteKeyPairRequest);
                ImportKeyPairRequest importKeyPairRequest = new ImportKeyPairRequest(keyPairName, encodedPublicKey);
                ec2.importKeyPair(importKeyPairRequest);
            }

View Full Code Here

      if (ami_id != null)
        this.instance = startInstances(ec2client, ami_id, ins_type, invocationContext.getExecutionContext().getNotificationService()).get(0);
      else {

        // already running instance
        DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
        DescribeInstancesResult describeInstancesResult = ec2client.describeInstances(describeInstancesRequest.withInstanceIds(ins_id));

        if (describeInstancesResult.getReservations().size() == 0 || describeInstancesResult.getReservations().get(0).getInstances().size() == 0) {
          throw new GfacException("Instance not found:" + ins_id, FaultCode.InvalidRequest);
        }
View Full Code Here

        Thread.sleep(SLEEP_TIME_SECOND * 1000l);
      } catch (Exception ex) {
        // no op
      }

      DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
      describeInstancesRequest.setInstanceIds(getInstanceIDs(instances));

      DescribeInstancesResult describeInstancesResult = ec2.describeInstances(describeInstancesRequest);
      instances = describeInstancesResult.getReservations().get(0).getInstances();
    }

View Full Code Here

TOP

Related Classes of com.amazonaws.services.ec2.AmazonEC2Client

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.