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

/**
 *
 * @param groupName//w w  w .  j  a v  a 2 s  .com
 * @param vpcId leave null if you do not want your security group to be associated with a VPC
 * @param descr
 * @param ec2Client
 * @return
 */
public String createSecurityGroup(String groupName, String vpcId, String descr, AmazonEC2 ec2Client) {
    String groupId = null;
    try {
        CreateSecurityGroupRequest request = new CreateSecurityGroupRequest().withGroupName(groupName)
                .withDescription(descr);

        if (vpcId != null) {
            request = request.withVpcId(vpcId);
        }

        CreateSecurityGroupResult result = ec2Client.createSecurityGroup(request);
        groupId = result.getGroupId();

    } catch (AmazonServiceException e) {
        log.error("Failed to create Security Group", e);
        if (!"InvalidVpcID.NotFound".equalsIgnoreCase(e.getErrorCode())) {
            throw e;
        }
    }

    return groupId;
}

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

License:Apache License

/**
 *
 * @param groupId//from  w  w  w.  j  av a2  s . c  o m
 * @param ec2Client
 */
public void deleteSecurityGroup(String groupId, AmazonEC2 ec2Client) {
    try {
        log.info("Deleting Securty Group (" + groupId + ")");
        DeleteSecurityGroupRequest request = new DeleteSecurityGroupRequest().withGroupId(groupId);
        ec2Client.deleteSecurityGroup(request);
    } catch (AmazonServiceException e) {
        log.error("Failed to delete Security Group", e);
        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 ww .  j a v  a 2  s  . c  om
 * @param ec2Client
 */
public void deleteVpc(String vpcId, AmazonEC2 ec2Client) {
    try {
        log.info("Deleting Vpc (" + vpcId + ")");
        DeleteVpcRequest request = new DeleteVpcRequest().withVpcId(vpcId);
        ec2Client.deleteVpc(request);
    } catch (AmazonServiceException e) {
        log.error("Failed to delete Vpc", e);
        if (!"InvalidVpcID.NotFound".equalsIgnoreCase(e.getErrorCode())) {
            throw e;
        }
    }
}

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

License:Apache License

/**
 *
 * @param loadBalancerName//w w  w  .  j av  a 2s . c  o m
 * @param elbClient
 */
public void deleteLoadBalancer(String loadBalancerName, AmazonElasticLoadBalancing elbClient) {
    try {
        DeleteLoadBalancerRequest deleteRequest = new DeleteLoadBalancerRequest()
                .withLoadBalancerName(loadBalancerName);
        elbClient.deleteLoadBalancer(deleteRequest);
    } catch (AmazonServiceException e) {
        log.error("Could not delete Load Balancer " + loadBalancerName, e);
        if (e.getErrorCode().equals("LoadBalancerNotFound")) {
            log.warn("Could not find load balancer " + loadBalancerName);
        } else {
            throw e;
        }
    }
}

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

License:Apache License

/**
 *
 * @param name/*  www.  j  ava2 s.c o m*/
 * @param elbClient
 * @return
 */
public LoadBalancerDescription getLoadBalancerForName(String name, AmazonElasticLoadBalancing elbClient) {
    LoadBalancerDescription loadBalancer = null;
    try {
        DescribeLoadBalancersRequest request = new DescribeLoadBalancersRequest().withLoadBalancerNames(name);
        DescribeLoadBalancersResult result = elbClient.describeLoadBalancers(request);

        if (result != null && result.getLoadBalancerDescriptions() != null) {
            loadBalancer = result.getLoadBalancerDescriptions().get(0);
        }
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().equals("LoadBalancerNotFound")) {
            // if we can't find the ELB to delete, it's already gone
            log.warn("Could not find Load Balancer " + name, e);
        }
    }
    return loadBalancer;
}

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

License:Apache License

/**
 *
 * @param name//ww w  .j a v  a2s.  co  m
 * @param ec2Client
 * @return
 */
public SecurityGroup getSecurityGroupForName(String name, AmazonEC2 ec2Client) {
    SecurityGroup group = null;
    try {
        DescribeSecurityGroupsRequest request = new DescribeSecurityGroupsRequest().withGroupNames(name);
        DescribeSecurityGroupsResult result = ec2Client.describeSecurityGroups(request);

        if (result != null && result.getSecurityGroups() != null) {
            group = result.getSecurityGroups().get(0);
        }
    } catch (AmazonServiceException e) {
        log.warn("Could not find Security Group with name " + name, e);
        if (!e.getErrorCode().equals("InvalidGroup.NotFound")) {
            throw e;
        }
    }

    return group;
}

From source file:com.urbancode.terraform.tasks.aws.InstanceTask.java

License:Apache License

@Override
public void destroy() throws EnvironmentDestructionException {
    if (ec2Client == null) {
        ec2Client = context.fetchEC2Client();
    }//from www. j  a va  2s .  c  om
    if (elbClient == null) {
        elbClient = context.fetchELBClient();
    }
    try {
        log.info("Shutting down instance " + getId());
        List<String> instanceIds = new ArrayList<String>();
        instanceIds.add(instanceId);

        try {
            deregisterWithLoadBalancer();
        } catch (AmazonServiceException e) {
            // swallow exception if we get invalidInstance
            // this will ignore the exception if you try to
            // deregister and instance that is not registered with
            // the load balancer - allowing the instance to terminate
            // completely.
            if (!e.getErrorCode().equals("InvalidInstance")) {
                throw e;
            }
        }

        if (elasticIpAllocId != null) {
            String assocId = helper.getAssociationIdForAllocationId(elasticIpAllocId, ec2Client);
            if (assocId != null && !assocId.isEmpty()) {
                helper.disassociateElasticIp(assocId, ec2Client);
                helper.releaseElasticIp(getElasticIpAllocId(), ec2Client);
                setElasticIpAllocId(null);
                setPublicIp(null);
            } else {
                log.error("Could not find asssociation Id for instance " + getName()
                        + "\nUnable to disassociate and release elastic ip with " + "allocation id: "
                        + elasticIpAllocId);
            }
        }

        if (instanceIds != null && !instanceIds.isEmpty()) {
            helper.terminateInstances(instanceIds, ec2Client);
            helper.waitForState(getId(), "terminated", 8, ec2Client);
            setId(null);
            setSubnetId(null);
        }

        if (bootActions != null) {
            bootActions.destroy();
        }

        if (secRefs != null) {
            for (SecurityGroupRefTask group : secRefs) {
                group.destroy();
            }
        }
    } catch (Exception e) {
        log.error("Did not destroy instance " + name + " completely", e);
        throw new EnvironmentDestructionException("Failed to destroy instance " + name, e);
    } finally {
        ec2Client = null;
        elbClient = null;
        log.info("Instance Destroyed.");
    }
}

From source file:com.vb.services.database.rds.MySQLDBUtils.java

private void updateMySQLDBInstanceSpecificationsConfig() {

    MySQLDBInstanceSpecificationsConfig mysqlDBInstanceSpecificationsConfig = createDefaultDBInstanceSpecificationsConfig();
    this.mysqlDB.setDbInstanceSpecificationsConfig(mysqlDBInstanceSpecificationsConfig);

    try {/*ww w .  j  ava2s  .  c  o  m*/
        this.createDBInstanceRequest.setEngine(this.mysqlDB.getDbInstanceSpecificationsConfig().getDbEngine());
        this.createDBInstanceRequest
                .setLicenseModel(this.mysqlDB.getDbInstanceSpecificationsConfig().getLicenceModel());
        this.createDBInstanceRequest
                .setEngineVersion(this.mysqlDB.getDbInstanceSpecificationsConfig().getDbVersion());
        this.createDBInstanceRequest
                .setDBInstanceClass(this.mysqlDB.getDbInstanceSpecificationsConfig().getDbInstanceClass());
        this.createDBInstanceRequest
                .setMultiAZ(this.mysqlDB.getDbInstanceSpecificationsConfig().getMultiAZDeployment());
        this.createDBInstanceRequest
                .setStorageType(this.mysqlDB.getDbInstanceSpecificationsConfig().getStorageType());
        this.createDBInstanceRequest
                .setAllocatedStorage(this.mysqlDB.getDbInstanceSpecificationsConfig().getAllocatedStorage());
    } catch (AmazonServiceException ase) {
        System.out.println("ERROR : error during MySQLDBInstanceSpecificationsConfig setup.");
        System.out.println("Caught an AmazonServiceException");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
        ase.printStackTrace();
    } catch (AmazonClientException ace) {
        System.out.println("ERROR : error during MySQLDBInstanceSpecificationsConfig setup.");
        System.out.println("Caught an AmazonClientException");
        System.out.println("Error Message: " + ace.getMessage());
        ace.printStackTrace();
    }
}

From source file:com.vb.services.database.rds.MySQLDBUtils.java

private void updateMySQLDBSettingsConfig(String dbInstanceIdentifier, String masterUserName,
        String masterPassword) {//from   www.  j a  v a  2  s  .c om
    MySQLDBSettingsConfig mysqlDBSettingsConfig = createDBSettingsConfig(dbInstanceIdentifier, masterUserName,
            masterPassword);
    this.mysqlDB.setDbSettingsConfig(mysqlDBSettingsConfig);

    try {
        this.createDBInstanceRequest
                .setDBInstanceIdentifier(this.mysqlDB.getDbSettingsConfig().getDbInstanceIdentifier());
        this.createDBInstanceRequest.setMasterUsername(this.mysqlDB.getDbSettingsConfig().getMasterUserName());
        this.createDBInstanceRequest
                .setMasterUserPassword(this.mysqlDB.getDbSettingsConfig().getMasterPassword());
    } catch (AmazonServiceException ase) {
        System.out.println("ERROR : error during MySQLDBSettingsConfig setup.");
        System.out.println("Caught an AmazonServiceException");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
        ase.printStackTrace();
    } catch (AmazonClientException ace) {
        System.out.println("ERROR : error during MySQLDBSettingsConfig setup.");
        System.out.println("Caught an AmazonClientException");
        System.out.println("Error Message: " + ace.getMessage());
        ace.printStackTrace();
    }

}

From source file:com.vb.services.database.rds.MySQLDBUtils.java

private void updateMySQLDBNetworkSecurityConfig() {

    MySQLDBNetworkSecurityConfig mysqlDBNetworkSecurityConfig = new MySQLDBNetworkSecurityConfig();
    this.mysqlDB.setDbNetworkSecurityConfig(mysqlDBNetworkSecurityConfig);

    try {/*from  www .  j a v a  2 s. c o m*/

    } catch (AmazonServiceException ase) {
        System.out.println("ERROR : error during MySQLDBNetworkSecurityConfig setup.");
        System.out.println("Caught an AmazonServiceException");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
        ase.printStackTrace();
    } catch (AmazonClientException ace) {
        System.out.println("ERROR : error during MySQLDBNetworkSecurityConfig setup.");
        System.out.println("Caught an AmazonClientException");
        System.out.println("Error Message: " + ace.getMessage());
        ace.printStackTrace();
    }
}