Example usage for com.amazonaws.services.ec2 AmazonEC2Client describeSecurityGroups

List of usage examples for com.amazonaws.services.ec2 AmazonEC2Client describeSecurityGroups

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2 AmazonEC2Client describeSecurityGroups.

Prototype

@Override
public DescribeSecurityGroupsResult describeSecurityGroups(DescribeSecurityGroupsRequest request) 

Source Link

Document

Describes the specified security groups or all of your security groups.

Usage

From source file:com.cloudera.director.aws.ec2.EC2InstanceTemplateConfigurationValidator.java

License:Apache License

/**
 * Validates the configured security group IDs.
 *
 * @param client              the EC2 client
 * @param configuration       the configuration to be validated
 * @param accumulator         the exception condition accumulator
 * @param localizationContext the localization context
 *///from   ww w  . j  a va 2 s . c o  m
@VisibleForTesting
void checkSecurityGroupIds(AmazonEC2Client client, Configured configuration,
        PluginExceptionConditionAccumulator accumulator, LocalizationContext localizationContext) {

    List<String> securityGroupsIds = EC2InstanceTemplate.CSV_SPLITTER
            .splitToList(configuration.getConfigurationValue(SECURITY_GROUP_IDS, localizationContext));

    for (String securityGroupId : securityGroupsIds) {
        LOG.info(">> Describing security group '{}'", securityGroupId);

        try {
            DescribeSecurityGroupsResult result = client
                    .describeSecurityGroups(new DescribeSecurityGroupsRequest().withGroupIds(securityGroupId));
            checkCount(accumulator, SECURITY_GROUP_IDS, localizationContext, securityGroupId,
                    result.getSecurityGroups());
        } catch (AmazonServiceException e) {
            if (e.getErrorCode().startsWith(INVALID_SECURITY_GROUP)) {
                addError(accumulator, SECURITY_GROUP_IDS, localizationContext, null, INVALID_SECURITY_GROUP_MSG,
                        securityGroupId);
            } else {
                throw Throwables.propagate(e);
            }
        }
    }
}

From source file:com.lunabeat.dooper.ClusterList.java

License:Apache License

public static Map<String, Map<String, List<Instance>>> getClusterMap(ClusterConfig config) {
    HashMap<String, Map<String, List<Instance>>> clusterMap = new HashMap<String, Map<String, List<Instance>>>();
    AmazonEC2Client ec2 = new AmazonEC2Client(config);
    DescribeSecurityGroupsRequest dsr = new DescribeSecurityGroupsRequest().withFilters(
            new Filter().withName(HadoopCluster.GROUP_NAME_KEY).withValues("*" + HadoopCluster.MASTER_SUFFIX));
    DescribeSecurityGroupsResult groupsResult = ec2.describeSecurityGroups(dsr);
    if (groupsResult == null)
        return null;
    List<SecurityGroup> groups = groupsResult.getSecurityGroups();
    ArrayList<String> groupNames = new ArrayList<String>();
    for (SecurityGroup sg : groups) {
        groupNames.add(sg.getGroupName());
        groupNames.add(sg.getGroupName().replace(HadoopCluster.MASTER_SUFFIX, ""));
    }//from w w  w. j a  v a2  s.  com
    for (String group : groupNames) {
        String clusterName = group.replace(HadoopCluster.MASTER_SUFFIX, "");
        if (clusterMap.get(clusterName) == null)
            clusterMap.put(clusterName, new HashMap<String, List<Instance>>());
        if (clusterMap.get(clusterName).get(group) == null)
            clusterMap.get(clusterName).put(group, new ArrayList<Instance>());
    }
    DescribeInstancesRequest dir = new DescribeInstancesRequest()
            .withFilters(new Filter().withName(HadoopCluster.GROUP_NAME_KEY).withValues(groupNames));
    DescribeInstancesResult instanceResult = ec2.describeInstances(dir);
    if (instanceResult == null)
        return null;
    for (Reservation r : instanceResult.getReservations()) {
        String group = r.getGroupNames().get(0);
        String clusterName = group.replace(HadoopCluster.MASTER_SUFFIX, "");
        if (clusterMap.get(clusterName) == null)
            clusterMap.put(clusterName, new HashMap<String, List<Instance>>());
        if (clusterMap.get(clusterName).get(group) == null)
            clusterMap.get(clusterName).put(group, new ArrayList<Instance>());
        for (Instance i : r.getInstances()) {
            clusterMap.get(clusterName).get(group).add(i);
        }
    }

    return clusterMap;
}

From source file:n3phele.factory.ec2.VirtualServerResource.java

License:Open Source License

protected boolean checkSecurityGroup(String groupName, String id, String secret, URI location) {
    AmazonEC2Client client = null;
    client = getEC2Client(id, secret, location);
    boolean found = true;
    try {// w  ww  .  j  a v a2 s  .  co  m
        DescribeSecurityGroupsResult response = client.describeSecurityGroups(
                new DescribeSecurityGroupsRequest().withGroupNames("n3phele-" + groupName));
        if (response.getSecurityGroups() == null || response.getSecurityGroups().isEmpty()) {
            log.warning("No groups found");
            found = false;
        } else {
            log.warning("Found " + response.getSecurityGroups().size() + " "
                    + response.getSecurityGroups().toString());
        }
    } catch (Exception e) {
        log.severe("Check security group exception " + e.getMessage());
        found = false;
    }

    return found;
}

