Search in sources :

Example 36 with DescribeInstancesRequest

use of com.amazonaws.services.ec2.model.DescribeInstancesRequest in project cloudbreak by hortonworks.

the class TagsUtil method checkTagsAws.

protected static void checkTagsAws(Regions region, Collection<String> instanceIdList, Map<String, String> tagsToCheckMap) {
    Map<String, String> extractedTagsToCheck = new HashMap<>();
    List<Tag> extractedTags;
    AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withRegion(region).build();
    DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
    describeInstancesRequest.withInstanceIds(instanceIdList);
    DescribeInstancesResult describeInstancesResultAll = ec2.describeInstances(describeInstancesRequest);
    List<Reservation> reservationsAll = describeInstancesResultAll.getReservations();
    for (Reservation reservation : reservationsAll) {
        for (Instance instance : reservation.getInstances()) {
            extractedTags = instance.getTags();
            Assert.assertNotNull(extractedTags);
            for (Tag tag : extractedTags) {
                extractedTagsToCheck.put(tag.getKey(), tag.getValue());
            }
            checkTags(tagsToCheckMap, extractedTagsToCheck);
            extractedTags.clear();
        }
    }
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) HashMap(java.util.HashMap) Instance(com.amazonaws.services.ec2.model.Instance) Tag(com.amazonaws.services.ec2.model.Tag) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 37 with DescribeInstancesRequest

use of com.amazonaws.services.ec2.model.DescribeInstancesRequest in project onebusaway-application-modules by camsys.

the class BundleServerServiceImpl method getInstance.

private Instance getInstance(String instanceId) {
    try {
        DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
        List<String> list = new ArrayList<String>();
        list.add(instanceId);
        describeInstancesRequest.setInstanceIds(list);
        // this call can timeout and throw AmazonClientException if AWS is having
        // a bad day
        DescribeInstancesResult result = _ec2.describeInstances(describeInstancesRequest);
        if (!result.getReservations().isEmpty()) {
            if (!result.getReservations().get(0).getInstances().isEmpty()) {
                Instance i = result.getReservations().get(0).getInstances().get(0);
                return i;
            }
        }
    } catch (Exception e) {
        _log.error("Call to AWS threw exception", e);
    }
    return null;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException)

Example 38 with DescribeInstancesRequest

use of com.amazonaws.services.ec2.model.DescribeInstancesRequest in project druid by druid-io.

the class EC2AutoScaler method ipToIdLookup.

@Override
public List<String> ipToIdLookup(List<String> ips) {
    final List<String> retVal = FluentIterable.from(Lists.partition(ips, MAX_AWS_FILTER_VALUES)).transformAndConcat(new Function<List<String>, Iterable<Reservation>>() {

        @Override
        public Iterable<Reservation> apply(List<String> input) {
            return amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("private-ip-address", input))).getReservations();
        }
    }).transformAndConcat(new Function<Reservation, Iterable<Instance>>() {

        @Override
        public Iterable<Instance> apply(Reservation reservation) {
            return reservation.getInstances();
        }
    }).transform(new Function<Instance, String>() {

        @Override
        public String apply(Instance instance) {
            return instance.getInstanceId();
        }
    }).toList();
    log.debug("Performing lookup: %s --> %s", ips, retVal);
    return retVal;
}
Also used : Function(com.google.common.base.Function) Reservation(com.amazonaws.services.ec2.model.Reservation) FluentIterable(com.google.common.collect.FluentIterable) Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) List(java.util.List) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 39 with DescribeInstancesRequest

use of com.amazonaws.services.ec2.model.DescribeInstancesRequest in project druid by druid-io.

the class EC2AutoScaler method terminate.

@Override
public AutoScalingData terminate(List<String> ips) {
    if (ips.isEmpty()) {
        return new AutoScalingData(new ArrayList<>());
    }
    DescribeInstancesResult result = amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("private-ip-address", ips)));
    List<Instance> instances = new ArrayList<>();
    for (Reservation reservation : result.getReservations()) {
        instances.addAll(reservation.getInstances());
    }
    try {
        return terminateWithIds(Lists.transform(instances, new Function<Instance, String>() {

            @Override
            public String apply(Instance input) {
                return input.getInstanceId();
            }
        }));
    } catch (Exception e) {
        log.error(e, "Unable to terminate any instances.");
    }
    return null;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Function(com.google.common.base.Function) Reservation(com.amazonaws.services.ec2.model.Reservation) AutoScalingData(org.apache.druid.indexing.overlord.autoscaling.AutoScalingData) Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Aggregations

DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)32 Instance (com.amazonaws.services.ec2.model.Instance)24 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)22 Reservation (com.amazonaws.services.ec2.model.Reservation)21 Filter (com.amazonaws.services.ec2.model.Filter)19 ArrayList (java.util.ArrayList)14 List (java.util.List)9 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)7 Function (com.google.common.base.Function)6 AWSUtils.getAWSNonTerminatedInstancesFilter (com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getAWSNonTerminatedInstancesFilter)6 HashMap (java.util.HashMap)6 Tag (com.amazonaws.services.ec2.model.Tag)5 Map (java.util.Map)5 Volume (com.amazonaws.services.ec2.model.Volume)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3 FluentIterable (com.google.common.collect.FluentIterable)3 AmazonClientException (com.amazonaws.AmazonClientException)2 AmazonWebServiceRequest (com.amazonaws.AmazonWebServiceRequest)2 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)2 AsyncHandler (com.amazonaws.handlers.AsyncHandler)2