fix(cloudformation): return StackId from DescribeStackResources and DescribeStackResource (#9553)

This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-11-27 07:02:51 -04:00
1 parent b8dcbeceb6
commit dd6835a9a8
3 files changed
+108 -12

No files matched your search

@@ -527,9 +527,9 @@ public class CloudFormationQueryHandler {
try {
CloudFormationService.StackResourcesView view = cfnService.describeStackResourcesView(stackName,
params.getFirst("LogicalResourceId"), params.getFirst("PhysicalResourceId"), region);
// The stack's own name, not what the caller passed: it may be an ARN, or absent
// entirely when the call named only a PhysicalResourceId.
return stackResourcesXml(view.resources(), view.stackName(), region);
// The view carries the stack's own name and id, not what the caller passed: that may
// be an ARN, or absent entirely when the call named only a PhysicalResourceId.
return stackResourcesXml(view);
} catch (AwsException e) {
return xmlError(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
}
@@ -565,8 +565,9 @@ public class CloudFormationQueryHandler {
String stackName = params.getFirst("StackName");
String logicalId = params.getFirst("LogicalResourceId");
try {
List<StackResource> resources = cfnService.describeStackResources(stackName, region);
StackResource res = resources.stream()
CloudFormationService.StackResourcesView view =
cfnService.describeStackResourcesView(stackName, null, null, region);
StackResource res = view.resources().stream()
.filter(r -> logicalId.equals(r.getLogicalId()))
.findFirst()
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
@@ -575,6 +576,8 @@ public class CloudFormationQueryHandler {
.start("DescribeStackResourceResponse", CF_NS)
.start("DescribeStackResourceResult")
.start("StackResourceDetail")
.elem("StackName", view.stackName())
.elem("StackId", view.stackId())
.elem("LogicalResourceId", res.getLogicalId())
.elem("PhysicalResourceId", res.getPhysicalId())
.elem("ResourceType", res.getResourceType())
@@ -800,14 +803,15 @@ public class CloudFormationQueryHandler {
return xml.build();
}
private Response stackResourcesXml(List<StackResource> resources, String stackName, String region) {
private Response stackResourcesXml(CloudFormationService.StackResourcesView view) {
XmlBuilder xml = new XmlBuilder()
.start("DescribeStackResourcesResponse", CF_NS)
.start("DescribeStackResourcesResult")
.start("StackResources");
for (StackResource r : resources) {
for (StackResource r : view.resources()) {
xml.start("member")
.elem("StackName", stackName)
.elem("StackName", view.stackName())
.elem("StackId", view.stackId())
.elem("LogicalResourceId", r.getLogicalId())
.elem("PhysicalResourceId", r.getPhysicalId())
.elem("ResourceType", r.getResourceType())
@@ -2732,9 +2732,9 @@ public class CloudFormationService implements ResourceProvider {
return describeStackResourcesView(stackName, logicalResourceId, physicalResourceId, region).resources();
}
/** The stack a call resolved to and the resources it answered with; the stack's own name is
* what AWS stamps on every member, and the caller may not have supplied it. */
public record StackResourcesView(String stackName, List<StackResource> resources) {
/** The stack a call resolved to and the resources it answered with; the stack's own name and
* id are what AWS stamps on every member, and the caller may not have supplied either. */
public record StackResourcesView(String stackName, String stackId, List<StackResource> resources) {
}
/** @see #describeStackResources(String, String, String, String) */
@@ -2764,7 +2764,7 @@ public class CloudFormationService implements ResourceProvider {
if (logicalResourceId != null && !logicalResourceId.isBlank()) {
resources.removeIf(r -> !logicalResourceId.equals(r.getLogicalId()));
}
return new StackResourcesView(stack.getStackName(), resources);
return new StackResourcesView(stack.getStackName(), stack.getStackId(), resources);
}
/**
@@ -0,0 +1,92 @@
package io.floci.aws.services.cloudformation;
import io.floci.aws.testing.AwsTestClients;
import io.floci.aws.testing.CfnStackWaits;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
import software.amazon.awssdk.services.cloudformation.model.Stack;
import software.amazon.awssdk.services.cloudformation.model.StackResource;
import software.amazon.awssdk.services.cloudformation.model.StackResourceDetail;
import java.util.List;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
/**
* {@code StackResource} and {@code StackResourceDetail} both carry the owning stack's
* {@code StackId} and {@code StackName} (issue #9509).
*/
@QuarkusTest
class CloudFormationStackResourceStackIdSdkIntegrationTest {
private static final String TEMPLATE = """
{"Resources": {"Q": {"Type": "AWS::SQS::Queue"}, "T": {"Type": "AWS::SNS::Topic"}}}
""";
private CloudFormationClient cfn;
private String stackName;
private String stackId;
@BeforeEach
void createStack() {
cfn = AwsTestClients.create(CloudFormationClient.builder(), "test", Region.US_EAST_1);
stackName = "cfn-srsid-" + UUID.randomUUID().toString().substring(0, 8);
Stack stack = CfnStackWaits.createStack(cfn, stackName, TEMPLATE, "CREATE_COMPLETE");
stackId = stack.stackId();
}
@AfterEach
void deleteStack() {
CfnStackWaits.deleteStack(cfn, stackName);
cfn.close();
}
@Test
void describeStackResourcesStampsTheStackIdOnEveryMember() {
List<StackResource> resources = cfn.describeStackResources(request -> request.stackName(stackName))
.stackResources();
assertEquals(2, resources.size());
for (StackResource resource : resources) {
assertEquals(stackId, resource.stackId());
assertEquals(stackName, resource.stackName());
}
}
@Test
void describeStackResourcesByStackIdAnswersWithTheStacksOwnName() {
List<StackResource> resources = cfn.describeStackResources(request -> request.stackName(stackId))
.stackResources();
assertFalse(resources.isEmpty());
for (StackResource resource : resources) {
assertEquals(stackId, resource.stackId());
assertEquals(stackName, resource.stackName());
}
}
@Test
void describeStackResourceReturnsTheStackIdAndName() {
StackResourceDetail detail = cfn.describeStackResource(request ->
request.stackName(stackName).logicalResourceId("Q")).stackResourceDetail();
assertEquals(stackId, detail.stackId());
assertEquals(stackName, detail.stackName());
assertEquals("Q", detail.logicalResourceId());
}
@Test
void describeStackResourceByStackIdAnswersWithTheStacksOwnName() {
StackResourceDetail detail = cfn.describeStackResource(request ->
request.stackName(stackId).logicalResourceId("T")).stackResourceDetail();
assertEquals(stackId, detail.stackId());
assertEquals(stackName, detail.stackName());
}
}