Example usage for com.amazonaws.services.ec2.model DescribeInstancesRequest getFilters

List of usage examples for com.amazonaws.services.ec2.model DescribeInstancesRequest getFilters

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model DescribeInstancesRequest getFilters.

Prototype


public java.util.List<Filter> getFilters() 

Source Link

Document

The filters.

Usage

From source file:com.netflix.edda.EddaEc2Client.java

License:Apache License

public DescribeInstancesResult describeInstances(DescribeInstancesRequest request) {
    validateEmpty("Filter", request.getFilters());

    TypeReference<List<Reservation>> ref = new TypeReference<List<Reservation>>() {
    };//ww  w. ja  va  2s  .  co m
    String url = config.url() + "/api/v2/aws/instances;_expand";
    try {
        List<Reservation> reservations = parse(ref, doGet(url));

        List<String> ids = request.getInstanceIds();
        if (shouldFilter(ids)) {
            List<Reservation> rs = new ArrayList<Reservation>();
            for (Reservation r : reservations) {
                List<Instance> is = new ArrayList<Instance>();
                for (Instance i : r.getInstances()) {
                    if (matches(ids, i.getInstanceId()))
                        is.add(i);
                }
                if (is.size() > 0)
                    rs.add(r.withInstances(is));
            }
            reservations = rs;
        }
        return new DescribeInstancesResult().withReservations(reservations);
    } catch (IOException e) {
        throw new AmazonClientException("Faled to parse " + url, e);
    }
}

From source file:com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSEnumerationAndCreationAdapterService.java

License:Open Source License

/**
 * Initializes and saves a reference to the request object that is sent to AWS to get a page of instances. Also saves an instance
 * to the async handler that will be used to handle the responses received from AWS. It sets the nextToken value in the request
 * object sent to AWS for getting the next page of results from AWS.
 * @param aws/*from  w w  w  .  j av a 2  s.  com*/
 */
private void creatAWSRequestAndAsyncHandler(EnumerationCreationContext aws) {
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    Filter runningInstanceFilter = getAWSNonTerminatedInstancesFilter();
    request.getFilters().add(runningInstanceFilter);
    request.setMaxResults(getQueryPageSize());
    request.setNextToken(aws.nextToken);
    aws.describeInstancesRequest = request;
    AsyncHandler<DescribeInstancesRequest, DescribeInstancesResult> resultHandler = new AWSEnumerationAsyncHandler(
            this, aws);
    aws.resultHandler = resultHandler;
}

From source file:com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSEnumerationAndDeletionAdapterService.java

License:Open Source License

/**
 * Get the instances from AWS filtered by the instances Ids known to the local system.
 *//*  w  w  w  . j ava 2 s.  com*/
public void getRemoteInstances(EnumerationDeletionContext aws, AWSEnumerationDeletionSubStage next) {
    if (aws.localInstanceIds == null || aws.localInstanceIds.size() == 0) {
        logInfo("There are no local records found for which state needs to be fetched from the remote system.");
        aws.subStage = next;
        deleteResourcesInLocalSystem(aws);
        return;
    }
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    Filter runningInstanceFilter = getAWSNonTerminatedInstancesFilter();
    request.getFilters().add(runningInstanceFilter);
    // Get only the instances from the remote system for which a compute state exists in the
    // local system.
    logInfo("Fetching instance details for %d instances on the AWS endpoint",
            aws.localInstanceIds.keySet().size());
    request.getInstanceIds().addAll(new ArrayList<String>(aws.localInstanceIds.keySet()));
    AsyncHandler<DescribeInstancesRequest, DescribeInstancesResult> resultHandler = new AWSEnumerationAsyncHandler(
            this, aws, next);
    aws.amazonEC2Client = this.clientManager.getOrCreateEC2Client(aws.parentAuth,
            aws.computeHostDescription.zoneId, this, aws.computeEnumerationRequest.taskReference, true);
    aws.amazonEC2Client.describeInstancesAsync(request, resultHandler);
}

From source file:datameer.awstasks.ant.ec2.Ec2ListRunningInstances.java

