Example usage for com.amazonaws AmazonServiceException getErrorCode

List of usage examples for com.amazonaws AmazonServiceException getErrorCode

Introduction

In this page you can find the example usage for com.amazonaws AmazonServiceException getErrorCode.

Prototype

public String getErrorCode() 

Source Link

Document

Returns the AWS error code represented by this exception.

Usage

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

License:Apache License

/**
 * Deletes the given internetGateway. Does not detach the gateway if it is attached.
 *
 * @param gatewayId/*from  w  ww. j av  a 2  s  . co  m*/
 * @param ec2Client
 */
public void deleteInternetGateway(String gatewayId, AmazonEC2 ec2Client) {
    try {
        DeleteInternetGatewayRequest request = new DeleteInternetGatewayRequest()
                .withInternetGatewayId(gatewayId);
        ec2Client.deleteInternetGateway(request);
    } catch (AmazonServiceException e) {
        log.error("Failed to delete Internet Gateway", e);
        if (!"InvalidInternetGatewayID.NotFound".equalsIgnoreCase(e.getErrorCode())) {
            // swallow the exception only if the gateway id was not found
            throw e;
        }
    }
}

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

License:Apache License

/**
 * Attaches the given gateway to the given Vpc.
 *
 * @param gatewayId/*from w w  w  . j a  v  a2  s  . com*/
 * @param vpcId
 * @param ec2Client
 */
public void attachInternetGatewayToVpc(String gatewayId, String vpcId, AmazonEC2 ec2Client) {
    try {
        AttachInternetGatewayRequest request = new AttachInternetGatewayRequest()
                .withInternetGatewayId(gatewayId).withVpcId(vpcId);
        ec2Client.attachInternetGateway(request);
    } catch (AmazonServiceException e) {
        log.error("Failed to attach Internet Gateway to Vpc", e);
        if ("InvalidVpcID.NotFound".equalsIgnoreCase(e.getErrorCode())) {
            // could not find vpc
            log.error("Vpc " + vpcId + " not found.");
        } else if ("InvalidInternetGatewayID.NotFound".equalsIgnoreCase(e.getErrorCode())) {
            // swallow the exception only if the gateway id was not found
            log.error("Internet Gateway " + gatewayId + " not found.");
        } else {
            throw e;
        }
    }
}

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

License:Apache License

/**
 *
 * @param routeTableId/*from  w w w . j  a  v  a 2s. c om*/
 * @param subnetId
 * @param ec2Client
 * @return
 */
public String associateRouteTableWithSubnet(String routeTableId, String subnetId, AmazonEC2 ec2Client) {
    String assId = null;
    try {
        AssociateRouteTableRequest request = new AssociateRouteTableRequest().withRouteTableId(routeTableId)
                .withSubnetId(subnetId);
        AssociateRouteTableResult result = ec2Client.associateRouteTable(request);

        if (result != null) {
            assId = result.getAssociationId();
        }
    } catch (AmazonServiceException e) {
        log.error("Failed to associate Route Table with Subnet", e);
        if (!"InvalidRouteTableID.NotFound".equalsIgnoreCase(e.getErrorCode())) {
            throw e;
        }
    }

    return assId;
}

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

License:Apache License

/**
 *
 * @param vpcId/*  w  ww .  j  a  v  a  2 s.com*/
 * @param ec2Client
 * @return
 */
public String createRouteTable(String vpcId, AmazonEC2 ec2Client) {
    String routeTableId = null;
    try {
        CreateRouteTableRequest request = new CreateRouteTableRequest().withVpcId(vpcId);
        CreateRouteTableResult result = ec2Client.createRouteTable(request);
        if (result != null && result.getRouteTable() != null) {
            routeTableId = result.getRouteTable().getRouteTableId();
        }
    } catch (AmazonServiceException e) {
        log.error("Failed to create Route Table in Vpc " + vpcId, e);
        if (!"InvalidVpcID.NotFound".equalsIgnoreCase(e.getErrorCode())) {
            throw e;
        }
    }
    return routeTableId;
}

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

License:Apache License

/**
 *
 * @param routeTableId//from w w w.  j  av  a  2 s.  c  o m
 * @param ec2Client
 */
