Search in sources :

Example 16 with DescribeSecurityGroupsResult

use of com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult in project SimianArmy by Netflix.

the class AWSClient method describeSecurityGroups.

/**
 * Describe a set of security groups.
 *
 * @param groupNames the names of the groups to find
 * @return a list of matching groups
 */
public List<SecurityGroup> describeSecurityGroups(String... groupNames) {
    AmazonEC2 ec2Client = ec2Client();
    DescribeSecurityGroupsRequest request = new DescribeSecurityGroupsRequest();
    if (groupNames == null || groupNames.length == 0) {
        LOGGER.info(String.format("Getting all EC2 security groups in region %s.", region));
    } else {
        LOGGER.info(String.format("Getting EC2 security groups for %d names in region %s.", groupNames.length, region));
        request.withGroupNames(groupNames);
    }
    DescribeSecurityGroupsResult result;
    try {
        result = ec2Client.describeSecurityGroups(request);
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().equals("InvalidGroup.NotFound")) {
            LOGGER.info("Got InvalidGroup.NotFound error for security groups; returning empty list");
            return Collections.emptyList();
        }
        throw e;
    }
    List<SecurityGroup> securityGroups = result.getSecurityGroups();
    LOGGER.info(String.format("Got %d EC2 security groups in region %s.", securityGroups.size(), region));
    return securityGroups;
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2)

Aggregations

DescribeSecurityGroupsResult (com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)15 DescribeSecurityGroupsRequest (com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest)13 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)11 Filter (com.amazonaws.services.ec2.model.Filter)7 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)2 CreateSecurityGroupRequest (com.amazonaws.services.ec2.model.CreateSecurityGroupRequest)2 DeleteSecurityGroupRequest (com.amazonaws.services.ec2.model.DeleteSecurityGroupRequest)2 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)2 KeyPairInfo (com.amazonaws.services.ec2.model.KeyPairInfo)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AsyncHandler (com.amazonaws.handlers.AsyncHandler)1 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)1 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)1 AttachInternetGatewayRequest (com.amazonaws.services.ec2.model.AttachInternetGatewayRequest)1 AttachNetworkInterfaceRequest (com.amazonaws.services.ec2.model.AttachNetworkInterfaceRequest)1 AttachNetworkInterfaceResult (com.amazonaws.services.ec2.model.AttachNetworkInterfaceResult)1 AuthorizeSecurityGroupIngressRequest (com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest)1 AvailabilityZone (com.amazonaws.services.ec2.model.AvailabilityZone)1