Example usage for com.amazonaws.services.ec2 AmazonEC2 authorizeSecurityGroupEgress

List of usage examples for com.amazonaws.services.ec2 AmazonEC2 authorizeSecurityGroupEgress

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2 AmazonEC2 authorizeSecurityGroupEgress.

Prototype

AuthorizeSecurityGroupEgressResult authorizeSecurityGroupEgress(
        AuthorizeSecurityGroupEgressRequest authorizeSecurityGroupEgressRequest);

Source Link

Document

[VPC only] Adds the specified egress rules to a security group for use with a VPC.

Usage

From source file:com.urbancode.terraform.tasks.aws.helpers.AWSHelper.java

License:Apache License

/**
 *
 * @param groupId/*from w w w.  j  a  v  a2  s  .  co m*/
 * @param protocol
 * @param startPort
 * @param endPort
 * @param cidr
 * @param inbound
 * @param ec2Client
 */
public void createRuleForSecurityGroup(String groupId, String protocol, int startPort, int endPort, String cidr,
        boolean inbound, AmazonEC2 ec2Client) {
    try {
        // protocol should be lowercase
        protocol = protocol.toLowerCase();

        // create container for request
        // we need to use IpPermission object here because the other (old) way
        // is deprecated and no longer works
        IpPermission perm = new IpPermission().withFromPort(startPort).withToPort(endPort)
                .withIpProtocol(protocol).withIpRanges(cidr);
        if (inbound) {
            // inbound rule
            AuthorizeSecurityGroupIngressRequest request = new AuthorizeSecurityGroupIngressRequest()
                    .withGroupId(groupId).withIpPermissions(perm);
            ec2Client.authorizeSecurityGroupIngress(request);
        } else {
            // outbound rule
            AuthorizeSecurityGroupEgressRequest request = new AuthorizeSecurityGroupEgressRequest()
                    .withGroupId(groupId).withIpPermissions(perm);
            ec2Client.authorizeSecurityGroupEgress(request);
        }
    } catch (AmazonServiceException e) {
        log.error("Failed to create Rule on Security Group " + groupId, e);
        if (!"InvalidGroup.NotFound".equalsIgnoreCase(e.getErrorCode())) {
            throw e;
        }
    }
}

From source file:jp.classmethod.aws.gradle.ec2.AmazonEC2AuthorizeSecurityGroupEgressTask.java

License:Apache License

@TaskAction
public void authorizeEgress() {
    // to enable conventionMappings feature
    String groupId = getGroupId();
    Object ipPermissions = getIpPermissions();

    if (groupId == null) {
        throw new GradleException("groupId is not specified");
    }/*from w  w w  . j a  va2s .  c  om*/
    if (ipPermissions == null) {
        throw new GradleException("ipPermissions is not specified");
    }

    AmazonEC2PluginExtension ext = getProject().getExtensions().getByType(AmazonEC2PluginExtension.class);
    AmazonEC2 ec2 = ext.getClient();

    try {
        ec2.authorizeSecurityGroupEgress(new AuthorizeSecurityGroupEgressRequest().withGroupId(groupId)
                .withIpPermissions(parse(ipPermissions)));
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().equals("InvalidPermission.Duplicate")) {
            getLogger().warn(e.getMessage());
        } else {
            throw e;
        }
    }
}

From source file:org.xmlsh.aws.gradle.ec2.AmazonEC2AuthorizeSecurityGroupEgressTask.java

License:BSD License

@TaskAction
public void authorizeEgress() {
    // to enable conventionMappings feature
    String groupId = getGroupId();
    Object ipPermissions = getIpPermissions();

    if (groupId == null)
        throw new GradleException("groupId is not specified");
    if (ipPermissions == null)
        throw new GradleException("ipPermissions is not specified");

    AmazonEC2PluginExtension ext = getProject().getExtensions().getByType(AmazonEC2PluginExtension.class);
    AmazonEC2 ec2 = ext.getClient();

    try {// www .j  a va 2  s .c  o  m
        ec2.authorizeSecurityGroupEgress(new AuthorizeSecurityGroupEgressRequest().withGroupId(groupId)
                .withIpPermissions(parse(ipPermissions)));
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().equals("InvalidPermission.Duplicate")) {
            getLogger().warn(e.getMessage());
        } else {
            throw e;
        }
    }
}