use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.
the class TestAWSSetupUtils method getBaseLineInstanceCount.
/**
* Gets the instance count of non-terminated instances on the AWS endpoint. This is used to run
* the asserts and validate the results for the data that is collected during enumeration.This
* also calculates the compute descriptions that will be used to represent the instances that
* were discovered on the AWS endpoint. Further factoring in the
*
* @throws Throwable
*/
public static BaseLineState getBaseLineInstanceCount(VerificationHost host, AmazonEC2AsyncClient client, List<String> testComputeDescriptions) throws Throwable {
BaseLineState baseLineState = new BaseLineState();
AWSEnumerationAsyncHandler enumerationHandler = new AWSEnumerationAsyncHandler(host, AWSEnumerationAsyncHandler.MODE.GET_COUNT, null, null, null, testComputeDescriptions, baseLineState);
DescribeInstancesRequest request = new DescribeInstancesRequest();
Filter runningInstanceFilter = getAWSNonTerminatedInstancesFilter();
request.getFilters().add(runningInstanceFilter);
client.describeInstancesAsync(request, enumerationHandler);
host.waitFor("Error waiting to get base line instance count from AWS in test ", () -> {
return baseLineState.isCountPopulated;
});
return baseLineState;
}
use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.
the class TestAWSSetupUtils method deleteVMsUsingEC2Client.
/**
* Deletes instances on the AWS endpoint for the set of instance Ids that are passed in.
*
* @param instanceIdsToDelete
* @throws Throwable
*/
public static void deleteVMsUsingEC2Client(AmazonEC2AsyncClient client, VerificationHost host, List<String> instanceIdsToDelete) throws Throwable {
TerminateInstancesRequest termRequest = new TerminateInstancesRequest(instanceIdsToDelete);
AsyncHandler<TerminateInstancesRequest, TerminateInstancesResult> terminateHandler = new AWSTerminateHandlerAsync(host);
client.terminateInstancesAsync(termRequest, terminateHandler);
waitForInstancesToBeTerminated(client, host, instanceIdsToDelete);
}
use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.
the class TestAWSSetupUtils method deleteSecurityGroupUsingEC2Client.
public static void deleteSecurityGroupUsingEC2Client(AmazonEC2AsyncClient client, VerificationHost host, String awsGroupId) {
host.log(Level.INFO, "Starting to delete aws Security group with id %s", awsGroupId);
if (awsGroupId == null) {
return;
}
try {
DeleteSecurityGroupRequest deleteSecurityGroupRequest = new DeleteSecurityGroupRequest().withGroupId(awsGroupId);
client.deleteSecurityGroup(deleteSecurityGroupRequest);
host.waitFor("Timeout waiting for AWS to delete a SecurityGroup with name " + awsGroupId, () -> {
// Check if the SG is actually not present on AWS after the delete operation
SecurityGroup discoveredSGOnAWS = getSecurityGroupsIdUsingEC2Client(client, awsGroupId);
if (discoveredSGOnAWS != null) {
// Requested SG was not deleted from AWS
return false;
}
host.log("Deleted SG with id: %s", awsGroupId);
return true;
});
} catch (Exception e) {
String message = e.getMessage();
if (!message.contains("The security group '" + awsGroupId + "' already exists")) {
throw e;
}
}
}
use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.
the class TestAWSSetupUtils method getEC2InstanceIdsAssociatedWithVpcId.
/**
* Get a list of all EC2 instance ids associated with a given VPC id.
*/
public static List<String> getEC2InstanceIdsAssociatedWithVpcId(AmazonEC2AsyncClient client, String vpcId) {
DescribeInstancesRequest req = new DescribeInstancesRequest();
if (vpcId != null) {
req.withFilters(new Filter(AWS_VPC_ID_FILTER, Collections.singletonList(vpcId)));
}
DescribeInstancesResult instancesResult = client.describeInstances(req);
return instancesResult == null ? Collections.emptyList() : instancesResult.getReservations().get(0).getInstances().stream().map(instance -> instance.getInstanceId()).collect(Collectors.toList());
}
use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.
the class TestAWSSetupUtils method getSecurityGroupsIdUsingEC2Client.
public static SecurityGroup getSecurityGroupsIdUsingEC2Client(AmazonEC2AsyncClient client, String awsGroupId) {
if (awsGroupId == null) {
return null;
}
DescribeSecurityGroupsRequest describeSGsRequest = new DescribeSecurityGroupsRequest().withFilters(new Filter(AWSConstants.AWS_GROUP_ID_FILTER, Collections.singletonList(awsGroupId)));
DescribeSecurityGroupsResult describeSGResult = client.describeSecurityGroups(describeSGsRequest);
if (describeSGResult.getSecurityGroups().size() > 0) {
return describeSGResult.getSecurityGroups().get(0);
} else {
return null;
}
}
Aggregations