com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult Java Examples

The following examples show how to use com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult. 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 vote down vote up
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 vote down vote up
@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: OutputsUtil.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
public static String getStackResourcesToJson(DescribeStackResourcesResult describeStackResources) throws IOException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final ObjectMapper mapper = new ObjectMapper();
    mapper.writeValue(out, describeStackResources.getStackResources());
    final byte[] stackResources = out.toByteArray();

    return new String(stackResources);
}
 
Example #4
Source File: AutoDetectingStackNameProvider.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
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 #5
Source File: TestStackEnvironment.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
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 #6
Source File: AutoScalingGroupHandler.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
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 #7
Source File: AutoScalingGroupHandlerTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@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 #8
Source File: AmazonServiceCatalogService.java    From cs-actions with Apache License 2.0 4 votes vote down vote up
public static DescribeStackResourcesResult describeStackResourcesResult(final String stackName, final AmazonCloudFormation cloudFormationClient) {
    DescribeStackResourcesRequest stackResourceRequest = new DescribeStackResourcesRequest()
            .withStackName(stackName);
    return cloudFormationClient.describeStackResources(stackResourceRequest);
}