Example usage for com.amazonaws.services.ec2.model DescribeSecurityGroupsRequest setGroupIds

List of usage examples for com.amazonaws.services.ec2.model DescribeSecurityGroupsRequest setGroupIds

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model DescribeSecurityGroupsRequest setGroupIds.

Prototype


public void setGroupIds(java.util.Collection<String> groupIds) 

Source Link

Document

The IDs of the security groups.

Usage

From source file:com.haskins.cloudtrailviewer.dialog.resourcedetail.detailpanels.EC2SecurityGroupDetail.java

License:Open Source License

@Override
public String retrieveDetails(ResourceDetailRequest detailRequest) {

    String response = null;/*from  ww  w.  j  av  a  2 s. co m*/

    try {
        AmazonEC2 ec2Client = new AmazonEC2Client(credentials);
        ec2Client.setRegion(Region.getRegion(Regions.fromName(detailRequest.getRegion())));

        DescribeSecurityGroupsRequest request = new DescribeSecurityGroupsRequest();
        request.setGroupIds(Collections.singletonList(detailRequest.getResourceName()));

        DescribeSecurityGroupsResult result = ec2Client.describeSecurityGroups(request);
        buildUI(result);

    } catch (IllegalArgumentException | AmazonClientException e) {
        response = e.getMessage();
        LOGGER.log(Level.WARNING, "Problem retrieving EC2 Securuty Group details from AWS", e);
    }

    return response;
}

From source file:org.apache.stratos.aws.extension.AWSHelper.java

License:Apache License

/**
 * Adds inbound rule to the security group which allows users to access load
 * balancer at specified port and using the specified protocol. Port
 * specified should be a proxy port mentioned in the port mappings of the
 * cartridge.//from  w  ww.j  a v  a2s .c  o m
 *
 * @param groupId  to which this rule to be added
 * @param region   of the security group
 * @param protocol with which load balancer can be accessed
 * @param port     at which load balancer can be accessed
 * @throws LoadBalancerExtensionException
 */
public void addInboundRuleToSecurityGroup(String groupId, String region, String protocol, int port)
        throws LoadBalancerExtensionException {
    if (groupId == null || groupId.isEmpty()) {
        throw new LoadBalancerExtensionException(
                "Invalid security group Id for addInboundRuleToSecurityGroup.");
    }

    boolean ruleAlreadyPresent = false;

    DescribeSecurityGroupsRequest describeSecurityGroupsRequest = new DescribeSecurityGroupsRequest();

    List<String> groupIds = new ArrayList<String>();
    groupIds.add(groupId);

    describeSecurityGroupsRequest.setGroupIds(groupIds);

    SecurityGroup secirutyGroup = null;

    try {
        ec2Client.setEndpoint(String.format(Constants.EC2_ENDPOINT_URL_FORMAT, region));

        DescribeSecurityGroupsResult describeSecurityGroupsResult = ec2Client
                .describeSecurityGroups(describeSecurityGroupsRequest);

        List<SecurityGroup> securityGroups = describeSecurityGroupsResult.getSecurityGroups();

        if (securityGroups != null && securityGroups.size() > 0) {
            secirutyGroup = securityGroups.get(0);
        } else {
            log.warn("No Security Groups found for group id " + groupId);
        }

    } catch (AmazonClientException e) {
        log.error("Could not describe security groups.", e);
    }

    if (secirutyGroup != null) {
        List<IpPermission> existingPermissions = secirutyGroup.getIpPermissions();

        IpPermission neededPermission = new IpPermission();
        neededPermission.setFromPort(port);
        neededPermission.setToPort(port);
        neededPermission.setIpProtocol(protocol);

        Collection<String> ipRanges = new HashSet<String>();
        ipRanges.add(this.allowedCidrIpForLBSecurityGroup);

        neededPermission.setIpRanges(ipRanges);

        if (existingPermissions.contains(neededPermission)) {
            ruleAlreadyPresent = true;
        }
    }

    if (!ruleAlreadyPresent) {
        AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = new AuthorizeSecurityGroupIngressRequest();
        authorizeSecurityGroupIngressRequest.setGroupId(groupId);
        authorizeSecurityGroupIngressRequest.setCidrIp(this.allowedCidrIpForLBSecurityGroup);
        authorizeSecurityGroupIngressRequest.setFromPort(port);
        authorizeSecurityGroupIngressRequest.setToPort(port);
        authorizeSecurityGroupIngressRequest.setIpProtocol(protocol);

        try {
            ec2Client.setEndpoint(String.format(Constants.EC2_ENDPOINT_URL_FORMAT, region));

            ec2Client.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);

        } catch (AmazonClientException e) {
            throw new LoadBalancerExtensionException(
                    "Could not add inbound rule to security group " + groupId + ".", e);
        }
    } else {
        log.info("Rules already present for security group " + groupId);
    }
}

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  .jav  a 2s  . co  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 {/*w ww  .java 2s .com*/
        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  www.  ja  v a  2  s  . c  om*/

        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;
    }
}