From dd6835a9a8ecdbeeeef67d9a8cb0f00fa51e1ef7 Mon Sep 17 00:00:00 2001 From: Niels Kootstra <545768+nkootstra@users.noreply.github.com> Date: Fri, 27 Nov 2026 12:02:51 +0100 Subject: [PATCH] fix(cloudformation): return StackId from DescribeStackResources and DescribeStackResource (#9553) --- .../CloudFormationQueryHandler.java | 20 ++-- .../cloudformation/CloudFormationService.java | 8 +- ...tackResourceStackIdSdkIntegrationTest.java | 92 +++++++++++++++++++ 3 files changed, 108 insertions(+), 12 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/cloudformation/CloudFormationStackResourceStackIdSdkIntegrationTest.java diff --git a/src/main/java/io/floci/aws/services/cloudformation/CloudFormationQueryHandler.java b/src/main/java/io/floci/aws/services/cloudformation/CloudFormationQueryHandler.java index 9910ccf42f..325d672bf0 100644 --- a/src/main/java/io/floci/aws/services/cloudformation/CloudFormationQueryHandler.java +++ b/src/main/java/io/floci/aws/services/cloudformation/CloudFormationQueryHandler.java @@ -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 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 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()) diff --git a/src/main/java/io/floci/aws/services/cloudformation/CloudFormationService.java b/src/main/java/io/floci/aws/services/cloudformation/CloudFormationService.java index 05d0b892e9..18b2d2aa71 100644 --- a/src/main/java/io/floci/aws/services/cloudformation/CloudFormationService.java +++ b/src/main/java/io/floci/aws/services/cloudformation/CloudFormationService.java @@ -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 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 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); } /** diff --git a/src/test/java/io/floci/aws/services/cloudformation/CloudFormationStackResourceStackIdSdkIntegrationTest.java b/src/test/java/io/floci/aws/services/cloudformation/CloudFormationStackResourceStackIdSdkIntegrationTest.java new file mode 100644 index 0000000000..41f2d78ebf --- /dev/null +++ b/src/test/java/io/floci/aws/services/cloudformation/CloudFormationStackResourceStackIdSdkIntegrationTest.java @@ -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 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 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()); + } +}