fix(cloudformation): use bus|rule as the physical id of an Events rule on a custom bus (#8864)

This commit is contained in:
glorfindel33 authored and GitHub committed 2026-11-18 13:58:05 -04:00
1 parent 2ac4554ea8
commit 9e65c12f8c
4 files changed
+228 -17

No files matched your search

@@ -0,0 +1,66 @@
package io.floci.aws.services.cloudformation;
import io.floci.aws.testing.CfnStackWaits;
import io.floci.aws.testing.CfnTestStack;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
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.eventbridge.EventBridgeClient;
import software.amazon.awssdk.services.eventbridge.model.ResourceNotFoundException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
/** The physical id and {@code Ref} of an {@code AWS::Events::Rule} through the AWS SDK. */
@QuarkusTest
class CloudFormationEventsRulePhysicalIdIntegrationTest {
@RegisterExtension
final CfnTestStack stack = new CfnTestStack("cfn-rule-id");
@Test
void aRuleOnACustomBusIsReportedAsTheBusAndTheRuleNameAndTheStackDeleteRemovesIt() {
String bus = stack.name() + "-bus";
Stack created = stack.create("""
{"Resources": {
"Bus": {"Type": "AWS::Events::EventBus", "Properties": {"Name": "%s"}},
"Queue": {"Type": "AWS::SQS::Queue"},
"Rule": {"Type": "AWS::Events::Rule",
"Properties": {"EventBusName": {"Ref": "Bus"}, "EventPattern": {"source": ["my.source"]},
"Targets": [{"Id": "q", "Arn": {"Fn::GetAtt": ["Queue", "Arn"]}}]}},
"DefaultBusRule": {"Type": "AWS::Events::Rule",
"Properties": {"EventPattern": {"source": ["my.source"]}}}},
"Outputs": {"RuleRef": {"Value": {"Ref": "Rule"}},
"RuleName": {"Value": {"Fn::GetAtt": ["Rule", "RuleName"]}},
"NameFromRef": {"Value": {"Fn::Select": [1, {"Fn::Split": ["|", {"Ref": "Rule"}]}]}},
"BusFromRef": {"Value": {"Fn::Select": [0, {"Fn::Split": ["|", {"Ref": "Rule"}]}]}}}}"""
.formatted(bus));
String ruleName = CfnStackWaits.output(created, "RuleName");
assertEquals(bus + "|" + ruleName, CfnStackWaits.output(created, "RuleRef"));
assertEquals(bus + "|" + ruleName, physicalId("Rule"));
assertEquals(ruleName, CfnStackWaits.output(created, "NameFromRef"));
assertEquals(bus, CfnStackWaits.output(created, "BusFromRef"));
assertFalse(physicalId("DefaultBusRule").contains("|"));
EventBridgeClient events = stack.client(EventBridgeClient.builder());
assertEquals(ruleName, events.describeRule(r -> r.name(ruleName).eventBusName(bus)).name());
CfnStackWaits.deleteStack(stack.client(CloudFormationClient.builder()), stack.name());
assertThrows(ResourceNotFoundException.class,
() -> events.describeRule(r -> r.name(ruleName).eventBusName(bus)));
assertThrows(ResourceNotFoundException.class, () -> events.describeEventBus(r -> r.name(bus)));
}
private String physicalId(String logicalId) {
return stack.client(CloudFormationClient.builder()).describeStackResources(r -> r.stackName(stack.name()))
.stackResources().stream()
.filter(resource -> logicalId.equals(resource.logicalResourceId()))
.map(StackResource::physicalResourceId)
.findFirst().orElseThrow();
}
}
@@ -16,6 +16,7 @@ import io.floci.aws.services.eventbridge.model.Target;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
@@ -128,6 +129,81 @@ class EventsCfnProvisionerTest {
assertEquals("orders", r.getAttributes().get("RuleName"));
}
// ── the physical id names a custom bus the way CloudFormation's does ─────
@ParameterizedTest(name = "{0}")
@CsvSource(nullValues = "-", value = {
"no bus, -, orders",
"a blank bus, ' ', orders",
"the default bus by name, default, orders",
"the default bus by ARN, arn:aws:events:us-east-1:000000000000:event-bus/default, orders",
"a custom bus by name, orders-bus, orders-bus|orders",
"a custom bus by ARN, arn:aws:events:us-east-1:000000000000:event-bus/orders-bus, orders-bus|orders",
"a custom bus by ARN in another partition,"
+ " arn:aws-us-gov:events:us-gov-west-1:000000000000:event-bus/orders-bus, orders-bus|orders",
"a bus of another account, arn:aws:events:us-east-1:111122223333:event-bus/orders-bus,"
+ " arn:aws:events:us-east-1:111122223333:event-bus/orders-bus|orders",
"a bus of another account in another partition,"
+ " arn:aws-us-gov:events:us-gov-west-1:111122223333:event-bus/orders-bus,"
+ " arn:aws-us-gov:events:us-gov-west-1:111122223333:event-bus/orders-bus|orders"})
void aRulesPhysicalIdNamesItsBusUnlessItIsTheDefaultBus(String label, String bus, String physicalId) {
stubPutRule();
StackResource r = resource("AWS::Events::Rule", "Rule");
ObjectNode props = mapper.createObjectNode().put("Name", "orders");
if (bus != null) {
props.put("EventBusName", bus);
}
provisioner.provision(r, props, ctx(null));
assertEquals(physicalId, r.getPhysicalId());
}
@Test
void anUnnamedRuleOnACustomBusKeepsItsNameAcrossUpdates() {
stubPutRule();
StackResource r = resource("AWS::Events::Rule", "Rule");
provisioner.provision(r, mapper.createObjectNode().put("EventBusName", "orders-bus"),
ctx("orders-bus|my-stack-Rule-ab12cd"));
assertEquals("orders-bus|my-stack-Rule-ab12cd", r.getPhysicalId());
verify(events).putRule(eq("my-stack-Rule-ab12cd"), eq("orders-bus"), any(), any(), any(), any(), any(),
any(), anyString());
}
@ParameterizedTest(name = "{0}")
@CsvSource(nullValues = "-", value = {
"a custom bus in the id, orders-bus|orders, -, orders-bus",
"a bus ARN in the id, arn:aws:events:us-east-1:111122223333:event-bus/orders-bus|orders, -,"
+ " arn:aws:events:us-east-1:111122223333:event-bus/orders-bus",
"a bare id and the stored bus, orders, orders-bus, orders-bus"})
void deletingARuleTakesItsBusFromTheIdOrFromTheBusItStored(String label, String physicalId, String storedBus,
String bus) {
when(events.ruleExists("orders", bus, REGION)).thenReturn(true);
when(events.listTargetsByRule("orders", bus, REGION)).thenReturn(List.of(storedTarget("t1")));
StackResource r = resource("AWS::Events::Rule", "Rule");
r.setPhysicalId(physicalId);
if (storedBus != null) {
r.getAttributes().put("EventBusName", storedBus);
}
provisioner.delete(r, REGION);
verify(events).removeTargets("orders", bus, List.of("t1"), REGION);
verify(events).deleteRule("orders", bus, REGION);
}
@Test
void deletingByTheIdAloneFindsTheCustomBusInTheId() {
when(events.ruleExists("orders", "orders-bus", REGION)).thenReturn(true);
when(events.listTargetsByRule("orders", "orders-bus", REGION)).thenReturn(List.of());
provisioner.delete("AWS::Events::Rule", "orders-bus|orders", REGION);
verify(events).deleteRule("orders", "orders-bus", REGION);
}
// ── targets are driven to the template's desired state ───────────────────
@Test