Files
floci/src
Hector Ventura 3218d2404e refactor(cloudformation): move the Events types into EventsCfnProvisioner (#3286)
* refactor(cloudformation): move the Events types into a per-service provisioner

AWS::Events::Rule, EventBus and EventBusPolicy move out of the legacy monolith
into EventsCfnProvisioner, continuing the decomposition. The block was already
contiguous and self-contained: 18 methods and five constants, none of them
referenced from outside, and EventBridgeService was the monolith's only user, so
the field, its constructor parameter and both imports go too.

Behaviour preserving. The event bus arm in particular already carried real logic
worth keeping intact: rollback ownership, the created-time check that refuses to
delete a bus recreated out of band, managed tag and policy tracking, and the
rejection of an unsupported mutable update. All of it moves unchanged.

Rule and EventBus were listed in DELETE_NEEDS_STACK_RESOURCE because deleting
them needs stored attributes rather than the physical id alone: a rule remembers
the custom bus it lives on, and a bus remembers the creation time proving the
stack still owns it. That logic moves into the provisioner's
delete(StackResource, String) override and both entries leave the set, which is
what CfnDeletePrecedenceTest gates. EventBusPolicy keeps the id-only form, its
composite busName|statementId id being enough.

The four existing Events test classes needed no changes: they build the
provisioner through CfnProvisionerFixture, so wiring the new provisioner there is
all it took for them to exercise it. 40 tests across them still pass, and the
schema-coverage gate finds no new attribute gaps.

Monolith 6,457 to 5,954 lines, legacy types 47 to 44.

* fix(cloudformation): keep an Events rule's identity and targets across an update

provision runs again on every UpdateStack, and the rule arm got both halves of
that wrong.

An unnamed rule generated a fresh physical name each time, so the second update
created a second rule and left the first behind, still matching events and still
delivering to its targets. Name is createOnly and a rule's physical id is its
name, so ctx.stablePhysicalName applies directly here, unlike the Batch types
whose physical id is an ARN and which need the name read from a stored attribute.

Targets were never driven to the template's desired state. PutTargets upserts by
id and removes nothing, so a target dropped from the template stayed on the rule
and kept receiving events indefinitely. The desired set is now reconciled after
the put, which also covers a template that declares no Targets at all: that
means no targets, so the sweep runs either way. The Targets property is checked
for being an array before it is iterated.

Five unit tests: an unnamed rule keeping its name, an explicit name still
winning, a dropped target being removed, a cleared Targets list removing all of
them, and an unchanged set removing nothing.

* fix(cloudformation): surface a failed Events policy removal instead of hiding it

Deleting an AWS::Events::EventBusPolicy wrapped the whole removal in a bare
catch (Exception) that logged at debug and returned. Every failure was hidden:
the statement stayed on the bus while the stack reported DELETE_COMPLETE, which
is the outcome AGENTS.md rules out for deletes, since a real failure has to
reach the stack as DELETE_FAILED.

RemovePermission distinguishes its failures, unlike the Batch services where a
single ClientException covers everything. It raises ResourceNotFoundException
both when the bus is gone and when the statement is not on it, and those are the
two cases that genuinely mean "already deleted". So the removal now goes through
the shared CfnDeletes.safeDelete tolerating only that code, and anything else,
a malformed statement id for instance, propagates.

Two unit tests: an already-gone statement counting as deleted, and a
ValidationException reaching the caller rather than being swallowed.

* fix(cloudformation): drop the em dash from the moved Events javadoc

The javadoc came across from CloudFormationResourceProvisioner verbatim. That file holds
30 em dashes but is an existing file, and the doc-style gate only scans the files a PR
adds, so moving the prose into a new file is what surfaced a violation that was always
there.

* fix(cloudformation): read the prior event bus id from the provision context

provision serves create and update, and it assigns the new physical id as it runs, so a
resource-derived identity check can flip mid-method. The moved code read the prior bus
name off the resource. It was captured before the assignment so the behaviour was right,
but the convention exists precisely so the next edit cannot quietly break it.

* fix(cloudformation): validate the Events policy StatementId

The registry schema for AWS::Events::EventBusPolicy and the botocore model agree that
StatementId is [a-zA-Z0-9-_]+, 1 to 64 characters, but nothing checked it, so an
AWS-invalid value was accepted.

It also made the physical id ambiguous. This provisioner keys a policy statement as
"<bus name>|<statement id>" and the delete splits on the last separator. Neither the
modeled bus-name pattern nor the statement-id one admits a pipe, so holding the value to
the model closes that at source rather than by guessing at the split.

* fix(cloudformation): restore rule targets when a stack update rolls back

The reconciliation sweep this branch added can delete a live target, which nothing could
put back: without a rollbackUpdate hook the resource fell to "Rollback is not implemented
for AWS::Events::Rule", so a failed stack update left the rule no longer delivering to it
and the stack reported UPDATE_ROLLBACK_FAILED. Before the sweep, targets were only ever
upserted and there was nothing to restore.

Snapshot the rule's targets before the first target call of an update and put them back on
failure, following PipesCfnProvisioner: the snapshot is spent by whichever end the update
reaches, restoring inside provision when this rule's own reconciliation failed and through
rollbackUpdate when a later resource did. The restore is exact rather than additive, so a
target the failed update added does not survive it. A restore that fails itself is recorded
on the resource instead of thrown, so the stack reports the original failure.

The snapshot carries the rule name, its bus and the region because the rollback hook is
handed the stack resource alone, and a rule on a custom bus is keyed by that bus.

This restores targets, not the rule's own properties, which putRule overwrites on the same
path. That gap predates this branch.

* refactor(cloudformation): hold the Events provisioner to a single injected service

AGENTS.md requires a CloudFormation provisioner to inject only the service it wraps. This
one also took an ObjectMapper, carried over from PipesCfnProvisioner when its rollback
snapshot was modelled on that class.

The package already has a second convention for exactly this job, and it is the one that
satisfies the rule: LambdaEventInvokeConfigCfnProvisioner, the other snapshot-based rollback
provisioner, holds a private static mapper and injects only its service, as do
LambdaEventSourceMappingCfnProvisioner, Ec2NetworkCfnProvisioner and ReplacementCleanup.

The configured mapper adds a 100 MB string limit and lenient empty-bean serialization.
Neither reaches this code: EventBridge caps a bus policy near 10 KB and a target's Input at
8192 characters, both far under Jackson's default, and every type serialized here has real
properties. No behaviour changes, so the existing tests are what prove the swap is inert.
2026-09-09 09:10:22 -04:00
..