License:Apache License

@Override
protected void doExecute(AmazonEC2 ec2) {
    DescribeInstancesRequest describeRequest = createDescribeRequest();
    List<Filter> filters = describeRequest.getFilters();
    System.out.println("Setup following filters:");
    for (Filter filter : filters) {
        System.out.println("\t" + filter);
    }/*w  ww  .j  ava  2s .com*/
    List<Reservation> reservations = ec2.describeInstances(describeRequest).getReservations();
    System.out.println("Retrieved following reservations:");
    int runningInstanceGroups = 0;
    int runningInstances = 0;
    for (Reservation reservation : reservations) {
        List<Instance> instances = reservation.getInstances();
        runningInstanceGroups++;
        runningInstances += instances.size();
        System.out.println(reservation.getGroupNames() + ":");
        for (Instance instance : instances) {
            System.out.println("\t" + getInstanceName(instance) + " (" + instance.getInstanceId() + " / "
                    + instance.getState().getName() + "): " + instance.getPublicDnsName());
        }
    }
    System.out.println("Found " + runningInstances + " running instances in " + runningInstanceGroups
            + " instance groups");
}

From source file:org.elasticsearch.discovery.ec2.AmazonEC2Mock.java

License:Apache License

@Override
public DescribeInstancesResult describeInstances(DescribeInstancesRequest describeInstancesRequest)
        throws AmazonServiceException, AmazonClientException {
    Collection<Instance> filteredInstances = new ArrayList<>();

    logger.debug("--> mocking describeInstances");

    for (Instance instance : instances) {
        boolean tagFiltered = false;
        boolean instanceFound = false;

        Map<String, List<String>> expectedTags = new HashMap<>();
        Map<String, List<String>> instanceTags = new HashMap<>();

        for (Tag tag : instance.getTags()) {
            List<String> tags = instanceTags.get(tag.getKey());
            if (tags == null) {
                tags = new ArrayList<>();
                instanceTags.put(tag.getKey(), tags);
            }//from  ww  w.  j av  a 2 s  . c  om
            tags.add(tag.getValue());
        }

        for (Filter filter : describeInstancesRequest.getFilters()) {
            // If we have the same tag name and one of the values, we add the instance
            if (filter.getName().startsWith("tag:")) {
                tagFiltered = true;
                String tagName = filter.getName().substring(4);
                // if we have more than one value for the same key, then the key is appended with .x
                Pattern p = Pattern.compile("\\.\\d+", Pattern.DOTALL);
                Matcher m = p.matcher(tagName);
                if (m.find()) {
                    int i = tagName.lastIndexOf(".");
                    tagName = tagName.substring(0, i);
                }

                List<String> tags = expectedTags.get(tagName);
                if (tags == null) {
                    tags = new ArrayList<>();
                    expectedTags.put(tagName, tags);
                }
                tags.addAll(filter.getValues());
            }
        }

        if (tagFiltered) {
            logger.debug("--> expected tags: [{}]", expectedTags);
            logger.debug("--> instance tags: [{}]", instanceTags);

            instanceFound = true;
            for (Map.Entry<String, List<String>> expectedTagsEntry : expectedTags.entrySet()) {
                List<String> instanceTagValues = instanceTags.get(expectedTagsEntry.getKey());
                if (instanceTagValues == null) {
                    instanceFound = false;
                    break;
                }

                for (String expectedValue : expectedTagsEntry.getValue()) {
                    boolean valueFound = false;
                    for (String instanceTagValue : instanceTagValues) {
                        if (instanceTagValue.equals(expectedValue)) {
                            valueFound = true;
                        }
                    }
                    if (valueFound == false) {
                        instanceFound = false;
                    }
                }
            }
        }

        if (tagFiltered == false || instanceFound) {
            logger.debug("--> instance added");
            filteredInstances.add(instance);
        } else {
            logger.debug("--> instance filtered");
        }
    }

    return new DescribeInstancesResult().withReservations(new Reservation().withInstances(filteredInstances));
}