Java Code Examples for com.amazonaws.services.ec2.model.DescribeInstancesRequest#setNextToken()

The following examples show how to use com.amazonaws.services.ec2.model.DescribeInstancesRequest#setNextToken() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Ec2TableProvider.java    From aws-athena-query-federation with Apache License 2.0 5 votes vote down vote up
/**
 * Calls DescribeInstances on the AWS EC2 Client returning all instances that match the supplied predicate and attempting
 * to push down certain predicates (namely queries for specific ec2 instance) to EC2.
 *
 * @See TableProvider
 */
@Override
public void readWithConstraint(BlockSpiller spiller, ReadRecordsRequest recordsRequest, QueryStatusChecker queryStatusChecker)
{
    boolean done = false;
    DescribeInstancesRequest request = new DescribeInstancesRequest();

    ValueSet idConstraint = recordsRequest.getConstraints().getSummary().get("instance_id");
    if (idConstraint != null && idConstraint.isSingleValue()) {
        request.setInstanceIds(Collections.singletonList(idConstraint.getSingleValue().toString()));
    }

    while (!done) {
        DescribeInstancesResult response = ec2.describeInstances(request);

        for (Reservation reservation : response.getReservations()) {
            for (Instance instance : reservation.getInstances()) {
                instanceToRow(instance, spiller);
            }
        }

        request.setNextToken(response.getNextToken());

        if (response.getNextToken() == null || !queryStatusChecker.isQueryRunning()) {
            done = true;
        }
    }
}
 
Example 2
Source File: Ec2InstanceStore.java    From soundwave with Apache License 2.0 5 votes vote down vote up
@Override
public List<Instance> getInstancesForZone(AvailabilityZone zone, AmazonEC2Client client)
    throws Exception {
  OperationStats op = new OperationStats("ec2InstanceStore", "getInstancesForZone");
  try {
    List<Instance> ret = new ArrayList<>();
    DescribeInstancesRequest request = new DescribeInstancesRequest()
        .withMaxResults(1000)
        .withFilters(new Filter("availability-zone", Arrays.asList(zone.getZoneName())))
        .withSdkClientExecutionTimeout(
            600 * 1000) //10 minutes time out for total execution including retries
        .withSdkRequestTimeout(300 * 1000); //5 minutes time out for a single request

    List<Reservation> reservations = new ArrayList<>();
    DescribeInstancesResult result = client.describeInstances(request);
    while (result != null) {
      reservations.addAll(result.getReservations());
      if (result.getNextToken() != null) {
        request.setNextToken(result.getNextToken());
        result = client.describeInstances(request);
      } else {
        result = null;
      }
    }

    for (Reservation reservation : reservations) {
      //Reservation refers to one launch command in EC2. Most time it should
      //only contains one instance
      for (Instance inst : reservation.getInstances()) {
        ret.add(inst);
      }
    }
    op.succeed();
    return ret;
  } catch (Exception ex) {
    op.failed();
    throw ex;
  }
}
 
Example 3
Source File: DescribeInstances.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)
{
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    boolean done = false;

    DescribeInstancesRequest request = new DescribeInstancesRequest();
    while(!done) {
        DescribeInstancesResult response = ec2.describeInstances(request);

        for(Reservation reservation : response.getReservations()) {
            for(Instance instance : reservation.getInstances()) {
                System.out.printf(
                    "Found instance with id %s, " +
                    "AMI %s, " +
                    "type %s, " +
                    "state %s " +
                    "and monitoring state %s",
                    instance.getInstanceId(),
                    instance.getImageId(),
                    instance.getInstanceType(),
                    instance.getState().getName(),
                    instance.getMonitoring().getState());
            }
        }

        request.setNextToken(response.getNextToken());

        if(response.getNextToken() == null) {
            done = true;
        }
    }
}
 
Example 4
Source File: EC2Api.java    From ec2-spot-jenkins-plugin with Apache License 2.0 4 votes vote down vote up
private static void describeInstancesBatch(
        final AmazonEC2 ec2, final Map<String, Instance> described, final List<String> batch) {
    // we are going to modify list, so copy
    final List<String> copy = new ArrayList<>(batch);

    // just to simplify debug by having consist order
    Collections.sort(copy);

    // because instances could be terminated at any time we do multiple
    // retry to get status and all time remove from request all non found instances if any
    while (copy.size() > 0) {
        try {
            final DescribeInstancesRequest request = new DescribeInstancesRequest().withInstanceIds(copy);

            DescribeInstancesResult result;
            do {
                result = ec2.describeInstances(request);
                request.setNextToken(result.getNextToken());

                for (final Reservation r : result.getReservations()) {
                    for (final Instance instance : r.getInstances()) {
                        // if instance not in terminated state, add it to described
                        if (!TERMINATED_STATES.contains(instance.getState().getName())) {
                            described.put(instance.getInstanceId(), instance);
                        }
                    }
                }
            } while (result.getNextToken() != null);

            // all good, clear request batch to stop
            copy.clear();
        } catch (final AmazonEC2Exception exception) {
            // if we cannot find instance, that's fine assume them as terminated
            // remove from request and try again
            if (exception.getErrorCode().equals(NOT_FOUND_ERROR_CODE)) {
                final List<String> notFoundInstanceIds = parseInstanceIdsFromNotFoundException(exception.getMessage());
                if (notFoundInstanceIds.isEmpty()) {
                    // looks like we cannot parse correctly, rethrow
                    throw exception;
                }
                copy.removeAll(notFoundInstanceIds);
            }
        }
    }
}