Search in sources :

Example 1 with DeleteStackRequest

use of com.amazonaws.services.cloudformation.model.DeleteStackRequest in project cloudbreak by hortonworks.

the class AwsDeleteVpcTest method deleteNetwork.

@AfterSuite
@Parameters({ "regionName", "vpcStackName" })
public void deleteNetwork(String regionName, @Optional("it-vpc-stack") String vpcStackName) {
    AmazonCloudFormationClient client = new AmazonCloudFormationClient();
    client.setRegion(RegionUtils.getRegion(regionName));
    client.deleteStack(new DeleteStackRequest().withStackName(vpcStackName));
}
Also used : DeleteStackRequest(com.amazonaws.services.cloudformation.model.DeleteStackRequest) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient) Parameters(org.testng.annotations.Parameters) AfterSuite(org.testng.annotations.AfterSuite)

Example 2 with DeleteStackRequest

use of com.amazonaws.services.cloudformation.model.DeleteStackRequest in project cloudbreak by hortonworks.

the class AwsResourceConnector method terminate.

@Override
public List<CloudResourceStatus> terminate(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources) {
    LOGGER.info("Deleting stack: {}", ac.getCloudContext().getId());
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    if (resources != null && !resources.isEmpty()) {
        AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
        CloudResource stackResource = getCloudFormationStackResource(resources);
        if (stackResource == null) {
            return Collections.emptyList();
        }
        String cFStackName = stackResource.getName();
        LOGGER.info("Deleting CloudFormation stack for stack: {} [cf stack id: {}]", cFStackName, ac.getCloudContext().getId());
        DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
        try {
            retryService.testWith2SecDelayMax5Times(() -> {
                try {
                    cfClient.describeStacks(describeStacksRequest);
                } catch (AmazonServiceException e) {
                    if (!e.getErrorMessage().contains(cFStackName + " does not exist")) {
                        throw e;
                    }
                    throw new ActionWentFailException("Stack not exists");
                }
                return Boolean.TRUE;
            });
        } catch (ActionWentFailException ignored) {
            LOGGER.info(String.format("Stack not found with name: %s", cFStackName));
            AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
            releaseReservedIp(amazonEC2Client, resources);
            return Collections.emptyList();
        }
        resumeAutoScalingPolicies(ac, stack);
        DeleteStackRequest deleteStackRequest = new DeleteStackRequest().withStackName(cFStackName);
        cfClient.deleteStack(deleteStackRequest);
        PollTask<Boolean> task = awsPollTaskFactory.newAwsTerminateStackStatusCheckerTask(ac, cfClient, DELETE_COMPLETE, DELETE_FAILED, ERROR_STATUSES, cFStackName);
        try {
            Boolean statePollerResult = task.call();
            if (!task.completed(statePollerResult)) {
                syncPollingScheduler.schedule(task);
            }
        } catch (Exception e) {
            throw new CloudConnectorException(e.getMessage(), e);
        }
        AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
        releaseReservedIp(amazonEC2Client, resources);
        deleteKeyPair(ac, stack);
    } else if (resources != null) {
        AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
        releaseReservedIp(amazonEC2Client, resources);
        LOGGER.info("No CloudFormation stack saved for stack.");
    } else {
        LOGGER.info("No resources to release.");
    }
    return check(ac, resources);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) DeleteStackRequest(com.amazonaws.services.cloudformation.model.DeleteStackRequest) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) IOException(java.io.IOException) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient)

Aggregations

AmazonCloudFormationClient (com.amazonaws.services.cloudformation.AmazonCloudFormationClient)2 DeleteStackRequest (com.amazonaws.services.cloudformation.model.DeleteStackRequest)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)1 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)1 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)1 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)1 ActionWentFailException (com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException)1 IOException (java.io.IOException)1 AfterSuite (org.testng.annotations.AfterSuite)1 Parameters (org.testng.annotations.Parameters)1