Example usage for com.amazonaws.services.ec2 AmazonEC2 describeInstances

List of usage examples for com.amazonaws.services.ec2 AmazonEC2 describeInstances

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2 AmazonEC2 describeInstances.

Prototype

DescribeInstancesResult describeInstances();

Source Link

Document

Simplified method form for invoking the DescribeInstances operation.

Usage

From source file:com.oracle.coherence.cloud.amazon.EC2AddressProvider.java

License:Open Source License

/**
 * Generates the WKA list using an AmazonEC2 client.
 * //from   w ww  . j  a v a2s  .c o m
 * @param ec2 the {@link AmazonEC2} client to use.
 * 
 * @return the WKA list
 */
protected List<InetSocketAddress> generateWKAList(AmazonEC2 ec2) {
    String portString = System.getProperty("tangosol.coherence.ec2addressprovider.port", "8088");
    int wkaPort = Integer.parseInt(portString);

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

    DescribeInstancesResult describeInstancesResult = ec2.describeInstances();
    List<Reservation> reservations = describeInstancesResult.getReservations();
    Set<Instance> instances = new HashSet<Instance>();

    for (Reservation reservation : reservations) {
        instances.addAll(reservation.getInstances());
        if (logger.isLoggable(Level.CONFIG)) {
            logger.log(Level.CONFIG, "Examining EC2 reservation:" + reservation);
        }
    }
    logAllInstances(instances);
    DescribeAddressesResult elasticAddressesResult = ec2.describeAddresses();

    if (elasticAddressesResult != null) {
        for (Iterator<Address> elasticAddressIter = elasticAddressesResult.getAddresses()
                .iterator(); elasticAddressIter.hasNext();) {
            Address elasticAddress = elasticAddressIter.next();

            for (Iterator<Instance> instIter = instances.iterator(); instIter.hasNext();) {
                Instance instance = instIter.next();

                if (instance.getInstanceId().equals(elasticAddress.getInstanceId())) {
                    //Now we have a match - add with default port
                    if (logger.isLoggable(Level.CONFIG)) {
                        logger.log(Level.CONFIG,
                                "EC2AddressProvider - adding {0} from instance {1} to WKA list",
                                new Object[] { instance.getPrivateIpAddress(), instance });
                    }
                    resultList.add(new InetSocketAddress(instance.getPrivateIpAddress(), wkaPort));
                }
            }
        }
        if (resultList.size() == 0) {
            throw new RuntimeException(
                    "The EC2AddressProvider could not find any instance mapped to an Elastic IP");
        }
    } else {
        throw new RuntimeException("The EC2AddressProvider could not enumerate the Elastic IP Addresses");
    }
    return resultList;
}

From source file:edu.umass.cs.aws.support.AWSEC2.java

License:Apache License

/**
 * Describe Current Instances//from  ww  w . j  a  v  a  2  s  .  c  o  m
 *
 * @param ec2
 */
public static void describeInstances(AmazonEC2 ec2) {
    StringBuilder output = new StringBuilder();
    String prefix = currentTab + "Current Instances: ";
    DescribeInstancesResult describeInstancesResult = ec2.describeInstances();
    List<Reservation> reservations = describeInstancesResult.getReservations();
    Set<Instance> instances = new HashSet<Instance>();
    // add all instances to a Set.
    for (Reservation reservation : reservations) {
        instances.addAll(reservation.getInstances());
    }
    prefix = prefix.concat(" [" + instances.size() + " total] ");

    for (Instance ins : instances) {
        // instance state
        output.append(prefix);
        prefix = ", ";
        output.append(ins.getPublicDnsName());
        output.append(" (");
        output.append(ins.getInstanceId());
        output.append(") ");
        output.append(ins.getState().getName());
    }
    System.out.println(output);
}

From source file:edu.umass.cs.aws.support.AWSEC2.java

License:Apache License

/**
 * Find an instance//  ww  w.  j a va2s  .c  o  m
 *
 * @param ec2
 * @param createdInstanceId
 * @return the name of the instance or null
 */
public static Instance findInstance(AmazonEC2 ec2, String createdInstanceId) {
    DescribeInstancesResult describeInstancesResult = ec2.describeInstances();
    List<Reservation> reservations = describeInstancesResult.getReservations();
    Set<Instance> instances = new HashSet<>();
    // add all instances to a Set.
    for (Reservation reservation : reservations) {
        instances.addAll(reservation.getInstances());
    }
    for (Instance instance : instances) {
        if (createdInstanceId.equals(instance.getInstanceId())) {
            return instance;
        }
    }
    return null;
}

From source file:edu.umass.cs.aws.support.AWSEC2.java

License:Apache License

/**
 * Returns all the instances in this region.
 *
 * @param ec2/*w  ww  .  ja  v  a  2  s. c  o  m*/
 * @return a set of instance instances
 */
public static Set<Instance> getInstances(AmazonEC2 ec2) {
    Set<Instance> instances = new HashSet<>();
    DescribeInstancesResult describeInstancesResult = ec2.describeInstances();
    List<Reservation> reservations = describeInstancesResult.getReservations();

    // add all instances to a Set.
    for (Reservation reservation : reservations) {
        instances.addAll(reservation.getInstances());
    }
    return instances;
}

From source file:org.ow2.petals.cloud.manager.ec2.commands.ListServersCommand.java

License:Open Source License

@Override
protected Object doExecute() throws Exception {
    AmazonEC2 client = getClient();
    System.out.println("Servers :");

    DescribeInstancesResult result = client.describeInstances();
    for (Reservation reservation : result.getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            System.out.println(" - " + instance.toString());
        }//from  w w  w .ja  v a  2  s . c om
    }
    return null;
}