Files
floci/src/main/java/io
Hector Ventura 82eaaaeee7 refactor(cloudformation): move the Batch types into BatchCfnProvisioner (#3285)
* refactor(cloudformation): move the Batch types into a per-service provisioner

AWS::Batch::ComputeEnvironment, JobQueue and JobDefinition move out of the
legacy monolith into BatchCfnProvisioner, continuing the decomposition. The
three provisioning methods and the three helpers only they called
(computeEnvironmentOrder, containerProperties, retryStrategy) move across
unchanged; the generic property helpers they leaned on have other callers and
stay behind, re-expressed here against ProvisionContext, which already offers
resolveOptional, generatePhysicalName and resolveTags.

Behaviour preserving: same request shapes, same physical ids, same Fn::GetAtt
attributes. The defects these arms carry (nothing is deleted on stack delete,
updates re-create rather than update, Priority is defaulted although the schema
requires it) are left exactly as they were and are fixed in the commits that
follow, so this move stays reviewable on its own.

BatchService was the monolith's only remaining use of the field, so the field,
its constructor parameter and its import go too. Batch is absent from
DELETE_NEEDS_STACK_RESOURCE, so that set is untouched.

The inventory rows move from LEGACY_SWITCH to BatchCfnProvisioner and the test
fixture gains the inferred wiring; without the latter a fixture test naming
Batch would fall through to the stub arm and pass on a fake id. docs-sync
reports the generated table already current, and the schema-coverage gate finds
no new attribute gaps: each type's only top-level read-only property is its ARN,
which was already set.

Monolith 6,457 to 6,292 lines, legacy types 47 to 44.

* fix(cloudformation): delete Batch resources when their stack is deleted

The legacy switch had no AWS::Batch arm in any of its delete paths, so deleting
a stack left the compute environment, job queue and job definition behind. The
stack reported DELETE_COMPLETE over resources that were still there, and a
re-created stack then failed on the names it had supposedly released.

Batch refuses to remove a running entity, as AWS does: DeleteJobQueue rejects an
ENABLED queue, and DeleteComputeEnvironment rejects one that is not DISABLED or
is still referenced by a queue. So each delete disables first and then removes,
which is what the real resource handlers do. CloudFormation already tears down
in reverse dependency order, so a queue is gone before the environment it
points at. A job definition is deregistered, which is AWS's own semantic for
that type: the revision goes INACTIVE rather than disappearing.

Deliberately not CfnDeletes.safeDelete. Every BatchService failure carries the
same ClientException code, so tolerating it would be the catch-all AGENTS.md
rules out: a genuine refusal such as "still associated with a job queue" would
be swallowed into a green stack delete instead of surfacing as DELETE_FAILED.
Already-gone is handled by looking first instead, which also keeps the disable
step from throwing on a resource removed out of band, and makes a repeated
delete idempotent. That check matters most for the job definition, the one call
of the three that throws when its target is missing.

Nine unit tests cover the ordering, the idempotent repeat, and that a refused
delete propagates rather than being swallowed.

* fix(cloudformation): update Batch resources in place instead of re-creating them

provision runs again on every UpdateStack, and the legacy switch always created.
That broke both ways. A named compute environment or job queue called create a
second time under the same name, and createComputeEnvironment and createJobQueue
both reject a duplicate, so the stack update failed. An unnamed one generated a
fresh random name each time, creating a second entity and orphaning the first.

An update that keeps the name now goes through UpdateComputeEnvironment or
UpdateJobQueue, the schemas' update handlers, and keeps the prior physical id.
Only the properties those handlers accept are pushed through: the names, Type
and JobQueueType are createOnly, so a change there is a replacement the engine
drives rather than something to force into an update. A job definition needs no
update branch, because RegisterJobDefinition on an existing name records a new
revision, which is exactly how AWS updates one.

The name is kept steady by reading the attribute recorded at create time, the
way SqsCfnProvisioner reads QueueName beside the queue URL. ProvisionContext's
stablePhysicalName and reusesPriorEntity do not fit here: both fall back to the
prior physical id, which for Batch is an ARN rather than a name, so they would
feed an ARN back in as a name. Their javadoc says as much. For a stack created
before that attribute was recorded, the name is recovered from the prior ARN so
those stacks update in place too rather than duplicating.

Six more unit tests: in-place update for both types, a rename creating instead
of updating, an unnamed resource keeping its generated name, the ARN fallback,
and a job definition re-registering under the same name.

* fix(cloudformation): enforce the Batch schemas' required properties

Three properties the registry schemas mark required were optional here, so a
template missing one applied cleanly against floci and then failed on AWS, which
is the divergence the emulator exists to prevent.

AWS::Batch::JobQueue requires Priority; the switch substituted 1, so the
service's own "priority is required" check could never fire. ComputeEnvironment
and JobDefinition both require Type; an absent one reached the service as a null
and JobDefinition additionally defaulted to "container". All three now fail the
resource with the repo's ValidationError wording, matching how
Ec2NetworkCfnProvisioner reports a missing required property.

Also drops the Revision attribute from AWS::Batch::JobDefinition. Revision is
not a property of that type in the registry schema, and the legacy resource
spec gives the type no attributes at all, so Fn::GetAtt on it fails on AWS.
Offering it let a template pass here and break there. Nothing in the repo reads
it. The physical id stays the job definition ARN despite the schema's
primaryIdentifier naming JobDefinitionName, because the legacy spec and the
type's documented Ref both point at the ARN; changing it would be visible to
existing templates on evidence that does not agree with itself.

The integration test now covers the two stack-level defects end to end: an
update that changes only updatable properties keeps both physical ids and moves
the queue's priority, and a stack delete leaves neither entity behind. Both
assertions fail without the preceding two commits.

* fix(cloudformation): clean up the Batch entity a replacing update displaces

Renaming a compute environment or job queue creates the replacement and leaves the
original live in Batch forever. The provisioner recorded nothing, so the engine was
never told a replacement had happened: no DELETE_IN_PROGRESS event, no cleanup phase,
and nothing to delete the displaced entity even under the default UpdateReplacePolicy.

Wired to the shared ReplacementCleanup the way CloudFrontCfnProvisioner does it rather
than re-derived: attributesBefore is copied as the first statement of provision, and
record runs at the end of each arm once the new physical id is assigned, which is what
lets it compare the prior id against the new one. The four hooks delegate straight
through. An in-place update keeps the prior ARN, so it records nothing and stays
outside the cleanup path.

AWS::Batch::JobDefinition is deliberately not wired the same way. The registry schema
gives it primaryIdentifier JobDefinitionName, not the ARN, and registering revision 2
does not retire revision 1 on AWS: the prior revision stays ACTIVE and a running job
may still name it. Every registration mints a new ARN, so treating a changed physical
id as a replacement here would have the cleanup deregister a live revision. Only a
changed name replaces the definition, and that decision is taken before the register
call overwrites the recorded name, since the check reads that attribute.

Five unit tests: a renamed environment recording its predecessor, completeUpdate
actually deleting it, an in-place update owing nothing, a revision bump owing nothing,
and a renamed definition owing its predecessor. Mutation checked by dropping the record
call, which fails the first two and nothing else.

* fix(cloudformation): restore an in-place Batch update when the stack rolls back

An in-place compute environment or job queue update committed and then could not be
undone. Without a rollbackUpdate hook the resource fell to "Rollback is not implemented
for AWS::Batch::JobQueue", which is not a quiet gap: CloudFormationService adds it to
the rollback failures, so the whole stack reports UPDATE_ROLLBACK_FAILED and the
committed template is not restored either. A stack whose Batch resource sat anywhere
ahead of the failing one could not roll back at all.

Snapshot the entity before the update call and put it back, following
LambdaEventInvokeConfigCfnProvisioner: replacement first through the cleanup record,
then the snapshot for the in-place case. The describe is the only source, because the
template holds the desired state rather than the current one. The snapshot is spent
only once the restore succeeded, so a restore that throws leaves it for the next
attempt instead of reporting a rollback that never happened, and a resource this update
never touched carries none and answers false rather than claiming one.

Simpler than the Events rule this pattern comes from: each Batch update is a single
service call, so a throw leaves nothing half-applied and no in-provision restore arm is
needed. A job definition instead names the revision the failed update registered, which
is deregistered on rollback while the resource points at the prior one again.

Snapshotting is best effort. An entity the describe cannot find leaves none, and
rollbackUpdate then answers false rather than restoring a guess; failing the update
there would break a working update over missing rollback insurance.

The integration test is the one that proves the hook is reached: a stack whose later
resource cannot provision now reaches UPDATE_ROLLBACK_COMPLETE with the queue back on
its original priority. Reverting rollbackUpdate puts it back to
UPDATE_ROLLBACK_FAILED, reason "The following resource(s) failed to roll back:
[Queue, Compute]".

* fix(cloudformation): reject a Batch job queue Priority that is not a valid integer

Priority reached Integer.parseInt unguarded on both the create and the update path, so
a template carrying "high", or a Ref resolving to something that is not a number, threw
NumberFormatException out of the provisioner. The stack surfaced that as a 500 rather
than the failed validation it is, while every other bad property in this class raises
ValidationError with a 400.

Parsed through a helper that raises the same ValidationError instead, and bounded while
there: the registry schema types AWS::Batch::JobQueue Priority as integer with minimum 0
and maximum 1000, so a value outside that is as invalid as a non-numeric one and AWS
rejects both. This is the same schema-sourced enforcement the required-properties commit
already applies, reaching the one property it left parsing unchecked.

Three unit tests: a non-numeric value on create, the same on the update path, and an
out-of-range one. Mutation checked by restoring Integer.parseInt, which fails all three
and nothing else.

* docs(cloudformation): say why the Batch deletes cannot use CfnDeletes.safeDelete

The javadoc already said safeDelete was deliberate, but not enough to stop the question
being asked again: it read as a limitation of BatchService rather than of the API.

It is the API. Batch declares exactly two errors on DeleteComputeEnvironment,
DeleteJobQueue and DeregisterJobDefinition, ClientException at 400 and ServerException
at 500, and no not-found code anywhere in the service model. safeDelete takes the
specific already-gone code by design, so there is none here to pass it. Tolerating
ClientException instead would be the catch-all AGENTS.md rules out, swallowing a real
refusal such as "Cannot delete compute environment still associated with a job queue"
into a green stack delete. Giving Batch a Floci-only not-found code to satisfy the
pattern would put a code on the wire AWS never sends, which is the wire contract this
emulator exists to keep.

Also names EcsCfnProvisioner, which does tolerate a blanket ClientException for its
task-definition arm, so the divergence reads as deliberate rather than as an oversight.

Comments only. No behaviour change, so the existing tests are what prove it inert.

* docs(cloudformation): record what a Batch rollback cannot undo

A compute-environment rollback restores every value the environment already
carried and cannot remove one the failed update introduced. The snapshot holds
what the describe returned, so a serviceRole or computeResources key that was
absent beforehand is absent from the snapshot, the restore call omits it, and
UpdateComputeEnvironment leaves an omitted field alone while merging
computeResources rather than replacing it. The value survives a rollback the
stack reports as successful.

That cannot be closed here. UpdateComputeEnvironment is the only way to put the
environment back and its request shape has no removal: every member other than
computeEnvironment is an optional set-value, and the partial-merge semantics are
AWS's own. So this is disclosed on rollbackUpdate and pinned by a test, the way
CognitoCfnProvisioner records its own in-place rollback limitation, rather than
worked around.

Job queues are not affected. updateJobQueue assigns computeEnvironmentOrder
wholesale rather than merging it, and state and priority are always present, so
that restore is exact.

The test asserts the surviving value rather than only that the rollback returned
true, so it goes red if the merge semantics ever change without this note
changing with them. Mutation-checked: making the restore send an empty
serviceRole fails it.
2026-09-10 07:20:44 -04:00
..