Java Code Examples for com.amazonaws.services.cloudformation.model.DeleteStackRequest#setStackName()

The following examples show how to use com.amazonaws.services.cloudformation.model.DeleteStackRequest#setStackName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AWSProvider.java    From testgrid with Apache License 2.0 5 votes vote down vote up
/**
 * This method releases the provisioned AWS infrastructure.
 *
 * @param script            the script config
 * @param inputs
 * @return true or false to indicate the result of destroy operation.
 * @throws TestGridInfrastructureException when AWS error occurs in deletion process.
 * @throws InterruptedException            when there is an interruption while waiting for the result.
 */
private boolean doRelease(Script script, Properties inputs, TestPlan testPlan)
        throws TestGridInfrastructureException, InterruptedException, TestGridDAOException {
    Path configFilePath;
    String stackName = script.getName();
    String region = inputs.getProperty(AWS_REGION_PARAMETER);
    configFilePath = TestGridUtil.getConfigFilePath();
    AmazonCloudFormation stackdestroy = AmazonCloudFormationClientBuilder.standard()
            .withCredentials(new PropertiesFileCredentialsProvider(configFilePath.toString()))
            .withRegion(region)
            .build();
    DeleteStackRequest deleteStackRequest = new DeleteStackRequest();
    deleteStackRequest.setStackName(stackName);
    stackdestroy.deleteStack(deleteStackRequest);
    logger.info(StringUtil.concatStrings("Stack : ", stackName, " is handed over for deletion!"));

    //Notify AWSResourceManager about stack destruction to release acquired resources
    AWSResourceManager awsResourceManager = new AWSResourceManager();
    awsResourceManager.notifyStackDeletion(testPlan, script, region);

    boolean waitForStackDeletion = Boolean
            .parseBoolean(getProperty(ConfigurationProperties.WAIT_FOR_STACK_DELETION));
    if (waitForStackDeletion) {
        logger.info(StringUtil.concatStrings("Waiting for stack : ", stackName, " to delete.."));
        Waiter<DescribeStacksRequest> describeStacksRequestWaiter = new
                AmazonCloudFormationWaiters(stackdestroy).stackDeleteComplete();
        try {
            describeStacksRequestWaiter.run(new WaiterParameters<>(new DescribeStacksRequest()
                    .withStackName(stackName)));
        } catch (WaiterUnrecoverableException e) {

            throw new TestGridInfrastructureException("Error occurred while waiting for Stack :"
                                                      + stackName + " deletion !");
        }
    }
    return true;
}
 
Example 2
Source File: CloudFormationManager.java    From AWS-MIMIC-IIItoOMOP with Apache License 2.0 5 votes vote down vote up
public void terminateStack(String name) 
{
    DeleteStackRequest deleteStackRequest = new DeleteStackRequest();
    
    deleteStackRequest.setStackName(name);
    
    cf.deleteStack(deleteStackRequest);
}
 
Example 3
Source File: AwsNetworkConnector.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public void deleteNetworkWithSubnets(NetworkDeletionRequest networkDeletionRequest) {
    if (!networkDeletionRequest.isExisting()) {
        AwsCredentialView credentialView = new AwsCredentialView(networkDeletionRequest.getCloudCredential());
        AmazonCloudFormationRetryClient cloudFormationRetryClient = getCloudFormationRetryClient(credentialView, networkDeletionRequest.getRegion());
        DeleteStackRequest deleteStackRequest = new DeleteStackRequest();
        deleteStackRequest.setStackName(networkDeletionRequest.getStackName());
        cloudFormationRetryClient.deleteStack(deleteStackRequest);
        AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, networkDeletionRequest.getRegion());
        Waiter<DescribeStacksRequest> deletionWaiter = cfClient.waiters().stackDeleteComplete();
        LOGGER.debug("CloudFormation stack deletion request sent with stack name: '{}' ", networkDeletionRequest.getStackName());
        DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(networkDeletionRequest.getStackName());
        run(deletionWaiter, describeStacksRequest, null);
    }
}
 
Example 4
Source File: CloudFormationClient.java    From herd-mdl with Apache License 2.0 4 votes vote down vote up
/**
 * Delete the stack {@link #stackName}
 */
public void deleteStack() throws Exception {

    CFTStackInfo cftStackInfo = getStackInfo();
    String rootStackId = cftStackInfo.stackId(); // Use the stack id to track the delete operation
    LOGGER.info("rootStackId   =   " + rootStackId);

    // Go through the stack and pick up resources that we want
    // to finalize before deleting the stack.
    List<String> s3BucketIds = new ArrayList<>();

    DescribeStacksResult describeStacksResult = amazonCloudFormation.describeStacks();
    for (Stack currentStack : describeStacksResult.getStacks()) {
        if (rootStackId.equals(currentStack.getRootId()) || rootStackId
                .equals(currentStack.getStackId())) {
            LOGGER.info("stackId   =   " + currentStack.getStackId());
            DescribeStackResourcesRequest describeStackResourcesRequest = new DescribeStackResourcesRequest();
            describeStackResourcesRequest.setStackName(currentStack.getStackName());
            List<StackResource> stackResources = amazonCloudFormation
                    .describeStackResources(describeStackResourcesRequest).getStackResources();
            for (StackResource stackResource : stackResources) {
                if (!stackResource.getResourceStatus()
                        .equals(ResourceStatus.DELETE_COMPLETE.toString())) {
                    if (stackResource.getResourceType().equals("AWS::S3::Bucket")) {
                        s3BucketIds.add(stackResource.getPhysicalResourceId());
                    }
                }
            }
        }
    }

    // Now empty S3 buckets, clean up will be done when the stack is deleted
    AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard().withRegion(Regions.getCurrentRegion().getName())
            .withCredentials(new InstanceProfileCredentialsProvider(true)).build();
    for (String s3BucketPhysicalId : s3BucketIds) {
        String s3BucketName = s3BucketPhysicalId;
        if(!amazonS3.doesBucketExistV2(s3BucketName)){
            break;
        }
        LOGGER.info("Empyting S3 bucket, " + s3BucketName);
        ObjectListing objectListing = amazonS3.listObjects(s3BucketName);
        while (true) {
            for (Iterator<?> iterator = objectListing.getObjectSummaries().iterator(); iterator
                    .hasNext(); ) {
                S3ObjectSummary summary = (S3ObjectSummary) iterator.next();
                amazonS3.deleteObject(s3BucketName, summary.getKey());
            }
            if (objectListing.isTruncated()) {
                objectListing = amazonS3.listNextBatchOfObjects(objectListing);
            }
            else {
                break;
            }
        }
    }

    //Proceed with the regular stack deletion operation
    DeleteStackRequest deleteRequest = new DeleteStackRequest();
    deleteRequest.setStackName(stackName);
    amazonCloudFormation.deleteStack(deleteRequest);
    LOGGER.info("Stack deletion initiated");

    CFTStackStatus cftStackStatus = waitForCompletionAndGetStackStatus(amazonCloudFormation,
            rootStackId);
    LOGGER.info(
            "Stack deletion completed, the stack " + stackName + " completed with " + cftStackStatus);

    // Throw exception if failed
    if (!cftStackStatus.getStackStatus().equals(StackStatus.DELETE_COMPLETE.toString())) {
        throw new Exception(
                "deleteStack operation failed for stack " + stackName + " - " + cftStackStatus);
    }
}