Search in sources :

Example 36 with AmazonEC2AsyncClient

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;
}
Also used : AWSUtils.getAWSNonTerminatedInstancesFilter(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getAWSNonTerminatedInstancesFilter) Filter(com.amazonaws.services.ec2.model.Filter) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 37 with AmazonEC2AsyncClient

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);
}
Also used : TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest)

Example 38 with AmazonEC2AsyncClient

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;
        }
    }
}
Also used : DeleteSecurityGroupRequest(com.amazonaws.services.ec2.model.DeleteSecurityGroupRequest) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) UnknownHostException(java.net.UnknownHostException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) TimeoutException(java.util.concurrent.TimeoutException)

Example 39 with AmazonEC2AsyncClient

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());
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AWSUtils.getAWSNonTerminatedInstancesFilter(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getAWSNonTerminatedInstancesFilter) Filter(com.amazonaws.services.ec2.model.Filter) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 40 with AmazonEC2AsyncClient

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;
    }
}
Also used : DescribeSecurityGroupsRequest(com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest) AWSUtils.getAWSNonTerminatedInstancesFilter(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getAWSNonTerminatedInstancesFilter) Filter(com.amazonaws.services.ec2.model.Filter) DescribeSecurityGroupsResult(com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)

Aggregations

AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)18 Operation (com.vmware.xenon.common.Operation)13 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)11 Filter (com.amazonaws.services.ec2.model.Filter)11 StatelessService (com.vmware.xenon.common.StatelessService)11 AWSClientManager (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager)10 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)9 StopInstancesRequest (com.amazonaws.services.ec2.model.StopInstancesRequest)9 StopInstancesResult (com.amazonaws.services.ec2.model.StopInstancesResult)9 ArrayList (java.util.ArrayList)9 AsyncHandler (com.amazonaws.handlers.AsyncHandler)8 AWSClientManagerFactory (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory)8 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)8 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)8 HashMap (java.util.HashMap)8 List (java.util.List)8 TimeUnit (java.util.concurrent.TimeUnit)8 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)7 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)7 AWSSecurityGroupClient (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSSecurityGroupClient)7