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;
}
Aggregations