Search in sources :

Example 1 with AmazonEC2Async

use of com.amazonaws.services.ec2.AmazonEC2Async in project rhizome by geekbeast.

the class AmazonConfiguration method getNodesWithTagKeyAndValueInRegion.

public static List<InetAddress> getNodesWithTagKeyAndValueInRegion(String region, Optional<String> nodeKey, Optional<String> nodeValue, Logger logger) {
    AmazonEC2Async ec2 = AmazonEC2AsyncClientBuilder.standard().withRegion(region).build();
    Filter tagKey = new Filter().withName("tag-key").withValues(nodeKey.orElse(null));
    Filter tagValue = new Filter().withName("tag-value").withValues(nodeValue.orElse(null));
    DescribeInstancesRequest req = new DescribeInstancesRequest().withFilters(tagKey, tagValue);
    DescribeInstancesResult describeInstances = ec2.describeInstances(req);
    List<Reservation> reservations = describeInstances.getReservations();
    ArrayList<InetAddress> addresses = new ArrayList<>();
    for (Reservation res : reservations) {
        for (Instance instance : res.getInstances()) {
            try {
                if (instance.getState().getCode() < 17) {
                    addresses.add(InetAddress.getByName(instance.getPrivateIpAddress()));
                }
            } catch (UnknownHostException e) {
                logger.error("Couldn't identify host {}", instance.getPrivateIpAddress(), e);
            }
        }
    }
    return addresses;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) UnknownHostException(java.net.UnknownHostException) Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) AmazonEC2Async(com.amazonaws.services.ec2.AmazonEC2Async) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) InetAddress(java.net.InetAddress)

Aggregations

AmazonEC2Async (com.amazonaws.services.ec2.AmazonEC2Async)1 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)1 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)1 Filter (com.amazonaws.services.ec2.model.Filter)1 Instance (com.amazonaws.services.ec2.model.Instance)1 Reservation (com.amazonaws.services.ec2.model.Reservation)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1