feat(cloudformation): refuse updates after a failed rollback and add ContinueUpdateRollback (#4949)

This commit is contained in:
Mario Toffia authored and GitHub committed 2026-10-14 08:01:54 -04:00
1 parent f6c8ebd5a0
commit c4a809f47f
15 files changed
+1443 -71

No files matched your search

@@ -581,6 +581,7 @@ public class AwsQueryController {
private static final Set<String> CLOUDFORMATION_ACTIONS = Set.of(
"CreateStack", "DeleteStack", "UpdateStack", "DescribeStacks", "UpdateTerminationProtection",
"ContinueUpdateRollback",
"ListStacks", "ListExports", "GetTemplate", "GetTemplateSummary", "ValidateTemplate",
"CreateChangeSet", "DeleteChangeSet", "DescribeChangeSet", "ExecuteChangeSet", "ListChangeSets",
"DescribeStackEvents", "DescribeStackResources", "ListStackResources", "DescribeStackResource",
@@ -51,6 +51,7 @@ public class CloudFormationQueryHandler {
case "CreateStack" -> createStack(params, region);
case "UpdateStack" -> updateStack(params, region);
case "DeleteStack" -> deleteStack(params, region);
case "ContinueUpdateRollback" -> continueUpdateRollback(params, region);
case "UpdateTerminationProtection" -> updateTerminationProtection(params, region);
case "CreateChangeSet" -> createChangeSet(params, region);
case "DescribeChangeSet" -> describeChangeSet(params, region);
@@ -189,6 +190,28 @@ public class CloudFormationQueryHandler {
}
}
// ── ContinueUpdateRollback ───────────────────────────────────────────────
private Response continueUpdateRollback(MultivaluedMap<String, String> params, String region) {
String stackName = params.getFirst("StackName");
List<String> resourcesToSkip = extractList(params, "ResourcesToSkip.member.");
boolean forceRollback = Boolean.parseBoolean(params.getFirst("ForceRollback"));
// RoleARN and ClientRequestToken are accepted and have no effect: Floci assumes no role
// for stack operations and does not deduplicate retried requests.
try {
cfnService.continueUpdateRollback(stackName, resourcesToSkip, forceRollback, region);
} catch (AwsException e) {
return xmlError(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
}
String xml = new XmlBuilder()
.start("ContinueUpdateRollbackResponse", CF_NS)
.raw("<ContinueUpdateRollbackResult/>")
.raw(AwsQueryResponse.responseMetadata())
.end("ContinueUpdateRollbackResponse")
.build();
return Response.ok(xml).type("text/xml").build();
}
// ── UpdateTerminationProtection ─────────────────────────────────────────────
private Response updateTerminationProtection(MultivaluedMap<String, String> params, String region) {
@@ -450,6 +473,7 @@ public class CloudFormationQueryHandler {
.elem("PhysicalResourceId", res.getPhysicalId())
.elem("ResourceType", res.getResourceType())
.elem("ResourceStatus", res.getStatus())
.elem("ResourceStatusReason", res.getStatusReason())
.elem("LastUpdatedTimestamp", ISO.format(res.getTimestamp()))
.end("StackResourceDetail")
.end("DescribeStackResourceResult")
@@ -21,6 +21,7 @@ import io.github.hectorvent.floci.services.cloudformation.model.ChangeSet;
import io.github.hectorvent.floci.services.cloudformation.model.Stack;
import io.github.hectorvent.floci.services.cloudformation.model.StackEvent;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.cloudformation.model.StackUpdateSnapshot;
import io.github.hectorvent.floci.services.cloudformation.model.TemplateSummary;
import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnDynamicReferences;
import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnResourceDispatcher;
@@ -66,6 +67,30 @@ public class CloudFormationService implements ResourceProvider {
private static final int MAX_OPERATION_THREADS = 16;
private static final int MAX_QUEUED_OPERATIONS = 128;
// Texts a client may match on, kept here so each has one place to change. All but
// SKIP_NESTED_UNSUPPORTED are the texts real CloudFormation emits, measured against a real
// account; that one is Floci's own, for a skip AWS accepts and Floci does not support.
private static final String UPDATE_REFUSED = "Stack:%s is in %s state and can not be updated.";
private static final String CONTINUE_ROLLBACK_REFUSED =
"RollbackUpdatedStack cannot be called from current stack status";
private static final String SKIP_NOT_UPDATE_FAILED =
"Only the resources in UPDATE_FAILED state are allowed to be skipped";
private static final String SKIP_NOT_IN_STACK = "Resources with logicalIds [%s] do not belong to stack %s";
private static final String SKIP_NESTED_STACK_MISSING = "Stack [%s] does not exist";
private static final String SKIP_NESTED_UNSUPPORTED =
"Skipping a resource of a nested stack is not supported by Floci: %s";
private static final String SKIPPED_RESOURCE_REASON = "Resource skipped during UpdateRollback";
private static final String FORCED_ROLLBACK_COMPLETE_REASON =
"One or more resources were skipped during UpdateRollback. These resources were not rolled"
+ " back and may have drifted from the template. Run drift detection (DetectStackDrift)"
+ " to review their current state before your next update.";
private static final String FORCE_WITH_SKIPS = "ForceRollback and ResourcesToSkip are mutually exclusive";
private static final String CONTINUE_STACK_MISSING = "Stack: %s does not exist";
private static final Pattern CONTINUE_STACK_NAME =
Pattern.compile("[a-zA-Z][-a-zA-Z0-9]*|arn:[-a-zA-Z0-9:/._+]*");
private static final Pattern RESOURCE_TO_SKIP =
Pattern.compile("[a-zA-Z0-9]+|[a-zA-Z][-a-zA-Z0-9]*\\.[a-zA-Z0-9]+");
private final ConcurrentHashMap<String, Stack> stacks = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, DeletedStackEntry> deletedStacks = new ConcurrentHashMap<>();
// Account-scoped exports registry: account:region:exportName -> exportValue
@@ -235,10 +260,15 @@ public class CloudFormationService implements ResourceProvider {
* rather than by suffix, because {@code UPDATE_ROLLBACK_COMPLETE} ends the same way and is a
* perfectly updatable stack: it is where an update that failed and rolled back settles, and
* retrying the update is how it is repaired.
*
* <p>{@code UPDATE_ROLLBACK_FAILED} is refused because the rollback of the last update left
* resources in an unknown state, and an update would build on them. ContinueUpdateRollback
* finishes that rollback, and DeleteStack removes the stack; both stay allowed.
*/
static boolean refusesUpdate(String status) {
return status != null
&& (status.endsWith("_IN_PROGRESS") || "ROLLBACK_COMPLETE".equals(status));
&& (status.endsWith("_IN_PROGRESS") || "ROLLBACK_COMPLETE".equals(status)
|| "UPDATE_ROLLBACK_FAILED".equals(status));
}
/**
@@ -470,8 +500,7 @@ public class CloudFormationService implements ResourceProvider {
}
private static AwsException cannotBeUpdated(String stackId, String status) {
return new AwsException("ValidationError",
"Stack:" + stackId + " is in " + status + " state and can not be updated.", 400);
return new AwsException("ValidationError", UPDATE_REFUSED.formatted(stackId, status), 400);
}
// AWS refuses a deleted stack's ID by its state, while a name, or an ID it never issued,
@@ -1050,6 +1079,91 @@ public class CloudFormationService implements ResourceProvider {
}
}
// ── ContinueUpdateRollback ────────────────────────────────────────────────
/**
* Retries the rollback of a stack in {@code UPDATE_ROLLBACK_FAILED}, the only status AWS accepts
* it from; any other status is refused here, before anything changes, as is a malformed request
* or a forced rollback that also names skips. The status check and the move to
* {@code UPDATE_ROLLBACK_IN_PROGRESS} are one step under the stack's lock, so a racing update or
* second retry is refused. Everything else, the {@code resourcesToSkip} checks against the stack
* included, runs on the operation executor, because AWS accepts the call and reports a bad skip
* as the reason the rollback failed again.
*/
public Future<?> continueUpdateRollback(
String stackNameOrArn, List<String> resourcesToSkip, boolean forceRollback, String region) {
validateContinueUpdateRollback(stackNameOrArn, resourcesToSkip);
if (forceRollback && !resourcesToSkip.isEmpty()) {
throw new AwsException("ValidationError", FORCE_WITH_SKIPS, 400);
}
String accountId = currentAccount();
Stack found = resolveStack(stackNameOrArn, region, accountId);
if (found == null) {
throw new AwsException("ValidationError", CONTINUE_STACK_MISSING.formatted(stackNameOrArn), 400);
}
String stackName = found.getStackName();
List<String> skips = List.copyOf(resourcesToSkip);
StackMutationSnapshot[] snapshot = new StackMutationSnapshot[1];
Stack stack = stacks.compute(stackKey(accountId, stackName, region), (k, existing) -> {
if (existing == null) {
throw new AwsException("ValidationError", CONTINUE_STACK_MISSING.formatted(stackNameOrArn), 400);
}
if (!"UPDATE_ROLLBACK_FAILED".equals(existing.getStatus())) {
throw new AwsException("ValidationError", CONTINUE_ROLLBACK_REFUSED, 400);
}
snapshot[0] = snapshot(existing);
existing.setStatus("UPDATE_ROLLBACK_IN_PROGRESS");
addEvent(existing, existing.getStackName(), existing.getStackId(),
"AWS::CloudFormation::Stack", "UPDATE_ROLLBACK_IN_PROGRESS", "User Initiated");
return existing;
});
try {
persistStack(stack);
return submitOperation(() -> RequestScopes.runAs(accountId, region,
() -> resumeUpdateRollback(stack, skips, forceRollback, region)));
} catch (AwsException e) {
if ("LimitExceededException".equals(e.getErrorCode())) {
Stack restored = restoreStack(accountId, stackName, region, snapshot[0]);
if (restored != null) {
persistStack(restored);
}
}
throw e;
}
}
/**
* Refuses a malformed ContinueUpdateRollback request as AWS does, before the stack is looked up:
* every constraint violation of {@code StackName} and {@code ResourcesToSkip} in one
* {@code ValidationError}, with AWS's texts. A skip has no length limit.
*/
private static void validateContinueUpdateRollback(String stackNameOrArn, List<String> resourcesToSkip) {
List<String> violations = new ArrayList<>();
if (stackNameOrArn == null) {
violations.add("Value null at 'stackName' failed to satisfy constraint: Member must not be null");
} else {
if (stackNameOrArn.isEmpty()) {
violations.add("Value '' at 'stackName' failed to satisfy constraint:"
+ " Member must have length greater than or equal to 1");
}
if (!CONTINUE_STACK_NAME.matcher(stackNameOrArn).matches()) {
violations.add("Value '" + stackNameOrArn + "' at 'stackName' failed to satisfy constraint:"
+ " Member must satisfy regular expression pattern: " + CONTINUE_STACK_NAME.pattern());
}
}
if (resourcesToSkip.stream().anyMatch(skip -> !RESOURCE_TO_SKIP.matcher(skip).matches())) {
violations.add("Value '" + resourcesToSkip + "' at 'resourcesToSkip' failed to satisfy constraint:"
+ " Member must satisfy constraint: [Member must satisfy regular expression pattern: "
+ RESOURCE_TO_SKIP.pattern() + "]");
}
if (!violations.isEmpty()) {
String prefix = violations.size() == 1
? "1 validation error detected: "
: violations.size() + " validation errors detected: ";
throw new AwsException("ValidationError", prefix + String.join("; ", violations), 400);
}
}
private StackMutationSnapshot snapshot(Stack stack) {
Map<ChangeSet, ChangeSetState> states = new IdentityHashMap<>();
for (ChangeSet changeSet : stack.changeSetsSnapshot().values()) {
@@ -2115,6 +2229,109 @@ public class CloudFormationService implements ResourceProvider {
}
}
/**
* The body of ContinueUpdateRollback, on a stack already moved to UPDATE_ROLLBACK_IN_PROGRESS:
* checks {@code resourcesToSkip}, reports each skipped resource UPDATE_COMPLETE, runs the
* rollback walker again over the resources still owed a rollback, and finishes as the rollback
* of the failed update does. A bad skip touches no resource: the stack returns to
* UPDATE_ROLLBACK_FAILED with the reason, keeping what a later retry needs. A forced rollback
* skips every UPDATE_FAILED resource of the stack instead, and says so when it completes.
*/
void resumeUpdateRollback(Stack stack, List<String> resourcesToSkip, boolean forceRollback, String region) {
try {
List<String> skips = forceRollback
? stack.resourcesSnapshot().values().stream()
.filter(resource -> "UPDATE_FAILED".equals(resource.getStatus()))
.map(StackResource::getLogicalId)
.toList()
: resourcesToSkip;
String invalidSkip = invalidSkipReason(stack, skips);
if (invalidSkip != null) {
// Reason and event before the status, which is the volatile publishing write.
stack.setStatusReason(invalidSkip);
addEvent(stack, stack.getStackName(), stack.getStackId(),
"AWS::CloudFormation::Stack", "UPDATE_ROLLBACK_FAILED", invalidSkip);
stack.setStatus("UPDATE_ROLLBACK_FAILED");
persistStack(stack);
LOG.warnv("Stack {0} update rollback not continued: {1}", stack.getStackName(), invalidSkip);
return;
}
StackUpdateSnapshot previousState = stack.getUpdateRollbackSnapshot();
List<String> owedResourceIds = stack.getUpdateRollbackResourceIds();
if (previousState == null || owedResourceIds == null) {
// A stack stored before the snapshot was kept: every UPDATE_FAILED resource is owed a
// rollback, and the template the stack holds now is the one it keeps.
previousState = snapshotForUpdate(stack);
owedResourceIds = new ArrayList<>();
for (StackResource previous : previousState.resources().values()) {
if ("UPDATE_FAILED".equals(previous.getStatus())) {
owedResourceIds.add(previous.getLogicalId());
previous.setStatus("UPDATE_COMPLETE");
previous.setStatusReason(null);
}
}
}
Set<String> owed = new LinkedHashSet<>(owedResourceIds);
for (String logicalId : skips) {
StackResource resource = stack.getResources().get(logicalId);
resource.setStatus("UPDATE_COMPLETE");
resource.setStatusReason(SKIPPED_RESOURCE_REASON);
addEvent(stack, logicalId, resource.getPhysicalId(), resource.getResourceType(),
"UPDATE_COMPLETE", SKIPPED_RESOURCE_REASON);
owed.remove(logicalId);
}
// Recorded before and after the walker, so an unexpected failure never re-runs a skip or a done rollback.
stack.setUpdateRollbackSnapshot(previousState);
stack.setUpdateRollbackResourceIds(List.copyOf(owed));
List<String> rollbackFailures = rollbackUpdatedResources(
stack, previousState.resources(), owed, region);
stack.setUpdateRollbackResourceIds(List.copyOf(rollbackFailures));
finishUpdateRollback(stack, region, previousState, rollbackFailures,
forceRollback && !skips.isEmpty() ? FORCED_ROLLBACK_COMPLETE_REASON : null);
} catch (Exception e) {
// Nobody reads this worker's future: a stack left UPDATE_ROLLBACK_IN_PROGRESS here could
// never be continued. The snapshot and the resources still owed a rollback are kept.
LOG.errorv("Stack {0} update rollback could not continue: {1}",
stack.getStackName(), e.getMessage());
stack.setStatusReason(e.getMessage());
addEvent(stack, stack.getStackName(), stack.getStackId(),
"AWS::CloudFormation::Stack", "UPDATE_ROLLBACK_FAILED", e.getMessage());
stack.setStatus("UPDATE_ROLLBACK_FAILED");
persistStack(stack);
}
}
/**
* Why AWS would fail the rollback over {@code resourcesToSkip}, or {@code null} when every
* skip is an UPDATE_FAILED resource of this stack. AWS names a nested stack's resource
* {@code NestedStackName.ResourceLogicalID}; Floci does not support skipping one.
*/
private static String invalidSkipReason(Stack stack, List<String> resourcesToSkip) {
List<String> notInStack = new ArrayList<>();
boolean notUpdateFailed = false;
for (String logicalId : resourcesToSkip) {
int dot = logicalId.indexOf('.');
if (dot >= 0) {
String nestedStackId = logicalId.substring(0, dot);
StackResource nested = stack.getResources().get(nestedStackId);
if (nested == null || !"AWS::CloudFormation::Stack".equals(nested.getResourceType())) {
return SKIP_NESTED_STACK_MISSING.formatted(nestedStackId);
}
return SKIP_NESTED_UNSUPPORTED.formatted(logicalId);
}
StackResource resource = stack.getResources().get(logicalId);
if (resource == null) {
notInStack.add(logicalId);
} else if (!"UPDATE_FAILED".equals(resource.getStatus())) {
notUpdateFailed = true;
}
}
if (!notInStack.isEmpty()) {
return SKIP_NOT_IN_STACK.formatted(String.join(", ", notInStack), stack.getStackName());
}
return notUpdateFailed ? SKIP_NOT_UPDATE_FAILED : null;
}
private void rollbackFailedUpdate(
Stack stack,
String region,
@@ -2128,6 +2345,23 @@ public class CloudFormationService implements ResourceProvider {
List<String> rollbackFailures = rollbackUpdatedResources(
stack, previousState.resources(), attemptedResourceIds, region);
finishUpdateRollback(stack, region, previousState, rollbackFailures, null);
}
/**
* Ends an update rollback, the first one and every ContinueUpdateRollback retry alike: restores
* the parameters, outputs and exports, and the template once every resource rolled back, then
* settles the stack in UPDATE_ROLLBACK_COMPLETE, with {@code completionReason} as its reason, or
* UPDATE_ROLLBACK_FAILED. A rollback that fails keeps the snapshot and the resources still owed
* a rollback on the stack for a retry.
*/
private void finishUpdateRollback(
Stack stack,
String region,
StackUpdateSnapshot previousState,
List<String> rollbackFailures,
String completionReason) {
List<String> owedResourceIds = List.copyOf(rollbackFailures);
// Parameters are independent of resource-rollback outcome - always restore them to the last
// successfully deployed values, even when resource rollback itself fails and the stack lands
// in UPDATE_ROLLBACK_FAILED, so DescribeStacks and later change-set previews don't keep
@@ -2152,19 +2386,24 @@ public class CloudFormationService implements ResourceProvider {
}
stack.setLastUpdatedTime(now());
if (rollbackFailures.isEmpty()) {
stack.setStatus("UPDATE_ROLLBACK_COMPLETE");
stack.setStatusReason(null);
stack.setUpdateRollbackSnapshot(null);
stack.setUpdateRollbackResourceIds(null);
// Reason and event before the status, which is the volatile publishing write.
stack.setStatusReason(completionReason);
addEvent(stack, stack.getStackName(), stack.getStackId(),
"AWS::CloudFormation::Stack", "UPDATE_ROLLBACK_COMPLETE", null);
"AWS::CloudFormation::Stack", "UPDATE_ROLLBACK_COMPLETE", completionReason);
stack.setStatus("UPDATE_ROLLBACK_COMPLETE");
LOG.infov("Stack {0} update rolled back (UPDATE_ROLLBACK_COMPLETE)",
stack.getStackName());
} else {
String reason = "The following resource(s) failed to roll back: ["
+ String.join(", ", rollbackFailures) + "].";
stack.setStatus("UPDATE_ROLLBACK_FAILED");
stack.setUpdateRollbackSnapshot(previousState);
stack.setUpdateRollbackResourceIds(owedResourceIds);
stack.setStatusReason(reason);
addEvent(stack, stack.getStackName(), stack.getStackId(),
"AWS::CloudFormation::Stack", "UPDATE_ROLLBACK_FAILED", reason);
stack.setStatus("UPDATE_ROLLBACK_FAILED");
LOG.errorv("Stack {0} update rollback failed: {1}", stack.getStackName(), reason);
}
persistStack(stack);
@@ -2382,17 +2621,6 @@ public class CloudFormationService implements ResourceProvider {
return copy;
}
private record StackUpdateSnapshot(
String templateBody,
String originalTemplateBody,
Map<String, String> parameters,
Map<String, String> resolvedParameters,
Map<String, String> outputs,
Map<String, String> exports,
Map<String, String> outputExportNames,
Map<String, StackResource> resources) {
}
private record UpdateCleanupFailure(
String logicalId, String physicalId, String failureReason) {
}
@@ -2593,6 +2821,8 @@ public class CloudFormationService implements ResourceProvider {
throw new IllegalStateException(reason);
}
stack.setUpdateRollbackSnapshot(null);
stack.setUpdateRollbackResourceIds(null);
// Before the status, never after. Status is the volatile publishing write, so a reader
// that observes DELETE_COMPLETE is guaranteed to observe every write that preceded it.
// Assigned afterwards, a concurrent DescribeStacks can report the terminal status with
@@ -64,6 +64,14 @@ public class Stack {
private Map<String, ChangeSet> changeSets = orderedMap();
private Map<String, String> tags = orderedMap();
private boolean enableTerminationProtection = false;
/**
* Set when an update rollback fails, and kept until a rollback completes or the stack is
* deleted: the state from before the failed update, and the logical ids whose rollback has not
* succeeded yet. A ContinueUpdateRollback retry reads them in UPDATE_ROLLBACK_IN_PROGRESS, so
* that transition must keep them. Absent on a stack stored before they were kept.
*/
private StackUpdateSnapshot updateRollbackSnapshot;
private List<String> updateRollbackResourceIds;
public String getStackId() { return stackId; }
public void setStackId(String stackId) { this.stackId = stackId; }
@@ -187,4 +195,8 @@ public class Stack {
}
public boolean isEnableTerminationProtection() { return enableTerminationProtection; }
public void setEnableTerminationProtection(boolean enableTerminationProtection) { this.enableTerminationProtection = enableTerminationProtection; }
public StackUpdateSnapshot getUpdateRollbackSnapshot() { return updateRollbackSnapshot; }
public void setUpdateRollbackSnapshot(StackUpdateSnapshot updateRollbackSnapshot) { this.updateRollbackSnapshot = updateRollbackSnapshot; }
public List<String> getUpdateRollbackResourceIds() { return updateRollbackResourceIds; }
public void setUpdateRollbackResourceIds(List<String> updateRollbackResourceIds) { this.updateRollbackResourceIds = updateRollbackResourceIds; }
}
@@ -0,0 +1,24 @@
package io.github.hectorvent.floci.services.cloudformation.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.Map;
/**
* A stack's state from before an update, which the rollback of that update restores. A rollback
* that fails keeps it on the stack, so ContinueUpdateRollback can run the rollback again after the
* update that took it has returned, or after a restart.
*/
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public record StackUpdateSnapshot(
String templateBody,
String originalTemplateBody,
Map<String, String> parameters,
Map<String, String> resolvedParameters,
Map<String, String> outputs,
Map<String, String> exports,
Map<String, String> outputExportNames,
Map<String, StackResource> resources) {
}
@@ -23,9 +23,10 @@ import java.util.Set;
* or put back when a later resource fails the update and it rolls back.
*
* <p>The record on the resource lists every entity owed a delete, not only the one this update
* displaced: a replacement whose rollback could not delete it stays listed, so the next committed
* update or the stack delete removes it instead of it being forgotten. The prior entity's id and
* attributes are kept beside the list only while this update's replacement can still be rolled back.
* displaced: a replacement whose rollback could not delete it stays listed, so a retried rollback,
* the next committed update or the stack delete removes it instead of it being forgotten. The prior
* entity's id and attributes are kept beside the list only while this update's replacement can
* still be rolled back.
*
* <p>{@code PipesCfnProvisioner} carries its own richer record because it also snapshots the
* entity's configuration for in-place rollback; this is the plain version for types without one.
@@ -70,6 +71,7 @@ public final class ReplacementCleanup {
boolean replaced) {
ObjectNode cleanup = readOrEmpty(r);
cleanup.remove("priorPhysicalId");
cleanup.remove("undeletedReplacement");
cleanup.remove("priorAttributes");
cleanup.put("region", ctx.region());
String prior = ctx.priorPhysicalId();
@@ -99,11 +101,28 @@ public final class ReplacementCleanup {
* standing again, and a resource still owing its delete would put it on the next cleanup's list.
* A replacement whose delete fails is listed in its place, never retained (a failed update
* created it), so the next committed update or the stack delete removes it, and the failure
* propagates so the stack reports it. Returns false when this update replaced nothing, which
* propagates so the stack reports it. A retry (CloudFormation {@code ContinueUpdateRollback})
* re-attempts that failed delete. Returns false when this update replaced nothing, which
* leaves the engine's handling of in-place updates unchanged.
*/
static boolean rollback(StackResource r, Deleter deleter) {
ObjectNode cleanup = read(r);
String undeleted = cleanup == null ? null : cleanup.path("undeletedReplacement").asText(null);
if (undeleted != null) {
JsonNode owed = null;
for (JsonNode entry : cleanup.path("displaced")) {
if (undeleted.equals(entry.path("physicalId").asText(null))) {
owed = entry;
}
}
if (owed == null) {
cleanup.remove("undeletedReplacement");
write(r, cleanup);
return true;
}
return deleteReplacement(r, cleanup, deleter, owed.path("resourceType").asText(r.getResourceType()),
undeleted);
}
if (cleanup == null || cleanup.path("priorPhysicalId").asText(null) == null) {
return false;
}
@@ -125,13 +144,23 @@ public final class ReplacementCleanup {
if (prior.equals(replacement) && !CfnResourceDispatcher.isStub(prior, r.getAttributes())) {
return true;
}
return deleteReplacement(r, cleanup, deleter, r.getResourceType(), replacement);
}
/** Deletes a rolled-back replacement; one whose delete throws stays owed and named for the retry. */
private static boolean deleteReplacement(StackResource r, ObjectNode cleanup, Deleter deleter,
String resourceType, String replacement) {
try {
deleter.delete(r.getResourceType(), replacement, region(cleanup));
deleter.delete(resourceType, replacement, region(cleanup));
} catch (RuntimeException deleteFailure) {
addDisplaced(cleanup, replacement, r.getResourceType(), region(cleanup), false);
addDisplaced(cleanup, replacement, resourceType, region(cleanup), false);
cleanup.put("undeletedReplacement", replacement);
write(r, cleanup);
throw deleteFailure;
}
removeDisplaced(cleanup, replacement);
cleanup.remove("undeletedReplacement");
write(r, cleanup);
return true;
}