use of com.amazonaws.services.ec2.model.DescribeInstancesRequest in project photon-model by vmware.
the class AWSEnumerationAndCreationAdapterService method createAWSRequestAndAsyncHandler.
/**
* 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.
*/
private void createAWSRequestAndAsyncHandler(EnumerationCreationContext aws, AWSEnumerationRefreshSubStage next) {
DescribeInstancesRequest request = new DescribeInstancesRequest();
Filter runningInstanceFilter = getAWSNonTerminatedInstancesFilter();
request.getFilters().add(runningInstanceFilter);
request.setMaxResults(getQueryPageSize());
request.setNextToken(aws.nextToken);
aws.describeInstancesRequest = request;
aws.resultHandler = new AWSEnumerationAsyncHandler(this, aws, next);
}
use of com.amazonaws.services.ec2.model.DescribeInstancesRequest in project photon-model by vmware.
the class AWSEnumerationAndDeletionAdapterService method getRemoteInstances.
/**
* Get the instances from AWS filtered by the instances Ids known to the local system.
*/
public void getRemoteInstances(EnumerationDeletionContext aws, AWSEnumerationDeletionSubStage next) {
if (aws.localInstanceIds == null || aws.localInstanceIds.size() == 0) {
logFine(() -> "No local records found. No states need to be fetched from the AWS" + " endpoint.");
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.
logFine(() -> String.format("Fetching instance details for %d instances on the AWS" + " endpoint.", aws.localInstanceIds.keySet().size()));
request.getInstanceIds().addAll(new ArrayList<>(aws.localInstanceIds.keySet()));
AsyncHandler<DescribeInstancesRequest, DescribeInstancesResult> resultHandler = new AWSEnumerationAsyncHandler(this, aws, next);
aws.amazonEC2Client.describeInstancesAsync(request, resultHandler);
}
use of com.amazonaws.services.ec2.model.DescribeInstancesRequest in project photon-model by vmware.
the class AWSTaskStatusChecker method buildRequest.
private AmazonWebServiceRequest buildRequest(T type) {
if (type instanceof Instance) {
DescribeInstancesRequest descRequest = new DescribeInstancesRequest();
List<String> instanceIdList = new ArrayList<>();
instanceIdList.add(this.instanceId);
descRequest.setInstanceIds(instanceIdList);
return descRequest;
} else if (type instanceof NatGateway) {
DescribeNatGatewaysRequest descRequest = new DescribeNatGatewaysRequest();
List<String> instanceIdList = new ArrayList<>();
instanceIdList.add(this.instanceId);
descRequest.setNatGatewayIds(instanceIdList);
return descRequest;
} else if (type instanceof Volume) {
DescribeVolumesRequest descRequest = new DescribeVolumesRequest();
List<String> volumeIdList = new ArrayList<>();
volumeIdList.add(this.instanceId);
descRequest.setVolumeIds(volumeIdList);
return descRequest;
} else {
AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalArgumentException("Invalid type " + type));
return null;
}
}
use of com.amazonaws.services.ec2.model.DescribeInstancesRequest in project photon-model by vmware.
the class AWSTaskStatusChecker method runSearch.
private void runSearch(T type) {
AmazonWebServiceRequest descRequest = buildRequest(type);
AsyncHandler describeHandler = buildHandler(type);
if (type instanceof Instance) {
this.amazonEC2Client.describeInstancesAsync((DescribeInstancesRequest) descRequest, describeHandler);
} else if (type instanceof NatGateway) {
this.amazonEC2Client.describeNatGatewaysAsync((DescribeNatGatewaysRequest) descRequest, describeHandler);
} else if (type instanceof Volume) {
this.amazonEC2Client.describeVolumesAsync((DescribeVolumesRequest) descRequest, describeHandler);
} else {
AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalArgumentException("Invalid type " + type));
}
}
use of com.amazonaws.services.ec2.model.DescribeInstancesRequest in project photon-model by vmware.
the class AWSUtils method waitForTransitionCompletion.
public static void waitForTransitionCompletion(ServiceHost host, List<InstanceStateChange> stateChangeList, final String desiredState, AmazonEC2AsyncClient client, BiConsumer<InstanceState, Exception> callback) {
InstanceStateChange stateChange = stateChangeList.get(0);
try {
DescribeInstancesRequest request = new DescribeInstancesRequest();
request.withInstanceIds(stateChange.getInstanceId());
DescribeInstancesResult result = client.describeInstances(request);
Instance instance = result.getReservations().stream().flatMap(r -> r.getInstances().stream()).filter(i -> i.getInstanceId().equalsIgnoreCase(stateChange.getInstanceId())).findFirst().orElseThrow(() -> new IllegalArgumentException(String.format("%s instance not found", stateChange.getInstanceId())));
String state = instance.getState().getName();
if (state.equals(desiredState)) {
callback.accept(instance.getState(), null);
} else {
host.schedule(() -> waitForTransitionCompletion(host, stateChangeList, desiredState, client, callback), 5, TimeUnit.SECONDS);
}
} catch (AmazonServiceException | IllegalArgumentException ase) {
callback.accept(null, ase);
}
}
Aggregations