Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cloudformation): reconcile AWS::Pipes::Pipe on UpdateStack (#2970)

Facundo Paredesa31a9599fd5f
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java
index a3ade4a491..79663040d9 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java
@@ -17,6 +17,7 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.Provision
import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnDynamicReferences;
import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnResourceProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.Ec2SecurityGroupRuleCfnProvisioner;
+import io.github.hectorvent.floci.services.cloudformation.provisioners.UpdateCleanupResult;
import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
import io.github.hectorvent.floci.services.eventbridge.model.BatchParameters;
@@ -4555,10 +4556,23 @@ public class CloudFormationResourceProvisioner {
}
}
+ /**
+ * One attempt at deleting what this update's replacement displaced. A resource type with an
+ * extracted provisioner owns its own cleanup; only the types still living in this class fall
+ * through to the Step Functions arm below.
+ */
UpdateCleanupResult completeUpdate(StackResource resource) {
+ Optional<CfnResourceProvisioner> owner =
+ resourceRegistry.forType(resource.getResourceType());
+ if (owner.isPresent()) {
+ UpdateCleanupResult ownResult = owner.get().completeUpdate(resource);
+ if (ownResult.applicable()) {
+ return ownResult;
+ }
+ }
String rawSnapshot = resource.getAttributes().get(SFN_UPDATE_SNAPSHOT_ATTR);
if (rawSnapshot == null) {
- return new UpdateCleanupResult(false, true, null, 0, null);
+ return UpdateCleanupResult.notApplicable();
}
try {
JsonNode snapshot = objectMapper.readTree(rawSnapshot);
@@ -4608,7 +4622,20 @@ public class CloudFormationResourceProvisioner {
}
}
+ /**
+ * The physical id this update displaced, announced as DELETE_IN_PROGRESS before the stack
+ * update closes. An extracted provisioner answers for its own types; the rest fall through to
+ * the Step Functions arm.
+ */
String updateCleanupPhysicalId(StackResource resource) {
+ Optional<CfnResourceProvisioner> owner =
+ resourceRegistry.forType(resource.getResourceType());
+ if (owner.isPresent()) {
+ String ownCleanupPhysicalId = owner.get().updateCleanupPhysicalId(resource);
+ if (ownCleanupPhysicalId != null) {
+ return ownCleanupPhysicalId;
+ }
+ }
if ("Retain".equals(resource.getUpdateReplacePolicy())) {
return null;
}
@@ -4629,7 +4656,17 @@ public class CloudFormationResourceProvisioner {
}
}
+ /**
+ * Whether this update replaced the resource's physical entity, so the stack has cleanup
+ * pending. An extracted provisioner answers for its own types; the rest fall through to the
+ * Step Functions arm.
+ */
boolean hasReplacementUpdate(StackResource resource) {
+ Optional<CfnResourceProvisioner> owner =
+ resourceRegistry.forType(resource.getResourceType());
+ if (owner.isPresent() && owner.get().hasReplacementUpdate(resource)) {
+ return true;
+ }
String rawSnapshot = resource.getAttributes().get(SFN_UPDATE_SNAPSHOT_ATTR);
if (rawSnapshot == null) {
return false;
@@ -4643,11 +4680,24 @@ public class CloudFormationResourceProvisioner {
}
}
+ /**
+ * Drops the cleanup bookkeeping this update left on the resource. An extracted provisioner
+ * drops its own; the Step Functions snapshot below is dropped for the types still living here.
+ */
void clearUpdate(StackResource resource) {
+ resourceRegistry.forType(resource.getResourceType())
+ .ifPresent(owner -> owner.clearUpdate(resource));
resource.getAttributes().remove(SFN_UPDATE_SNAPSHOT_ATTR);
}
boolean rollbackUpdate(StackResource resource) {
+ // A resource type with an extracted provisioner owns its own restore; only the types still
+ // living in this class fall through to the Step Functions arm below.
+ Optional<CfnResourceProvisioner> owner =
+ resourceRegistry.forType(resource.getResourceType());
+ if (owner.isPresent() && owner.get().rollbackUpdate(resource)) {
+ return true;
+ }
String rawSnapshot = resource.getAttributes().get(SFN_UPDATE_SNAPSHOT_ATTR);
if (rawSnapshot == null) {
return false;
@@ -4720,14 +4770,6 @@ public class CloudFormationResourceProvisioner {
}
}
- record UpdateCleanupResult(
- boolean applicable,
- boolean complete,
- String previousPhysicalId,
- int attempts,
- String failureReason) {
- }
-
private String resolveStateMachineDefinition(JsonNode props, CloudFormationTemplateEngine engine) {
if (props == null) {
throw new AwsException(
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java
index 7867677521..6ef1f403b0 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java
@@ -16,6 +16,7 @@ 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.TemplateSummary;
import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnRollback;
+import io.github.hectorvent.floci.services.cloudformation.provisioners.UpdateCleanupResult;
import io.github.hectorvent.floci.services.s3.S3Service;
import io.github.hectorvent.floci.services.ssm.SsmService;
import com.fasterxml.jackson.core.type.TypeReference;
@@ -292,6 +293,21 @@ public class CloudFormationService implements ResourceProvider {
throw new AwsException("AlreadyExistsException",
"Stack [" + stackName + "] already exists", 400);
}
+ // A status ending in _IN_PROGRESS says an operation owns the stack: a create, an
+ // update, a rollback or the cleanup phase of a committed update. AWS refuses to
+ // start a second one over it, and offers nothing that finishes a phase whose
+ // process is gone - DeleteStack is the only way out of one. Refusing here keeps
+ // every abandoned phase out of the next update's transaction.
+ //
+ // The message is the one real CloudFormation emits, down to its own "can not"
+ // spelling and the stack id carried as "Stack:<arn>" with no space: clients match
+ // on this string.
+ if (!isCreateType && existing.getStatus() != null
+ && existing.getStatus().endsWith("_IN_PROGRESS")) {
+ throw new AwsException("ValidationError",
+ "Stack:" + existing.getStackId() + " is in " + existing.getStatus()
+ + " state and can not be updated.", 400);
+ }
target = existing;
}
@@ -1307,8 +1323,7 @@ public class CloudFormationService implements ResourceProvider {
null);
}
while (true) {
- CloudFormationResourceProvisioner.UpdateCleanupResult result =
- provisioner.completeUpdate(resource);
+ UpdateCleanupResult result = provisioner.completeUpdate(resource);
if (!result.applicable()) {
break;
}
@@ -1778,6 +1793,13 @@ public class CloudFormationService implements ResourceProvider {
Collections.reverse(resources); // Dependents go before what they depend on
List<String> failedResources = new ArrayList<>();
+ // The walk below addresses each resource by its physical id, which names the entity the
+ // last update left in place. An entity displaced by a replacement whose cleanup phase
+ // never ended is named only by the cleanup the resource still carries, so the stack
+ // deletes that one too: nothing else ever will.
+ for (UpdateCleanupFailure displacedFailure : finishCommittedResourceCleanup(stack)) {
+ failedResources.add(displacedFailure.logicalId());
+ }
for (StackResource resource : resources) {
// CREATE_COMPLETE/UPDATE_COMPLETE: first delete attempt. DELETE_FAILED: a previous
// delete left the resource behind (e.g. the bucket was non-empty); AWS re-attempts
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceProvisioner.java
index 30cac5b62f..602d19ee84 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceProvisioner.java
@@ -34,4 +34,49 @@ public interface CfnResourceProvisioner {
default void delete(StackResource resource, String region) {
delete(resource.getResourceType(), resource.getPhysicalId(), region);
}
+
+ /**
+ * Puts the physical entity back to the configuration it had before the failed stack update
+ * that is now rolling back, and returns whether it did. Only a provisioner that snapshots
+ * that configuration before it mutates can answer true; {@code CloudFormationService} reports
+ * the resource as UPDATE_FAILED with "Rollback is not implemented" on false.
+ */
+ default boolean rollbackUpdate(StackResource resource) {
+ return false;
+ }
+
+ /**
+ * The physical id of the entity this update's replacement displaced, or null when this type
+ * owes no replacement cleanup. {@code CloudFormationService} announces it as DELETE_IN_PROGRESS
+ * before the stack update is closed, and names it in the status reason when the delete fails.
+ * A type whose {@code UpdateReplacePolicy} is {@code Retain} answers null: nothing is deleted.
+ */
+ default String updateCleanupPhysicalId(StackResource resource) {
+ return null;
+ }
+
+ /**
+ * Runs one attempt at deleting the displaced entity, after the stack update has committed.
+ * Called again for each retry, so the attempt count and the last failure live on the resource
+ * rather than in the caller. The default reports no cleanup owed.
+ */
+ default UpdateCleanupResult completeUpdate(StackResource resource) {
+ return UpdateCleanupResult.notApplicable();
+ }
+
+ /**
+ * Whether this update replaced the physical entity, so the stack has cleanup pending and enters
+ * UPDATE_COMPLETE_CLEANUP_IN_PROGRESS. The default reports no replacement.
+ */
+ default boolean hasReplacementUpdate(StackResource resource) {
+ return false;
+ }
+
+ /**
+ * Drops the bookkeeping the replacement cleanup kept on the resource, once the displaced entity
+ * is gone or its deletion is given up on. The default has none to drop.
+ */
+ default void clearUpdate(StackResource resource) {
+ // no-op by default: a type with no replacement cleanup records nothing to clear
+ }
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java
index b0edabbe9a..c250e58d6a 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java
@@ -36,6 +36,24 @@ public final class CfnRollback {
*/
public static final String UPDATE_ROLLBACK_FAILURE_ATTR = "__FlociUpdateRollbackFailure";
+ /**
+ * Holds the configuration a pipe carried before the update in flight mutated it, so a failed
+ * stack update can put it back. Written by {@code PipesCfnProvisioner} before its first
+ * mutating call and spent by its {@code rollbackUpdate}. Lives here beside the other rollback
+ * markers rather than on the provisioner, so the marker names stay in one place.
+ */
+ public static final String PIPE_UPDATE_SNAPSHOT_ATTR = "__FlociPipeUpdateSnapshot";
+
+ /**
+ * Holds the pipe a rename displaced: the name it still lives under, the region that addresses
+ * it, how many times deleting it has been attempted, and when the replacement was created.
+ * Written by {@code PipesCfnProvisioner} when it creates the replacement, and spent by whichever
+ * end the update reaches: {@code completeUpdate} deletes the displaced pipe once the update has
+ * committed, and {@code rollbackUpdate} points the resource back at it and deletes the
+ * replacement when the update fails instead.
+ */
+ public static final String PIPE_RENAME_CLEANUP_ATTR = "__FlociPipeRenameCleanup";
+
private CfnRollback() {
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/PipesCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/PipesCfnProvisioner.java
index 024b431d69..93a94d5563 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/PipesCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/PipesCfnProvisioner.java
@@ -1,12 +1,17 @@
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.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.pipes.PipesService;
-import io.github.hectorvent.floci.services.pipes.model.Pipe;
import io.github.hectorvent.floci.services.pipes.model.DesiredState;
+import io.github.hectorvent.floci.services.pipes.model.Pipe;
import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import org.jboss.logging.Logger;
import java.util.HashMap;
import java.util.List;
@@ -17,12 +22,17 @@ import java.util.Set;
@ApplicationScoped
public class PipesCfnProvisioner implements CfnResourceProvisioner {
+ private static final Logger LOG = Logger.getLogger(PipesCfnProvisioner.class);
+
private static final int PIPE_NAME_MAX_LENGTH = 64;
private final PipesService pipesService;
+ private final ObjectMapper objectMapper;
- public PipesCfnProvisioner(PipesService pipesService) {
+ @Inject
+ public PipesCfnProvisioner(PipesService pipesService, ObjectMapper objectMapper) {
this.pipesService = pipesService;
+ this.objectMapper = objectMapper;
}
@Override
@@ -32,6 +42,13 @@ public class PipesCfnProvisioner implements CfnResourceProvisioner {
@Override
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
+ // A snapshot describes the update in flight. The one an earlier update left behind goes
+ // before this run decides whether it mutates the pipe at all, so a rollback never puts back
+ // a target the pipe stopped carrying two updates ago. The rename cleanup goes with it: only
+ // a rename this run performs owes a pipe deleted after the update commits.
+ r.getAttributes().remove(CfnRollback.PIPE_UPDATE_SNAPSHOT_ATTR);
+ r.getAttributes().remove(CfnRollback.PIPE_RENAME_CLEANUP_ATTR);
+ String priorPhysicalId = ctx.priorPhysicalId();
String name = ctx.stablePhysicalName(ctx.resolveOptional(props, "Name"),
r.getLogicalId(), PIPE_NAME_MAX_LENGTH, false);
@@ -51,32 +68,404 @@ public class PipesCfnProvisioner implements CfnResourceProvisioner {
Map<String, String> tags = parseCfnTags(props != null ? props.get("Tags") : null, ctx);
// provision is also the update path. createPipe throws ConflictException on an existing
- // name, and stablePhysicalName now keeps that name steady across updates, so a second
+ // name, and stablePhysicalName keeps that name steady across updates, so a second
// UpdateStack must reconcile the pipe rather than recreate it. A replacing update derives a
// different name and still creates, which is why this asks reusesPriorEntity rather than
- // isUpdate.
- Pipe pipe;
- if (ctx.reusesPriorEntity(name)) {
- Pipe existing = pipesService.describePipe(name, ctx.region());
- // Source is a createOnly property. With the name reused there is no replacement to
- // move to, which is the update CloudFormation refuses for a custom-named resource, so
- // it is refused here rather than silently kept on the old source.
- if (source != null && !source.equals(existing.getSource())) {
- throw new AwsException("ValidationError",
- "Updating Source requires resource replacement, which is not supported.", 400);
- }
- pipe = pipesService.updatePipe(name, target, roleArn, description, desiredState,
+ // isUpdate. Null here is a first create, a rename, or a pipe deleted out of band since the
+ // prior deploy, and all three create.
+ Pipe existingPipe = ctx.reusesPriorEntity(name) ? pipeOnFile(name, ctx.region()) : null;
+
+ Pipe pipe = existingPipe != null
+ ? updateExistingPipe(r, existingPipe, name, source, target, roleArn, description,
+ desiredState, enrichment, sourceParameters, targetParameters,
+ enrichmentParameters, tags, ctx)
+ : createPipeAndRecordRenamedPrior(r, name, source, target, roleArn, description,
+ desiredState, enrichment, sourceParameters, targetParameters,
+ enrichmentParameters, tags, priorPhysicalId, ctx);
+
+ // Ref returns the pipe name; Fn::GetAtt Arn returns the pipe ARN.
+ r.setPhysicalId(name);
+ r.getAttributes().put("Arn", pipe.getArn());
+ }
+
+ /**
+ * Reconciles the pipe already on file under this name, tags included.
+ *
+ * <p>updatePipe and the tag calls each write the pipe as they go, so a failure among them would
+ * otherwise leave the pipe carrying an update the stack is about to disown. This path therefore
+ * restores eagerly: the pipe goes back to the snapshot taken a line earlier before the failure
+ * leaves the provisioner, and the failure itself is what reaches the caller.
+ */
+ private Pipe updateExistingPipe(StackResource r, Pipe existingPipe, String name, String source,
+ String target, String roleArn, String description,
+ DesiredState desiredState, String enrichment,
+ JsonNode sourceParameters, JsonNode targetParameters,
+ JsonNode enrichmentParameters, Map<String, String> tags,
+ ProvisionContext ctx) {
+ // A Source that is absent, null or resolves blank is a missing required property, not a
+ // changed one. The create path reports it as such, so this path reports it identically
+ // instead of blaming a replacement the template never asked for.
+ if (source == null || source.isBlank()) {
+ throw new AwsException("ValidationException", "Source is required", 400);
+ }
+ // Source is a createOnly property. With the name reused there is no replacement to move to,
+ // which is the update CloudFormation refuses for a custom-named resource, so it is refused
+ // here rather than silently kept on the old source. The 13 create-only paths nested under
+ // SourceParameters (the StartingPosition, TopicName, QueueName, VirtualHost,
+ // ConsumerGroupID and AdditionalBootstrapServers entries) are reconciled in place, where
+ // AWS replaces the pipe. That gap is left to a later change.
+ if (!source.equals(existingPipe.getSource())) {
+ throw new AwsException("ValidationError",
+ "Updating Source requires resource replacement, which is not supported.", 400);
+ }
+ snapshotPipeBeforeUpdate(r, existingPipe, ctx.region());
+ try {
+ Pipe pipe = pipesService.updatePipe(name, target, roleArn, description, desiredState,
enrichment, sourceParameters, targetParameters, enrichmentParameters,
ctx.region());
reconcileTags(pipe, tags, ctx.region());
- } else {
+ return pipe;
+ } catch (RuntimeException updateFailure) {
+ restorePipeAfterFailedUpdate(r, updateFailure);
+ throw updateFailure;
+ }
+ }
+
+ /**
+ * Puts the pipe back from the snapshot this update took, while the failure that interrupted the
+ * update is on its way out of the provisioner. The snapshot is spent here, so the rollback hook
+ * finds nothing left to act on for a resource already restored.
+ *
+ * <p>A restore that fails itself leaves the pipe in a configuration nobody recorded. Its reason
+ * goes on the resource under {@link CfnRollback#UPDATE_ROLLBACK_FAILURE_ATTR}, which is what
+ * makes the stack report UPDATE_ROLLBACK_FAILED naming this resource instead of a clean
+ * rollback, and it is attached to the update failure as suppressed so neither is lost. The
+ * restore repeats the calls the update just made, so it can come back carrying the very
+ * exception that interrupted the update; suppressing a throwable under itself is rejected by
+ * the JDK, and that one is already the failure being reported.
+ */
+ private void restorePipeAfterFailedUpdate(StackResource r, RuntimeException updateFailure) {
+ String rawSnapshot = r.getAttributes().get(CfnRollback.PIPE_UPDATE_SNAPSHOT_ATTR);
+ try {
+ restoreSnapshottedPipe(r, objectMapper.readTree(rawSnapshot));
+ } catch (RuntimeException | JsonProcessingException restoreFailure) {
+ if (restoreFailure != updateFailure) {
+ updateFailure.addSuppressed(restoreFailure);
+ }
+ String reason = restoreFailure.getMessage() != null
+ ? restoreFailure.getMessage()
+ : restoreFailure.getClass().getSimpleName();
+ LOG.errorv("Could not restore pipe {0} after a failed update: {1}",
+ r.getPhysicalId(), reason);
+ r.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR, reason);
+ }
+ }
+
+ /**
+ * Records what the pipe carries before updatePipe and the tag calls change it, so it can be put
+ * back when the stack update fails: by {@link #restorePipeAfterFailedUpdate} when this pipe's
+ * own update is what failed, and by {@link #rollbackUpdate} when a later resource is. Only the
+ * properties
+ * updatePipe accepts, plus the tags: Name, Source and Arn cannot change on this path. The
+ * region travels with them because the rollback hook is handed the stack resource alone, and
+ * name plus region is how PipesService addresses a pipe.
+ */
+ private void snapshotPipeBeforeUpdate(StackResource r, Pipe existingPipe, String region) {
+ ObjectNode snapshot = objectMapper.createObjectNode();
+ snapshot.put("region", region);
+ snapshot.put("target", existingPipe.getTarget());
+ snapshot.put("roleArn", existingPipe.getRoleArn());
+ snapshot.put("description", existingPipe.getDescription());
+ snapshot.put("desiredState", existingPipe.getDesiredState() == null
+ ? null : existingPipe.getDesiredState().name());
+ snapshot.put("enrichment", existingPipe.getEnrichment());
+ snapshot.set("sourceParameters", existingPipe.getSourceParameters());
+ snapshot.set("targetParameters", existingPipe.getTargetParameters());
+ snapshot.set("enrichmentParameters", existingPipe.getEnrichmentParameters());
+ snapshot.set("tags", objectMapper.valueToTree(
+ existingPipe.getTags() == null ? Map.of() : existingPipe.getTags()));
+ r.getAttributes().put(CfnRollback.PIPE_UPDATE_SNAPSHOT_ATTR, snapshot.toString()); }
+
+ /**
+ * Puts the pipe back to what it carried before this update, under either shape the update takes:
+ * a reconciliation in place, undone from the snapshot, or a rename, undone by pointing the
+ * resource at the prior pipe again. A run performs one or the other, so the two never meet on
+ * the same resource.
+ *
+ * <p>This is the hook for an update a <em>later</em> resource failed: this pipe's own update
+ * committed, so its snapshot is still unspent and holds the configuration to put back. When the
+ * pipe's own update is what failed, {@link #restorePipeAfterFailedUpdate} has already restored
+ * it inside {@code provision} and spent the snapshot there, and the service reaches this hook
+ * for that resource neither by the restored marker nor by the rollback-failure one.
+ */
+ @Override
+ public boolean rollbackUpdate(StackResource resource) {
+ String rawSnapshot = resource.getAttributes().get(CfnRollback.PIPE_UPDATE_SNAPSHOT_ATTR);
+ if (rawSnapshot != null) {
+ try {
+ restoreSnapshottedPipe(resource, objectMapper.readTree(rawSnapshot));
+ } catch (JsonProcessingException unreadableSnapshot) {
+ throw new IllegalStateException("Could not read the pipe update snapshot for "
+ + resource.getLogicalId(), unreadableSnapshot);
+ }
+ return true;
+ }
+ JsonNode cleanup = renameCleanup(resource);
+ if (cleanup == null) {
+ return false;
+ }
+ restoreRenamedPipe(resource, cleanup);
+ return true;
+ }
+
+ /**
+ * Undoes a rename: the resource names the prior pipe again and the replacement this update
+ * created is deleted, so the failed update leaves nothing behind. The prior pipe is only read,
+ * never written: the rename displaced it without changing it.
+ *
+ * <p>The rename cleanup is spent here. It owes a delete only once the stack update commits, and
+ * a rollback is the update never committing, so leaving it on the resource would put the pipe
+ * this rollback just restored on the next cleanup's delete list.
+ *
+ * <p>The resource names the prior pipe and the cleanup is spent before the delete is attempted,
+ * because that delete can fail and its failure leaves the provisioner. A resource still naming
+ * the replacement, with the cleanup still on it, is one the next update deletes the prior pipe
+ * for, which is the pipe this rollback exists to keep. The delete failing therefore leaves the
+ * replacement orphaned and the prior pipe standing, and the stack reports the failure.
+ */
+ private void restoreRenamedPipe(StackResource resource, JsonNode cleanup) {
+ String priorPipeName = cleanup.path("priorPipeName").asText(null);
+ String region = cleanup.path("region").asText(null);
+ Pipe priorPipe = pipeOnFile(priorPipeName, region);
+ if (priorPipe == null) {
+ throw new IllegalStateException("Cannot roll back resource " + resource.getLogicalId()
+ + ": the pipe " + priorPipeName + " it was renamed from is no longer on file.");
+ }
+ String replacementPipeName = resource.getPhysicalId();
+ resource.setPhysicalId(priorPipeName);
+ resource.getAttributes().put("Arn", priorPipe.getArn());
+ resource.getAttributes().remove(CfnRollback.PIPE_RENAME_CLEANUP_ATTR);
+ deleteReplacementThisUpdateCreated(resource, replacementPipeName, cleanup, region);
+ }
+
+ /**
+ * Deletes the pipe the failed update created under the new name, and only that one. A pipe
+ * standing under that name whose creation time is not the one recorded when this update created
+ * it belongs to someone else, so the rollback refuses and leaves both pipes where they are
+ * rather than guess.
+ *
+ * <p>{@code UpdateReplacePolicy: Retain} does not reach this delete. It governs the resource a
+ * committed replacement displaces, which here is the prior pipe, and this path keeps that pipe
+ * either way by making it the resource's physical id again. The replacement is a resource this
+ * update created, which a rollback removes like any other.
+ *
+ * <p>The replacement is named by the caller: the resource already points at the prior pipe by
+ * the time this runs, so its physical id no longer holds the name to delete.
+ */
+ private void deleteReplacementThisUpdateCreated(StackResource resource,
+ String replacementPipeName, JsonNode cleanup,
+ String region) {
+ Pipe replacement = pipeOnFile(replacementPipeName, region);
+ if (replacement == null) {
+ // Already deleted out of band: there is nothing left to remove and nothing to identify.
+ return;
+ }
+ long createdAtEpochMilli = cleanup.path("replacementCreatedAtEpochMilli").asLong(-1);
+ if (replacement.getCreationTime() == null
+ || replacement.getCreationTime().toEpochMilli() != createdAtEpochMilli) {
+ throw new IllegalStateException("Refusing to delete pipe " + replacementPipeName
+ + " while rolling back resource " + resource.getLogicalId()
+ + ": it was not created by this stack update, so both pipes are left as they are.");
+ }
+ pipesService.deletePipe(replacementPipeName, region);
+ }
+
+ /**
+ * Puts the pipe back to the configuration the snapshot holds and spends the snapshot. Tags go
+ * through the same reconciliation the update uses.
+ *
+ * <p>The snapshot holds the whole configuration, so a property the pipe did not carry is
+ * cleared: restorePipe writes every property as the snapshot has it, and what the failed update
+ * added does not survive the rollback.
+ */
+ private void restoreSnapshottedPipe(StackResource resource, JsonNode snapshot) {
+ String region = snapshot.get("region").asText();
+ String desiredState = snapshotText(snapshot, "desiredState");
+ Pipe restored = pipesService.restorePipe(resource.getPhysicalId(),
+ snapshotText(snapshot, "target"),
+ snapshotText(snapshot, "roleArn"),
+ snapshotText(snapshot, "description"),
+ desiredState == null ? null : DesiredState.valueOf(desiredState),
+ snapshotText(snapshot, "enrichment"),
+ snapshotValue(snapshot, "sourceParameters"),
+ snapshotValue(snapshot, "targetParameters"),
+ snapshotValue(snapshot, "enrichmentParameters"),
+ region);
+ Map<String, String> tags = new HashMap<>();
+ snapshot.path("tags").fields().forEachRemaining(
+ tag -> tags.put(tag.getKey(), tag.getValue().asText()));
+ reconcileTags(restored, tags, region);
+ resource.getAttributes().remove(CfnRollback.PIPE_UPDATE_SNAPSHOT_ATTR);
+ }
+
+ /** A snapshotted property, or null when the pipe carried none. */
+ private static JsonNode snapshotValue(JsonNode snapshot, String property) {
+ JsonNode value = snapshot.get(property);
+ return value == null || value.isNull() ? null : value;
+ }
+
+ private static String snapshotText(JsonNode snapshot, String property) {
+ JsonNode value = snapshotValue(snapshot, property);
+ return value == null ? null : value.asText();
+ }
+
+ /**
+ * Creates the pipe, and on a rename records the pipe the prior deploy left under the old name
+ * so {@link #completeUpdate} deletes it once the stack update has committed. Nothing is deleted
+ * here: the prior pipe outlives this call, which is what lets a rollback of a later resource
+ * failure still find it.
+ *
+ * <p>A createPipe that throws therefore leaves the original intact, and the resource is marked
+ * as already restored: rollback does not restore what was never deleted.
+ */
+ private Pipe createPipeAndRecordRenamedPrior(StackResource r, String name, String source,
+ String target, String roleArn, String description,
+ DesiredState desiredState, String enrichment,
+ JsonNode sourceParameters, JsonNode targetParameters,
+ JsonNode enrichmentParameters,
+ Map<String, String> tags, String priorPhysicalId,
+ ProvisionContext ctx) {
+ Pipe preservedPriorPipe = priorPhysicalId != null && !priorPhysicalId.equals(name)
+ ? pipeOnFile(priorPhysicalId, ctx.region())
+ : null;
+ Pipe pipe;
+ try {
pipe = pipesService.createPipe(name, source, target, roleArn, description, desiredState,
enrichment, sourceParameters, targetParameters, enrichmentParameters, tags,
ctx.region());
+ } catch (RuntimeException failure) {
+ if (preservedPriorPipe != null) {
+ // The prior pipe is untouched: the rename records its deletion for the committed
+ // update cleanup, which this failure means the stack never reaches.
+ r.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_RESTORED_ATTR, "true");
+ }
+ throw failure;
+ }
+ if (preservedPriorPipe != null) {
+ recordRenamedPriorPipe(r, priorPhysicalId, pipe, ctx.region());
}
+ return pipe;
+ }
- r.setPhysicalId(name);
- r.getAttributes().put("Arn", pipe.getArn());
+ /**
+ * Records the pipe this rename displaced, with the region that addresses it, the attempt count
+ * {@link #completeUpdate} carries across its retries, and the moment the replacement was
+ * created, which is how {@link #rollbackUpdate} tells the pipe this run created from one that
+ * already stood under the same name.
+ */
+ private void recordRenamedPriorPipe(StackResource r, String priorPhysicalId, Pipe replacement,
+ String region) {
+ ObjectNode cleanup = objectMapper.createObjectNode();
+ cleanup.put("priorPipeName", priorPhysicalId);
+ cleanup.put("region", region);
+ cleanup.put("cleanupAttempts", 0);
+ cleanup.put("replacementCreatedAtEpochMilli", replacement.getCreationTime() == null
+ ? -1 : replacement.getCreationTime().toEpochMilli());
+ r.getAttributes().put(CfnRollback.PIPE_RENAME_CLEANUP_ATTR, cleanup.toString());
+ }
+
+ @Override
+ public String updateCleanupPhysicalId(StackResource resource) {
+ if ("Retain".equals(resource.getUpdateReplacePolicy())) {
+ return null;
+ }
+ JsonNode cleanup = renameCleanup(resource);
+ return cleanup == null ? null : cleanup.path("priorPipeName").asText(null);
+ }
+
+ @Override
+ public boolean hasReplacementUpdate(StackResource resource) {
+ JsonNode cleanup = renameCleanup(resource);
+ return cleanup != null && cleanup.path("priorPipeName").asText(null) != null;
+ }
+
+ @Override
+ public void clearUpdate(StackResource resource) {
+ resource.getAttributes().remove(CfnRollback.PIPE_RENAME_CLEANUP_ATTR);
+ }
+
+ /**
+ * Deletes the pipe the rename displaced, one attempt per call, now that the stack update has
+ * committed. The attempt count and the last failure are written back onto the resource, so the
+ * caller's retries pick up where this one left off.
+ */
+ @Override
+ public UpdateCleanupResult completeUpdate(StackResource resource) {
+ JsonNode cleanup = renameCleanup(resource);
+ if (cleanup == null) {
+ return UpdateCleanupResult.notApplicable();
+ }
+ String priorPipeName = cleanup.path("priorPipeName").asText(null);
+ String region = cleanup.path("region").asText(null);
+ if (priorPipeName == null || priorPipeName.equals(resource.getPhysicalId())) {
+ return new UpdateCleanupResult(true, true, priorPipeName, 0, null);
+ }
+ if ("Retain".equals(resource.getUpdateReplacePolicy())) {
+ return new UpdateCleanupResult(true, true, priorPipeName, 0, null);
+ }
+
+ int attempts = cleanup.path("cleanupAttempts").asInt(0);
+ String failureReason = cleanup.path("cleanupFailureReason").asText(null);
+ if (attempts >= 3) {
+ return new UpdateCleanupResult(true, false, priorPipeName, attempts, failureReason);
+ }
+ try {
+ if (pipeOnFile(priorPipeName, region) != null) {
+ pipesService.deletePipe(priorPipeName, region);
+ }
+ return new UpdateCleanupResult(true, true, priorPipeName, attempts, null);
+ } catch (RuntimeException deleteFailure) {
+ attempts++;
+ ((ObjectNode) cleanup).put("cleanupAttempts", attempts);
+ ((ObjectNode) cleanup).put("cleanupFailureReason", deleteFailure.getMessage());
+ resource.getAttributes().put(CfnRollback.PIPE_RENAME_CLEANUP_ATTR, cleanup.toString());
+ return new UpdateCleanupResult(
+ true, false, priorPipeName, attempts, deleteFailure.getMessage());
+ }
+ }
+
+ /** The rename cleanup recorded on this resource, or null when no rename displaced a pipe. */
+ private JsonNode renameCleanup(StackResource resource) {
+ String rawCleanup = resource.getAttributes().get(CfnRollback.PIPE_RENAME_CLEANUP_ATTR);
+ if (rawCleanup == null) {
+ return null;
+ }
+ try {
+ return objectMapper.readTree(rawCleanup);
+ } catch (JsonProcessingException unreadableCleanup) {
+ throw new IllegalStateException("Could not read the pipe rename cleanup for "
+ + resource.getLogicalId(), unreadableCleanup);
+ }
+ }
+
+ /**
+ * The pipe on file under this name, or null when it is not there. Only the not-found error
+ * yields null: any other failure reaches the user as itself, instead of sending the caller into
+ * the create arm to report ConflictException over an unrelated fault.
+ */
+ private Pipe pipeOnFile(String name, String region) {
+ try {
+ return pipesService.describePipe(name, region);
+ } catch (AwsException lookupFailure) {
+ if (!"NotFoundException".equals(lookupFailure.getErrorCode())
+ && lookupFailure.getHttpStatus() != 404) {
+ throw lookupFailure;
+ }
+ // Expected when the pipe was deleted out of band since the prior deploy, and on the
+ // rename arm when the prior pipe is already gone; both fall back to a plain create.
+ LOG.debugv(lookupFailure, "No pipe {0} found on file", name);
+ return null;
+ }
}
@Override
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/UpdateCleanupResult.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/UpdateCleanupResult.java
new file mode 100644
index 0000000000..6db7396fd0
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/UpdateCleanupResult.java
@@ -0,0 +1,27 @@
+package io.github.hectorvent.floci.services.cloudformation.provisioners;
+
+/**
+ * What one attempt at deleting the physical entity a replacement displaced reported back, read by
+ * {@code CloudFormationService} while it finishes a committed stack update.
+ *
+ * <p>{@code applicable} false means this resource has no replacement cleanup owed at all, and the
+ * remaining fields carry nothing. Otherwise {@code complete} says whether the displaced entity is
+ * gone; on false the caller retries until {@code attempts} reaches 3 and then reports
+ * {@code failureReason} against {@code previousPhysicalId}.
+ *
+ * <p>It lives in this package rather than inside {@code CloudFormationResourceProvisioner} so an
+ * extracted {@link CfnResourceProvisioner} can return one without depending on the monolith it was
+ * extracted from.
+ */
+public record UpdateCleanupResult(
+ boolean applicable,
+ boolean complete,
+ String previousPhysicalId,
+ int attempts,
+ String failureReason) {
+
+ /** The answer of a resource type that owes no replacement cleanup. */
+ public static UpdateCleanupResult notApplicable() {
+ return new UpdateCleanupResult(false, true, null, 0, null);
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/pipes/PipesService.java b/src/main/java/io/github/hectorvent/floci/services/pipes/PipesService.java
index a846757300..82c8ec02d6 100644
--- a/src/main/java/io/github/hectorvent/floci/services/pipes/PipesService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/pipes/PipesService.java
@@ -25,6 +25,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.function.Consumer;
import java.util.stream.Collectors;
@ApplicationScoped
@@ -123,20 +124,61 @@ public class PipesService implements TagHandler, ResourceProvider {
"Pipe " + name + " does not exist.", 404));
}
+ /**
+ * The UpdatePipe API: a property given as null is left as the pipe holds it.
+ */
public Pipe updatePipe(String name, String target, String roleArn, String description,
DesiredState desiredState, String enrichment,
JsonNode sourceParameters, JsonNode targetParameters,
JsonNode enrichmentParameters, String region) {
+ return writePipeConfiguration(name, target, roleArn, description, desiredState, enrichment,
+ sourceParameters, targetParameters, enrichmentParameters, region, false);
+ }
+
+ /**
+ * Puts the pipe back to a configuration held in full: a property given as null is cleared, so
+ * the pipe ends up carrying exactly what is passed here and nothing the caller left out.
+ */
+ public Pipe restorePipe(String name, String target, String roleArn, String description,
+ DesiredState desiredState, String enrichment,
+ JsonNode sourceParameters, JsonNode targetParameters,
+ JsonNode enrichmentParameters, String region) {
+ return writePipeConfiguration(name, target, roleArn, description, desiredState, enrichment,
+ sourceParameters, targetParameters, enrichmentParameters, region, true);
+ }
+
+ /**
+ * Writes the pipe's configuration under the two meanings an unset property carries.
+ *
+ * <p>With {@code clearUnsetProperties} false, a null property leaves the pipe's value alone:
+ * that is what UpdatePipe promises its callers. With it true, a null property clears the pipe's
+ * value, which is what putting a pipe back to a configuration recorded in full needs.
+ */
+ private Pipe writePipeConfiguration(String name, String target, String roleArn, String description,
+ DesiredState desiredState, String enrichment,
+ JsonNode sourceParameters, JsonNode targetParameters,
+ JsonNode enrichmentParameters, String region,
+ boolean clearUnsetProperties) {
String key = region + "::" + name;
Pipe pipe = storage.get(key)
.orElseThrow(() -> new AwsException("NotFoundException",
"Pipe " + name + " does not exist.", 404));
- validateSourceConfiguration(pipe.getSource(), sourceParameters != null ? sourceParameters : pipe.getSourceParameters());
-
- if (target != null) pipe.setTarget(target);
- if (roleArn != null) pipe.setRoleArn(roleArn);
- if (description != null) pipe.setDescription(description);
+ JsonNode effectiveSourceParameters = sourceParameters == null && !clearUnsetProperties
+ ? pipe.getSourceParameters()
+ : sourceParameters;
+ validateSourceConfiguration(pipe.getSource(), effectiveSourceParameters);
+
+ writeProperty(target, clearUnsetProperties, pipe::setTarget);
+ writeProperty(roleArn, clearUnsetProperties, pipe::setRoleArn);
+ writeProperty(description, clearUnsetProperties, pipe::setDescription);
+ writeProperty(enrichment, clearUnsetProperties, pipe::setEnrichment);
+ writeProperty(sourceParameters, clearUnsetProperties, pipe::setSourceParameters);
+ writeProperty(targetParameters, clearUnsetProperties, pipe::setTargetParameters);
+ writeProperty(enrichmentParameters, clearUnsetProperties, pipe::setEnrichmentParameters);
+ // A pipe always has a desired state: createPipe defaults it to RUNNING and currentState is
+ // read off it. A null therefore leaves both alone on either path, rather than clearing a
+ // state the poller and currentState would then contradict.
if (desiredState != null) {
pipe.setDesiredState(desiredState);
pipe.setCurrentState(desiredState == DesiredState.RUNNING ? PipeState.RUNNING : PipeState.STOPPED);
@@ -146,10 +188,6 @@ public class PipesService implements TagHandler, ResourceProvider {
poller.stopPolling(pipe);
}
}
- if (enrichment != null) pipe.setEnrichment(enrichment);
- if (sourceParameters != null) pipe.setSourceParameters(sourceParameters);
- if (targetParameters != null) pipe.setTargetParameters(targetParameters);
- if (enrichmentParameters != null) pipe.setEnrichmentParameters(enrichmentParameters);
pipe.setLastModifiedTime(Instant.now());
storage.put(key, pipe);
@@ -157,6 +195,13 @@ public class PipesService implements TagHandler, ResourceProvider {
return pipe;
}
+ /** Applies one property under the unset meaning {@code clearUnsetProperties} selects. */
+ private static <T> void writeProperty(T value, boolean clearUnsetProperties, Consumer<T> setter) {
+ if (value != null || clearUnsetProperties) {
+ setter.accept(value);
+ }
+ }
+
public void deletePipe(String name, String region) {
String key = region + "::" + name;
Pipe pipe = storage.get(key)
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java
index 74dbeb07d8..3a78aac487 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java
@@ -209,7 +209,7 @@ final class CfnProvisionerFixture {
discovered.add(new EcrCfnProvisioner(ecrService));
}
if (pipesService != null) {
- discovered.add(new PipesCfnProvisioner(pipesService));
+ discovered.add(new PipesCfnProvisioner(pipesService, objectMapper));
}
if (cognitoService != null) {
discovered.add(new CognitoCfnProvisioner(cognitoService));
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
index 5d8fdf2cc8..3b28b9f270 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
@@ -4274,6 +4274,208 @@ class CloudFormationIntegrationTest {
.statusCode(404);
}
+ @Test
+ void updateStack_reconcilesExistingPipe() {
+ // provision() re-runs on every UpdateStack for every resource regardless of whether its
+ // properties changed, so a fixed-name pipe used to call CreatePipe again and roll back with
+ // "Pipe cfn-update-test-pipe already exists.".
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", "cfn-pipe-update-stack")
+ .formParam("TemplateBody", pipeUpdateTemplate("FirstTargetQueue"))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "UpdateStack")
+ .formParam("StackName", "cfn-pipe-update-stack")
+ .formParam("TemplateBody", pipeUpdateTemplate("SecondTargetQueue"))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", "cfn-pipe-update-stack")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<StackStatus>UPDATE_COMPLETE</StackStatus>"))
+ .body(not(containsString("ROLLBACK")));
+
+ // The target change was reconciled in place under the same pipe name.
+ given()
+ .contentType("application/json")
+ .when()
+ .get("/v1/pipes/cfn-update-test-pipe")
+ .then()
+ .statusCode(200)
+ .body("Name", equalTo("cfn-update-test-pipe"))
+ .body("Source", containsString("cfn-pipe-update-source"))
+ .body("Target", containsString("cfn-pipe-update-target-second"));
+
+ // Delete the stack and verify the pipe is gone, so the pipe does not outlive this test in
+ // the shared emulator and skew a sibling test counting pipes globally.
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DeleteStack")
+ .formParam("StackName", "cfn-pipe-update-stack")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .contentType("application/json")
+ .when()
+ .get("/v1/pipes/cfn-update-test-pipe")
+ .then()
+ .statusCode(404);
+ }
+
+ private static String pipeUpdateTemplate(String targetLogicalId) {
+ return """
+ {
+ "Resources": {
+ "SourceQueue": {
+ "Type": "AWS::SQS::Queue",
+ "Properties": {"QueueName": "cfn-pipe-update-source"}
+ },
+ "FirstTargetQueue": {
+ "Type": "AWS::SQS::Queue",
+ "Properties": {"QueueName": "cfn-pipe-update-target-first"}
+ },
+ "SecondTargetQueue": {
+ "Type": "AWS::SQS::Queue",
+ "Properties": {"QueueName": "cfn-pipe-update-target-second"}
+ },
+ "MyPipe": {
+ "Type": "AWS::Pipes::Pipe",
+ "Properties": {
+ "Name": "cfn-update-test-pipe",
+ "Source": { "Fn::GetAtt": ["SourceQueue", "Arn"] },
+ "Target": { "Fn::GetAtt": ["%s", "Arn"] },
+ "RoleArn": "arn:aws:iam::000000000000:role/pipe-role",
+ "DesiredState": "STOPPED"
+ }
+ }
+ }
+ }
+ """.formatted(targetLogicalId);
+ }
+
+ /**
+ * CloudFormation rolls back every resource an update touched, not only the one that failed. A
+ * pipe reconciled in place before a later resource fails goes back to the target it carried,
+ * and the stack reaches UPDATE_ROLLBACK_COMPLETE instead of reporting the pipe as UPDATE_FAILED
+ * for want of a rollback.
+ */
+ @Test
+ void updateStack_laterFailureRestoresThePipeTarget() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "cfn-pipe-rollback-stack-" + suffix;
+ String pipeName = "cfn-pipe-rollback-pipe-" + suffix;
+ String failingSecret = """
+ ,
+ "BadSecret": {
+ "Type": "AWS::SecretsManager::Secret",
+ "DependsOn": "MyPipe",
+ "Properties": {
+ "Name": "cfn-pipe-rollback-secret-%s",
+ "SecretString": "explicit",
+ "GenerateSecretString": {"PasswordLength": 32}
+ }
+ }""".formatted(suffix);
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", stackName)
+ .formParam("TemplateBody",
+ pipeRollbackTemplate(pipeName, "cfn-pipe-rollback-target-first", ""))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "UpdateStack")
+ .formParam("StackName", stackName)
+ .formParam("TemplateBody",
+ pipeRollbackTemplate(pipeName, "cfn-pipe-rollback-target-second", failingSecret))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<StackStatus>UPDATE_ROLLBACK_COMPLETE</StackStatus>"));
+
+ given()
+ .contentType("application/json")
+ .when()
+ .get("/v1/pipes/" + pipeName)
+ .then()
+ .statusCode(200)
+ .body("Target", equalTo("arn:aws:sqs:us-east-1:000000000000:cfn-pipe-rollback-target-first"));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DeleteStack")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .contentType("application/json")
+ .when()
+ .get("/v1/pipes/" + pipeName)
+ .then()
+ .statusCode(404);
+ }
+
+ /**
+ * The pipe alone, addressing its queues by ARN. An AWS::SQS::Queue in the same stack would
+ * report UPDATE_FAILED for want of its own rollback and hide the pipe's outcome behind
+ * UPDATE_ROLLBACK_FAILED.
+ */
+ private static String pipeRollbackTemplate(String pipeName, String targetQueueName,
+ String failingResource) {
+ return """
+ {
+ "Resources": {
+ "MyPipe": {
+ "Type": "AWS::Pipes::Pipe",
+ "Properties": {
+ "Name": "%1$s",
+ "Source": "arn:aws:sqs:us-east-1:000000000000:cfn-pipe-rollback-source",
+ "Target": "arn:aws:sqs:us-east-1:000000000000:%2$s",
+ "RoleArn": "arn:aws:iam::000000000000:role/pipe-role",
+ "DesiredState": "STOPPED"
+ }
+ }%3$s
+ }
+ }
+ """.formatted(pipeName, targetQueueName, failingResource);
+ }
+
// ── TemplateURL (path-style AWS S3) ──────────────────────────────────────
@Test
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationPipesCleanupIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationPipesCleanupIntegrationTest.java
new file mode 100644
index 0000000000..9c98481b4e
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationPipesCleanupIntegrationTest.java
@@ -0,0 +1,642 @@
+package io.github.hectorvent.floci.services.cloudformation;
+
+import io.github.hectorvent.floci.services.pipes.PipesService;
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.mockito.InjectSpy;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.Matchers.nullValue;
+import static org.mockito.ArgumentMatchers.anyMap;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+/**
+ * Renaming an {@code AWS::Pipes::Pipe} replaces it, and the pipe left under the prior name is
+ * deleted after the stack update commits, on the same update cleanup path every replaced resource
+ * takes: three attempts, DELETE_IN_PROGRESS then DELETE_COMPLETE or DELETE_FAILED, and an orphan
+ * named in the stack's UPDATE_COMPLETE status reason.
+ */
+@QuarkusTest
+class CloudFormationPipesCleanupIntegrationTest {
+
+ @InjectSpy
+ PipesService pipesService;
+
+ @BeforeAll
+ static void configureRestAssured() {
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ }
+
+ @Test
+ void permanentRenameCleanupFailureKeepsCommittedUpdateAndNamesTheOrphanedPipe() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "pipe-cleanup-warning-" + suffix;
+ String oldName = "pipe-cleanup-old-" + suffix;
+ String newName = "pipe-cleanup-new-" + suffix;
+
+ createStack(stackName, template(oldName, suffix));
+
+ Mockito.doThrow(new IllegalStateException("simulated cleanup failure"))
+ .when(pipesService)
+ .deletePipe(eq(oldName), anyString());
+
+ updateStack(stackName, template(newName, suffix));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<StackStatus>UPDATE_COMPLETE</StackStatus>"))
+ .body(containsString(
+ "The following resource(s) could not be deleted during update cleanup: "
+ + "[MyPipe (" + oldName + ")]."));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStackEvents")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<PhysicalResourceId>" + oldName + "</PhysicalResourceId>"))
+ .body(containsString("<ResourceStatus>DELETE_IN_PROGRESS</ResourceStatus>"))
+ .body(containsString("<ResourceStatus>DELETE_FAILED</ResourceStatus>"));
+
+ verify(pipesService, times(3)).deletePipe(eq(oldName), anyString());
+ assertPipe(oldName);
+ assertPipe(newName);
+
+ Mockito.doCallRealMethod().when(pipesService).deletePipe(eq(oldName), anyString());
+ deleteStack(stackName);
+ pipesService.deletePipe(oldName, "us-east-1");
+ }
+
+ @Test
+ void transientRenameCleanupFailureSucceedsOnThirdAttempt() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "pipe-cleanup-retry-" + suffix;
+ String oldName = "pipe-cleanup-retry-old-" + suffix;
+ String newName = "pipe-cleanup-retry-new-" + suffix;
+
+ createStack(stackName, template(oldName, suffix));
+
+ Mockito.doThrow(new IllegalStateException("first cleanup failure"))
+ .doThrow(new IllegalStateException("second cleanup failure"))
+ .doCallRealMethod()
+ .when(pipesService)
+ .deletePipe(eq(oldName), anyString());
+
+ updateStack(stackName, template(newName, suffix));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<StackStatus>UPDATE_COMPLETE</StackStatus>"))
+ .body(not(containsString("<StackStatusReason>")));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStackEvents")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<PhysicalResourceId>" + oldName + "</PhysicalResourceId>"))
+ .body(containsString(
+ "<ResourceStatus>UPDATE_COMPLETE_CLEANUP_IN_PROGRESS</ResourceStatus>"))
+ .body(containsString("<ResourceStatus>DELETE_IN_PROGRESS</ResourceStatus>"))
+ .body(containsString("<ResourceStatus>DELETE_COMPLETE</ResourceStatus>"))
+ .body(not(containsString("<ResourceStatus>DELETE_FAILED</ResourceStatus>")));
+
+ verify(pipesService, times(3)).deletePipe(eq(oldName), anyString());
+ assertPipeMissing(oldName);
+ assertPipe(newName);
+
+ deleteStack(stackName);
+ }
+
+ /**
+ * The cleanup phase runs after the update has committed, so a stack that never reaches the end
+ * of it stays in UPDATE_COMPLETE_CLEANUP_IN_PROGRESS with the displaced pipe alive. That status
+ * means an operation still owns the stack, so the next update is refused rather than made to
+ * carry a phase it did not start. DeleteStack is the way out, and it takes the displaced pipe
+ * with it.
+ */
+ @Test
+ void anUpdateIsRefusedWhileAnInterruptedCleanupOwnsTheStack() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "pipe-cleanup-refused-" + suffix;
+ String oldName = "pipe-cleanup-refused-old-" + suffix;
+ String newName = "pipe-cleanup-refused-new-" + suffix;
+
+ createStack(stackName, template(oldName, suffix));
+
+ // An Error is not a deletion failure the cleanup retries: it leaves the phase where it
+ // stands, which is what the stack looks like when the process dies mid cleanup.
+ Mockito.doThrow(new Error("cleanup interrupted"))
+ .when(pipesService)
+ .deletePipe(eq(oldName), anyString());
+
+ updateStack(stackName, template(newName, suffix));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString(
+ "<StackStatus>UPDATE_COMPLETE_CLEANUP_IN_PROGRESS</StackStatus>"));
+ assertPipe(oldName);
+ assertPipe(newName);
+
+ Mockito.doCallRealMethod().when(pipesService).deletePipe(eq(oldName), anyString());
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "UpdateStack")
+ .formParam("StackName", stackName)
+ .formParam("TemplateBody", template(newName, suffix))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body(containsString("<Code>ValidationError</Code>"))
+ .body(containsString(
+ "Stack:arn:aws:cloudformation:us-east-1:000000000000:stack/" + stackName + "/"))
+ .body(containsString(
+ " is in UPDATE_COMPLETE_CLEANUP_IN_PROGRESS state and can not be updated."));
+
+ // The refusal changes nothing: the stack keeps the status and both pipes it had.
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString(
+ "<StackStatus>UPDATE_COMPLETE_CLEANUP_IN_PROGRESS</StackStatus>"));
+ assertPipe(oldName);
+ assertPipe(newName);
+
+ deleteStack(stackName);
+ assertPipeMissing(oldName);
+ assertPipeMissing(newName);
+ }
+
+ /**
+ * CreateChangeSet is refused on the same terms as UpdateStack: a status ending in
+ * _IN_PROGRESS says an operation owns the stack, and no change set attaches to it. Nothing on
+ * the stack is a way to finish the phase the interruption left open, so DeleteStack is again
+ * what removes both pipes.
+ */
+ @Test
+ void aChangeSetIsRefusedWhileAnInterruptedCleanupOwnsTheStack() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "pipe-cleanup-changeset-refused-" + suffix;
+ String oldName = "pipe-cleanup-changeset-refused-old-" + suffix;
+ String newName = "pipe-cleanup-changeset-refused-new-" + suffix;
+
+ createStack(stackName, template(oldName, suffix));
+
+ Mockito.doThrow(new Error("cleanup interrupted"))
+ .when(pipesService)
+ .deletePipe(eq(oldName), anyString());
+
+ updateStack(stackName, template(newName, suffix));
+
+ Mockito.doCallRealMethod().when(pipesService).deletePipe(eq(oldName), anyString());
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateChangeSet")
+ .formParam("StackName", stackName)
+ .formParam("ChangeSetName", "pipe-cleanup-changeset-" + suffix)
+ .formParam("ChangeSetType", "UPDATE")
+ .formParam("TemplateBody", template(newName, suffix))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body(containsString("<Code>ValidationError</Code>"))
+ .body(containsString(
+ "Stack:arn:aws:cloudformation:us-east-1:000000000000:stack/" + stackName + "/"))
+ .body(containsString(
+ " is in UPDATE_COMPLETE_CLEANUP_IN_PROGRESS state and can not be updated."));
+
+ assertPipe(oldName);
+ assertPipe(newName);
+
+ deleteStack(stackName);
+ assertPipeMissing(oldName);
+ assertPipeMissing(newName);
+ }
+
+ /**
+ * A rename is a replacement, and an update that fails at a later resource never commits it.
+ * CloudFormation puts a replaced resource back to the configuration it had before the update,
+ * so the stack resource points at the prior pipe again, the replacement the failed update
+ * created is gone, and the rename cleanup recorded for the committed path never fires.
+ */
+ @Test
+ void aRenameRolledBackPointsTheStackResourceAtThePriorPipe() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "pipe-rename-rollback-" + suffix;
+ String priorName = "pipe-rename-rollback-prior-" + suffix;
+ String replacementName = "pipe-rename-rollback-replacement-" + suffix;
+
+ createStack(stackName, renameRollbackTemplate(priorName, ""));
+
+ updateStack(stackName, renameRollbackTemplate(replacementName, """
+ ,
+ "BadSecret": {
+ "Type": "AWS::SecretsManager::Secret",
+ "DependsOn": "MyPipe",
+ "Properties": {
+ "Name": "pipe-rename-rollback-secret-%s",
+ "SecretString": "explicit",
+ "GenerateSecretString": {"PasswordLength": 32}
+ }
+ }""".formatted(suffix)));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<StackStatus>UPDATE_ROLLBACK_COMPLETE</StackStatus>"))
+ .body(not(containsString("Rollback is not implemented")));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStackResources")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<PhysicalResourceId>" + priorName + "</PhysicalResourceId>"))
+ .body(not(containsString(replacementName)));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStackEvents")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(not(containsString("Rollback is not implemented")))
+ .body(containsString(
+ "<ResourceStatusReason>Resource update rolled back</ResourceStatusReason>"));
+
+ given()
+ .contentType("application/json")
+ .when()
+ .get("/v1/pipes/" + priorName)
+ .then()
+ .statusCode(200)
+ .body("Arn", equalTo("arn:aws:pipes:us-east-1:000000000000:pipe/" + priorName))
+ .body("Target", equalTo("arn:aws:sqs:us-east-1:000000000000:pipe-rename-rollback-target"));
+ assertPipeMissing(replacementName);
+ verify(pipesService, never()).deletePipe(eq(priorName), anyString());
+
+ deleteStack(stackName);
+ assertPipeMissing(priorName);
+ }
+
+ /**
+ * The rollback of a rename deletes the replacement the failed update created, and that delete
+ * can fail. The pipe the rollback exists to preserve is the prior one, so the resource names it
+ * and the rename cleanup is spent before the delete is attempted. The stack still reports
+ * UPDATE_ROLLBACK_FAILED naming the resource, the replacement is what stays orphaned, and the
+ * next update finds nothing on the resource that would delete the prior pipe.
+ */
+ @Test
+ void aRollbackThatCannotDeleteTheReplacementStillKeepsThePriorPipe() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "pipe-rename-rollback-keep-" + suffix;
+ String priorName = "pipe-rename-rollback-keep-prior-" + suffix;
+ String replacementName = "pipe-rename-rollback-keep-replacement-" + suffix;
+
+ createStack(stackName, renameRollbackTemplate(priorName, ""));
+
+ // Only the replacement's delete fails, which is the delete the rollback performs itself.
+ Mockito.doThrow(new IllegalStateException("simulated replacement delete failure"))
+ .when(pipesService)
+ .deletePipe(eq(replacementName), anyString());
+
+ updateStack(stackName, renameRollbackTemplate(replacementName, """
+ ,
+ "BadSecret": {
+ "Type": "AWS::SecretsManager::Secret",
+ "DependsOn": "MyPipe",
+ "Properties": {
+ "Name": "pipe-rename-rollback-keep-secret-%s",
+ "SecretString": "explicit",
+ "GenerateSecretString": {"PasswordLength": 32}
+ }
+ }""".formatted(suffix)));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<StackStatus>UPDATE_ROLLBACK_FAILED</StackStatus>"))
+ .body(containsString("The following resource(s) failed to roll back: [MyPipe]."));
+
+ // The physical id is what a later cleanup and a later DeleteStack address, so it names the
+ // pipe that survived the rollback rather than the replacement left behind.
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStackResources")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<PhysicalResourceId>" + priorName + "</PhysicalResourceId>"))
+ .body(not(containsString(replacementName)));
+
+ assertPipe(priorName);
+ assertPipe(replacementName);
+
+ Mockito.doCallRealMethod().when(pipesService).deletePipe(eq(replacementName), anyString());
+ updateStack(stackName, renameRollbackTemplate(replacementName, ""));
+
+ given()
+ .contentType("application/json")
+ .when()
+ .get("/v1/pipes/" + priorName)
+ .then()
+ .statusCode(200)
+ .body("Name", equalTo(priorName))
+ .body("Target", equalTo("arn:aws:sqs:us-east-1:000000000000:pipe-rename-rollback-target"));
+ verify(pipesService, never()).deletePipe(eq(priorName), anyString());
+
+ deleteStack(stackName);
+ // The rollback left the replacement orphaned and the resource UPDATE_FAILED, so neither pipe
+ // is one DeleteStack removes and this test removes them itself.
+ given().when().delete("/v1/pipes/" + priorName);
+ given().when().delete("/v1/pipes/" + replacementName);
+ }
+
+ /**
+ * The tag reconciliation runs after updatePipe has already written the pipe, so a failure there
+ * leaves the pipe carrying the update the stack is about to disown. The provisioner puts it back
+ * from its own snapshot before the failure leaves it, description and tags alike, and the stack
+ * reports a clean rollback.
+ */
+ @Test
+ void aFailedTagReconcileRestoresThePipeAndTheStackRollsBackCleanly() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "pipe-tag-restore-" + suffix;
+ String pipeName = "pipe-tag-restore-pipe-" + suffix;
+
+ createStack(stackName, tagReconcileTemplate(pipeName, "before", "owner", "before"));
+
+ // The update's tag call fails; the restore's own tag call is left working, which is what
+ // makes this the clean-rollback case rather than the failed-restore one below.
+ Mockito.doThrow(new IllegalStateException("simulated tagResource failure"))
+ .doCallRealMethod()
+ .when(pipesService)
+ .tagResource(anyString(), anyString(), anyMap());
+
+ updateStack(stackName, tagReconcileTemplate(pipeName, "after", "team", "after"));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<StackStatus>UPDATE_ROLLBACK_COMPLETE</StackStatus>"));
+
+ given()
+ .contentType("application/json")
+ .when()
+ .get("/v1/pipes/" + pipeName)
+ .then()
+ .statusCode(200)
+ .body("Description", equalTo("before"))
+ .body("Tags.owner", equalTo("before"))
+ .body("Tags.team", nullValue());
+
+ Mockito.doCallRealMethod().when(pipesService).tagResource(anyString(), anyString(), anyMap());
+ deleteStack(stackName);
+ assertPipeMissing(pipeName);
+ }
+
+ /**
+ * The restore repeats the tag calls the update just made, so a tag failure that persists takes
+ * the restore down with it and nobody knows what the pipe carries. The stack says so:
+ * UPDATE_ROLLBACK_FAILED naming the pipe resource, rather than a clean rollback claiming a
+ * configuration the pipe does not hold.
+ */
+ @Test
+ void aFailedRestoreLeavesTheStackInUpdateRollbackFailedNamingThePipe() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "pipe-restore-failure-" + suffix;
+ String pipeName = "pipe-restore-failure-pipe-" + suffix;
+
+ createStack(stackName, tagReconcileTemplate(pipeName, "before", "owner", "before"));
+
+ Mockito.doThrow(new IllegalStateException("simulated persistent tagResource failure"))
+ .when(pipesService)
+ .tagResource(anyString(), anyString(), anyMap());
+
+ updateStack(stackName, tagReconcileTemplate(pipeName, "after", "team", "after"));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<StackStatus>UPDATE_ROLLBACK_FAILED</StackStatus>"))
+ .body(containsString(
+ "The following resource(s) failed to roll back: [MyPipe]."));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStackEvents")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("simulated persistent tagResource failure"))
+ .body(not(containsString(
+ "<ResourceStatusReason>Resource update rolled back</ResourceStatusReason>")));
+
+ Mockito.doCallRealMethod().when(pipesService).tagResource(anyString(), anyString(), anyMap());
+ deleteStack(stackName);
+ // A resource left UPDATE_FAILED by a failed rollback is not one DeleteStack removes, so the
+ // pipe outlives the stack and this test deletes it itself.
+ pipesService.deletePipe(pipeName, "us-east-1");
+ }
+
+ /** The pipe alone, under a fixed name, so every update reconciles it in place. */
+ private static String tagReconcileTemplate(String pipeName, String description,
+ String tagKey, String tagValue) {
+ return """
+ {
+ "Resources": {
+ "MyPipe": {
+ "Type": "AWS::Pipes::Pipe",
+ "Properties": {
+ "Name": "%1$s",
+ "Source": "arn:aws:sqs:us-east-1:000000000000:pipe-tag-restore-source",
+ "Target": "arn:aws:sqs:us-east-1:000000000000:pipe-tag-restore-target",
+ "RoleArn": "arn:aws:iam::000000000000:role/pipe-tag-restore-role",
+ "Description": "%2$s",
+ "DesiredState": "STOPPED",
+ "Tags": [{"Key": "%3$s", "Value": "%4$s"}]
+ }
+ }
+ }
+ }
+ """.formatted(pipeName, description, tagKey, tagValue);
+ }
+
+ /**
+ * The pipe alone, addressing its queues by ARN. A queue of this stack's own would report
+ * UPDATE_FAILED for want of its own rollback and hide the pipe's outcome behind
+ * UPDATE_ROLLBACK_FAILED.
+ */
+ private static String renameRollbackTemplate(String pipeName, String failingResource) {
+ return """
+ {
+ "Resources": {
+ "MyPipe": {
+ "Type": "AWS::Pipes::Pipe",
+ "Properties": {
+ "Name": "%1$s",
+ "Source": "arn:aws:sqs:us-east-1:000000000000:pipe-rename-rollback-source",
+ "Target": "arn:aws:sqs:us-east-1:000000000000:pipe-rename-rollback-target",
+ "RoleArn": "arn:aws:iam::000000000000:role/pipe-rename-rollback-role",
+ "DesiredState": "STOPPED"
+ }
+ }%2$s
+ }
+ }
+ """.formatted(pipeName, failingResource);
+ }
+
+ private static void createStack(String stackName, String templateBody) {
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", stackName)
+ .formParam("TemplateBody", templateBody)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
+
+ private static void updateStack(String stackName, String templateBody) {
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "UpdateStack")
+ .formParam("StackName", stackName)
+ .formParam("TemplateBody", templateBody)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
+
+ private static void deleteStack(String stackName) {
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DeleteStack")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
+
+ private static void assertPipe(String pipeName) {
+ given()
+ .contentType("application/json")
+ .when()
+ .get("/v1/pipes/" + pipeName)
+ .then()
+ .statusCode(200)
+ .body("Name", equalTo(pipeName));
+ }
+
+ private static void assertPipeMissing(String pipeName) {
+ given()
+ .contentType("application/json")
+ .when()
+ .get("/v1/pipes/" + pipeName)
+ .then()
+ .statusCode(404);
+ }
+
+ private static String template(String pipeName, String suffix) {
+ return """
+ {
+ "Resources": {
+ "SourceQueue": {
+ "Type": "AWS::SQS::Queue",
+ "Properties": {"QueueName": "pipe-cleanup-source-%s"}
+ },
+ "TargetQueue": {
+ "Type": "AWS::SQS::Queue",
+ "Properties": {"QueueName": "pipe-cleanup-target-%s"}
+ },
+ "MyPipe": {
+ "Type": "AWS::Pipes::Pipe",
+ "Properties": {
+ "Name": "%s",
+ "Source": {"Fn::GetAtt": ["SourceQueue", "Arn"]},
+ "Target": {"Fn::GetAtt": ["TargetQueue", "Arn"]},
+ "RoleArn": "arn:aws:iam::000000000000:role/pipe-cleanup-role",
+ "DesiredState": "STOPPED"
+ }
+ }
+ }
+ }
+ """.formatted(suffix, suffix, pipeName);
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnStableNameUpdatePathTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnStableNameUpdatePathTest.java
index 49c1442cf1..35a592ab8e 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnStableNameUpdatePathTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnStableNameUpdatePathTest.java
@@ -135,7 +135,7 @@ class CfnStableNameUpdatePathTest {
any(), any(), anyString())).thenReturn(createdPipe);
when(pipes.updatePipe(anyString(), any(), any(), any(), any(), any(), any(), any(), any(),
anyString())).thenReturn(updatedPipe);
- PipesCfnProvisioner provisioner = new PipesCfnProvisioner(pipes);
+ PipesCfnProvisioner provisioner = new PipesCfnProvisioner(pipes, mapper);
var props = mapper.createObjectNode()
.put("Source", "arn:aws:sqs:us-east-1:000000000000:src")
@@ -182,7 +182,7 @@ class CfnStableNameUpdatePathTest {
when(pipes.describePipe("my-stack-Pipe-0123456789ab", "us-east-1")).thenReturn(existing);
when(pipes.updatePipe(anyString(), any(), any(), any(), any(), any(), any(), any(), any(),
anyString())).thenReturn(existing);
- PipesCfnProvisioner provisioner = new PipesCfnProvisioner(pipes);
+ PipesCfnProvisioner provisioner = new PipesCfnProvisioner(pipes, mapper);
// Source is createOnly. With the name reused there is no replacement to move to, which is
// the update CloudFormation refuses for a custom-named resource.
@@ -439,7 +439,7 @@ class CfnStableNameUpdatePathTest {
pipe.setArn("arn:aws:pipes:us-east-1:000000000000:pipe/new");
when(pipes.createPipe(anyString(), any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), anyString())).thenReturn(pipe);
- PipesCfnProvisioner provisioner = new PipesCfnProvisioner(pipes);
+ PipesCfnProvisioner provisioner = new PipesCfnProvisioner(pipes, mapper);
// The template now names the pipe, so this update derives a different name from the prior
// physical id. That is a replacement, and it must create rather than update a pipe that
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LeafCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LeafCfnProvisionerTest.java
index 01b66821d5..d4fb00ef9d 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LeafCfnProvisionerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LeafCfnProvisionerTest.java
@@ -11,9 +11,6 @@ import io.github.hectorvent.floci.services.firehose.FirehoseService;
import io.github.hectorvent.floci.services.firehose.model.DeliveryStreamDescription;
import io.github.hectorvent.floci.services.kms.KmsService;
import io.github.hectorvent.floci.services.kms.model.KmsKey;
-import io.github.hectorvent.floci.services.pipes.PipesService;
-import io.github.hectorvent.floci.services.pipes.model.DesiredState;
-import io.github.hectorvent.floci.services.pipes.model.Pipe;
import io.github.hectorvent.floci.services.ssm.SsmService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
@@ -298,49 +295,6 @@ class LeafCfnProvisionerTest {
}
}
- @Nested
- class Pipes {
-
- private final PipesService pipes = mock(PipesService.class);
- private final PipesCfnProvisioner provisioner = new PipesCfnProvisioner(pipes);
-
- @Test
- void refIsThePipeNameAndGetAttExposesArn() {
- Pipe pipe = new Pipe();
- pipe.setArn("arn:aws:pipes:us-east-1:000000000000:pipe/p");
- when(pipes.createPipe(eq("p"), eq("src"), eq("tgt"), eq("role"), any(),
- eq(DesiredState.RUNNING), any(), any(), any(), any(), any(), eq(REGION)))
- .thenReturn(pipe);
-
- StackResource r = resource("Pipe", "AWS::Pipes::Pipe");
- provisioner.provision(r, props("""
- {"Name": "p", "Source": "src", "Target": "tgt", "RoleArn": "role"}
- """), ctx);
-
- assertEquals("p", r.getPhysicalId());
- assertEquals("arn:aws:pipes:us-east-1:000000000000:pipe/p", r.getAttributes().get("Arn"));
- }
-
- @Test
- void desiredStateStoppedIsHonouredAndAnythingElseRuns() {
- Pipe pipe = new Pipe();
- when(pipes.createPipe(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(),
- any(), any())).thenReturn(pipe);
-
- provisioner.provision(resource("Pipe", "AWS::Pipes::Pipe"), props("""
- {"Name": "p", "DesiredState": "STOPPED"}
- """), ctx);
- verify(pipes).createPipe(any(), any(), any(), any(), any(), eq(DesiredState.STOPPED),
- any(), any(), any(), any(), any(), any());
- }
-
- @Test
- void deleteReachesTheService() {
- provisioner.delete("AWS::Pipes::Pipe", "p", REGION);
- verify(pipes).deletePipe("p", REGION);
- }
- }
-
@Nested
class Firehose {
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/PipesCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/PipesCfnProvisionerTest.java
new file mode 100644
index 0000000000..74744f5dd7
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/PipesCfnProvisionerTest.java
@@ -0,0 +1,766 @@
+package io.github.hectorvent.floci.services.cloudformation.provisioners;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
+import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
+import io.github.hectorvent.floci.services.pipes.PipesService;
+import io.github.hectorvent.floci.services.pipes.model.DesiredState;
+import io.github.hectorvent.floci.services.pipes.model.Pipe;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InOrder;
+
+import java.time.Instant;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * {@code AWS::Pipes::Pipe}, whose provision body doubles as its update path.
+ *
+ * <p>{@code PipesService} is stubbed with the store it really keeps, so a second create under a
+ * name already on file raises the production {@code ConflictException} instead of a bare
+ * {@code verify(never())}.
+ */
+class PipesCfnProvisionerTest {
+
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+ private static final String REGION = "us-east-1";
+ private static final String ACCOUNT_ID = "000000000000";
+ private static final String SOURCE_QUEUE_ARN = "arn:aws:sqs:us-east-1:000000000000:example-queue";
+ private static final String TARGET_QUEUE_ARN = "arn:aws:sqs:us-east-1:000000000000:example-target-queue";
+ private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/example-pipe-role";
+ private static final String NEW_TARGET_QUEUE_ARN = "arn:aws:sqs:us-east-1:000000000000:new-target-queue";
+ private static final String ENRICHMENT_ARN =
+ "arn:aws:lambda:us-east-1:000000000000:function:example-enrichment";
+
+ private PipesService pipes;
+ private PipesCfnProvisioner provisioner;
+ private CloudFormationTemplateEngine engine;
+
+ /** The pipes the stubbed service holds, keyed by name, standing in for its storage backend. */
+ private Map<String, Pipe> pipesOnFile;
+
+ @BeforeEach
+ void setUp() {
+ pipes = mock(PipesService.class);
+ provisioner = new PipesCfnProvisioner(pipes, MAPPER);
+ pipesOnFile = new LinkedHashMap<>();
+
+ engine = mock(CloudFormationTemplateEngine.class);
+ when(engine.resolve(any())).thenAnswer(i -> {
+ JsonNode node = i.getArgument(0);
+ return node == null || node.isMissingNode() || node.isNull() ? null : node.asText();
+ });
+ when(engine.resolveNode(any())).thenAnswer(i -> i.getArgument(0));
+
+ when(pipes.createPipe(anyString(), any(), any(), any(), any(), any(), any(), any(), any(),
+ any(), any(), anyString())).thenAnswer(i -> {
+ String name = i.getArgument(0);
+ if (pipesOnFile.containsKey(name)) {
+ throw new AwsException("ConflictException", "Pipe " + name + " already exists.", 409);
+ }
+ Pipe pipe = new Pipe();
+ pipe.setName(name);
+ pipe.setArn("arn:aws:pipes:us-east-1:000000000000:pipe/" + name);
+ pipe.setSource(i.getArgument(1));
+ pipe.setTarget(i.getArgument(2));
+ pipe.setRoleArn(i.getArgument(3));
+ pipe.setDescription(i.getArgument(4));
+ pipe.setDesiredState(i.getArgument(5));
+ pipe.setEnrichment(i.getArgument(6));
+ pipe.setSourceParameters(i.getArgument(7));
+ pipe.setTargetParameters(i.getArgument(8));
+ pipe.setEnrichmentParameters(i.getArgument(9));
+ Map<String, String> tags = i.getArgument(10);
+ pipe.setTags(tags != null ? new HashMap<>(tags) : new HashMap<>());
+ // The creation time is how a rollback tells the pipe this run created from one that
+ // already stood under the same name, so the stub stamps it as the service does.
+ pipe.setCreationTime(Instant.now());
+ pipesOnFile.put(name, pipe);
+ return pipe;
+ });
+ when(pipes.listTags(anyString(), anyString()))
+ .thenAnswer(i -> pipeByArn(i.getArgument(1)).getTags());
+ doAnswer(i -> {
+ pipeByArn(i.getArgument(1)).getTags().putAll(i.<Map<String, String>>getArgument(2));
+ return null;
+ }).when(pipes).tagResource(anyString(), anyString(), any());
+ doAnswer(i -> {
+ Pipe pipe = pipeByArn(i.getArgument(1));
+ i.<List<String>>getArgument(2).forEach(pipe.getTags()::remove);
+ return null;
+ }).when(pipes).untagResource(anyString(), anyString(), any());
+ when(pipes.describePipe(anyString(), anyString())).thenAnswer(i -> {
+ String name = i.getArgument(0);
+ Pipe pipe = pipesOnFile.get(name);
+ if (pipe == null) {
+ throw new AwsException("NotFoundException", "Pipe " + name + " does not exist.", 404);
+ }
+ return pipe;
+ });
+ when(pipes.updatePipe(anyString(), any(), any(), any(), any(), any(), any(), any(), any(),
+ anyString())).thenAnswer(i -> {
+ String name = i.getArgument(0);
+ Pipe pipe = pipesOnFile.get(name);
+ if (pipe == null) {
+ throw new AwsException("NotFoundException", "Pipe " + name + " does not exist.", 404);
+ }
+ applyIfPresent(i.getArgument(1), pipe::setTarget);
+ applyIfPresent(i.getArgument(2), pipe::setRoleArn);
+ applyIfPresent(i.getArgument(3), pipe::setDescription);
+ applyIfPresent(i.getArgument(4), pipe::setDesiredState);
+ applyIfPresent(i.getArgument(5), pipe::setEnrichment);
+ applyIfPresent(i.getArgument(6), pipe::setSourceParameters);
+ applyIfPresent(i.getArgument(7), pipe::setTargetParameters);
+ applyIfPresent(i.getArgument(8), pipe::setEnrichmentParameters);
+ return pipe;
+ });
+ // restorePipe writes every property as given, clearing the ones passed as null; only the
+ // desired state is left alone when unset, since currentState is read off it.
+ when(pipes.restorePipe(anyString(), any(), any(), any(), any(), any(), any(), any(), any(),
+ anyString())).thenAnswer(i -> {
+ String name = i.getArgument(0);
+ Pipe pipe = pipesOnFile.get(name);
+ if (pipe == null) {
+ throw new AwsException("NotFoundException", "Pipe " + name + " does not exist.", 404);
+ }
+ pipe.setTarget(i.getArgument(1));
+ pipe.setRoleArn(i.getArgument(2));
+ pipe.setDescription(i.getArgument(3));
+ applyIfPresent(i.<DesiredState>getArgument(4), pipe::setDesiredState);
+ pipe.setEnrichment(i.getArgument(5));
+ pipe.setSourceParameters(i.getArgument(6));
+ pipe.setTargetParameters(i.getArgument(7));
+ pipe.setEnrichmentParameters(i.getArgument(8));
+ return pipe;
+ });
+ doAnswer(i -> {
+ String name = i.getArgument(0);
+ if (pipesOnFile.remove(name) == null) {
+ throw new AwsException("NotFoundException", "Pipe " + name + " does not exist.", 404);
+ }
+ return null;
+ }).when(pipes).deletePipe(anyString(), anyString());
+ }
+
+ /** UpdatePipe reads an absent value as "leave this one alone", which the stub mirrors. */
+ private static <T> void applyIfPresent(T value, java.util.function.Consumer<T> setter) {
+ if (value != null) {
+ setter.accept(value);
+ }
+ }
+
+ private Pipe pipeByArn(String arn) {
+ return pipesOnFile.values().stream()
+ .filter(pipe -> arn.equals(pipe.getArn()))
+ .findFirst()
+ .orElseThrow(() -> new AwsException("NotFoundException",
+ "Resource " + arn + " does not exist.", 404));
+ }
+
+ private static JsonNode props(String json) {
+ try {
+ return MAPPER.readTree(json);
+ } catch (Exception e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ private static StackResource stackResource(String priorPhysicalId) {
+ StackResource r = new StackResource();
+ r.setLogicalId("MyPipe");
+ r.setResourceType("AWS::Pipes::Pipe");
+ r.setPhysicalId(priorPhysicalId);
+ r.setAttributes(new HashMap<>());
+ return r;
+ }
+
+ private StackResource provision(String json, String priorPhysicalId) {
+ StackResource r = stackResource(priorPhysicalId);
+ provisioner.provision(r, props(json),
+ new ProvisionContext(engine, REGION, ACCOUNT_ID, "TestStack", priorPhysicalId));
+ return r;
+ }
+
+ private static String pipeTemplate(String name, String source, String target) {
+ return """
+ {"Name": "%s", "Source": "%s", "Target": "%s", "RoleArn": "%s"}
+ """.formatted(name, source, target, ROLE_ARN);
+ }
+
+ private static String taggedPipeTemplate(String tagsJson) {
+ return """
+ {"Name": "MyPipe", "Source": "%s", "Target": "%s", "RoleArn": "%s", "Tags": %s}
+ """.formatted(SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN, ROLE_ARN, tagsJson);
+ }
+
+ /** Every property the update path can change, so a rollback has all of them to put back. */
+ private static String pipeTemplateWithEveryUpdatableProperty(String target, String description,
+ String tagsJson) {
+ return """
+ {"Name": "MyPipe", "Source": "%s", "Target": "%s", "RoleArn": "%s",
+ "Description": "%s", "DesiredState": "STOPPED", "Enrichment": "%s",
+ "TargetParameters": {"InputTemplate": "%s"}, "Tags": %s}
+ """.formatted(SOURCE_QUEUE_ARN, target, ROLE_ARN, description, ENRICHMENT_ARN,
+ description, tagsJson);
+ }
+
+ private static JsonNode targetParametersFor(String description) {
+ return props("""
+ {"InputTemplate": "%s"}
+ """.formatted(description));
+ }
+
+ @Test
+ void refIsThePipeNameAndGetAttExposesArn() {
+ StackResource r = provision(pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+
+ verify(pipes).createPipe(eq("MyPipe"), eq(SOURCE_QUEUE_ARN), eq(TARGET_QUEUE_ARN), eq(ROLE_ARN),
+ any(), eq(DesiredState.RUNNING), any(), any(), any(), any(), any(), eq(REGION));
+ assertEquals("MyPipe", r.getPhysicalId());
+ assertEquals("arn:aws:pipes:us-east-1:000000000000:pipe/MyPipe", r.getAttributes().get("Arn"));
+ }
+
+ @Test
+ void desiredStateStoppedIsHonouredAndAnythingElseRuns() {
+ provision("""
+ {"Name": "MyPipe", "DesiredState": "STOPPED"}
+ """, null);
+ verify(pipes).createPipe(any(), any(), any(), any(), any(), eq(DesiredState.STOPPED),
+ any(), any(), any(), any(), any(), any());
+
+ provision("""
+ {"Name": "OtherPipe", "DesiredState": "SOMETHING_ELSE"}
+ """, null);
+ verify(pipes).createPipe(eq("OtherPipe"), any(), any(), any(), any(), eq(DesiredState.RUNNING),
+ any(), any(), any(), any(), any(), any());
+ }
+
+ @Test
+ void deleteReachesTheService() {
+ pipesOnFile.put("MyPipe", new Pipe());
+ provisioner.delete("AWS::Pipes::Pipe", "MyPipe", REGION);
+ verify(pipes).deletePipe("MyPipe", REGION);
+ }
+
+ /**
+ * provision() re-runs on every UpdateStack, so an unchanged name must update the pipe rather
+ * than call createPipe again, which the service rejects with ConflictException.
+ */
+ @Test
+ void anUnchangedNameUpdatesThePipeInsteadOfRecreatingIt() {
+ StackResource created = provision(
+ pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+
+ StackResource updated = provision(
+ pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, "arn:aws:sqs:us-east-1:000000000000:new-target-queue"),
+ created.getPhysicalId());
+
+ verify(pipes).updatePipe(eq("MyPipe"),
+ eq("arn:aws:sqs:us-east-1:000000000000:new-target-queue"), eq(ROLE_ARN), any(),
+ eq(DesiredState.RUNNING), any(), any(), any(), any(), eq(REGION));
+ verify(pipes, never()).deletePipe(anyString(), anyString());
+ assertEquals("MyPipe", updated.getPhysicalId(), "Ref is unchanged by the update");
+ assertEquals("arn:aws:pipes:us-east-1:000000000000:pipe/MyPipe",
+ updated.getAttributes().get("Arn"), "Fn::GetAtt Arn is unchanged by the update");
+ }
+
+ /** Source is create-only on AWS::Pipes::Pipe, which is why updatePipe takes no source. */
+ @Test
+ void aChangedSourceIsRefusedAsReplacementWorthy() {
+ StackResource created = provision(
+ pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+
+ AwsException refusal = assertThrows(AwsException.class, () -> provision(
+ pipeTemplate("MyPipe", "arn:aws:sqs:us-east-1:000000000000:other-queue", TARGET_QUEUE_ARN),
+ created.getPhysicalId()));
+
+ assertEquals("ValidationError", refusal.getErrorCode());
+ assertEquals("Updating Source requires resource replacement, which is not supported.",
+ refusal.getMessage());
+ assertEquals(400, refusal.getHttpStatus());
+ verify(pipes, never()).updatePipe(anyString(), any(), any(), any(), any(), any(), any(), any(),
+ any(), anyString());
+ }
+
+ /**
+ * A template with no Source is missing a required property, the same fault the create path
+ * reports. Diagnosing it as a replacement sends the reader after a change that never happened.
+ */
+ @Test
+ void anAbsentSourceOnUpdateReportsTheRequiredPropertyFault() {
+ provision(pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+
+ AwsException failure = assertThrows(AwsException.class, () -> provision("""
+ {"Name": "MyPipe", "Target": "%s", "RoleArn": "%s"}
+ """.formatted(TARGET_QUEUE_ARN, ROLE_ARN), "MyPipe"));
+
+ assertEquals("ValidationException", failure.getErrorCode());
+ assertEquals("Source is required", failure.getMessage());
+ assertEquals(400, failure.getHttpStatus());
+ verify(pipes, never()).updatePipe(anyString(), any(), any(), any(), any(), any(), any(), any(),
+ any(), anyString());
+ }
+
+ /** A Source that resolves blank is the same missing required property, not a changed Source. */
+ @Test
+ void aBlankSourceOnUpdateReportsTheRequiredPropertyFault() {
+ provision(pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+
+ AwsException failure = assertThrows(AwsException.class,
+ () -> provision(pipeTemplate("MyPipe", "", TARGET_QUEUE_ARN), "MyPipe"));
+
+ assertEquals("ValidationException", failure.getErrorCode());
+ assertEquals("Source is required", failure.getMessage());
+ }
+
+ @Test
+ void aChangedTagValueReachesThePipe() {
+ provision(taggedPipeTemplate("""
+ [{"Key": "Env", "Value": "dev"}]
+ """), null);
+
+ provision(taggedPipeTemplate("""
+ [{"Key": "Env", "Value": "prod"}]
+ """), "MyPipe");
+
+ verify(pipes).tagResource(REGION, "arn:aws:pipes:us-east-1:000000000000:pipe/MyPipe",
+ Map.of("Env", "prod"));
+ assertEquals(Map.of("Env", "prod"), pipesOnFile.get("MyPipe").getTags());
+ }
+
+ @Test
+ void aTagDroppedFromTheTemplateIsRemovedFromThePipe() {
+ provision(taggedPipeTemplate("""
+ [{"Key": "Env", "Value": "dev"}, {"Key": "Owner", "Value": "example-team"}]
+ """), null);
+
+ provision(taggedPipeTemplate("""
+ [{"Key": "Env", "Value": "dev"}]
+ """), "MyPipe");
+
+ verify(pipes).untagResource(REGION, "arn:aws:pipes:us-east-1:000000000000:pipe/MyPipe",
+ List.of("Owner"));
+ assertEquals(Map.of("Env", "dev"), pipesOnFile.get("MyPipe").getTags());
+ }
+
+ /**
+ * No tags on either side leaves the pipe's tags alone. The stored tags are still read, since a
+ * template that drops every tag has to have them removed.
+ */
+ @Test
+ void noTagsOnEitherSideMutatesNoTag() {
+ provision(pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+
+ provision(pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), "MyPipe");
+
+ verify(pipes, never()).tagResource(anyString(), anyString(), any());
+ verify(pipes, never()).untagResource(anyString(), anyString(), any());
+ }
+
+ /**
+ * On a rename provision creates the replacement and records the pipe left under the old name,
+ * which is deleted only once the stack update commits and completeUpdate runs.
+ */
+ @Test
+ void aRenameCreatesTheReplacementAndLeavesTheOriginalToTheCommittedCleanup() {
+ provision(pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+
+ StackResource renamed = provision(
+ pipeTemplate("MyRenamedPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), "MyPipe");
+
+ verify(pipes).createPipe(eq("MyRenamedPipe"), any(), any(), any(), any(), any(), any(),
+ any(), any(), any(), any(), any());
+ verify(pipes, never()).deletePipe(anyString(), anyString());
+ assertTrue(pipesOnFile.containsKey("MyPipe"), "the prior pipe outlives provision");
+ assertEquals("MyRenamedPipe", renamed.getPhysicalId());
+ assertEquals("arn:aws:pipes:us-east-1:000000000000:pipe/MyRenamedPipe",
+ renamed.getAttributes().get("Arn"));
+
+ assertTrue(provisioner.hasReplacementUpdate(renamed));
+ assertEquals("MyPipe", provisioner.updateCleanupPhysicalId(renamed));
+
+ UpdateCleanupResult cleanup = provisioner.completeUpdate(renamed);
+
+ InOrder order = inOrder(pipes);
+ order.verify(pipes).createPipe(eq("MyRenamedPipe"), any(), any(), any(), any(), any(), any(),
+ any(), any(), any(), any(), any());
+ order.verify(pipes).deletePipe("MyPipe", REGION);
+ assertEquals(new UpdateCleanupResult(true, true, "MyPipe", 0, null), cleanup);
+ assertFalse(pipesOnFile.containsKey("MyPipe"), "the prior pipe is gone once cleanup runs");
+
+ provisioner.clearUpdate(renamed);
+ assertFalse(provisioner.hasReplacementUpdate(renamed),
+ "the cleanup record is spent once the prior pipe is gone");
+ }
+
+ /** A pipe kept by UpdateReplacePolicy Retain is not deleted when the rename cleanup runs. */
+ @Test
+ void aRetainedPriorPipeSurvivesTheCommittedCleanup() {
+ provision(pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+ StackResource renamed = provision(
+ pipeTemplate("MyRenamedPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), "MyPipe");
+ renamed.setUpdateReplacePolicy("Retain");
+
+ assertNull(provisioner.updateCleanupPhysicalId(renamed));
+ assertEquals(new UpdateCleanupResult(true, true, "MyPipe", 0, null),
+ provisioner.completeUpdate(renamed));
+ verify(pipes, never()).deletePipe(anyString(), anyString());
+ assertTrue(pipesOnFile.containsKey("MyPipe"));
+ }
+
+ /** A pipe with no rename behind it owes no cleanup, so the caller moves on to the next one. */
+ @Test
+ void aPipeNoRenameDisplacedOwesNoCleanup() {
+ StackResource created = provision(
+ pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+
+ assertFalse(provisioner.hasReplacementUpdate(created));
+ assertNull(provisioner.updateCleanupPhysicalId(created));
+ assertEquals(new UpdateCleanupResult(false, true, null, 0, null),
+ provisioner.completeUpdate(created));
+ verify(pipes, never()).deletePipe(anyString(), anyString());
+ }
+
+ /**
+ * A rename whose createPipe fails leaves the prior pipe on file, so the rollback walker must be
+ * told not to restore something that was never deleted.
+ */
+ @Test
+ void aFailedRenameKeepsThePriorPipeAndMarksTheResourceRestored() {
+ provision(pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+ Pipe squatter = new Pipe();
+ squatter.setName("MyRenamedPipe");
+ pipesOnFile.put("MyRenamedPipe", squatter);
+
+ StackResource r = stackResource("MyPipe");
+ JsonNode template = props(pipeTemplate("MyRenamedPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN));
+ ProvisionContext ctx = new ProvisionContext(engine, REGION, ACCOUNT_ID, "TestStack", "MyPipe");
+
+ AwsException failure = assertThrows(AwsException.class,
+ () -> provisioner.provision(r, template, ctx));
+
+ assertEquals("ConflictException", failure.getErrorCode());
+ assertTrue(pipesOnFile.containsKey("MyPipe"), "the prior pipe is still on file");
+ verify(pipes, never()).deletePipe(anyString(), anyString());
+ assertEquals("true", r.getAttributes().get(CfnRollback.UPDATE_ROLLBACK_RESTORED_ATTR));
+ assertEquals("MyPipe", r.getPhysicalId(), "the resource still points at the prior pipe");
+ }
+
+ /**
+ * The replacement is already on file by the time the old pipe is dropped, so a failing
+ * deletePipe must not fail the resource: the update stays committed and the failure is reported
+ * one attempt at a time, until the caller gives up on the third and names the pipe left behind.
+ */
+ @Test
+ void aFailedDeleteOfTheRenamedPipeReportsEachAttemptAndKeepsTheUpdate() {
+ provision(pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+ doThrow(new RuntimeException("boom")).when(pipes).deletePipe("MyPipe", REGION);
+
+ StackResource renamed = provision(
+ pipeTemplate("MyRenamedPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), "MyPipe");
+
+ assertEquals("MyRenamedPipe", renamed.getPhysicalId(),
+ "the new pipe is still the resource's physical id");
+ assertEquals("arn:aws:pipes:us-east-1:000000000000:pipe/MyRenamedPipe",
+ renamed.getAttributes().get("Arn"));
+
+ assertEquals(new UpdateCleanupResult(true, false, "MyPipe", 1, "boom"),
+ provisioner.completeUpdate(renamed));
+ assertEquals(new UpdateCleanupResult(true, false, "MyPipe", 2, "boom"),
+ provisioner.completeUpdate(renamed));
+ assertEquals(new UpdateCleanupResult(true, false, "MyPipe", 3, "boom"),
+ provisioner.completeUpdate(renamed));
+ assertEquals(new UpdateCleanupResult(true, false, "MyPipe", 3, "boom"),
+ provisioner.completeUpdate(renamed),
+ "a fourth call reports the same give-up without another delete");
+
+ verify(pipes, times(3)).deletePipe("MyPipe", REGION);
+ assertTrue(pipesOnFile.containsKey("MyPipe"), "the pipe the rename displaced is left behind");
+ }
+
+ /** A prior pipe already deleted out of band leaves the cleanup nothing to do. */
+ @Test
+ void aPriorPipeAlreadyGoneCompletesTheCleanupWithoutADelete() {
+ provision(pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+ StackResource renamed = provision(
+ pipeTemplate("MyRenamedPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), "MyPipe");
+ pipesOnFile.remove("MyPipe");
+
+ assertEquals(new UpdateCleanupResult(true, true, "MyPipe", 0, null),
+ provisioner.completeUpdate(renamed));
+ verify(pipes, never()).deletePipe(anyString(), anyString());
+ }
+
+ /**
+ * Only a missing pipe falls back to the create arm. Any other describePipe failure reaches the
+ * user as itself, instead of being turned into ConflictException by a create that cannot work.
+ */
+ @Test
+ void anErrorOtherThanNotFoundFromDescribePipePropagates() {
+ provision(pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+ doThrow(new AwsException("InternalFailure", "storage is unavailable", 500))
+ .when(pipes).describePipe("MyPipe", REGION);
+
+ AwsException failure = assertThrows(AwsException.class, () -> provision(
+ pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), "MyPipe"));
+
+ assertEquals("InternalFailure", failure.getErrorCode());
+ assertEquals("storage is unavailable", failure.getMessage());
+ assertEquals(500, failure.getHttpStatus());
+ }
+
+ /** A pipe with no template Name keeps its generated physical name across updates. */
+ @Test
+ void anUnnamedPipeKeepsItsGeneratedNameAcrossUpdates() {
+ String unnamedTemplate = """
+ {"Source": "%s", "Target": "%s", "RoleArn": "%s"}
+ """.formatted(SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN, ROLE_ARN);
+
+ StackResource created = provision(unnamedTemplate, null);
+ assertTrue(created.getPhysicalId().startsWith("TestStack-MyPipe-"), created.getPhysicalId());
+
+ StackResource updated = provision(unnamedTemplate, created.getPhysicalId());
+
+ assertEquals(created.getPhysicalId(), updated.getPhysicalId());
+ verify(pipes).updatePipe(eq(created.getPhysicalId()), any(), any(), any(), any(), any(), any(),
+ any(), any(), eq(REGION));
+ assertEquals(1, pipesOnFile.size(), "no second pipe was created under a fresh name");
+ }
+
+ /** A pipe deleted out of band since the prior deploy is created again under the same name. */
+ @Test
+ void aPipeDeletedOutOfBandFallsBackToCreate() {
+ StackResource r = provision(pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), "MyPipe");
+
+ verify(pipes).createPipe(eq("MyPipe"), any(), any(), any(), any(), any(), any(), any(), any(),
+ any(), any(), eq(REGION));
+ verify(pipes, never()).updatePipe(anyString(), any(), any(), any(), any(), any(), any(), any(),
+ any(), anyString());
+ assertEquals("MyPipe", r.getPhysicalId());
+ assertNull(r.getAttributes().get(CfnRollback.UPDATE_ROLLBACK_RESTORED_ATTR));
+ }
+
+ /**
+ * updatePipe lands before the tag calls, so a tagResource that fails would leave the pipe
+ * carrying the new configuration under tags that are neither the old nor the new set. The
+ * provisioner puts the configuration and the tags back from its snapshot before the failure
+ * leaves it, and reports the failure that interrupted the update.
+ */
+ @Test
+ void aPipeMutatedByAFailedUpdateIsRestoredBeforeTheFailureLeavesTheProvisioner() {
+ provision(pipeTemplateWithEveryUpdatableProperty(TARGET_QUEUE_ARN, "the first target", """
+ [{"Key": "Env", "Value": "dev"}, {"Key": "Owner", "Value": "example-team"}]
+ """), null);
+ doThrow(new AwsException("InternalFailure", "tagging is unavailable", 500))
+ .when(pipes).tagResource(anyString(), anyString(), eq(Map.of("Env", "prod")));
+
+ StackResource r = stackResource("MyPipe");
+ JsonNode template = props(pipeTemplateWithEveryUpdatableProperty(
+ NEW_TARGET_QUEUE_ARN, "the second target", """
+ [{"Key": "Env", "Value": "prod"}]
+ """));
+ ProvisionContext ctx = new ProvisionContext(engine, REGION, ACCOUNT_ID, "TestStack", "MyPipe");
+
+ AwsException reported = assertThrows(AwsException.class,
+ () -> provisioner.provision(r, template, ctx));
+ assertEquals("tagging is unavailable", reported.getMessage(),
+ "the failure that interrupted the update is what the caller gets");
+
+ verify(pipes).restorePipe("MyPipe", TARGET_QUEUE_ARN, ROLE_ARN, "the first target",
+ DesiredState.STOPPED, ENRICHMENT_ARN, null, targetParametersFor("the first target"),
+ null, REGION);
+ Pipe restored = pipesOnFile.get("MyPipe");
+ assertEquals(TARGET_QUEUE_ARN, restored.getTarget());
+ assertEquals("the first target", restored.getDescription());
+ assertEquals(targetParametersFor("the first target"), restored.getTargetParameters());
+ assertEquals(Map.of("Env", "dev", "Owner", "example-team"), restored.getTags());
+ assertNull(r.getAttributes().get(CfnRollback.PIPE_UPDATE_SNAPSHOT_ATTR),
+ "the snapshot is spent once the pipe is back");
+ assertNull(r.getAttributes().get(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR),
+ "the restore succeeded, so the stack rolls back cleanly");
+ assertFalse(provisioner.rollbackUpdate(r),
+ "the rollback hook finds nothing left to put back");
+ }
+
+ /**
+ * A restore that fails itself leaves the pipe in a configuration nobody recorded. The reason
+ * goes on the resource, which is what makes the stack report UPDATE_ROLLBACK_FAILED, and the
+ * failure that interrupted the update still reaches the caller carrying it.
+ */
+ @Test
+ void aRestoreThatFailsMarksTheResourceAndKeepsBothFailures() {
+ provision(pipeTemplateWithEveryUpdatableProperty(TARGET_QUEUE_ARN, "the first target", """
+ [{"Key": "Env", "Value": "dev"}]
+ """), null);
+ doThrow(new AwsException("InternalFailure", "tagging is unavailable", 500))
+ .when(pipes).tagResource(anyString(), anyString(), eq(Map.of("Env", "prod")));
+ doThrow(new AwsException("InternalFailure", "the pipe cannot be written", 500))
+ .when(pipes).restorePipe(anyString(), any(), any(), any(), any(), any(), any(),
+ any(), any(), anyString());
+
+ StackResource r = stackResource("MyPipe");
+ JsonNode template = props(pipeTemplateWithEveryUpdatableProperty(
+ NEW_TARGET_QUEUE_ARN, "the second target", """
+ [{"Key": "Env", "Value": "prod"}]
+ """));
+ ProvisionContext ctx = new ProvisionContext(engine, REGION, ACCOUNT_ID, "TestStack", "MyPipe");
+
+ AwsException reported = assertThrows(AwsException.class,
+ () -> provisioner.provision(r, template, ctx));
+
+ assertEquals("tagging is unavailable", reported.getMessage());
+ assertEquals("the pipe cannot be written", reported.getSuppressed()[0].getMessage(),
+ "the restore failure travels with the one that interrupted the update");
+ assertEquals("the pipe cannot be written",
+ r.getAttributes().get(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR));
+ }
+
+ /**
+ * A restore failure carrying no message still has to name something the stack event can show,
+ * so the exception's own type is what the resource records.
+ */
+ @Test
+ void aRestoreFailureWithoutAMessageIsRecordedUnderItsType() {
+ provision(pipeTemplateWithEveryUpdatableProperty(TARGET_QUEUE_ARN, "the first target", """
+ [{"Key": "Env", "Value": "dev"}]
+ """), null);
+ doThrow(new AwsException("InternalFailure", "tagging is unavailable", 500))
+ .when(pipes).tagResource(anyString(), anyString(), eq(Map.of("Env", "prod")));
+ doThrow(new IllegalStateException())
+ .when(pipes).restorePipe(anyString(), any(), any(), any(), any(), any(), any(),
+ any(), any(), anyString());
+
+ StackResource r = stackResource("MyPipe");
+ JsonNode template = props(pipeTemplateWithEveryUpdatableProperty(
+ NEW_TARGET_QUEUE_ARN, "the second target", """
+ [{"Key": "Env", "Value": "prod"}]
+ """));
+ ProvisionContext ctx = new ProvisionContext(engine, REGION, ACCOUNT_ID, "TestStack", "MyPipe");
+
+ assertThrows(AwsException.class, () -> provisioner.provision(r, template, ctx));
+
+ assertEquals("IllegalStateException",
+ r.getAttributes().get(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR));
+ }
+
+ /** A pipe this stack update never mutated has no snapshot, so there is nothing to put back. */
+ @Test
+ void aPipeThisUpdateNeverMutatedIsNotRolledBack() {
+ StackResource created = provision(
+ pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+
+ assertFalse(provisioner.rollbackUpdate(created));
+
+ verify(pipes, never()).restorePipe(anyString(), any(), any(), any(), any(), any(), any(),
+ any(), any(), anyString());
+ }
+
+ /**
+ * A snapshot describes the update in flight. The one a previous update left behind is dropped
+ * before this run decides whether it mutates the pipe, so a rollback never puts back a target
+ * the pipe stopped carrying two updates ago.
+ */
+ @Test
+ void anUpdateThatFailsBeforeMutatingThePipeRollsNothingBack() {
+ provision(pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+ StackResource updated = provision(
+ pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, NEW_TARGET_QUEUE_ARN), "MyPipe");
+
+ StackResource r = stackResource("MyPipe");
+ r.getAttributes().putAll(updated.getAttributes());
+ JsonNode template = props(pipeTemplate("MyPipe",
+ "arn:aws:sqs:us-east-1:000000000000:other-queue", NEW_TARGET_QUEUE_ARN));
+ ProvisionContext ctx = new ProvisionContext(engine, REGION, ACCOUNT_ID, "TestStack", "MyPipe");
+
+ assertThrows(AwsException.class, () -> provisioner.provision(r, template, ctx));
+
+ assertFalse(provisioner.rollbackUpdate(r));
+ assertEquals(NEW_TARGET_QUEUE_ARN, pipesOnFile.get("MyPipe").getTarget());
+ }
+
+ /**
+ * A rename is a replacement, and a stack update that fails after it never commits. The rollback
+ * points the resource back at the prior pipe, which was only displaced and is put back untouched,
+ * and drops the replacement so the failed update leaves nothing behind.
+ */
+ @Test
+ void aRenameRolledBackPointsAtThePriorPipeAndDropsTheReplacement() {
+ provision(pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+ StackResource renamed = provision(
+ pipeTemplate("MyRenamedPipe", SOURCE_QUEUE_ARN, NEW_TARGET_QUEUE_ARN), "MyPipe");
+
+ assertTrue(provisioner.rollbackUpdate(renamed));
+
+ assertEquals("MyPipe", renamed.getPhysicalId(), "Ref names the prior pipe again");
+ assertEquals("arn:aws:pipes:us-east-1:000000000000:pipe/MyPipe",
+ renamed.getAttributes().get("Arn"), "Fn::GetAtt Arn names the prior pipe again");
+ verify(pipes).deletePipe("MyRenamedPipe", REGION);
+ assertFalse(pipesOnFile.containsKey("MyRenamedPipe"),
+ "the replacement the failed update created is gone");
+ assertEquals(TARGET_QUEUE_ARN, pipesOnFile.get("MyPipe").getTarget(),
+ "the prior pipe was only displaced, so it is put back untouched");
+ verify(pipes, never()).restorePipe(anyString(), any(), any(), any(), any(), any(), any(),
+ any(), any(), anyString());
+ verify(pipes, never()).updatePipe(anyString(), any(), any(), any(), any(), any(), any(),
+ any(), any(), anyString());
+ assertFalse(provisioner.hasReplacementUpdate(renamed),
+ "the rename cleanup does not fire after a rollback");
+ assertEquals(new UpdateCleanupResult(false, true, null, 0, null),
+ provisioner.completeUpdate(renamed));
+ }
+
+ /**
+ * Deleting the pipe standing under the replacement's name is only safe when this update is what
+ * created it. Any other pipe found there belongs to somebody else, so the rollback refuses and
+ * leaves both pipes where they are.
+ */
+ @Test
+ void aRenameRollbackRefusesToDeleteAPipeThisUpdateDidNotCreate() {
+ provision(pipeTemplate("MyPipe", SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN), null);
+ StackResource renamed = provision(
+ pipeTemplate("MyRenamedPipe", SOURCE_QUEUE_ARN, NEW_TARGET_QUEUE_ARN), "MyPipe");
+ Pipe unrelatedPipe = new Pipe();
+ unrelatedPipe.setName("MyRenamedPipe");
+ unrelatedPipe.setArn("arn:aws:pipes:us-east-1:000000000000:pipe/MyRenamedPipe");
+ unrelatedPipe.setTarget(TARGET_QUEUE_ARN);
+ unrelatedPipe.setCreationTime(Instant.ofEpochMilli(1_000_000L));
+ pipesOnFile.put("MyRenamedPipe", unrelatedPipe);
+
+ IllegalStateException refusal = assertThrows(IllegalStateException.class,
+ () -> provisioner.rollbackUpdate(renamed));
+
+ assertEquals("Refusing to delete pipe MyRenamedPipe while rolling back resource MyPipe: "
+ + "it was not created by this stack update, so both pipes are left as they are.",
+ refusal.getMessage());
+ verify(pipes, never()).deletePipe(anyString(), anyString());
+ assertTrue(pipesOnFile.containsKey("MyPipe"), "the prior pipe is left alone");
+ assertEquals(unrelatedPipe, pipesOnFile.get("MyRenamedPipe"),
+ "the pipe under the replacement's name is left alone");
+ assertEquals("MyPipe", renamed.getPhysicalId(),
+ "the resource names the prior pipe the refusal keeps");
+ assertFalse(provisioner.hasReplacementUpdate(renamed),
+ "the rename cleanup is spent, so no later update deletes the prior pipe");
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/PipesCfnRollbackRestoresExactConfigurationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/PipesCfnRollbackRestoresExactConfigurationTest.java
new file mode 100644
index 0000000000..00669427fe
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/PipesCfnRollbackRestoresExactConfigurationTest.java
@@ -0,0 +1,156 @@
+package io.github.hectorvent.floci.services.cloudformation.provisioners;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.github.hectorvent.floci.core.common.RegionResolver;
+import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
+import io.github.hectorvent.floci.core.storage.StorageFactory;
+import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
+import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
+import io.github.hectorvent.floci.services.pipes.PipesPoller;
+import io.github.hectorvent.floci.services.pipes.PipesService;
+import io.github.hectorvent.floci.services.pipes.model.Pipe;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+/**
+ * CloudFormation puts a resource back to the configuration it carried before a failed update. For
+ * {@code AWS::Pipes::Pipe} that includes the properties the pipe did not carry at all: an optional
+ * property the failed update added is gone once the rollback is done.
+ *
+ * <p>The stack update here fails at a later resource, so this pipe's own update committed and
+ * {@code rollbackUpdate} is the hook that puts it back. The pipe service is the real one over
+ * in-memory storage, so the restore is measured against the semantics the service really applies.
+ */
+class PipesCfnRollbackRestoresExactConfigurationTest {
+
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+ private static final String REGION = "us-east-1";
+ private static final String ACCOUNT_ID = "000000000000";
+ private static final String SOURCE_QUEUE_ARN = "arn:aws:sqs:us-east-1:000000000000:example-queue";
+ private static final String TARGET_QUEUE_ARN = "arn:aws:sqs:us-east-1:000000000000:example-target-queue";
+ private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/example-pipe-role";
+ private static final String ENRICHMENT_ARN =
+ "arn:aws:lambda:us-east-1:000000000000:function:example-enrichment";
+
+ private PipesService pipes;
+ private PipesCfnProvisioner provisioner;
+ private CloudFormationTemplateEngine engine;
+
+ @BeforeEach
+ void setUp() {
+ StorageFactory storageFactory = mock(StorageFactory.class);
+ when(storageFactory.create(anyString(), anyString(), any()))
+ .thenReturn(AccountAwareStorageBackend.inMemory(ACCOUNT_ID));
+ pipes = spy(new PipesService(storageFactory, new RegionResolver(REGION, ACCOUNT_ID),
+ mock(PipesPoller.class)));
+ provisioner = new PipesCfnProvisioner(pipes, MAPPER);
+
+ engine = mock(CloudFormationTemplateEngine.class);
+ when(engine.resolve(any())).thenAnswer(i -> {
+ JsonNode node = i.getArgument(0);
+ return node == null || node.isMissingNode() || node.isNull() ? null : node.asText();
+ });
+ when(engine.resolveNode(any())).thenAnswer(i -> i.getArgument(0));
+ }
+
+ private static JsonNode props(String json) {
+ try {
+ return MAPPER.readTree(json);
+ } catch (Exception unparseableTemplate) {
+ throw new IllegalArgumentException(unparseableTemplate);
+ }
+ }
+
+ private static StackResource stackResource(String priorPhysicalId) {
+ StackResource r = new StackResource();
+ r.setLogicalId("MyPipe");
+ r.setResourceType("AWS::Pipes::Pipe");
+ r.setPhysicalId(priorPhysicalId);
+ r.setAttributes(new HashMap<>());
+ return r;
+ }
+
+ private StackResource provision(String json, String priorPhysicalId) {
+ StackResource r = stackResource(priorPhysicalId);
+ provisioner.provision(r, props(json),
+ new ProvisionContext(engine, REGION, ACCOUNT_ID, "TestStack", priorPhysicalId));
+ return r;
+ }
+
+ /** A pipe with no Description and no Enrichment, tagged {@code Env=dev}. */
+ private static String pipeWithoutOptionalProperties() {
+ return """
+ {"Name": "MyPipe", "Source": "%s", "Target": "%s", "RoleArn": "%s",
+ "Tags": [{"Key": "Env", "Value": "dev"}]}
+ """.formatted(SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN, ROLE_ARN);
+ }
+
+ /** The same pipe with a Description and an Enrichment, under the tag value given. */
+ private static String pipeWithOptionalProperties(String description, String envTagValue) {
+ return """
+ {"Name": "MyPipe", "Source": "%s", "Target": "%s", "RoleArn": "%s",
+ "Description": "%s", "Enrichment": "%s",
+ "Tags": [{"Key": "Env", "Value": "%s"}]}
+ """.formatted(SOURCE_QUEUE_ARN, TARGET_QUEUE_ARN, ROLE_ARN, description,
+ ENRICHMENT_ARN, envTagValue);
+ }
+
+ /** The pipe's own update commits; the stack fails further on and rolls this one back. */
+ private StackResource committedUpdateOf(String createTemplate, String updateTemplate) {
+ provision(createTemplate, null);
+ return provision(updateTemplate, "MyPipe");
+ }
+
+ /**
+ * A property the pipe never carried is not part of the configuration it goes back to, so the
+ * rollback has to clear what the failed update added.
+ */
+ @Test
+ void aPropertyTheFailedUpdateAddedIsGoneAfterTheRollback() {
+ StackResource r = committedUpdateOf(pipeWithoutOptionalProperties(),
+ pipeWithOptionalProperties("added by the failed update", "prod"));
+ Pipe mutated = pipes.describePipe("MyPipe", REGION);
+ assertEquals("added by the failed update", mutated.getDescription(),
+ "the update wrote the description before the stack failed");
+ assertEquals(ENRICHMENT_ARN, mutated.getEnrichment(),
+ "the update wrote the enrichment before the stack failed");
+
+ assertTrue(provisioner.rollbackUpdate(r));
+
+ Pipe restored = pipes.describePipe("MyPipe", REGION);
+ assertNull(restored.getDescription(),
+ "the pipe carried no description before the update");
+ assertNull(restored.getEnrichment(),
+ "the pipe carried no enrichment before the update");
+ assertEquals(Map.of("Env", "dev"), restored.getTags());
+ }
+
+ /** A property the pipe did carry goes back to the value it held, not to nothing. */
+ @Test
+ void aPropertyTheFailedUpdateChangedGoesBackToItsFormerValue() {
+ StackResource r = committedUpdateOf(
+ pipeWithOptionalProperties("the original description", "dev"),
+ pipeWithOptionalProperties("the description the failed update wrote", "prod"));
+
+ assertTrue(provisioner.rollbackUpdate(r));
+
+ Pipe restored = pipes.describePipe("MyPipe", REGION);
+ assertEquals("the original description", restored.getDescription());
+ assertEquals(ENRICHMENT_ARN, restored.getEnrichment());
+ assertEquals(TARGET_QUEUE_ARN, restored.getTarget());
+ assertEquals(ROLE_ARN, restored.getRoleArn());
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/pipes/PipesServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/pipes/PipesServiceTest.java
index 4007414ea6..669e5687b0 100644
--- a/src/test/java/io/github/hectorvent/floci/services/pipes/PipesServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/pipes/PipesServiceTest.java
@@ -173,6 +173,62 @@ class PipesServiceTest {
assertEquals(PipeState.STOPPED, updated.getCurrentState());
}
+ /**
+ * The UpdatePipe contract: a property the caller omits keeps the value the pipe holds. It is
+ * the contract the CloudFormation update path relies on, and restorePipe is what reads an
+ * omitted property as a value to clear.
+ */
+ @Test
+ void updatePipeLeavesAnOmittedPropertyAlone() {
+ pipesService.createPipe("partial-update-pipe",
+ "arn:aws:sqs:us-east-1:000000000000:source",
+ "arn:aws:sqs:us-east-1:000000000000:target",
+ "arn:aws:iam::000000000000:role/original-role",
+ "the original description", DesiredState.RUNNING,
+ "arn:aws:lambda:us-east-1:000000000000:function:original-enrichment",
+ null, null, null, null, "us-east-1");
+
+ Pipe updated = pipesService.updatePipe("partial-update-pipe",
+ "arn:aws:sqs:us-east-1:000000000000:new-target",
+ null, null, null, null, null, null, null, "us-east-1");
+
+ assertEquals("arn:aws:sqs:us-east-1:000000000000:new-target", updated.getTarget());
+ assertEquals("arn:aws:iam::000000000000:role/original-role", updated.getRoleArn());
+ assertEquals("the original description", updated.getDescription());
+ assertEquals("arn:aws:lambda:us-east-1:000000000000:function:original-enrichment",
+ updated.getEnrichment());
+ assertEquals(DesiredState.RUNNING, updated.getDesiredState());
+ assertEquals(PipeState.RUNNING, updated.getCurrentState());
+ }
+
+ /**
+ * restorePipe is handed a whole configuration, so an omitted property is one the pipe must
+ * stop carrying. The desired state is the exception: currentState is read off it, so a null
+ * leaves both as they are instead of contradicting each other.
+ */
+ @Test
+ void restorePipeClearsAnOmittedPropertyAndKeepsTheDesiredState() {
+ pipesService.createPipe("restore-pipe",
+ "arn:aws:sqs:us-east-1:000000000000:source",
+ "arn:aws:sqs:us-east-1:000000000000:target",
+ "arn:aws:iam::000000000000:role/original-role",
+ "the original description", DesiredState.STOPPED,
+ "arn:aws:lambda:us-east-1:000000000000:function:original-enrichment",
+ null, null, null, null, "us-east-1");
+
+ Pipe restored = pipesService.restorePipe("restore-pipe",
+ "arn:aws:sqs:us-east-1:000000000000:target",
+ "arn:aws:iam::000000000000:role/original-role",
+ null, null, null, null, null, null, "us-east-1");
+
+ assertNull(restored.getDescription());
+ assertNull(restored.getEnrichment());
+ assertEquals("arn:aws:sqs:us-east-1:000000000000:target", restored.getTarget());
+ assertEquals("arn:aws:iam::000000000000:role/original-role", restored.getRoleArn());
+ assertEquals(DesiredState.STOPPED, restored.getDesiredState());
+ assertEquals(PipeState.STOPPED, restored.getCurrentState());
+ }
+
@Test
void deletePipe() {
pipesService.createPipe("del-pipe",