Search in sources :

Example 1 with DescribeSecurityGroupsRequest

use of com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest in project aws-doc-sdk-examples by awsdocs.

the class DescribeSecurityGroups method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply a group id\n" + "Ex: DescribeSecurityGroups <group-id>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String group_id = args[0];
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    DescribeSecurityGroupsRequest request = new DescribeSecurityGroupsRequest().withGroupIds(group_id);
    DescribeSecurityGroupsResult response = ec2.describeSecurityGroups(request);
    for (SecurityGroup group : response.getSecurityGroups()) {
        System.out.printf("Found security group with id %s, " + "vpc id %s " + "and description %s", group.getGroupId(), group.getVpcId(), group.getDescription());
    }
}
Also used : DescribeSecurityGroupsRequest(com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeSecurityGroupsResult(com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup)

Example 2 with DescribeSecurityGroupsRequest

use of com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest 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

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 DescribeSecurityGroupsRequest (com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest)1 DescribeSecurityGroupsResult (com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)1 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)1