com.amazonaws.services.cloudformation.model.DescribeStackResourcesRequest Java Examples
The following examples show how to use
com.amazonaws.services.cloudformation.model.DescribeStackResourcesRequest.
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: LambdaVersionCleanupStep.java From pipeline-aws-plugin with Apache License 2.0 | 5 votes |
private void deleteAllStackFunctionVersions(AWSLambda client, String stackName) throws Exception { TaskListener listener = Execution.this.getContext().get(TaskListener.class); listener.getLogger().format("Deleting old versions from stackName=%s%n", stackName); AmazonCloudFormation cloudformation = AWSClientFactory.create(AmazonCloudFormationClientBuilder.standard(), this.getContext()); DescribeStackResourcesResult result = cloudformation.describeStackResources(new DescribeStackResourcesRequest() .withStackName(stackName) ); for (StackResource stackResource : result.getStackResources()) { if ("AWS::Lambda::Function".equals(stackResource.getResourceType())) { deleteAllVersions(client, stackResource.getPhysicalResourceId()); } } }
Example #2
Source File: LambdaVersionCleanupStepTest.java From pipeline-aws-plugin with Apache License 2.0 | 5 votes |
@Test public void deleteCloudFormationStack() throws Exception { WorkflowJob job = this.jenkinsRule.jenkins.createProject(WorkflowJob.class, "cfnTest"); Mockito.when(this.awsLambda.listAliases(Mockito.eq(new ListAliasesRequest().withFunctionName("foo")))).thenReturn(new ListAliasesResult()); Mockito.when(this.awsLambda.listVersionsByFunction(Mockito.eq(new ListVersionsByFunctionRequest().withFunctionName("foo")))).thenReturn(new ListVersionsByFunctionResult() .withVersions(Arrays.asList( new FunctionConfiguration().withVersion("v1").withLastModified(ZonedDateTime.now().format(DateTimeFormatter.ISO_ZONED_DATE_TIME)), new FunctionConfiguration().withVersion("v2").withLastModified("2018-02-05T11:15:12Z") )) ); Mockito.when(this.cloudformation.describeStackResources(new DescribeStackResourcesRequest().withStackName("baz"))).thenReturn(new DescribeStackResourcesResult().withStackResources( new StackResource() .withResourceType("AWS::Lambda::Function") .withPhysicalResourceId("foo"), new StackResource() .withResourceType("AWS::Baz::Function") .withPhysicalResourceId("bar") ) ); job.setDefinition(new CpsFlowDefinition("" + "node {\n" + " lambdaVersionCleanup(stackName: 'baz', daysAgo: 5)\n" + "}\n", true) ); this.jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0)); Mockito.verify(this.awsLambda).deleteFunction(new DeleteFunctionRequest() .withQualifier("v2") .withFunctionName("foo") ); Mockito.verify(this.awsLambda).listVersionsByFunction(Mockito.any()); Mockito.verify(this.awsLambda).listAliases(Mockito.any()); Mockito.verifyNoMoreInteractions(this.awsLambda); }
Example #3
Source File: AutoDetectingStackNameProvider.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
private String autoDetectStackName(String instanceId) { Assert.notNull(instanceId, "No valid instance id defined"); DescribeStackResourcesResult describeStackResourcesResult = this.amazonCloudFormationClient .describeStackResources(new DescribeStackResourcesRequest() .withPhysicalResourceId(instanceId)); if (describeStackResourcesResult != null && describeStackResourcesResult.getStackResources() != null && !describeStackResourcesResult.getStackResources().isEmpty()) { return describeStackResourcesResult.getStackResources().get(0).getStackName(); } if (this.amazonEc2Client != null) { DescribeTagsResult describeTagsResult = this.amazonEc2Client .describeTags(new DescribeTagsRequest().withFilters( new Filter("resource-id", Collections.singletonList(instanceId)), new Filter("resource-type", Collections.singletonList("instance")), new Filter("key", Collections .singletonList("aws:cloudformation:stack-name")))); if (describeTagsResult != null && describeTagsResult.getTags() != null && !describeTagsResult.getTags().isEmpty()) { return describeTagsResult.getTags().get(0).getValue(); } } return null; }
Example #4
Source File: TestStackEnvironment.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
private DescribeStackResourcesResult getStackResources(String stackName) throws InterruptedException, IOException { try { DescribeStacksResult describeStacksResult = this.amazonCloudFormationClient .describeStacks(new DescribeStacksRequest().withStackName(stackName)); for (Stack stack : describeStacksResult.getStacks()) { if (isAvailable(stack)) { return this.amazonCloudFormationClient .describeStackResources(new DescribeStackResourcesRequest() .withStackName(stack.getStackName())); } if (isError(stack)) { if (this.stackCreatedByThisInstance) { throw new IllegalArgumentException("Could not create stack"); } this.amazonCloudFormationClient.deleteStack( new DeleteStackRequest().withStackName(stack.getStackName())); return getStackResources(stackName); } if (isInProgress(stack)) { // noinspection BusyWait Thread.sleep(5000L); return getStackResources(stackName); } } } catch (AmazonClientException e) { String templateBody = FileCopyUtils.copyToString(new InputStreamReader( new ClassPathResource(TEMPLATE_PATH).getInputStream())); this.amazonCloudFormationClient.createStack(new CreateStackRequest() .withTemplateBody(templateBody).withOnFailure(OnFailure.DELETE) .withStackName(stackName) .withTags(new Tag().withKey("tag1").withValue("value1")) .withParameters(new Parameter().withParameterKey("RdsPassword") .withParameterValue(this.rdsPassword))); this.stackCreatedByThisInstance = true; } return getStackResources(stackName); }
Example #5
Source File: AutoScalingGroupHandler.java From cloudbreak with Apache License 2.0 | 5 votes |
public Map<AutoScalingGroup, String> getAutoScalingGroups(AmazonCloudFormationClient cloudFormationClient, AmazonAutoScalingClient autoScalingClient, CloudResource cfResource) { DescribeStackResourcesRequest resourcesRequest = new DescribeStackResourcesRequest(); resourcesRequest.setStackName(cfResource.getName()); DescribeStackResourcesResult resourcesResult = cloudFormationClient.describeStackResources(resourcesRequest); Map<String, String> autoScalingGroups = resourcesResult.getStackResources().stream() .filter(stackResource -> "AWS::AutoScaling::AutoScalingGroup".equalsIgnoreCase(stackResource.getResourceType())) .collect(Collectors.toMap(StackResource::getPhysicalResourceId, StackResource::getLogicalResourceId)); DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest(); request.setAutoScalingGroupNames(autoScalingGroups.keySet()); List<AutoScalingGroup> scalingGroups = autoScalingClient.describeAutoScalingGroups(request).getAutoScalingGroups(); return scalingGroups.stream() .collect(Collectors.toMap(scalingGroup -> scalingGroup, scalingGroup -> autoScalingGroups.get(scalingGroup.getAutoScalingGroupName()))); }
Example #6
Source File: AutoScalingGroupHandlerTest.java From cloudbreak with Apache License 2.0 | 5 votes |
@Test public void testGetAutoScalingGroups() { CloudResource cfResource = CloudResource.builder() .type(ResourceType.CLOUDFORMATION_STACK) .name("cf") .build(); DescribeStackResourcesResult resourcesResult = new DescribeStackResourcesResult(); StackResource stackResource = new StackResource() .withLogicalResourceId("logicalResourceId") .withPhysicalResourceId("physicalResourceId") .withResourceType("AWS::AutoScaling::AutoScalingGroup"); resourcesResult.getStackResources().add(stackResource); resourcesResult.getStackResources().add(new StackResource().withResourceType("other")); when(cloudFormationClient.describeStackResources(any(DescribeStackResourcesRequest.class))).thenReturn(resourcesResult); DescribeAutoScalingGroupsResult scalingGroupsResult = new DescribeAutoScalingGroupsResult(); AutoScalingGroup autoScalingGroup = new AutoScalingGroup().withAutoScalingGroupName(stackResource.getPhysicalResourceId()); scalingGroupsResult.getAutoScalingGroups().add(autoScalingGroup); when(autoScalingClient.describeAutoScalingGroups(any(DescribeAutoScalingGroupsRequest.class))).thenReturn(scalingGroupsResult); Map<AutoScalingGroup, String> autoScalingGroups = underTest.getAutoScalingGroups(cloudFormationClient, autoScalingClient, cfResource); assertEquals(1, autoScalingGroups.size()); assertEquals(autoScalingGroup, autoScalingGroups.entrySet().stream().findFirst().get().getKey()); assertEquals(stackResource.getLogicalResourceId(), autoScalingGroups.entrySet().stream().findFirst().get().getValue()); ArgumentCaptor<DescribeStackResourcesRequest> stackResourcesRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeStackResourcesRequest.class); verify(cloudFormationClient).describeStackResources(stackResourcesRequestArgumentCaptor.capture()); assertEquals(cfResource.getName(), stackResourcesRequestArgumentCaptor.getValue().getStackName()); ArgumentCaptor<DescribeAutoScalingGroupsRequest> scalingGroupsRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class); verify(autoScalingClient).describeAutoScalingGroups(scalingGroupsRequestArgumentCaptor.capture()); assertEquals(1, scalingGroupsRequestArgumentCaptor.getValue().getAutoScalingGroupNames().size()); assertEquals(stackResource.getPhysicalResourceId(), scalingGroupsRequestArgumentCaptor.getValue().getAutoScalingGroupNames().get(0)); }
Example #7
Source File: CloudFormationClient.java From herd-mdl with Apache License 2.0 | 4 votes |
/** * 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); } }
Example #8
Source File: GetStackDetailsAction.java From cs-actions with Apache License 2.0 | 4 votes |
/** * Get AWS Cloud Formation Stack Details * * @param identity Access key associated with your Amazon AWS or IAM account. * Example: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" * @param credential Secret access key ID associated with your Amazon AWS or IAM account. * @param proxyHost Optional - proxy server used to connect to Amazon API. If empty no proxy will be used. * Default: "" * @param proxyPort Optional - proxy server port. You must either specify values for both proxyHost and * proxyPort inputs or leave them both empty. * Default: "" * @param proxyUsername Optional - proxy server user name. * Default: "" * @param proxyPassword Optional - proxy server password associated with the proxyUsername input value. * Default: "" * @param region AWS region name * Example: "eu-central-1" * @param stackName CloudFormation stack name * Example: "mystack" * @return A map with strings as keys and strings as values that contains: outcome of the action, returnCode of the * operation, or failure message and the exception if there is one */ @Action(name = "Get AWS Cloud Formation Stack Details", outputs = { @Output(Outputs.RETURN_CODE), @Output(Outputs.RETURN_RESULT), @Output(Outputs.STACK_NAME_RESULT), @Output(Outputs.STACK_ID_RESULT), @Output(Outputs.STACK_STATUS_RESULT), @Output(Outputs.STACK_STATUS_RESULT_REASON), @Output(Outputs.STACK_CREATION_TIME_RESULT), @Output(Outputs.STACK_DESCRIPTION_RESULT), @Output(Outputs.STACK_OUTPUTS_RESULT), @Output(Outputs.STACK_RESOURCES_RESULT), @Output(Outputs.EXCEPTION) }, responses = { @Response(text = Outputs.SUCCESS, field = Outputs.RETURN_CODE, value = Outputs.SUCCESS_RETURN_CODE, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.RESOLVED), @Response(text = Outputs.FAILURE, field = Outputs.RETURN_CODE, value = Outputs.FAILURE_RETURN_CODE, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.ERROR) } ) public Map<String, String> execute( @Param(value = IDENTITY, required = true) String identity, @Param(value = CREDENTIAL, required = true, encrypted = true) String credential, @Param(value = REGION, required = true) String region, @Param(value = PROXY_HOST) String proxyHost, @Param(value = PROXY_PORT) String proxyPort, @Param(value = PROXY_USERNAME) String proxyUsername, @Param(value = PROXY_PASSWORD, encrypted = true) String proxyPassword, @Param(value = CONNECT_TIMEOUT) String connectTimeoutMs, @Param(value = EXECUTION_TIMEOUT) String execTimeoutMs, @Param(value = STACK_NAME, required = true) String stackName) { proxyPort = defaultIfEmpty(proxyPort, DefaultValues.PROXY_PORT); connectTimeoutMs = defaultIfEmpty(connectTimeoutMs, DefaultValues.CONNECT_TIMEOUT); execTimeoutMs = defaultIfEmpty(execTimeoutMs, DefaultValues.EXEC_TIMEOUT); AmazonCloudFormation stackBuilder = CloudFormationClientBuilder.getCloudFormationClient(identity, credential, proxyHost, proxyPort, proxyUsername, proxyPassword, connectTimeoutMs, execTimeoutMs, region); final Map<String, String> results = new HashMap(); try { final DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest(); describeStacksRequest.withStackName(stackName); final DescribeStackResourcesRequest stackResourceRequest = new DescribeStackResourcesRequest(); for (Stack stack : stackBuilder.describeStacks(describeStacksRequest).getStacks()) { results.put(Outputs.RETURN_RESULT, stack.getStackName() + "[" + stack.getStackStatus() + "]" ); results.put(Outputs.STACK_NAME_RESULT, stack.getStackName()); results.put(Outputs.STACK_ID_RESULT,stack.getStackId()); results.put(Outputs.STACK_STATUS_RESULT,stack.getStackStatus()); results.put(Outputs.STACK_STATUS_RESULT_REASON,stack.getStackStatusReason()); results.put(Outputs.STACK_CREATION_TIME_RESULT,stack.getCreationTime().toString()); results.put(Outputs.STACK_DESCRIPTION_RESULT,stack.getDescription()); results.put(Outputs.STACK_OUTPUTS_RESULT,stack.getOutputs().toString()); stackResourceRequest.setStackName(stack.getStackName()); results.put(Outputs.STACK_RESOURCES_RESULT,stackBuilder.describeStackResources(stackResourceRequest).getStackResources().toString()); results.put(Outputs.RETURN_CODE, Outputs.SUCCESS_RETURN_CODE); results.put(Outputs.EXCEPTION, StringUtils.EMPTY); } } catch (Exception e) { results.put(Outputs.RETURN_RESULT, e.getMessage()); results.put(Outputs.STACK_ID_RESULT, StringUtils.EMPTY); results.put(Outputs.STACK_STATUS_RESULT,StringUtils.EMPTY); results.put(Outputs.STACK_STATUS_RESULT_REASON,StringUtils.EMPTY); results.put(Outputs.STACK_CREATION_TIME_RESULT,StringUtils.EMPTY); results.put(Outputs.STACK_DESCRIPTION_RESULT,StringUtils.EMPTY); results.put(Outputs.STACK_OUTPUTS_RESULT,StringUtils.EMPTY); results.put(Outputs.STACK_RESOURCES_RESULT,StringUtils.EMPTY); results.put(Outputs.RETURN_CODE, Outputs.FAILURE_RETURN_CODE); results.put(Outputs.EXCEPTION, e.getStackTrace().toString()); } return results; }
Example #9
Source File: AmazonServiceCatalogService.java From cs-actions with Apache License 2.0 | 4 votes |
public static DescribeStackResourcesResult describeStackResourcesResult(final String stackName, final AmazonCloudFormation cloudFormationClient) { DescribeStackResourcesRequest stackResourceRequest = new DescribeStackResourcesRequest() .withStackName(stackName); return cloudFormationClient.describeStackResources(stackResourceRequest); }