From source file:org.elasticdroid.model.SecurityGroupsModel.java

License:Open Source License

/**
 * The method that does the actual work 
 * //from www .  j av  a 2  s  .c  om
 * Can return
 */
public Object getSecurityGroupData(Filter... filters) {
    //create credentials using the BasicAWSCredentials class
    BasicAWSCredentials credentials = new BasicAWSCredentials(connectionData.get("accessKey"),
            connectionData.get("secretAccessKey"));
    //create Amazon EC2 Client object, and set tye end point to the region. params[3]
    //contains endpoint
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);
    amazonEC2Client.setEndpoint(connectionData.get("endpoint"));

    DescribeSecurityGroupsRequest securityGroupsRequest = new DescribeSecurityGroupsRequest();
    //add filters to the request
    securityGroupsRequest.withFilters(new ArrayList<Filter>(Arrays.asList(filters)));

    List<SecurityGroup> securityGroups;
    try {
        securityGroups = amazonEC2Client.describeSecurityGroups(securityGroupsRequest).getSecurityGroups();
    } catch (AmazonServiceException amazonServiceException) {
        return amazonServiceException;
    } catch (AmazonClientException amazonClientException) {
        return amazonClientException;
    }

    List<SerializableSecurityGroup> serSecurityGroups = new ArrayList<SerializableSecurityGroup>();
    for (SecurityGroup securityGroup : securityGroups) {
        serSecurityGroups.add(new SerializableSecurityGroup(securityGroup));
    }

    return serSecurityGroups;
}

From source file:org.zalando.stups.fullstop.plugin.instance.RunInstancePlugin.java

License:Apache License

protected Optional<List<SecurityGroup>> getSecurityGroupsForIds(final List<String> securityGroupIds,
        final CloudTrailEvent event) {

    Region region = getRegion(event);
    String accountId = getAccountId(event);

    AmazonEC2Client amazonEC2Client = getClient(accountId, region);

    if (amazonEC2Client == null) {
        throw new RuntimeException(
                String.format("Somehow we could not create an Client with accountId: %s and region: %s",
                        accountId, region.toString()));
    } else {/*from  w ww  . j a va 2  s  . c o  m*/
        try {
            DescribeSecurityGroupsRequest request = new DescribeSecurityGroupsRequest();
            request.setGroupIds(securityGroupIds);

            DescribeSecurityGroupsResult result = amazonEC2Client.describeSecurityGroups(request);

            return Optional.of(result.getSecurityGroups());
        } catch (AmazonClientException e) {
            LOG.warn("Unable to get SecurityGroups for SecurityGroupIds [{}] | {}", securityGroupIds.toString(),
                    e.getMessage());
            return Optional.empty();
        }

    }

}

From source file:org.zalando.stups.fullstop.plugin.RunInstancePlugin.java

License:Apache License

protected Optional<List<SecurityGroup>> getSecurityGroupsForIds(final List<String> securityGroupIds,
        final CloudTrailEvent event) {

    Region region = getRegion(event);
    String accountId = getAccountId(event);

    AmazonEC2Client amazonEC2Client = getClient(accountId, region);

    if (amazonEC2Client == null) {
        throw new RuntimeException(
                String.format("Somehow we could not create an Client with accountId: %s and region: %s",
                        accountId, region.toString()));
    } else {/* ww w  . j  a  v  a  2 s .c om*/
        try {
            DescribeSecurityGroupsRequest request = new DescribeSecurityGroupsRequest();
            request.setGroupIds(securityGroupIds);

            DescribeSecurityGroupsResult result = amazonEC2Client.describeSecurityGroups(request);

            return Optional.of(result.getSecurityGroups());
        } catch (AmazonClientException e) {

            // TODO, better ways?
            String message = String.format("Unable to get SecurityGroups for SecurityGroupIds [%s] | %s",
                    securityGroupIds.toString(), e.getMessage());

            violationStore.save(new ViolationBuilder(message).withEvent(event).build());
            return Optional.empty();
        }

    }

}

From source file:org.zalando.stups.fullstop.plugin.SaveSecurityGroupsPlugin.java

License:Apache License

public String getSecurityGroup(List<String> securityGroupIds, Region region, String accountId) {

    DescribeSecurityGroupsResult result = null;
    ObjectMapper objectMapper = new ObjectMapper();
    String securityGroups = null;

    AmazonEC2Client amazonEC2Client = cachingClientProvider.getClient(AmazonEC2Client.class, accountId, region);

    if (amazonEC2Client == null) {
        throw new RuntimeException(
                String.format("Somehow we could not create an Client with accountId: %s and region: %s",
                        accountId, region.toString()));
    } else {//from   w  w  w .jav  a2s  . c o m

        try {
            DescribeSecurityGroupsRequest request = new DescribeSecurityGroupsRequest();
            request.setGroupIds(securityGroupIds);
            result = amazonEC2Client.describeSecurityGroups(request);
        } catch (AmazonClientException e) {
            LOG.error(e.getMessage());
        }
        try {
            securityGroups = objectMapper.writeValueAsString(result);
        } catch (JsonProcessingException e) {
            LOG.error(e.getMessage());
        }
        return securityGroups;
    }
}