public void deleteRouteTable(String routeTableId, AmazonEC2 ec2Client) {
    try {
        DeleteRouteTableRequest request = new DeleteRouteTableRequest().withRouteTableId(routeTableId);
        ec2Client.deleteRouteTable(request);
    } catch (AmazonServiceException e) {
        log.error("Failed to delete subnet", e);
        if (!"InvalidSubnetID.NotFound".equals(e.getErrorCode())
                && !"InvalidRouteTableID.NotFound".equals(e.getErrorCode())) {
            // swallow the exception if the subnet id was not found
            throw e;
        }
    }
}

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

License:Apache License

/**
 *
 * @param associationId/* w  ww. ja  v a 2 s  .  com*/
 * @param ec2Client
 */
public void disassociateRouteTableFromSubnet(String associationId, AmazonEC2 ec2Client) {
    try {
        DisassociateRouteTableRequest request = new DisassociateRouteTableRequest()
                .withAssociationId(associationId);
        ec2Client.disassociateRouteTable(request);
    } catch (AmazonServiceException e) {
        log.error("Failed to disassociate Route Table from Subnet", e);
        if (!"InvalidAssociationID.NotFound".equalsIgnoreCase(e.getErrorCode())) {
            throw e;
        }
    }
}

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

License:Apache License

/**
 *
 * @param groupId/*from ww  w  .  ja  v a 2 s.c o 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:com.urbancode.terraform.tasks.aws.helpers.AWSHelper.java

License:Apache License

/**
 *
 * @param groupId//from   www  .j ava  2 s  .c  o m
 * @param protocol
 * @param startPort
 * @param endPort
 * @param cidr
 * @param inbound
 * @param ec2Client
 */
public void deleteRuleForSecurityGroup(String groupId, String protocol, int startPort, int endPort, String cidr,
        boolean inbound, AmazonEC2 ec2Client) {

    IpPermission perm = new IpPermission().withFromPort(startPort).withToPort(endPort).withIpProtocol(protocol)
            .withIpRanges(cidr);
    try {
        if (inbound) {
            RevokeSecurityGroupIngressRequest request = new RevokeSecurityGroupIngressRequest()
                    .withGroupId(groupId).withIpPermissions(perm);
            ec2Client.revokeSecurityGroupIngress(request);
        } else {
            RevokeSecurityGroupEgressRequest request = new RevokeSecurityGroupEgressRequest()
                    .withGroupId(groupId).withIpPermissions(perm);
            ec2Client.revokeSecurityGroupEgress(request);
        }
    } catch (AmazonServiceException e) {
        log.error("Failed to delete Rule on Security Group " + groupId);
        if (!"InvalidGroup.NotFound".equals(e.getErrorCode())) {
            throw e;
        }
    }
}

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

License:Apache License

/**
 *
 * @param vpcId//from   w  w  w.jav  a2s. c o m
 * @param cidr
 * @param zone
 * @param ec2Client
 * @return
 */
public String createSubnet(String vpcId, String cidr, String zone, AmazonEC2 ec2Client) {
    String subnetId = null;
    try {
        CreateSubnetRequest request = new CreateSubnetRequest().withVpcId(vpcId).withCidrBlock(cidr)
                .withAvailabilityZone(zone);
        CreateSubnetResult result = ec2Client.createSubnet(request);
        if (result != null && result.getSubnet() != null) {
            subnetId = result.getSubnet().getSubnetId();
        }
    } catch (AmazonServiceException e) {
        log.error("Failed to create Subnet", e);
        if (!"InvalidVpcID.NotFound".equalsIgnoreCase(e.getErrorCode())) {
            throw e;
        }
    }

    return subnetId;
}

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

License:Apache License

/**
 *
 * @param subnetId//from ww w.  j  a va2s  . c  o  m
 * @param ec2Client
 */
public void deleteSubnet(String subnetId, AmazonEC2 ec2Client) {
    try {
        log.info("Deleting Subnet (" + subnetId + ")");
        DeleteSubnetRequest request = new DeleteSubnetRequest().withSubnetId(subnetId);
        ec2Client.deleteSubnet(request);
    } catch (AmazonServiceException e) {
        log.error("Failed to delete Subnet", e);
        if (!"InvalidSubnetId.NotFound".equals(e.getErrorCode())) {
            throw e;
        }
    }

}