mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
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.
This commit is contained in:
1 parent
65a9a65d8d
commit
82eaaaeee7
7 files changed
+1548
-169
No files matched your search
-165
@@ -3,7 +3,6 @@ package io.github.hectorvent.floci.services.cloudformation;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsNamespaces;
|
||||
import io.github.hectorvent.floci.core.common.XmlBuilder;
|
||||
import io.github.hectorvent.floci.services.batch.BatchService;
|
||||
import io.github.hectorvent.floci.services.cloudfront.CloudFrontService;
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.CacheBehavior;
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.DefaultCacheBehavior;
|
||||
@@ -194,9 +193,6 @@ public class CloudFormationResourceProvisioner {
|
||||
"AWS::ApiGatewayV2::Stage",
|
||||
"AWS::AutoScaling::AutoScalingGroup",
|
||||
"AWS::AutoScaling::LaunchConfiguration",
|
||||
"AWS::Batch::ComputeEnvironment",
|
||||
"AWS::Batch::JobDefinition",
|
||||
"AWS::Batch::JobQueue",
|
||||
"AWS::CloudFormation::CustomResource",
|
||||
"AWS::CloudFront::Distribution",
|
||||
"AWS::DynamoDB::GlobalTable",
|
||||
@@ -246,7 +242,6 @@ public class CloudFormationResourceProvisioner {
|
||||
private final CustomResourceResponseStore customResourceResponseStore;
|
||||
private final ContainerReachableEndpoint reachableEndpoint;
|
||||
private final StepFunctionsService stepFunctionsService;
|
||||
private final BatchService batchService;
|
||||
private final Ec2Service ec2Service;
|
||||
private final RdsService rdsService;
|
||||
private final EksService eksService;
|
||||
@@ -275,7 +270,6 @@ public class CloudFormationResourceProvisioner {
|
||||
CustomResourceResponseStore customResourceResponseStore,
|
||||
ContainerReachableEndpoint reachableEndpoint,
|
||||
StepFunctionsService stepFunctionsService,
|
||||
BatchService batchService,
|
||||
Ec2Service ec2Service,
|
||||
RdsService rdsService,
|
||||
EksService eksService,
|
||||
@@ -302,7 +296,6 @@ public class CloudFormationResourceProvisioner {
|
||||
this.customResourceResponseStore = customResourceResponseStore;
|
||||
this.reachableEndpoint = reachableEndpoint;
|
||||
this.stepFunctionsService = stepFunctionsService;
|
||||
this.batchService = batchService;
|
||||
this.ec2Service = ec2Service;
|
||||
this.rdsService = rdsService;
|
||||
this.eksService = eksService;
|
||||
@@ -391,12 +384,6 @@ public class CloudFormationResourceProvisioner {
|
||||
case "AWS::CloudFormation::CustomResource" ->
|
||||
provisionCustomResource(resource, properties, engine, region, accountId, stackName);
|
||||
case "Custom::DynamoDBReplica" -> provisionDynamoDbReplica(resource, properties, engine, region);
|
||||
case "AWS::Batch::ComputeEnvironment" ->
|
||||
provisionBatchComputeEnvironment(resource, properties, engine, region, stackName);
|
||||
case "AWS::Batch::JobQueue" ->
|
||||
provisionBatchJobQueue(resource, properties, engine, region, stackName);
|
||||
case "AWS::Batch::JobDefinition" ->
|
||||
provisionBatchJobDefinition(resource, properties, engine, region, stackName);
|
||||
// EC2 networking. These delegate to Ec2Service so the resources actually exist
|
||||
// (describe-subnets, ELBv2, etc. can find them) instead of being stubbed with a
|
||||
// fake physical id. Topological ordering guarantees parents are provisioned first.
|
||||
@@ -3405,158 +3392,6 @@ public class CloudFormationResourceProvisioner {
|
||||
}
|
||||
|
||||
|
||||
// ── Batch ────────────────────────────────────────────────────────────────
|
||||
|
||||
private void provisionBatchComputeEnvironment(StackResource r, JsonNode props,
|
||||
CloudFormationTemplateEngine engine,
|
||||
String region, String stackName) {
|
||||
String name = resolveOptional(props, "ComputeEnvironmentName", engine);
|
||||
if (name == null || name.isBlank()) {
|
||||
name = generatePhysicalName(stackName, r.getLogicalId(), 128, false);
|
||||
}
|
||||
|
||||
ObjectNode req = JsonNodeFactory.instance.objectNode();
|
||||
req.put("computeEnvironmentName", name);
|
||||
putResolvedText(req, "type", props, "Type", engine);
|
||||
putResolvedText(req, "state", props, "State", engine);
|
||||
putResolvedText(req, "serviceRole", props, "ServiceRole", engine);
|
||||
putResolvedObject(req, "computeResources", props, "ComputeResources", engine);
|
||||
putTagsObject(req, props, engine);
|
||||
|
||||
ObjectNode response = batchService.createComputeEnvironment(req, region);
|
||||
String arn = response.path("computeEnvironmentArn").asText();
|
||||
r.setPhysicalId(arn);
|
||||
r.getAttributes().put("Arn", arn);
|
||||
r.getAttributes().put("ComputeEnvironmentArn", arn);
|
||||
r.getAttributes().put("ComputeEnvironmentName", name);
|
||||
}
|
||||
|
||||
private void provisionBatchJobQueue(StackResource r, JsonNode props,
|
||||
CloudFormationTemplateEngine engine,
|
||||
String region, String stackName) {
|
||||
String name = resolveOptional(props, "JobQueueName", engine);
|
||||
if (name == null || name.isBlank()) {
|
||||
name = generatePhysicalName(stackName, r.getLogicalId(), 128, false);
|
||||
}
|
||||
|
||||
ObjectNode req = JsonNodeFactory.instance.objectNode();
|
||||
req.put("jobQueueName", name);
|
||||
String priority = resolveOptional(props, "Priority", engine);
|
||||
req.put("priority", priority != null ? Integer.parseInt(priority) : 1);
|
||||
putResolvedText(req, "state", props, "State", engine);
|
||||
putResolvedText(req, "jobQueueType", props, "JobQueueType", engine);
|
||||
req.set("computeEnvironmentOrder", batchComputeEnvironmentOrder(props, engine));
|
||||
putTagsObject(req, props, engine);
|
||||
|
||||
ObjectNode response = batchService.createJobQueue(req, region);
|
||||
String arn = response.path("jobQueueArn").asText();
|
||||
r.setPhysicalId(arn);
|
||||
r.getAttributes().put("Arn", arn);
|
||||
r.getAttributes().put("JobQueueArn", arn);
|
||||
r.getAttributes().put("JobQueueName", name);
|
||||
}
|
||||
|
||||
private void provisionBatchJobDefinition(StackResource r, JsonNode props,
|
||||
CloudFormationTemplateEngine engine,
|
||||
String region, String stackName) {
|
||||
String name = resolveOptional(props, "JobDefinitionName", engine);
|
||||
if (name == null || name.isBlank()) {
|
||||
name = generatePhysicalName(stackName, r.getLogicalId(), 128, false);
|
||||
}
|
||||
|
||||
ObjectNode req = JsonNodeFactory.instance.objectNode();
|
||||
req.put("jobDefinitionName", name);
|
||||
req.put("type", resolveOrDefault(props, "Type", engine, "container"));
|
||||
putResolvedArray(req, "platformCapabilities", props, "PlatformCapabilities", engine);
|
||||
if (props != null && props.has("ContainerProperties")) {
|
||||
req.set("containerProperties", batchContainerProperties(
|
||||
engine.resolveNode(props.get("ContainerProperties")), engine));
|
||||
}
|
||||
putStringMapFromObject(req, "parameters", props, "Parameters", engine);
|
||||
if (props != null && props.has("RetryStrategy")) {
|
||||
req.set("retryStrategy", batchRetryStrategy(engine.resolveNode(props.get("RetryStrategy"))));
|
||||
}
|
||||
if (props != null && props.has("Timeout")) {
|
||||
ObjectNode timeout = JsonNodeFactory.instance.objectNode();
|
||||
JsonNode resolved = engine.resolveNode(props.get("Timeout"));
|
||||
if (resolved.has("AttemptDurationSeconds")) {
|
||||
timeout.set("attemptDurationSeconds", resolved.get("AttemptDurationSeconds"));
|
||||
}
|
||||
req.set("timeout", timeout);
|
||||
}
|
||||
putTagsObject(req, props, engine);
|
||||
|
||||
ObjectNode response = batchService.registerJobDefinition(req, region);
|
||||
String arn = response.path("jobDefinitionArn").asText();
|
||||
r.setPhysicalId(arn);
|
||||
r.getAttributes().put("Arn", arn);
|
||||
r.getAttributes().put("JobDefinitionArn", arn);
|
||||
r.getAttributes().put("JobDefinitionName", name);
|
||||
r.getAttributes().put("Revision", response.path("revision").asText());
|
||||
}
|
||||
|
||||
private ArrayNode batchComputeEnvironmentOrder(JsonNode props, CloudFormationTemplateEngine engine) {
|
||||
ArrayNode out = JsonNodeFactory.instance.arrayNode();
|
||||
if (props == null || !props.has("ComputeEnvironmentOrder")) {
|
||||
return out;
|
||||
}
|
||||
JsonNode resolved = engine.resolveNode(props.get("ComputeEnvironmentOrder"));
|
||||
if (!resolved.isArray()) {
|
||||
return out;
|
||||
}
|
||||
for (JsonNode item : resolved) {
|
||||
ObjectNode order = out.addObject();
|
||||
order.put("order", item.path("Order").asInt());
|
||||
order.put("computeEnvironment", item.path("ComputeEnvironment").asText(null));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private ObjectNode batchContainerProperties(JsonNode resolved, CloudFormationTemplateEngine engine) {
|
||||
ObjectNode container = JsonNodeFactory.instance.objectNode();
|
||||
if (resolved == null || !resolved.isObject()) {
|
||||
return container;
|
||||
}
|
||||
copyIfPresent(container, "image", resolved, "Image");
|
||||
copyIfPresent(container, "command", resolved, "Command");
|
||||
copyIfPresent(container, "jobRoleArn", resolved, "JobRoleArn");
|
||||
copyIfPresent(container, "executionRoleArn", resolved, "ExecutionRoleArn");
|
||||
copyIfPresent(container, "logConfiguration", resolved, "LogConfiguration");
|
||||
copyIfPresent(container, "networkConfiguration", resolved, "NetworkConfiguration");
|
||||
copyIfPresent(container, "ephemeralStorage", resolved, "EphemeralStorage");
|
||||
if (resolved.has("ResourceRequirements") && resolved.get("ResourceRequirements").isArray()) {
|
||||
ArrayNode resources = container.putArray("resourceRequirements");
|
||||
for (JsonNode item : resolved.get("ResourceRequirements")) {
|
||||
ObjectNode requirement = resources.addObject();
|
||||
requirement.put("type", item.path("Type").asText(null));
|
||||
requirement.put("value", item.path("Value").asText(null));
|
||||
}
|
||||
}
|
||||
if (resolved.has("Environment") && resolved.get("Environment").isArray()) {
|
||||
ArrayNode env = container.putArray("environment");
|
||||
for (JsonNode item : resolved.get("Environment")) {
|
||||
ObjectNode entry = env.addObject();
|
||||
entry.put("name", item.path("Name").asText(null));
|
||||
entry.put("value", item.path("Value").asText(null));
|
||||
}
|
||||
}
|
||||
return container;
|
||||
}
|
||||
|
||||
private ObjectNode batchRetryStrategy(JsonNode resolved) {
|
||||
ObjectNode retry = JsonNodeFactory.instance.objectNode();
|
||||
if (resolved == null || !resolved.isObject()) {
|
||||
return retry;
|
||||
}
|
||||
if (resolved.has("Attempts")) {
|
||||
retry.set("attempts", resolved.get("Attempts"));
|
||||
}
|
||||
if (resolved.has("EvaluateOnExit")) {
|
||||
retry.set("evaluateOnExit", resolved.get("EvaluateOnExit"));
|
||||
}
|
||||
return retry;
|
||||
}
|
||||
|
||||
private void putResolvedText(ObjectNode req, String target, JsonNode props, String source,
|
||||
CloudFormationTemplateEngine engine) {
|
||||
String value = resolveOptional(props, source, engine);
|
||||
|
||||
+642
@@ -0,0 +1,642 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.batch.BatchService;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* CloudFormation provisioning for Batch: {@code AWS::Batch::ComputeEnvironment},
|
||||
* {@code AWS::Batch::JobQueue} and {@code AWS::Batch::JobDefinition}. Extracted from
|
||||
* {@code CloudFormationResourceProvisioner}.
|
||||
*
|
||||
* <p>The template property names are PascalCase and {@link BatchService} speaks the wire
|
||||
* shape's camelCase, so every arm builds a request node rather than passing properties
|
||||
* through.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class BatchCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final int NAME_MAX_LENGTH = 128;
|
||||
private static final int PRIORITY_MIN = 0;
|
||||
private static final int PRIORITY_MAX = 1000;
|
||||
|
||||
// Held rather than injected: AGENTS.md has a provisioner inject only the service it wraps, and
|
||||
// the snapshot below needs nothing the configured mapper adds.
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
private final BatchService batchService;
|
||||
|
||||
@Inject
|
||||
public BatchCfnProvisioner(BatchService batchService) {
|
||||
this.batchService = batchService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> resourceTypes() {
|
||||
return Set.of("AWS::Batch::ComputeEnvironment",
|
||||
"AWS::Batch::JobQueue",
|
||||
"AWS::Batch::JobDefinition");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
// A snapshot describes the update in flight; one an earlier update left behind is stale.
|
||||
r.getAttributes().remove(CfnRollback.BATCH_UPDATE_SNAPSHOT_ATTR);
|
||||
// Taken before the arms run: they overwrite the recorded name, and the prior-entity check
|
||||
// reads it, so a decision made afterwards would compare the new name against itself.
|
||||
Map<String, String> attributesBefore = Map.copyOf(r.getAttributes());
|
||||
switch (r.getResourceType()) {
|
||||
case "AWS::Batch::ComputeEnvironment" -> provisionComputeEnvironment(r, props, ctx, attributesBefore);
|
||||
case "AWS::Batch::JobQueue" -> provisionJobQueue(r, props, ctx, attributesBefore);
|
||||
case "AWS::Batch::JobDefinition" -> provisionJobDefinition(r, props, ctx, attributesBefore);
|
||||
default -> throw new IllegalStateException(
|
||||
"BatchCfnProvisioner cannot handle " + r.getResourceType());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this update replaced the physical entity, so the displaced one is still owed a
|
||||
* delete once the update commits. Without these four hooks a renamed compute environment or
|
||||
* job queue stayed live in Batch forever: the engine was never told a replacement happened.
|
||||
*/
|
||||
@Override
|
||||
public boolean hasReplacementUpdate(StackResource resource) {
|
||||
return ReplacementCleanup.hasReplacement(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String updateCleanupPhysicalId(StackResource resource) {
|
||||
return ReplacementCleanup.cleanupPhysicalId(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UpdateCleanupResult completeUpdate(StackResource resource) {
|
||||
return ReplacementCleanup.complete(resource, this::delete);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearUpdate(StackResource resource) {
|
||||
resource.getAttributes().remove(CfnRollback.BATCH_UPDATE_SNAPSHOT_ATTR);
|
||||
ReplacementCleanup.clear(resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undoes a Batch update when a later resource fails the same stack update. A replacement is
|
||||
* undone through the cleanup record, which points the resource back at the prior entity and
|
||||
* deletes the one this update created. Otherwise the update was in place, and the snapshot
|
||||
* taken before the mutating call is replayed through the same update call that changed it.
|
||||
*
|
||||
* <p>The snapshot is spent only once the restore succeeded: a restore that throws leaves it in
|
||||
* place for the next attempt rather than reporting a rollback that never happened. A resource
|
||||
* this update never touched carries no snapshot and answers false, so the engine keeps
|
||||
* reporting honestly instead of claiming a rollback it did not perform.
|
||||
*
|
||||
* <p><strong>Known limitation, compute environments only.</strong> The restore is exact for
|
||||
* every value the environment already carried, and cannot remove one the failed update
|
||||
* introduced. A {@code serviceRole}, or a {@code computeResources} key, that was absent before
|
||||
* the update is absent from the snapshot too, so the restore call simply omits it, and
|
||||
* UpdateComputeEnvironment leaves an omitted field alone and merges {@code computeResources}
|
||||
* rather than replacing it. The value therefore survives a rollback the stack reports as
|
||||
* successful.
|
||||
*
|
||||
* <p>This is not a gap that can be closed here. UpdateComputeEnvironment is the only way to
|
||||
* put the environment back, and its request shape has no removal: every member other than
|
||||
* {@code computeEnvironment} is an optional set-value, and the partial-merge semantics are
|
||||
* AWS's own. Recorded rather than worked around, in the way
|
||||
* {@code CognitoCfnProvisioner} records its own in-place rollback limitation, and pinned by
|
||||
* {@code BatchCfnProvisionerTest.aRollbackCannotRemoveAValueTheFailedUpdateAdded}.
|
||||
*
|
||||
* <p>Job queues are not affected: {@code updateJobQueue} assigns
|
||||
* {@code computeEnvironmentOrder} wholesale rather than merging it, and {@code state} and
|
||||
* {@code priority} are always present, so that restore is exact.
|
||||
*/
|
||||
@Override
|
||||
public boolean rollbackUpdate(StackResource resource) {
|
||||
if (ReplacementCleanup.rollback(resource, this::delete)) {
|
||||
return true;
|
||||
}
|
||||
String raw = resource.getAttributes().get(CfnRollback.BATCH_UPDATE_SNAPSHOT_ATTR);
|
||||
if (raw == null || raw.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
JsonNode snapshot;
|
||||
try {
|
||||
snapshot = MAPPER.readTree(raw);
|
||||
} catch (JsonProcessingException unreadableSnapshot) {
|
||||
throw new IllegalStateException("Could not read the Batch update snapshot for "
|
||||
+ resource.getLogicalId(), unreadableSnapshot);
|
||||
}
|
||||
restoreSnapshot(resource, snapshot);
|
||||
resource.getAttributes().remove(CfnRollback.BATCH_UPDATE_SNAPSHOT_ATTR);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void restoreSnapshot(StackResource resource, JsonNode snapshot) {
|
||||
String priorId = snapshot.path("physicalId").asText(null);
|
||||
switch (snapshot.path("type").asText("")) {
|
||||
case "AWS::Batch::ComputeEnvironment" -> {
|
||||
ObjectNode update = JsonNodeFactory.instance.objectNode();
|
||||
update.put("computeEnvironment", priorId);
|
||||
copySnapshotted(update, snapshot, "state", "serviceRole", "computeResources");
|
||||
batchService.updateComputeEnvironment(update);
|
||||
}
|
||||
case "AWS::Batch::JobQueue" -> {
|
||||
ObjectNode update = JsonNodeFactory.instance.objectNode();
|
||||
update.put("jobQueue", priorId);
|
||||
copySnapshotted(update, snapshot, "state", "priority", "computeEnvironmentOrder");
|
||||
batchService.updateJobQueue(update);
|
||||
}
|
||||
case "AWS::Batch::JobDefinition" -> {
|
||||
// A revision bump leaves the prior revision ACTIVE, as on AWS, so rolling back is
|
||||
// deregistering the revision this update registered and naming the prior one again.
|
||||
String registered = resource.getPhysicalId();
|
||||
if (registered != null && !registered.equals(priorId)) {
|
||||
deregisterJobDefinition(registered);
|
||||
}
|
||||
resource.setPhysicalId(priorId);
|
||||
resource.getAttributes().put("Arn", priorId);
|
||||
resource.getAttributes().put("JobDefinitionArn", priorId);
|
||||
}
|
||||
default -> throw new IllegalStateException(
|
||||
"Unreadable Batch update snapshot on " + resource.getLogicalId());
|
||||
}
|
||||
}
|
||||
|
||||
private static void copySnapshotted(ObjectNode into, JsonNode snapshot, String... fields) {
|
||||
for (String field : fields) {
|
||||
if (snapshot.has(field) && !snapshot.get(field).isNull()) {
|
||||
into.set(field, snapshot.get(field));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Records what the entity looks like now, before the update call about to change it. The
|
||||
* describe is the only source: the template holds the desired state, not the current one.
|
||||
*
|
||||
* <p>Best effort by design. An entity the describe cannot find leaves no snapshot, and
|
||||
* {@code rollbackUpdate} then answers false rather than restoring a guess, which is the honest
|
||||
* answer: the engine reports the resource as not rolled back instead of claiming a restore
|
||||
* that never had anything to restore from. Failing the update here would be worse, since it
|
||||
* would break a working update over missing rollback insurance.
|
||||
*/
|
||||
private void snapshotBeforeUpdate(StackResource r, String type, String physicalId,
|
||||
String requestKey, Function<ObjectNode, ObjectNode> describe,
|
||||
String... fields) {
|
||||
ObjectNode req = JsonNodeFactory.instance.objectNode();
|
||||
req.putArray(requestKey).add(physicalId);
|
||||
ObjectNode described = describe.apply(req);
|
||||
if (described == null) {
|
||||
return;
|
||||
}
|
||||
JsonNode found = described.path(requestKey);
|
||||
if (found.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
JsonNode current = found.get(0);
|
||||
ObjectNode snapshot = JsonNodeFactory.instance.objectNode();
|
||||
snapshot.put("type", type);
|
||||
snapshot.put("physicalId", physicalId);
|
||||
copySnapshotted(snapshot, current, fields);
|
||||
r.getAttributes().put(CfnRollback.BATCH_UPDATE_SNAPSHOT_ATTR, snapshot.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch refuses to delete a running entity, exactly as AWS does: DeleteJobQueue rejects an
|
||||
* {@code ENABLED} queue and DeleteComputeEnvironment rejects one that is not {@code DISABLED}
|
||||
* or is still referenced by a queue. So each delete disables first, 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.
|
||||
*
|
||||
* <p>Deliberately no {@code CfnDeletes.safeDelete} here, and it is the API that rules it out
|
||||
* rather than a gap in {@link BatchService}. Batch declares exactly two errors on
|
||||
* DeleteComputeEnvironment, DeleteJobQueue and DeregisterJobDefinition, {@code ClientException}
|
||||
* (400) and {@code ServerException} (500); there is no not-found code in the service model to
|
||||
* name, so there is none to tolerate. {@code safeDelete} takes the specific already-gone code
|
||||
* on purpose, and passing {@code ClientException} would be the catch-all AGENTS.md rules out:
|
||||
* a real refusal such as "Cannot delete compute environment still associated with a job queue"
|
||||
* would be swallowed into a green stack delete instead of failing it. Giving Batch a
|
||||
* Floci-only not-found code to satisfy the pattern would put a code on the wire that AWS never
|
||||
* sends.
|
||||
*
|
||||
* <p>Already-gone is handled by looking first instead, which is also what keeps the disable
|
||||
* step from throwing on a resource someone removed out of band. {@code EcsCfnProvisioner} does
|
||||
* tolerate a blanket {@code ClientException} for its task-definition arm, so the divergence
|
||||
* here is deliberate rather than an oversight.
|
||||
*/
|
||||
@Override
|
||||
public void delete(String resourceType, String physicalId, String region) {
|
||||
if (physicalId == null || physicalId.isBlank()) {
|
||||
return;
|
||||
}
|
||||
switch (resourceType) {
|
||||
case "AWS::Batch::ComputeEnvironment" -> deleteComputeEnvironment(physicalId);
|
||||
case "AWS::Batch::JobQueue" -> deleteJobQueue(physicalId);
|
||||
case "AWS::Batch::JobDefinition" -> deregisterJobDefinition(physicalId);
|
||||
default -> {
|
||||
// no other type reaches this provisioner
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteComputeEnvironment(String physicalId) {
|
||||
if (!exists("computeEnvironments", physicalId,
|
||||
req -> batchService.describeComputeEnvironments(req))) {
|
||||
return;
|
||||
}
|
||||
ObjectNode disable = JsonNodeFactory.instance.objectNode();
|
||||
disable.put("computeEnvironment", physicalId);
|
||||
disable.put("state", "DISABLED");
|
||||
batchService.updateComputeEnvironment(disable);
|
||||
|
||||
ObjectNode req = JsonNodeFactory.instance.objectNode();
|
||||
req.put("computeEnvironment", physicalId);
|
||||
batchService.deleteComputeEnvironment(req);
|
||||
}
|
||||
|
||||
private void deleteJobQueue(String physicalId) {
|
||||
if (!exists("jobQueues", physicalId, req -> batchService.describeJobQueues(req))) {
|
||||
return;
|
||||
}
|
||||
ObjectNode disable = JsonNodeFactory.instance.objectNode();
|
||||
disable.put("jobQueue", physicalId);
|
||||
disable.put("state", "DISABLED");
|
||||
batchService.updateJobQueue(disable);
|
||||
|
||||
ObjectNode req = JsonNodeFactory.instance.objectNode();
|
||||
req.put("jobQueue", physicalId);
|
||||
batchService.deleteJobQueue(req);
|
||||
}
|
||||
|
||||
private void deregisterJobDefinition(String physicalId) {
|
||||
// Unlike the other two, deregister throws when the definition is gone, so the existence
|
||||
// check is what makes a repeated stack delete idempotent.
|
||||
if (!exists("jobDefinitions", physicalId, req -> batchService.describeJobDefinitions(req))) {
|
||||
return;
|
||||
}
|
||||
ObjectNode req = JsonNodeFactory.instance.objectNode();
|
||||
req.put("jobDefinition", physicalId);
|
||||
batchService.deregisterJobDefinition(req);
|
||||
}
|
||||
|
||||
private boolean exists(String requestKey, String physicalId,
|
||||
Function<ObjectNode, ObjectNode> describe) {
|
||||
ObjectNode req = JsonNodeFactory.instance.objectNode();
|
||||
req.putArray(requestKey).add(physicalId);
|
||||
return !describe.apply(req).path(requestKey).isEmpty();
|
||||
}
|
||||
|
||||
private void provisionComputeEnvironment(StackResource r, JsonNode props, ProvisionContext ctx,
|
||||
Map<String, String> attributesBefore) {
|
||||
String name = stableName(r, ctx, props, "ComputeEnvironmentName");
|
||||
require("AWS::Batch::ComputeEnvironment", "Type", ctx.resolveOptional(props, "Type"));
|
||||
|
||||
String arn;
|
||||
if (reusesPriorEntity(r, ctx, name, "ComputeEnvironmentName")) {
|
||||
// UpdateComputeEnvironment is the schema's update handler and takes only these three;
|
||||
// ComputeEnvironmentName, Type and Tags are createOnly, so a change to those is a
|
||||
// replacement the engine drives, not something to push through here.
|
||||
snapshotBeforeUpdate(r, "AWS::Batch::ComputeEnvironment", ctx.priorPhysicalId(),
|
||||
"computeEnvironments", req -> batchService.describeComputeEnvironments(req),
|
||||
"state", "serviceRole", "computeResources");
|
||||
ObjectNode update = JsonNodeFactory.instance.objectNode();
|
||||
update.put("computeEnvironment", ctx.priorPhysicalId());
|
||||
putResolvedText(update, "state", props, "State", ctx);
|
||||
putResolvedText(update, "serviceRole", props, "ServiceRole", ctx);
|
||||
putResolvedObject(update, "computeResources", props, "ComputeResources", ctx);
|
||||
batchService.updateComputeEnvironment(update);
|
||||
arn = ctx.priorPhysicalId();
|
||||
} else {
|
||||
ObjectNode req = JsonNodeFactory.instance.objectNode();
|
||||
req.put("computeEnvironmentName", name);
|
||||
putResolvedText(req, "type", props, "Type", ctx);
|
||||
putResolvedText(req, "state", props, "State", ctx);
|
||||
putResolvedText(req, "serviceRole", props, "ServiceRole", ctx);
|
||||
putResolvedObject(req, "computeResources", props, "ComputeResources", ctx);
|
||||
putTags(req, props, ctx);
|
||||
arn = batchService.createComputeEnvironment(req, ctx.region())
|
||||
.path("computeEnvironmentArn").asText();
|
||||
}
|
||||
r.setPhysicalId(arn);
|
||||
r.getAttributes().put("Arn", arn);
|
||||
r.getAttributes().put("ComputeEnvironmentArn", arn);
|
||||
r.getAttributes().put("ComputeEnvironmentName", name);
|
||||
// An in-place update kept the prior ARN, so this records nothing; a rename minted a new
|
||||
// one, and the environment it displaced is owed a delete once the update commits.
|
||||
ReplacementCleanup.record(r, ctx, attributesBefore);
|
||||
}
|
||||
|
||||
private void provisionJobQueue(StackResource r, JsonNode props, ProvisionContext ctx,
|
||||
Map<String, String> attributesBefore) {
|
||||
String name = stableName(r, ctx, props, "JobQueueName");
|
||||
String priority = ctx.resolveOptional(props, "Priority");
|
||||
require("AWS::Batch::JobQueue", "Priority", priority);
|
||||
int priorityValue = requireInt("AWS::Batch::JobQueue", "Priority", priority);
|
||||
|
||||
String arn;
|
||||
if (reusesPriorEntity(r, ctx, name, "JobQueueName")) {
|
||||
// UpdateJobQueue is the schema's update handler; JobQueueName and JobQueueType are
|
||||
// createOnly, so only these three are pushed through.
|
||||
snapshotBeforeUpdate(r, "AWS::Batch::JobQueue", ctx.priorPhysicalId(),
|
||||
"jobQueues", req -> batchService.describeJobQueues(req),
|
||||
"state", "priority", "computeEnvironmentOrder");
|
||||
ObjectNode update = JsonNodeFactory.instance.objectNode();
|
||||
update.put("jobQueue", ctx.priorPhysicalId());
|
||||
putResolvedText(update, "state", props, "State", ctx);
|
||||
update.put("priority", priorityValue);
|
||||
update.set("computeEnvironmentOrder", computeEnvironmentOrder(props, ctx));
|
||||
batchService.updateJobQueue(update);
|
||||
arn = ctx.priorPhysicalId();
|
||||
} else {
|
||||
ObjectNode req = JsonNodeFactory.instance.objectNode();
|
||||
req.put("jobQueueName", name);
|
||||
req.put("priority", priorityValue);
|
||||
putResolvedText(req, "state", props, "State", ctx);
|
||||
putResolvedText(req, "jobQueueType", props, "JobQueueType", ctx);
|
||||
req.set("computeEnvironmentOrder", computeEnvironmentOrder(props, ctx));
|
||||
putTags(req, props, ctx);
|
||||
arn = batchService.createJobQueue(req, ctx.region()).path("jobQueueArn").asText();
|
||||
}
|
||||
r.setPhysicalId(arn);
|
||||
r.getAttributes().put("Arn", arn);
|
||||
r.getAttributes().put("JobQueueArn", arn);
|
||||
r.getAttributes().put("JobQueueName", name);
|
||||
ReplacementCleanup.record(r, ctx, attributesBefore);
|
||||
}
|
||||
|
||||
private void provisionJobDefinition(StackResource r, JsonNode props, ProvisionContext ctx,
|
||||
Map<String, String> attributesBefore) {
|
||||
// No update branch: RegisterJobDefinition on an existing name records a new revision,
|
||||
// which is how AWS updates a job definition. Only the name has to stay steady.
|
||||
String name = stableName(r, ctx, props, "JobDefinitionName");
|
||||
// Decided before the register call overwrites the recorded name below.
|
||||
boolean sameDefinition = reusesPriorEntity(r, ctx, name, "JobDefinitionName");
|
||||
String type = ctx.resolveOptional(props, "Type");
|
||||
require("AWS::Batch::JobDefinition", "Type", type);
|
||||
|
||||
ObjectNode req = JsonNodeFactory.instance.objectNode();
|
||||
req.put("jobDefinitionName", name);
|
||||
req.put("type", type);
|
||||
putResolvedArray(req, "platformCapabilities", props, "PlatformCapabilities", ctx);
|
||||
if (props != null && props.has("ContainerProperties")) {
|
||||
req.set("containerProperties",
|
||||
containerProperties(ctx.engine().resolveNode(props.get("ContainerProperties"))));
|
||||
}
|
||||
putStringMapFromObject(req, "parameters", props, "Parameters", ctx);
|
||||
if (props != null && props.has("RetryStrategy")) {
|
||||
req.set("retryStrategy", retryStrategy(ctx.engine().resolveNode(props.get("RetryStrategy"))));
|
||||
}
|
||||
if (props != null && props.has("Timeout")) {
|
||||
ObjectNode timeout = JsonNodeFactory.instance.objectNode();
|
||||
JsonNode resolved = ctx.engine().resolveNode(props.get("Timeout"));
|
||||
if (resolved.has("AttemptDurationSeconds")) {
|
||||
timeout.set("attemptDurationSeconds", resolved.get("AttemptDurationSeconds"));
|
||||
}
|
||||
req.set("timeout", timeout);
|
||||
}
|
||||
putTags(req, props, ctx);
|
||||
|
||||
String priorArn = ctx.isUpdate() ? ctx.priorPhysicalId() : null;
|
||||
ObjectNode response = batchService.registerJobDefinition(req, ctx.region());
|
||||
String arn = response.path("jobDefinitionArn").asText();
|
||||
r.setPhysicalId(arn);
|
||||
r.getAttributes().put("Arn", arn);
|
||||
r.getAttributes().put("JobDefinitionArn", arn);
|
||||
r.getAttributes().put("JobDefinitionName", name);
|
||||
|
||||
if (sameDefinition) {
|
||||
// A revision bump is not a replacement. The registry schema's primaryIdentifier for
|
||||
// this type is JobDefinitionName, not the ARN, and the prior revision stays ACTIVE on
|
||||
// AWS: registering revision 2 does not retire revision 1, which a job may still name.
|
||||
// Recording it as displaced would have the cleanup deregister a live revision. Only a
|
||||
// changed name replaces the entity, which is the branch below.
|
||||
//
|
||||
// Rolling one back is still possible, and is the snapshot's job: deregister the
|
||||
// revision this update registered and name the prior one again.
|
||||
ObjectNode snapshot = JsonNodeFactory.instance.objectNode();
|
||||
snapshot.put("type", "AWS::Batch::JobDefinition");
|
||||
snapshot.put("physicalId", priorArn);
|
||||
r.getAttributes().put(CfnRollback.BATCH_UPDATE_SNAPSHOT_ATTR, snapshot.toString());
|
||||
return;
|
||||
}
|
||||
ReplacementCleanup.record(r, ctx, attributesBefore);
|
||||
}
|
||||
|
||||
/** The schema's required properties fail the resource with the repo's ValidationError wording. */
|
||||
private static void require(String type, String property, String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
throw new AwsException("ValidationError", type + " requires " + property, 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A template value the schema types as a bounded integer. Template properties arrive as text,
|
||||
* so a non-numeric one reached {@code Integer.parseInt} and left as an uncaught
|
||||
* NumberFormatException, which the stack reported as a 500 rather than the failed validation
|
||||
* it is. The bounds are the registry schema's own: AWS::Batch::JobQueue Priority is
|
||||
* {@code integer, minimum 0, maximum 1000}.
|
||||
*/
|
||||
private static int requireInt(String type, String property, String value) {
|
||||
int parsed;
|
||||
try {
|
||||
parsed = Integer.parseInt(value.trim());
|
||||
} catch (NumberFormatException notAnInteger) {
|
||||
throw new AwsException("ValidationError",
|
||||
type + " " + property + " must be an integer, but was '" + value + "'", 400);
|
||||
}
|
||||
if (parsed < PRIORITY_MIN || parsed > PRIORITY_MAX) {
|
||||
throw new AwsException("ValidationError", type + " " + property + " must be between "
|
||||
+ PRIORITY_MIN + " and " + PRIORITY_MAX + ", but was " + parsed, 400);
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
/**
|
||||
* The name to use this time round: the template's, else the one this resource already has,
|
||||
* and only failing both a freshly generated one.
|
||||
*
|
||||
* <p>{@code ctx.stablePhysicalName} does not fit these types. It falls back to the prior
|
||||
* physical id, and for Batch that id is an ARN rather than the name, so it would feed an ARN
|
||||
* back in as a name. The prior name comes from the attribute recorded at create time instead,
|
||||
* the way SqsCfnProvisioner reads QueueName beside the queue URL. Without this, an unnamed
|
||||
* resource got a fresh random name on every UpdateStack, creating a second entity and
|
||||
* orphaning the first.
|
||||
*/
|
||||
private String stableName(StackResource r, ProvisionContext ctx, JsonNode props, String nameKey) {
|
||||
// For all three types the template property and the recorded attribute share a name.
|
||||
String explicit = ctx.resolveOptional(props, nameKey);
|
||||
if (explicit != null && !explicit.isBlank()) {
|
||||
return explicit;
|
||||
}
|
||||
String prior = priorName(r, ctx, nameKey);
|
||||
if (prior != null && !prior.isBlank()) {
|
||||
return prior;
|
||||
}
|
||||
return ctx.generatePhysicalName(r.getLogicalId(), NAME_MAX_LENGTH, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@code name} is the entity this resource already had, so it must be updated rather
|
||||
* than created. {@code ctx.reusesPriorEntity} compares against the physical id, which is the
|
||||
* ARN here, so the comparison is made against the recorded name instead. A replacing update
|
||||
* arrives with a prior id too but has derived a different name, and must still create.
|
||||
*/
|
||||
private boolean reusesPriorEntity(StackResource r, ProvisionContext ctx, String name, String attribute) {
|
||||
return ctx.isUpdate() && name.equals(priorName(r, ctx, attribute));
|
||||
}
|
||||
|
||||
/**
|
||||
* The recorded name, falling back to the one embedded in the prior ARN for a resource created
|
||||
* before that attribute was stored. Batch ARNs end in {@code <kind>/<name>}, and the job
|
||||
* definition's also carries {@code :<revision>}.
|
||||
*/
|
||||
private String priorName(StackResource r, ProvisionContext ctx, String attribute) {
|
||||
String recorded = r.getAttributes() != null ? r.getAttributes().get(attribute) : null;
|
||||
if (recorded != null && !recorded.isBlank()) {
|
||||
return recorded;
|
||||
}
|
||||
String priorId = ctx.priorPhysicalId();
|
||||
if (priorId == null || !priorId.startsWith("arn:")) {
|
||||
return null;
|
||||
}
|
||||
int slash = priorId.lastIndexOf('/');
|
||||
if (slash < 0 || slash == priorId.length() - 1) {
|
||||
return null;
|
||||
}
|
||||
String tail = priorId.substring(slash + 1);
|
||||
int colon = tail.lastIndexOf(':');
|
||||
return colon > 0 ? tail.substring(0, colon) : tail;
|
||||
}
|
||||
|
||||
private ArrayNode computeEnvironmentOrder(JsonNode props, ProvisionContext ctx) {
|
||||
ArrayNode out = JsonNodeFactory.instance.arrayNode();
|
||||
if (props == null || !props.has("ComputeEnvironmentOrder")) {
|
||||
return out;
|
||||
}
|
||||
JsonNode resolved = ctx.engine().resolveNode(props.get("ComputeEnvironmentOrder"));
|
||||
if (!resolved.isArray()) {
|
||||
return out;
|
||||
}
|
||||
for (JsonNode item : resolved) {
|
||||
ObjectNode order = out.addObject();
|
||||
order.put("order", item.path("Order").asInt());
|
||||
order.put("computeEnvironment", item.path("ComputeEnvironment").asText(null));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private ObjectNode containerProperties(JsonNode resolved) {
|
||||
ObjectNode container = JsonNodeFactory.instance.objectNode();
|
||||
if (resolved == null || !resolved.isObject()) {
|
||||
return container;
|
||||
}
|
||||
copyIfPresent(container, "image", resolved, "Image");
|
||||
copyIfPresent(container, "command", resolved, "Command");
|
||||
copyIfPresent(container, "jobRoleArn", resolved, "JobRoleArn");
|
||||
copyIfPresent(container, "executionRoleArn", resolved, "ExecutionRoleArn");
|
||||
copyIfPresent(container, "logConfiguration", resolved, "LogConfiguration");
|
||||
copyIfPresent(container, "networkConfiguration", resolved, "NetworkConfiguration");
|
||||
copyIfPresent(container, "ephemeralStorage", resolved, "EphemeralStorage");
|
||||
if (resolved.has("ResourceRequirements") && resolved.get("ResourceRequirements").isArray()) {
|
||||
ArrayNode resources = container.putArray("resourceRequirements");
|
||||
for (JsonNode item : resolved.get("ResourceRequirements")) {
|
||||
ObjectNode requirement = resources.addObject();
|
||||
requirement.put("type", item.path("Type").asText(null));
|
||||
requirement.put("value", item.path("Value").asText(null));
|
||||
}
|
||||
}
|
||||
if (resolved.has("Environment") && resolved.get("Environment").isArray()) {
|
||||
ArrayNode env = container.putArray("environment");
|
||||
for (JsonNode item : resolved.get("Environment")) {
|
||||
ObjectNode entry = env.addObject();
|
||||
entry.put("name", item.path("Name").asText(null));
|
||||
entry.put("value", item.path("Value").asText(null));
|
||||
}
|
||||
}
|
||||
return container;
|
||||
}
|
||||
|
||||
private ObjectNode retryStrategy(JsonNode resolved) {
|
||||
ObjectNode retry = JsonNodeFactory.instance.objectNode();
|
||||
if (resolved == null || !resolved.isObject()) {
|
||||
return retry;
|
||||
}
|
||||
if (resolved.has("Attempts")) {
|
||||
retry.set("attempts", resolved.get("Attempts"));
|
||||
}
|
||||
if (resolved.has("EvaluateOnExit")) {
|
||||
retry.set("evaluateOnExit", resolved.get("EvaluateOnExit"));
|
||||
}
|
||||
return retry;
|
||||
}
|
||||
|
||||
private void putTags(ObjectNode req, JsonNode props, ProvisionContext ctx) {
|
||||
Map<String, String> tags = ctx.resolveTags(props, "Tags");
|
||||
if (!tags.isEmpty()) {
|
||||
ObjectNode tagNode = req.putObject("tags");
|
||||
tags.forEach(tagNode::put);
|
||||
}
|
||||
}
|
||||
|
||||
private void putResolvedText(ObjectNode req, String target, JsonNode props, String source,
|
||||
ProvisionContext ctx) {
|
||||
String value = ctx.resolveOptional(props, source);
|
||||
if (value != null) {
|
||||
req.put(target, value);
|
||||
}
|
||||
}
|
||||
|
||||
private void putResolvedObject(ObjectNode req, String target, JsonNode props, String source,
|
||||
ProvisionContext ctx) {
|
||||
if (props == null || !props.has(source) || props.get(source).isNull()) {
|
||||
return;
|
||||
}
|
||||
JsonNode resolved = ctx.engine().resolveNode(props.get(source));
|
||||
if (resolved != null && resolved.isObject()) {
|
||||
req.set(target, resolved);
|
||||
}
|
||||
}
|
||||
|
||||
private void putResolvedArray(ObjectNode req, String target, JsonNode props, String source,
|
||||
ProvisionContext ctx) {
|
||||
if (props == null || !props.has(source) || props.get(source).isNull()) {
|
||||
return;
|
||||
}
|
||||
JsonNode resolved = ctx.engine().resolveNode(props.get(source));
|
||||
if (resolved != null && resolved.isArray()) {
|
||||
req.set(target, resolved);
|
||||
}
|
||||
}
|
||||
|
||||
private void putStringMapFromObject(ObjectNode req, String target, JsonNode props, String source,
|
||||
ProvisionContext ctx) {
|
||||
if (props == null || !props.has(source) || props.get(source).isNull()) {
|
||||
return;
|
||||
}
|
||||
JsonNode resolved = ctx.engine().resolveNode(props.get(source));
|
||||
if (!resolved.isObject()) {
|
||||
return;
|
||||
}
|
||||
ObjectNode out = JsonNodeFactory.instance.objectNode();
|
||||
resolved.fields().forEachRemaining(e -> out.put(e.getKey(), e.getValue().asText()));
|
||||
req.set(target, out);
|
||||
}
|
||||
|
||||
private void copyIfPresent(ObjectNode target, String targetName, JsonNode source, String sourceName) {
|
||||
if (source.has(sourceName) && !source.get(sourceName).isNull()) {
|
||||
target.set(targetName, source.get(sourceName));
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -53,6 +53,18 @@ public final class CfnRollback {
|
||||
*/
|
||||
public static final String RULE_TARGETS_SNAPSHOT_ATTR = "__FlociRuleTargetsSnapshot";
|
||||
|
||||
/**
|
||||
* Holds the configuration a Batch entity carried before the in-place update in flight changed
|
||||
* it, in the request shape its update call takes, so a failed stack update can put it back.
|
||||
* Written by {@code BatchCfnProvisioner} before its update call and spent by its
|
||||
* {@code rollbackUpdate}. Carries the resource type because one provisioner serves three, and
|
||||
* the rollback hook is handed the stack resource alone: a compute environment is restored
|
||||
* through UpdateComputeEnvironment and a job queue through UpdateJobQueue, and a job
|
||||
* definition instead names the revision the failed update registered so it can be
|
||||
* deregistered.
|
||||
*/
|
||||
public static final String BATCH_UPDATE_SNAPSHOT_ATTR = "__FlociBatchUpdateSnapshot";
|
||||
|
||||
/**
|
||||
* Holds the settings an event invoke configuration carried before an in-place update changed
|
||||
* them, in the request shape a put takes, so a failed stack update can put them back. Written
|
||||
|
||||
Reference in new issue
Block a user