Skip to content

armgit / floci

Public Imported from GitHub
0

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

feat(cloudformation): support AWS::Route53::RecordSetGroup (#6676)

Jackson Pricef3f8b422008a
diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.md
index 6e68669cc8..10ea328e45 100644
--- a/docs/services/cloudformation.md
+++ b/docs/services/cloudformation.md
@@ -193,7 +193,7 @@ cross-resource references.
| Elastic Load Balancing v2 | `LoadBalancer`, `TargetGroup`, `Listener`, `ListenerRule` |
| Auto Scaling | `LaunchConfiguration`, `AutoScalingGroup`, `LifecycleHook`, `ScalingPolicy` |
| Application Auto Scaling | `ScalableTarget` (`ScheduledActions` are accepted and not run; Floci has no scheduled scaling), `ScalingPolicy` |
-| Route 53 | `HostedZone`, `RecordSet`, `HealthCheck` |
+| Route 53 | `HostedZone`, `RecordSet`, `RecordSetGroup` (written in one change batch, so a record set that cannot be written leaves the zone unchanged; every record set must be in the group's hosted zone), `HealthCheck` |
| Cloud Map | `HttpNamespace`, `PrivateDnsNamespace`, `PublicDnsNamespace`, `Service` |
| API Gateway (v1) | `RestApi`, `Resource`, `Authorizer`, `Method`, `Deployment`, `Stage`, `Account`, `DomainName`, `BasePathMapping`, `GatewayResponse`, `ApiKey`, `UsagePlan`, `UsagePlanKey` |
| API Gateway v2 | `Api`, `Authorizer`, `Route`, `Integration`, `Stage`, `Deployment`, `DomainName`, `ApiMapping`, `VpcLink` |
@@ -902,11 +902,18 @@ UPDATE_ROLLBACK_FAILED state and can not be updated.`). Two ways out remain:
delete failed; a resource whose restore cannot be redone fails again, leaving the stack in
`UPDATE_ROLLBACK_FAILED`, and can be passed in `ResourcesToSkip`. `ResourcesToSkip` names
`UPDATE_FAILED` resources to report as `UPDATE_COMPLETE` with the reason `Resource skipped
- during UpdateRollback`, without rolling them back; a skipped resource keeps the state the failed
- update left it in. Once every other resource rolled back, the stack reaches
+ during UpdateRollback`, without rolling them back; a skipped resource keeps the state its failed
+ update and rollback left it in. Once every other resource rolled back, the stack reaches
`UPDATE_ROLLBACK_COMPLETE` with the template, parameters and outputs from before the failed
update, and takes updates again.
+An `AWS::Route53::RecordSetGroup` whose zone-change rollback cannot delete the records it wrote into
+the new zone first points back at its old records, so the group already matches the template the
+stack rolls back to. `ContinueUpdateRollback` retries that delete, and `DeleteStack` deletes those
+records while the group is still `UPDATE_FAILED`, giving up after three attempts as for any replaced
+resource. Nothing else deletes them, since nothing tells them apart from an identical record written
+there later: once the group is skipped, or a later update commits, they stay in their zone.
+
`ForceRollback=true` skips every `UPDATE_FAILED` resource of the stack, each reported as a
`ResourcesToSkip` skip is. When a forced rollback skipped a resource and completes, the stack's
`StackStatusReason` and the reason of its `UPDATE_ROLLBACK_COMPLETE` event are `One or more
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Route53CfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Route53CfnProvisioner.java
index 1b4ff8fbc8..f534396423 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Route53CfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Route53CfnProvisioner.java
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -8,6 +9,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.route53.Route53Service;
import io.github.hectorvent.floci.services.route53.model.AliasTarget;
+import io.github.hectorvent.floci.services.route53.model.ChangeInfo;
import io.github.hectorvent.floci.services.route53.model.HealthCheck;
import io.github.hectorvent.floci.services.route53.model.HealthCheckConfig;
import io.github.hectorvent.floci.services.route53.model.HostedZone;
@@ -28,8 +30,8 @@ import java.util.Set;
import java.util.UUID;
/**
- * Provisions {@code AWS::Route53::HostedZone}, {@code AWS::Route53::RecordSet} and
- * {@code AWS::Route53::HealthCheck}.
+ * Provisions {@code AWS::Route53::HostedZone}, {@code AWS::Route53::RecordSet},
+ * {@code AWS::Route53::RecordSetGroup} and {@code AWS::Route53::HealthCheck}.
*
* <p>A record set is written into its hosted zone through {@code ChangeResourceRecordSets}. On stack
* create, and on an update that changes the record's identity (zone, Name, Type or SetIdentifier), the
@@ -41,11 +43,15 @@ import java.util.UUID;
* written without one. No {@code Fn::GetAtt} attribute is published: the registry lists {@code Id} as
* read-only, but the type has no registry handlers and the resource specification gives it no
* attributes, so {@code Ref} is its only reference.
+ *
+ * <p>A record set group writes all its record sets in one change batch; see
+ * {@link #provisionRecordSetGroup}.
*/
@ApplicationScoped
public class Route53CfnProvisioner implements CfnResourceProvisioner {
static final String HOSTED_ZONE = "AWS::Route53::HostedZone";
static final String RECORD_SET = "AWS::Route53::RecordSet";
+ static final String RECORD_SET_GROUP = "AWS::Route53::RecordSetGroup";
static final String HEALTH_CHECK = "AWS::Route53::HealthCheck";
/** The registry schema's create-only HealthCheckConfig members. */
@@ -62,6 +68,15 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner {
private static final String RECORD_ZONE_ATTR = "__FlociRoute53RecordZoneId";
private static final String RECORD_TYPE_ATTR = "__FlociRoute53RecordType";
private static final String RECORD_SET_ID_ATTR = "__FlociRoute53RecordSetIdentifier";
+ // A group's records as it wrote them, which its DELETE changes must name exactly; its physical id
+ // and attributes before an update, kept until the update commits or rolls back; the records a
+ // failed update wrote that its rollback could not delete; and the cleanup's delete attempts.
+ private static final String GROUP_RECORDS_ATTR = "__FlociRoute53GroupRecords";
+ private static final String GROUP_UPDATE_ATTR = "__FlociRoute53GroupUpdate";
+ private static final String GROUP_OWED_ATTR = "__FlociRoute53GroupOwedDeletes";
+ private static final String GROUP_CLEANUP_ATTEMPTS_ATTR = "__FlociRoute53GroupCleanupAttempts";
+ private static final TypeReference<List<ResourceRecordSet>> RECORDS = new TypeReference<>() { };
+ private static final TypeReference<Map<String, String>> ATTRIBUTES = new TypeReference<>() { };
private static final Logger LOG = Logger.getLogger(Route53CfnProvisioner.class);
@@ -74,7 +89,7 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner {
@Override
public Set<String> resourceTypes() {
- return Set.of(HOSTED_ZONE, RECORD_SET, HEALTH_CHECK);
+ return Set.of(HOSTED_ZONE, RECORD_SET, RECORD_SET_GROUP, HEALTH_CHECK);
}
@Override
@@ -82,6 +97,7 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner {
switch (resource.getResourceType()) {
case "AWS::Route53::HostedZone" -> provisionHostedZone(resource, props, ctx);
case "AWS::Route53::RecordSet" -> provisionRecordSet(resource, props, ctx);
+ case "AWS::Route53::RecordSetGroup" -> provisionRecordSetGroup(resource, props, ctx);
case "AWS::Route53::HealthCheck" -> provisionHealthCheck(resource, props, ctx);
default -> throw new IllegalStateException(
"Route53CfnProvisioner received an unsupported type: " + resource.getResourceType());
@@ -90,17 +106,52 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner {
private void provisionRecordSet(StackResource resource, JsonNode props, ProvisionContext ctx) {
Map<String, String> priorAttributes = new HashMap<>(resource.getAttributes());
+ ResourceRecordSet rrs = recordSet(RECORD_SET, props, ctx);
+ String name = ctx.resolveOptional(props, "Name");
+ String type = rrs.getType();
+ String setIdentifier = rrs.getSetIdentifier();
+ String zoneId = resolveZoneId(RECORD_SET, props, ctx);
+
+ // The record's identity is Name + Type + SetIdentifier within a zone. Re-applying the identity
+ // this resource wrote last time is an UPSERT, which replaces whatever record holds that
+ // identity. Any other identity, on stack create or on an update that changes it, is a record
+ // the stack does not own yet, so it is a CREATE: a record another owner already holds under
+ // that identity fails the stack instead of being taken over.
+ boolean sameIdentity = isPriorIdentity(ctx, priorAttributes, zoneId, name, type, setIdentifier);
+ route53Service.changeResourceRecordSets(zoneId, List.of(change(sameIdentity ? "UPSERT" : "CREATE", rrs)),
+ "CloudFormation " + ctx.stackName() + "/" + resource.getLogicalId());
+
+ // An update that changes the identity leaves the prior record orphaned, so remove it,
+ // otherwise it lingers in the zone and later blocks the zone's own delete.
+ if (!sameIdentity) {
+ removeSupersededRecord(ctx, priorAttributes);
+ }
+
+ resource.setPhysicalId(name);
+ resource.getAttributes().put(RECORD_ZONE_ATTR, zoneId);
+ resource.getAttributes().put(RECORD_TYPE_ATTR, type);
+ if (setIdentifier != null && !setIdentifier.isBlank()) {
+ resource.getAttributes().put(RECORD_SET_ID_ATTR, setIdentifier);
+ } else {
+ resource.getAttributes().remove(RECORD_SET_ID_ATTR);
+ }
+ }
+
+ /**
+ * The record an {@code AWS::Route53::RecordSet}, or one entry of a group's {@code RecordSets},
+ * describes. {@code owner} names it in validation errors.
+ */
+ private ResourceRecordSet recordSet(String owner, JsonNode props, ProvisionContext ctx) {
String name = ctx.resolveOptional(props, "Name");
String type = ctx.resolveOptional(props, "Type");
// Type and Name are the schema's required properties; reject rather than write a nameless
// or typeless record the zone would refuse.
if (name == null || name.isBlank()) {
- throw new AwsException("ValidationError", "AWS::Route53::RecordSet requires Name.", 400);
+ throw new AwsException("ValidationError", owner + " requires Name.", 400);
}
if (type == null || type.isBlank()) {
- throw new AwsException("ValidationError", "AWS::Route53::RecordSet requires Type.", 400);
+ throw new AwsException("ValidationError", owner + " requires Type.", 400);
}
- String zoneId = resolveZoneId(props, ctx);
ResourceRecordSet rrs = new ResourceRecordSet();
rrs.setName(Route53Service.normalizeName(name));
@@ -113,13 +164,11 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner {
try {
rrs.setTtl(Long.parseLong(ttl.trim()));
} catch (NumberFormatException e) {
- throw new AwsException("ValidationError",
- "AWS::Route53::RecordSet TTL must be an integer: " + ttl, 400);
+ throw new AwsException("ValidationError", owner + " TTL must be an integer: " + ttl, 400);
}
}
// Route 53 requires a set identifier of at least one character, so a blank one means none.
- String setIdentifier = ctx.resolveOrDefault(props, "SetIdentifier", null);
- rrs.setSetIdentifier(setIdentifier);
+ rrs.setSetIdentifier(ctx.resolveOrDefault(props, "SetIdentifier", null));
rrs.setRecords(parseResourceRecords(props, ctx));
rrs.setAliasTarget(parseAliasTarget(props, ctx));
rrs.setRegion(ctx.resolveOrDefault(props, "Region", null));
@@ -130,36 +179,389 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner {
try {
rrs.setWeight(Long.parseLong(weight.trim()));
} catch (NumberFormatException e) {
+ throw new AwsException("ValidationError", owner + " Weight must be an integer: " + weight, 400);
+ }
+ }
+ return rrs;
+ }
+
+ /**
+ * Writes a group's record sets in one {@code ChangeResourceRecordSets} batch. Route 53 checks a
+ * batch whole before it changes anything, so a record set that cannot be written, such as one
+ * whose name another owner holds, leaves the zone as it was. Each record set is sent as a CREATE,
+ * as for a single record set the stack does not own yet, and an update in the same zone first
+ * DELETEs each record the group wrote last time. A zone change, a replacement in the registry
+ * schema, writes the group into the new zone and leaves the old records until the update
+ * commits, when {@link #completeUpdate} deletes them unless {@code UpdateReplacePolicy} is
+ * {@code Retain}. A failed update puts the group back from the snapshot taken before it
+ * ({@link #rollbackGroupUpdate}). {@code Ref} is the logical id, the group name AWS documents
+ * ({@code MyRecordSetGroup} in its example), and {@code Fn::GetAtt Id} is the batch's change
+ * ID, which {@code GetChange} reads.
+ */
+ private void provisionRecordSetGroup(StackResource resource, JsonNode props, ProvisionContext ctx) {
+ JsonNode members = ctx.engine().resolveNodeOmittingNoValue(props != null ? props.get("RecordSets") : null);
+ if (members == null || !members.isArray() || members.isEmpty()) {
+ throw new AwsException("ValidationError",
+ RECORD_SET_GROUP + " requires at least one record set in RecordSets.", 400);
+ }
+ String zoneId = resolveGroupZoneId(props, members, ctx);
+ List<ResourceRecordSet> desired = new ArrayList<>();
+ for (int i = 0; i < members.size(); i++) {
+ ResourceRecordSet record = recordSet(memberLabel(i), members.get(i), ctx);
+ for (int j = 0; j < i; j++) {
+ if (Route53Service.sameRecord(desired.get(j), record)) {
+ throw new AwsException("ValidationError", memberLabel(i)
+ + " has the Name, Type and SetIdentifier of RecordSets[" + j + "].", 400);
+ }
+ }
+ desired.add(record);
+ }
+ String records = writeJson(desired);
+
+ // A snapshot an earlier update left is stale: ContinueUpdateRollback skipped that update's
+ // rollback, so the records it wrote are the group's now, and the ones it could not delete are
+ // left in their zone.
+ resource.getAttributes().remove(GROUP_UPDATE_ATTR);
+ resource.getAttributes().remove(GROUP_CLEANUP_ATTEMPTS_ATTR);
+ forgetOwedRecords(resource);
+ String snapshot = ctx.isUpdate() ? snapshotOf(resource) : null;
+ List<Map<String, Object>> changes = new ArrayList<>();
+ if (ctx.isUpdate() && zoneId.equals(resource.getAttributes().get(RECORD_ZONE_ATTR))) {
+ changes.addAll(deletionsOf(zoneId, groupRecords(resource.getAttributes())));
+ }
+ desired.forEach(record -> changes.add(change("CREATE", record)));
+ ChangeInfo change = route53Service.changeResourceRecordSets(zoneId, changes, ctx.resolveOrDefault(props,
+ "Comment", "CloudFormation " + ctx.stackName() + "/" + resource.getLogicalId()));
+
+ resource.setPhysicalId(resource.getLogicalId());
+ resource.getAttributes().put("Id", change.getId());
+ resource.getAttributes().put(RECORD_ZONE_ATTR, zoneId);
+ resource.getAttributes().put(GROUP_RECORDS_ATTR, records);
+ if (snapshot != null) {
+ resource.getAttributes().put(GROUP_UPDATE_ATTR, snapshot);
+ }
+ }
+
+ /**
+ * The zone a group writes into: its own {@code HostedZoneId} or {@code HostedZoneName}, or, when it
+ * names neither, the one its record sets name. AWS fails a group whose record set sets the
+ * {@code HostedZoneId} the group already sets, and a group is one change batch, which Route 53
+ * applies to a single zone, so a record set naming another zone is refused rather than written
+ * into this one.
+ */
+ private String resolveGroupZoneId(JsonNode props, JsonNode members, ProvisionContext ctx) {
+ boolean groupSetsZoneId = isSet(ctx.resolveOptional(props, "HostedZoneId"));
+ String zoneId = namesZone(props, ctx) ? resolveZoneId(RECORD_SET_GROUP, props, ctx) : null;
+ for (int i = 0; i < members.size(); i++) {
+ JsonNode member = members.get(i);
+ if (groupSetsZoneId && isSet(ctx.resolveOptional(member, "HostedZoneId"))) {
throw new AwsException("ValidationError",
- "AWS::Route53::RecordSet Weight must be an integer: " + weight, 400);
+ memberLabel(i) + " sets HostedZoneId, which the group already sets.", 400);
+ }
+ if (!namesZone(member, ctx)) {
+ continue;
}
+ String memberZoneId = resolveZoneId(memberLabel(i), member, ctx);
+ if (zoneId == null) {
+ zoneId = memberZoneId;
+ } else if (!zoneId.equals(memberZoneId)) {
+ throw new AwsException("ValidationError", memberLabel(i) + " is in hosted zone " + memberZoneId
+ + ", not " + zoneId + ": a record set group writes to a single hosted zone.", 400);
+ }
+ }
+ if (zoneId == null) {
+ throw new AwsException("ValidationError",
+ RECORD_SET_GROUP + " requires HostedZoneId or HostedZoneName.", 400);
}
+ return zoneId;
+ }
- // The record's identity is Name + Type + SetIdentifier within a zone. Re-applying the identity
- // this resource wrote last time is an UPSERT, which replaces whatever record holds that
- // identity. Any other identity, on stack create or on an update that changes it, is a record
- // the stack does not own yet, so it is a CREATE: a record another owner already holds under
- // that identity fails the stack instead of being taken over.
- boolean sameIdentity = isPriorIdentity(ctx, priorAttributes, zoneId, name, type, setIdentifier);
+ private static boolean namesZone(JsonNode props, ProvisionContext ctx) {
+ return isSet(ctx.resolveOptional(props, "HostedZoneId")) || isSet(ctx.resolveOptional(props, "HostedZoneName"));
+ }
+
+ private static boolean isSet(String value) {
+ return value != null && !value.isBlank();
+ }
+
+ private static String memberLabel(int index) {
+ return RECORD_SET_GROUP + " RecordSets[" + index + "]";
+ }
+
+ private static Map<String, Object> change(String action, ResourceRecordSet record) {
Map<String, Object> change = new HashMap<>();
- change.put("action", sameIdentity ? "UPSERT" : "CREATE");
- change.put("rrs", rrs);
- route53Service.changeResourceRecordSets(zoneId, List.of(change),
- "CloudFormation " + ctx.stackName() + "/" + resource.getLogicalId());
+ change.put("action", action);
+ change.put("rrs", record);
+ return change;
+ }
- // An update that changes the identity leaves the prior record orphaned, so remove it,
- // otherwise it lingers in the zone and later blocks the zone's own delete.
- if (!sameIdentity) {
- removeSupersededRecord(ctx, priorAttributes);
+ /** A DELETE for each of {@code records} the zone still holds; one already gone needs none. */
+ private List<Map<String, Object>> deletionsOf(String zoneId, List<ResourceRecordSet> records) {
+ List<ResourceRecordSet> current = route53Service.listResourceRecordSets(zoneId, null, null, 0);
+ List<Map<String, Object>> changes = new ArrayList<>();
+ for (ResourceRecordSet record : records) {
+ if (current.stream().anyMatch(existing -> Route53Service.sameRecord(existing, record))) {
+ changes.add(change("DELETE", record));
+ }
}
+ return changes;
+ }
- resource.setPhysicalId(name);
- resource.getAttributes().put(RECORD_ZONE_ATTR, zoneId);
- resource.getAttributes().put(RECORD_TYPE_ATTR, type);
- if (setIdentifier != null && !setIdentifier.isBlank()) {
- resource.getAttributes().put(RECORD_SET_ID_ATTR, setIdentifier);
+ /**
+ * Deletes the records of {@code records} the zone still holds. Each DELETE names the values the
+ * group wrote, so a record changed outside the stack fails the batch instead of being deleted. A
+ * zone already gone has nothing left to delete.
+ */
+ private void deleteGroupRecords(String zoneId, List<ResourceRecordSet> records, String logicalId) {
+ if (zoneId == null || zoneId.isBlank() || records.isEmpty()) {
+ return;
+ }
+ CfnDeletes.safeDelete("Route53 record set group", logicalId, () -> {
+ List<Map<String, Object>> changes = deletionsOf(zoneId, records);
+ if (!changes.isEmpty()) {
+ route53Service.changeResourceRecordSets(zoneId, changes, "CloudFormation delete " + logicalId);
+ }
+ }, "NoSuchHostedZone");
+ }
+
+ /**
+ * Deletes what a group owes beyond its own records and returns the first failure, or null. The
+ * records a committed zone change left in the old zone go unless {@code UpdateReplacePolicy:
+ * Retain} keeps them. The records a failed zone-change rollback could not delete go only when
+ * {@code deleteOwed}, which only DeleteStack's cleanup passes, while that rollback is unfinished;
+ * Retain never keeps those, because no update that wrote them committed. Any other caller leaves
+ * them in their zone ({@link #forgetOwedRecords}).
+ */
+ private RuntimeException deleteDisplacedRecords(StackResource resource, boolean deleteOwed) {
+ RuntimeException failure = null;
+ GroupSnapshot prior = groupSnapshot(resource);
+ if (prior != null) {
+ try {
+ if (movedZone(resource, prior) && !retained(resource)) {
+ deleteGroupRecords(prior.attributes().get(RECORD_ZONE_ATTR), groupRecords(prior.attributes()),
+ resource.getLogicalId());
+ }
+ resource.getAttributes().remove(GROUP_UPDATE_ATTR);
+ } catch (RuntimeException e) {
+ failure = e;
+ }
+ }
+ if (!deleteOwed) {
+ forgetOwedRecords(resource);
} else {
- resource.getAttributes().remove(RECORD_SET_ID_ATTR);
+ try {
+ deleteOwedRecords(resource);
+ } catch (RuntimeException e) {
+ failure = failure != null ? failure : e;
+ }
+ }
+ return failure;
+ }
+
+ /**
+ * Deletes the records a failed zone-change rollback could not delete. They stay owed when the
+ * delete fails, and the failure propagates.
+ */
+ private void deleteOwedRecords(StackResource resource) {
+ OwedRecords owed = owedRecords(resource);
+ if (owed != null) {
+ deleteGroupRecords(owed.zoneId(), owed.records(), resource.getLogicalId());
+ resource.getAttributes().remove(GROUP_OWED_ATTR);
+ }
+ }
+
+ /**
+ * Leaves in their zone the records a failed rollback could not delete, once nothing will retry that
+ * rollback, as AWS leaves a skipped resource's: nothing tells them apart from an identical record
+ * another writer puts there later, which deleting them would take. Only ContinueUpdateRollback
+ * and DeleteStack delete them; a commit, a provision or a removal of the group comes here.
+ */
+ private static void forgetOwedRecords(StackResource resource) {
+ if (resource.getAttributes().remove(GROUP_OWED_ATTR) != null) {
+ LOG.infov("Leaving in their zone the records a failed rollback of {0} could not delete",
+ resource.getLogicalId());
+ }
+ }
+
+ /** Whether the group's rollback failed and was neither retried to completion nor skipped since. */
+ private static boolean rollbackUnfinished(StackResource resource) {
+ return "UPDATE_FAILED".equals(resource.getStatus());
+ }
+
+ /**
+ * One attempt at the cleanup a group owes once an update commits ({@code deleteOwed} false) or
+ * the stack is deleted, the shape {@link ReplacementCleanup} gives other types: the attempts are
+ * counted on the resource, so the engine stops after its third and {@link #clearUpdate} then gives
+ * up on those records.
+ */
+ private UpdateCleanupResult completeGroupCleanup(StackResource resource, boolean deleteOwed) {
+ GroupSnapshot prior = groupSnapshot(resource);
+ boolean owed = resource.getAttributes().containsKey(GROUP_OWED_ATTR);
+ if (prior == null && !owed) {
+ return UpdateCleanupResult.notApplicable();
+ }
+ boolean deletes = (prior != null && movedZone(resource, prior) && !retained(resource))
+ || (owed && deleteOwed);
+ RuntimeException failure = deleteDisplacedRecords(resource, deleteOwed);
+ if (failure != null) {
+ int attempts = Integer.parseInt(
+ resource.getAttributes().getOrDefault(GROUP_CLEANUP_ATTEMPTS_ATTR, "0")) + 1;
+ resource.getAttributes().put(GROUP_CLEANUP_ATTEMPTS_ATTR, Integer.toString(attempts));
+ return new UpdateCleanupResult(true, false, resource.getPhysicalId(), attempts, failure.getMessage());
+ }
+ resource.getAttributes().remove(GROUP_CLEANUP_ATTEMPTS_ATTR);
+ return deletes ? new UpdateCleanupResult(true, true, resource.getPhysicalId(), 0, null)
+ : UpdateCleanupResult.notApplicable();
+ }
+
+ /**
+ * The group's physical id while a committed zone change owes the old zone's records a delete,
+ * which is what the cleanup announces. Records a failed rollback left are not announced: the
+ * engine asks before it says whether it commits, which never deletes them, or deletes the stack.
+ */
+ private static String groupCleanupPhysicalId(StackResource resource) {
+ GroupSnapshot prior = groupSnapshot(resource);
+ return prior != null && movedZone(resource, prior) && !retained(resource) ? resource.getPhysicalId() : null;
+ }
+
+ /**
+ * Puts a group back as the failed update found it. In the same zone one batch DELETEs the records
+ * the update wrote and CREATEs the ones it replaced, and the snapshot goes only once that
+ * succeeded. After a zone change the group first points at its old records again, which the update
+ * never touched, and then the new zone's records are deleted, as {@link ReplacementCleanup}
+ * rolls a replacement back. A delete that fails leaves those records owed: ContinueUpdateRollback
+ * retries it, and DeleteStack deletes them while the rollback is unfinished. A skip keeps the group
+ * matching the template it rolls back to and leaves those records in their zone, as anything else
+ * that moves the stack on does.
+ */
+ private boolean rollbackGroupUpdate(StackResource resource) {
+ GroupSnapshot prior = groupSnapshot(resource);
+ if (prior == null) {
+ // A retried rollback: the group already names its old records, and only the records the
+ // failed one could not delete are left to it.
+ deleteOwedRecords(resource);
+ return true;
+ }
+ String zoneId = resource.getAttributes().get(RECORD_ZONE_ATTR);
+ List<ResourceRecordSet> written = groupRecords(resource.getAttributes());
+ if (movedZone(resource, prior)) {
+ restore(resource, prior);
+ try {
+ deleteGroupRecords(zoneId, written, resource.getLogicalId());
+ } catch (RuntimeException failure) {
+ writeOwedRecords(resource, new OwedRecords(zoneId, written));
+ throw failure;
+ }
+ } else {
+ List<Map<String, Object>> changes = deletionsOf(zoneId, written);
+ groupRecords(prior.attributes()).forEach(record -> changes.add(change("CREATE", record)));
+ route53Service.changeResourceRecordSets(zoneId, changes,
+ "CloudFormation rollback " + resource.getLogicalId());
+ restore(resource, prior);
+ }
+ return true;
+ }
+
+ private static void restore(StackResource resource, GroupSnapshot prior) {
+ resource.setPhysicalId(prior.physicalId());
+ resource.getAttributes().clear();
+ resource.getAttributes().putAll(prior.attributes());
+ }
+
+ /**
+ * Deletes a group's records, after the old zone's records an unfinished zone change left. DeleteStack's
+ * cleanup has already dealt with the records a failed rollback left; a removal leaves them.
+ */
+ private void deleteRecordSetGroup(StackResource resource) {
+ RuntimeException failure = deleteDisplacedRecords(resource, false);
+ if (failure != null) {
+ throw failure;
+ }
+ deleteGroupRecords(resource.getAttributes().get(RECORD_ZONE_ATTR), groupRecords(resource.getAttributes()),
+ resource.getLogicalId());
+ }
+
+ /**
+ * Whether the update a snapshot describes moved the group to another zone. A stub an older Floci
+ * left in place of a group wrote no zone, so replacing it is not a move.
+ */
+ private static boolean movedZone(StackResource resource, GroupSnapshot prior) {
+ String priorZoneId = prior.attributes().get(RECORD_ZONE_ATTR);
+ return priorZoneId != null && !priorZoneId.equals(resource.getAttributes().get(RECORD_ZONE_ATTR));
+ }
+
+ private static boolean retained(StackResource resource) {
+ return "Retain".equals(resource.getUpdateReplacePolicy());
+ }
+
+ /** A group's physical id and attributes before an update. */
+ private record GroupSnapshot(String physicalId, Map<String, String> attributes) {
+ }
+
+ /** Records a failed update wrote into {@code zoneId} that its rollback could not delete. */
+ private record OwedRecords(String zoneId, List<ResourceRecordSet> records) {
+ }
+
+ private static String snapshotOf(StackResource resource) {
+ ObjectNode snapshot = MAPPER.createObjectNode();
+ snapshot.put("physicalId", resource.getPhysicalId());
+ snapshot.set("attributes", MAPPER.valueToTree(resource.getAttributes()));
+ return snapshot.toString();
+ }
+
+ /** What the group had before the update in flight, or null when none is. */
+ private static GroupSnapshot groupSnapshot(StackResource resource) {
+ String raw = resource.getAttributes().get(GROUP_UPDATE_ATTR);
+ if (raw == null) {
+ return null;
+ }
+ try {
+ JsonNode snapshot = MAPPER.readTree(raw);
+ return new GroupSnapshot(snapshot.path("physicalId").asText(null),
+ MAPPER.convertValue(snapshot.path("attributes"), ATTRIBUTES));
+ } catch (JsonProcessingException | IllegalArgumentException e) {
+ throw new IllegalStateException("Could not read the update snapshot of " + resource.getLogicalId(), e);
+ }
+ }
+
+ private static OwedRecords owedRecords(StackResource resource) {
+ String raw = resource.getAttributes().get(GROUP_OWED_ATTR);
+ if (raw == null) {
+ return null;
+ }
+ try {
+ JsonNode owed = MAPPER.readTree(raw);
+ return new OwedRecords(owed.path("zoneId").asText(), MAPPER.convertValue(owed.path("records"), RECORDS));
+ } catch (JsonProcessingException | IllegalArgumentException e) {
+ throw new IllegalStateException(
+ "Could not read the records " + resource.getLogicalId() + " owes a delete", e);
+ }
+ }
+
+ private static void writeOwedRecords(StackResource resource, OwedRecords owed) {
+ ObjectNode node = MAPPER.createObjectNode();
+ node.put("zoneId", owed.zoneId());
+ node.set("records", MAPPER.valueToTree(owed.records()));
+ resource.getAttributes().put(GROUP_OWED_ATTR, node.toString());
+ }
+
+ private static List<ResourceRecordSet> groupRecords(Map<String, String> attributes) {
+ String raw = attributes.get(GROUP_RECORDS_ATTR);
+ if (raw == null) {
+ return List.of();
+ }
+ try {
+ return MAPPER.readValue(raw, RECORDS);
+ } catch (JsonProcessingException e) {
+ throw new IllegalStateException("Could not read the records of a Route 53 record set group", e);
+ }
+ }
+
+ private static String writeJson(Object value) {
+ try {
+ return MAPPER.writeValueAsString(value);
+ } catch (JsonProcessingException e) {
+ throw new IllegalStateException("Could not record a Route 53 record set group", e);
}
}
@@ -203,19 +605,18 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner {
/**
* The hosted zone the record belongs to: {@code HostedZoneId} when given (a Ref resolves to the
* real zone id), otherwise the zone matching {@code HostedZoneName}. One of the two is required,
- * since a record cannot be written without a zone.
+ * since a record cannot be written without a zone. {@code owner} names the record in errors.
*/
- private String resolveZoneId(JsonNode props, ProvisionContext ctx) {
+ private String resolveZoneId(String owner, JsonNode props, ProvisionContext ctx) {
String zoneId = ctx.resolveOptional(props, "HostedZoneId");
if (zoneId != null && !zoneId.isBlank()) {
return zoneId;
}
String zoneName = ctx.resolveOptional(props, "HostedZoneName");
if (zoneName != null && !zoneName.isBlank()) {
- return zoneIdByName(zoneName);
+ return zoneIdByName(owner, zoneName);
}
- throw new AwsException("ValidationError",
- "AWS::Route53::RecordSet requires HostedZoneId or HostedZoneName.", 400);
+ throw new AwsException("ValidationError", owner + " requires HostedZoneId or HostedZoneName.", 400);
}
/**
@@ -225,7 +626,7 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner {
* stack both when no zone matches and when more than one carries the name, so only a single match
* resolves.
*/
- private String zoneIdByName(String zoneName) {
+ private String zoneIdByName(String owner, String zoneName) {
String normalized = zoneName.endsWith(".") ? zoneName : zoneName + ".";
List<HostedZone> matches = route53Service.listHostedZonesByName(zoneName, 0).stream()
.filter(zone -> normalized.equals(zone.getName()))
@@ -234,10 +635,8 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner {
return matches.get(0).getId();
}
throw new AwsException("InvalidChangeBatch", matches.isEmpty()
- ? "AWS::Route53::RecordSet references HostedZoneName " + zoneName
- + " but no such hosted zone exists."
- : "AWS::Route53::RecordSet HostedZoneName " + zoneName
- + " matches more than one hosted zone; use HostedZoneId.", 400);
+ ? owner + " references HostedZoneName " + zoneName + " but no such hosted zone exists."
+ : owner + " HostedZoneName " + zoneName + " matches more than one hosted zone; use HostedZoneId.", 400);
}
private List<ResourceRecord> parseResourceRecords(JsonNode props, ProvisionContext ctx) {
@@ -562,36 +961,70 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner {
@Override
public boolean hasReplacementUpdate(StackResource resource) {
+ if (RECORD_SET_GROUP.equals(resource.getResourceType())) {
+ GroupSnapshot prior = groupSnapshot(resource);
+ return prior != null && movedZone(resource, prior);
+ }
return HEALTH_CHECK.equals(resource.getResourceType()) && ReplacementCleanup.hasReplacement(resource);
}
@Override
public String updateCleanupPhysicalId(StackResource resource) {
+ if (RECORD_SET_GROUP.equals(resource.getResourceType())) {
+ return groupCleanupPhysicalId(resource);
+ }
return HEALTH_CHECK.equals(resource.getResourceType())
? ReplacementCleanup.cleanupPhysicalId(resource) : null;
}
@Override
public UpdateCleanupResult completeUpdate(StackResource resource) {
+ if (RECORD_SET_GROUP.equals(resource.getResourceType())) {
+ return completeGroupCleanup(resource, false);
+ }
return HEALTH_CHECK.equals(resource.getResourceType())
? ReplacementCleanup.complete(resource, this::delete) : UpdateCleanupResult.notApplicable();
}
+ /**
+ * DeleteStack's cleanup also deletes the records a failed zone-change rollback of a group could not
+ * delete, while that rollback is unfinished. A commit's cleanup never does: a nested stack's child
+ * can commit while its own rollback is still unfinished.
+ */
+ @Override
+ public UpdateCleanupResult completeDeleteCleanup(StackResource resource) {
+ return RECORD_SET_GROUP.equals(resource.getResourceType())
+ ? completeGroupCleanup(resource, rollbackUnfinished(resource)) : completeUpdate(resource);
+ }
+
+ /**
+ * The engine calls this once a cleanup completed or gave up. A group then forgets the records it
+ * could not delete, as {@link ReplacementCleanup#clear} forgets a displaced entity: they stay in
+ * their zone, outside the stack.
+ */
@Override
public void clearUpdate(StackResource resource) {
if (HEALTH_CHECK.equals(resource.getResourceType())) {
resource.getAttributes().remove(CfnRollback.HEALTH_CHECK_UPDATE_SNAPSHOT_ATTR);
ReplacementCleanup.clear(resource);
+ } else if (RECORD_SET_GROUP.equals(resource.getResourceType())) {
+ resource.getAttributes().remove(GROUP_UPDATE_ATTR);
+ resource.getAttributes().remove(GROUP_OWED_ATTR);
+ resource.getAttributes().remove(GROUP_CLEANUP_ATTEMPTS_ATTR);
}
}
/**
* A health check replacement is undone through the cleanup record, and an in-place update from
* the snapshot taken before it. With neither on the resource the provision failed before it
- * changed anything. Hosted zones and record sets keep the engine's default.
+ * changed anything. A record set group is put back from the snapshot its update kept. Hosted
+ * zones and record sets keep the engine's default.
*/
@Override
public boolean rollbackUpdate(StackResource resource) {
+ if (RECORD_SET_GROUP.equals(resource.getResourceType())) {
+ return rollbackGroupUpdate(resource);
+ }
if (!HEALTH_CHECK.equals(resource.getResourceType())) {
return false;
}
@@ -621,13 +1054,15 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner {
/**
* A record set's delete needs the zone id and type recorded at create time to build a matching
- * DELETE change; the physical id alone only carries the name. The zone-backed types fall through
- * to the id-only overload.
+ * DELETE change; the physical id alone only carries the name. A group's needs the records it
+ * wrote. The zone-backed types fall through to the id-only overload.
*/
@Override
public void delete(StackResource resource, String region) {
if (RECORD_SET.equals(resource.getResourceType())) {
deleteRecordSet(resource);
+ } else if (RECORD_SET_GROUP.equals(resource.getResourceType())) {
+ deleteRecordSetGroup(resource);
} else {
delete(resource.getResourceType(), resource.getPhysicalId(), region);
}
@@ -642,7 +1077,7 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner {
// The record's zone and type live on the StackResource, not the physical id, so the
// id-only path has nothing to match a DELETE change against; delete(resource, region)
// is the real removal.
- case "AWS::Route53::RecordSet" -> LOG.warnv(
+ case "AWS::Route53::RecordSet", "AWS::Route53::RecordSetGroup" -> LOG.warnv(
"No delete from the physical id alone for resource type {0}: {1} is removed via "
+ "delete(resource, region).", resourceType, physicalId);
default -> throw new IllegalStateException(
@@ -678,10 +1113,7 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner {
if (existing == null) {
return;
}
- Map<String, Object> change = new HashMap<>();
- change.put("action", "DELETE");
- change.put("rrs", existing);
- route53Service.changeResourceRecordSets(zoneId, List.of(change), comment);
+ route53Service.changeResourceRecordSets(zoneId, List.of(change("DELETE", existing)), comment);
}, "NoSuchHostedZone", "InvalidChangeBatch");
}
diff --git a/src/main/resources/cloudformation/create-only-properties.json b/src/main/resources/cloudformation/create-only-properties.json
index 4d5d6d10ed..a79905d050 100644
--- a/src/main/resources/cloudformation/create-only-properties.json
+++ b/src/main/resources/cloudformation/create-only-properties.json
@@ -711,6 +711,10 @@
"HostedZoneName",
"HostedZoneId"
],
+ "AWS::Route53::RecordSetGroup": [
+ "HostedZoneName",
+ "HostedZoneId"
+ ],
"AWS::S3::Bucket": [
"BucketName",
"BucketNamePrefix",
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnCreateOnlyPropertiesTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnCreateOnlyPropertiesTest.java
index 029770c2b8..c313e39abf 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnCreateOnlyPropertiesTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnCreateOnlyPropertiesTest.java
@@ -85,6 +85,15 @@ class CfnCreateOnlyPropertiesTest {
assertFalse(CfnCreateOnlyProperties.isCreateOnly(type, "LoggingFilter"));
}
+ @Test
+ void isCreateOnly_recordSetGroupReplacesOnlyForItsHostedZone() {
+ String type = "AWS::Route53::RecordSetGroup";
+ assertTrue(CfnCreateOnlyProperties.isCreateOnly(type, "HostedZoneId"));
+ assertTrue(CfnCreateOnlyProperties.isCreateOnly(type, "HostedZoneName"));
+ assertFalse(CfnCreateOnlyProperties.isCreateOnly(type, "RecordSets"));
+ assertFalse(CfnCreateOnlyProperties.isCreateOnly(type, "Comment"));
+ }
+
@Test
void isCreateOnly_treatsEmptySchemaListAsAuthoritative() {
assertFalse(CfnCreateOnlyProperties.isCreateOnly("AWS::ApiGateway::RestApi", "Name"));
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRoute53RecordSetIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRoute53RecordSetIntegrationTest.java
index fe6a83da5f..e2d09f5fba 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRoute53RecordSetIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRoute53RecordSetIntegrationTest.java
@@ -1,11 +1,18 @@
package io.github.hectorvent.floci.services.cloudformation;
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.services.route53.Route53Service;
import io.github.hectorvent.floci.testing.CfnStackWaits;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.mockito.InjectSpy;
import io.restassured.response.Response;
+import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import org.mockito.Mockito;
import java.time.Duration;
@@ -15,6 +22,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.anyList;
+import static org.mockito.ArgumentMatchers.eq;
/**
* {@code AWS::Route53::RecordSet} through the per-service provisioner: {@code Ref} is the record
@@ -34,6 +43,18 @@ class CloudFormationRoute53RecordSetIntegrationTest {
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/route53/aws4_request";
private static final Duration STACK_DELETE_TIMEOUT = Duration.ofSeconds(30);
private static final Duration STACK_DELETE_POLL_INTERVAL = Duration.ofMillis(50);
+ /** A record set in a zone that does not exist, created after the group, so it fails the update. */
+ private static final String FAILS_AFTER_GROUP = """
+ ,
+ "Broken": {
+ "Type": "AWS::Route53::RecordSet",
+ "DependsOn": "Group",
+ "Properties": {"HostedZoneId": "ZNOSUCHZONE00", "Name": "x.example.test.", "Type": "A",
+ "TTL": "60", "ResourceRecords": ["10.0.0.1"]}
+ }""";
+
+ @InjectSpy
+ Route53Service route53Service;
@BeforeAll
static void configureRestAssured() {
@@ -447,6 +468,582 @@ class CloudFormationRoute53RecordSetIntegrationTest {
}
}
+ @Test
+ void recordGroupCreatesUpdatesAndDeletesItsRecordsUnderAStableRef() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "r53-group-" + suffix;
+ String zoneName = suffix + ".cfn-group-it.example.com";
+ String zoneId = createHostedZone(zoneName, "cfn-group-" + suffix);
+ String unmanaged = "unmanaged." + zoneName;
+ changeRecordSet(zoneId, "CREATE", unmanaged, "192.0.2.9");
+
+ createStack(stackName, groupTemplate(zoneId,
+ groupRecord("first." + zoneName, "192.0.2.1") + "," + groupRecord("second." + zoneName, "192.0.2.2")));
+ assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+ String firstChange;
+ try {
+ // AWS documents Ref as the group's name, for example MyRecordSetGroup, and Fn::GetAtt Id as
+ // the ID GetChange takes.
+ assertEquals("Group", stackOutput(stackName, "GroupRef"));
+ assertEquals("Group", physicalId(stackName, "Group"));
+ firstChange = stackOutput(stackName, "GroupChange");
+ assertChangeInSync(firstChange);
+ String created = listResourceRecordSets(zoneId);
+ assertTrue(created.contains("<Name>first." + zoneName + ".</Name>"), created);
+ assertTrue(created.contains("<Name>second." + zoneName + ".</Name>"), created);
+
+ updateStack(stackName, groupTemplate(zoneId,
+ groupRecord("first." + zoneName, "192.0.2.3") + ","
+ + groupRecord("third." + zoneName, "192.0.2.4")));
+ assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+ assertEquals("Group", stackOutput(stackName, "GroupRef"));
+ String secondChange = stackOutput(stackName, "GroupChange");
+ assertNotEquals(firstChange, secondChange);
+ assertChangeInSync(secondChange);
+ String updated = listResourceRecordSets(zoneId);
+ assertTrue(updated.contains("192.0.2.3"), updated);
+ assertFalse(updated.contains("192.0.2.1"), updated);
+ assertTrue(updated.contains("<Name>third." + zoneName + ".</Name>"), updated);
+ assertFalse(updated.contains("second." + zoneName), updated);
+ } finally {
+ deleteStack(stackName);
+ CfnStackWaits.awaitStackDeleted(stackName);
+ }
+
+ String deleted = listResourceRecordSets(zoneId);
+ assertFalse(deleted.contains("first." + zoneName), deleted);
+ assertFalse(deleted.contains("third." + zoneName), deleted);
+ assertTrue(deleted.contains("192.0.2.9"), "a record the stack never wrote was deleted: " + deleted);
+ changeRecordSet(zoneId, "DELETE", unmanaged, "192.0.2.9");
+ deleteHostedZone(zoneId);
+ }
+
+ @Test
+ void recordGroupKeepsTheRoutingPolicyOfEachRecordSet() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "r53-group-routing-" + suffix;
+ String zoneName = suffix + ".cfn-group-it.example.test";
+ String zoneId = createHostedZone(zoneName, "cfn-group-routing-" + suffix);
+ String template = """
+ {
+ "Parameters": {"BlueWeight": {"Type": "Number"}},
+ "Resources": {
+ "Check": {
+ "Type": "AWS::Route53::HealthCheck",
+ "Properties": {"HealthCheckConfig": {"Type": "TCP", "IPAddress": "10.0.0.5", "Port": 22}}
+ },
+ "Group": {
+ "Type": "AWS::Route53::RecordSetGroup",
+ "Properties": {
+ "HostedZoneName": "%1$s.",
+ "RecordSets": [
+ {"Name": "api.%1$s.", "Type": "A", "TTL": "60", "SetIdentifier": "blue",
+ "Weight": {"Ref": "BlueWeight"}, "HealthCheckId": {"Ref": "Check"},
+ "ResourceRecords": ["10.0.0.7"]},
+ {"Name": "api.%1$s.", "Type": "A", "TTL": "60", "SetIdentifier": "green",
+ "Weight": 30, "ResourceRecords": ["10.0.0.8"]}
+ ]
+ }
+ }
+ }
+ }
+ """.formatted(zoneName);
+
+ parameterizedStackRequest(stackName, "CreateStack", template, "BlueWeight", "70");
+ assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+ try {
+ String checkId = physicalId(stackName, "Check");
+ String created = listResourceRecordSets(zoneId);
+ assertTrue(created.contains("<SetIdentifier>blue</SetIdentifier><Weight>70</Weight>"), created);
+ assertTrue(created.contains("<SetIdentifier>green</SetIdentifier><Weight>30</Weight>"), created);
+ assertTrue(created.contains("<HealthCheckId>" + checkId + "</HealthCheckId>"), created);
+
+ // The update's DELETE of each record the group wrote must name it exactly, routing
+ // policy included, or Route 53 refuses the whole batch.
+ parameterizedStackRequest(stackName, "UpdateStack", template, "BlueWeight", "60");
+ CfnStackWaits.StackState updated = CfnStackWaits.awaitTerminal(stackName);
+ assertEquals("UPDATE_COMPLETE", updated.status(), updated.reason());
+ String records = listResourceRecordSets(zoneId);
+ assertTrue(records.contains("<SetIdentifier>blue</SetIdentifier><Weight>60</Weight>"), records);
+ assertFalse(records.contains("<Weight>70</Weight>"), records);
+ } finally {
+ deleteStack(stackName);
+ CfnStackWaits.awaitStackDeleted(stackName);
+ }
+
+ String deleted = listResourceRecordSets(zoneId);
+ assertFalse(deleted.contains("api." + zoneName), deleted);
+ deleteHostedZone(zoneId);
+ }
+
+ @Test
+ void recordGroupZoneChangeIsAReplacementThatMovesItsRecords() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "r53-group-move-" + suffix;
+ String zoneName = suffix + ".cfn-group-it.example.com";
+ String oldZone = createHostedZone(zoneName, "cfn-group-move-old-" + suffix);
+ String newZone = createHostedZone(zoneName, "cfn-group-move-new-" + suffix);
+ String name = "www." + zoneName;
+ createStack(stackName, groupTemplate(oldZone, groupRecord(name, "192.0.2.1")));
+ assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+ try {
+ // HostedZoneId is create-only in the registry schema, so the change set reports the move as
+ // a replacement.
+ changeSetRequest("CreateChangeSet", stackName, "move")
+ .formParam("ChangeSetType", "UPDATE")
+ .formParam("TemplateBody", groupTemplate(newZone, groupRecord(name, "192.0.2.2")))
+ .when().post("/").then().statusCode(200);
+ changeSetRequest("DescribeChangeSet", stackName, "move")
+ .when().post("/").then().statusCode(200)
+ .body(containsString("<Replacement>True</Replacement>"));
+ changeSetRequest("ExecuteChangeSet", stackName, "move")
+ .when().post("/").then().statusCode(200);
+ assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+
+ assertFalse(listResourceRecordSets(oldZone).contains(name), "old zone kept the record");
+ assertTrue(listResourceRecordSets(newZone).contains("192.0.2.2"), "new zone lacks the record");
+ assertEquals("Group", stackOutput(stackName, "GroupRef"));
+ } finally {
+ deleteStack(stackName);
+ CfnStackWaits.awaitStackDeleted(stackName);
+ }
+ assertFalse(listResourceRecordSets(newZone).contains(name));
+ deleteHostedZone(oldZone);
+ deleteHostedZone(newZone);
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void recordGroupIsPutBackWhenALaterResourceFailsTheUpdate(boolean moveZone) {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "r53-group-rollback-" + suffix;
+ String zoneName = suffix + ".cfn-group-it.example.com";
+ String oldZone = createHostedZone(zoneName, "cfn-group-rollback-old-" + suffix);
+ String targetZone = moveZone ? createHostedZone(zoneName, "cfn-group-rollback-new-" + suffix) : oldZone;
+ String name = "www." + zoneName;
+ createStack(stackName, groupTemplate(oldZone, groupRecord(name, "192.0.2.1")));
+ assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+ String change = stackOutput(stackName, "GroupChange");
+ try {
+ updateStack(stackName, groupTemplate(targetZone, groupRecord(name, "192.0.2.2"), "", FAILS_AFTER_GROUP));
+ assertEquals("UPDATE_ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+
+ String restored = listResourceRecordSets(oldZone);
+ assertTrue(restored.contains("192.0.2.1"), restored);
+ assertFalse(restored.contains("192.0.2.2"), restored);
+ if (moveZone) {
+ assertFalse(listResourceRecordSets(targetZone).contains(name), "the moved record was left behind");
+ }
+ assertEquals(change, stackOutput(stackName, "GroupChange"));
+ } finally {
+ deleteStack(stackName);
+ CfnStackWaits.awaitStackDeleted(stackName);
+ }
+ assertFalse(listResourceRecordSets(oldZone).contains(name), "the restored record outlived the stack");
+ deleteHostedZone(oldZone);
+ if (moveZone) {
+ deleteHostedZone(targetZone);
+ }
+ }
+
+ @Test
+ void retainedRecordGroupLeavesItsOldRecordsWhenItMovesZone() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "r53-group-retain-" + suffix;
+ String zoneName = suffix + ".cfn-group-it.example.com";
+ String oldZone = createHostedZone(zoneName, "cfn-group-retain-old-" + suffix);
+ String newZone = createHostedZone(zoneName, "cfn-group-retain-new-" + suffix);
+ String name = "www." + zoneName;
+ String retain = ", \"UpdateReplacePolicy\": \"Retain\"";
+ createStack(stackName, groupTemplate(oldZone, groupRecord(name, "192.0.2.1"), retain, ""));
+ assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+ try {
+ updateStack(stackName, groupTemplate(newZone, groupRecord(name, "192.0.2.2"), retain, ""));
+ assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+ assertTrue(listResourceRecordSets(oldZone).contains("192.0.2.1"), "the retained record was deleted");
+ assertTrue(listResourceRecordSets(newZone).contains("192.0.2.2"), "new zone lacks the record");
+ } finally {
+ deleteStack(stackName);
+ CfnStackWaits.awaitStackDeleted(stackName);
+ }
+ assertFalse(listResourceRecordSets(newZone).contains(name));
+ assertTrue(listResourceRecordSets(oldZone).contains("192.0.2.1"), "the stack delete took the retained record");
+ changeRecordSet(oldZone, "DELETE", name, "192.0.2.1");
+ deleteHostedZone(oldZone);
+ deleteHostedZone(newZone);
+ }
+
+ @Test
+ void recordGroupMoveGivesUpOnAnOldRecordChangedOutsideTheStack() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "r53-group-drift-move-" + suffix;
+ String zoneName = suffix + ".cfn-group-it.example.com";
+ String oldZone = createHostedZone(zoneName, "cfn-group-drift-old-" + suffix);
+ String newZone = createHostedZone(zoneName, "cfn-group-drift-new-" + suffix);
+ String name = "www." + zoneName;
+ createStack(stackName, groupTemplate(oldZone, groupRecord(name, "192.0.2.1")));
+ assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+ changeRecordSet(oldZone, "UPSERT", name, "192.0.2.9");
+ try {
+ // The cleanup's DELETE names the values the group wrote, so Route 53 refuses it; as for any
+ // other replaced resource, the update still completes and says what it could not delete.
+ updateStack(stackName, groupTemplate(newZone, groupRecord(name, "192.0.2.2")));
+ CfnStackWaits.StackState moved = CfnStackWaits.awaitTerminal(stackName);
+ assertEquals("UPDATE_COMPLETE", moved.status(), moved.reason());
+ assertTrue(moved.reason().contains("could not be deleted during update cleanup"), moved.reason());
+ assertTrue(listResourceRecordSets(oldZone).contains("192.0.2.9"), "the changed record was deleted");
+ assertTrue(listResourceRecordSets(newZone).contains("192.0.2.2"), "new zone lacks the record");
+
+ // The given-up record no longer holds the group back.
+ updateStack(stackName, groupTemplate(newZone, groupRecord(name, "192.0.2.3")));
+ CfnStackWaits.StackState next = CfnStackWaits.awaitTerminal(stackName);
+ assertEquals("UPDATE_COMPLETE", next.status(), next.reason());
+ assertTrue(listResourceRecordSets(newZone).contains("192.0.2.3"));
+ } finally {
+ deleteStack(stackName);
+ CfnStackWaits.awaitStackDeleted(stackName);
+ }
+ assertFalse(listResourceRecordSets(newZone).contains(name));
+ assertTrue(listResourceRecordSets(oldZone).contains("192.0.2.9"), "the stack delete took the changed record");
+ changeRecordSet(oldZone, "DELETE", name, "192.0.2.9");
+ deleteHostedZone(oldZone);
+ deleteHostedZone(newZone);
+ }
+
+ @Test
+ void recordGroupThatCollidesWritesNothingAndLeavesTheOtherOwnersRecord() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "r53-group-taken-" + suffix;
+ String zoneName = suffix + ".cfn-group-it.example.com";
+ String zoneId = createHostedZone(zoneName, "cfn-group-taken-" + suffix);
+ String taken = "taken." + zoneName;
+ changeRecordSet(zoneId, "CREATE", taken, "192.0.2.9");
+ try {
+ createStack(stackName, groupTemplate(zoneId,
+ groupRecord("new." + zoneName, "192.0.2.1") + "," + groupRecord(taken, "192.0.2.2")));
+ CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stackName);
+ assertEquals("ROLLBACK_COMPLETE", state.status(), state.reason());
+ String failure = CfnStackWaits.failedResourceReason(stackName);
+ assertTrue(failure.contains("already exists"), failure);
+ String records = listResourceRecordSets(zoneId);
+ assertFalse(records.contains("new." + zoneName), "a partial group was written: " + records);
+ assertTrue(records.contains("192.0.2.9"), records);
+ assertFalse(records.contains("192.0.2.2"), records);
+ } finally {
+ deleteStack(stackName);
+ CfnStackWaits.awaitStackDeleted(stackName);
+ }
+ changeRecordSet(zoneId, "DELETE", taken, "192.0.2.9");
+ deleteHostedZone(zoneId);
+ }
+
+ @Test
+ void recordGroupDeleteFailsOnARecordChangedOutsideTheStackUntilItIsPutBack() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "r53-group-drift-" + suffix;
+ String zoneName = suffix + ".cfn-group-it.example.com";
+ String zoneId = createHostedZone(zoneName, "cfn-group-drift-" + suffix);
+ String name = "www." + zoneName;
+ createStack(stackName, groupTemplate(zoneId, groupRecord(name, "192.0.2.1")));
+ assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+ changeRecordSet(zoneId, "UPSERT", name, "192.0.2.2");
+
+ // As on AWS, the DELETE names the values the stack wrote, so a changed record fails it.
+ deleteStack(stackName);
+ CfnStackWaits.StackState failed = CfnStackWaits.awaitTerminal(stackName);
+ assertEquals("DELETE_FAILED", failed.status(), failed.reason());
+ assertTrue(listResourceRecordSets(zoneId).contains("192.0.2.2"), "the changed record was deleted");
+
+ changeRecordSet(zoneId, "UPSERT", name, "192.0.2.1");
+ deleteStack(stackName);
+ CfnStackWaits.awaitStackDeleted(stackName);
+ assertFalse(listResourceRecordSets(zoneId).contains(name));
+ deleteHostedZone(zoneId);
+ }
+
+ @Test
+ void recordGroupMoveWhoseRollbackCannotDeleteTheNewRecordsIsFinishedByContinueUpdateRollback() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "r53-group-retry-" + suffix;
+ String zoneName = suffix + ".cfn-group-it.example.com";
+ String oldZone = createHostedZone(zoneName, "cfn-group-retry-old-" + suffix);
+ String newZone = createHostedZone(zoneName, "cfn-group-retry-new-" + suffix);
+ String name = "www." + zoneName;
+ createStack(stackName, groupTemplate(oldZone, groupRecord(name, "192.0.2.1")));
+ assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+ try {
+ refuseFirstGroupDeleteIn(newZone);
+ updateStack(stackName, groupTemplate(newZone, groupRecord(name, "192.0.2.2"), "", FAILS_AFTER_GROUP));
+ assertEquals("UPDATE_ROLLBACK_FAILED", CfnStackWaits.awaitTerminal(stackName).status());
+ assertTrue(listResourceRecordSets(oldZone).contains("192.0.2.1"), "the old record was touched");
+ assertTrue(listResourceRecordSets(newZone).contains("192.0.2.2"), "the refused delete went through");
+
+ continueUpdateRollback(stackName);
+ CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stackName);
+ assertEquals("UPDATE_ROLLBACK_COMPLETE", state.status(), state.reason());
+ assertFalse(listResourceRecordSets(newZone).contains(name), "the retry left the moved record");
+ assertTrue(listResourceRecordSets(oldZone).contains("192.0.2.1"), "the retry touched the old record");
+ } finally {
+ deleteStack(stackName);
+ CfnStackWaits.awaitStackDeleted(stackName);
+ }
+ assertFalse(listResourceRecordSets(oldZone).contains(name));
+ deleteHostedZone(oldZone);
+ deleteHostedZone(newZone);
+ }
+
+ @Test
+ void skippingAFailedRecordGroupRollbackKeepsTheOldRecordsAndLeavesTheNewOnesInTheirZone() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "r53-group-skip-" + suffix;
+ String zoneName = suffix + ".cfn-group-it.example.com";
+ String oldZone = createHostedZone(zoneName, "cfn-group-skip-old-" + suffix);
+ String newZone = createHostedZone(zoneName, "cfn-group-skip-new-" + suffix);
+ String name = "www." + zoneName;
+ String taken = "taken." + zoneName;
+ String restoredTemplate = groupTemplate(oldZone, groupRecord(name, "192.0.2.1"));
+ createStack(stackName, restoredTemplate);
+ assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+ changeRecordSet(oldZone, "CREATE", taken, "192.0.2.9");
+ try {
+ refuseFirstGroupDeleteIn(newZone);
+ updateStack(stackName, groupTemplate(newZone, groupRecord(name, "192.0.2.2"), "", FAILS_AFTER_GROUP));
+ assertEquals("UPDATE_ROLLBACK_FAILED", CfnStackWaits.awaitTerminal(stackName).status());
+ continueUpdateRollback(stackName, "Group");
+ assertEquals("UPDATE_ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+
+ // The skipped group names the old zone's record again, as the restored template does, so
+ // an update of it that fails puts that record back rather than deleting it.
+ updateStack(stackName, groupTemplate(oldZone,
+ groupRecord(name, "192.0.2.1") + "," + groupRecord(taken, "192.0.2.3")));
+ assertEquals("UPDATE_ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+ assertTrue(listResourceRecordSets(oldZone).contains("192.0.2.1"), "the old record was deleted");
+
+ // An update that leaves the group alone commits. As on AWS, the record the skipped rollback
+ // could not delete stays where it is: nothing tells it apart from one written there later.
+ updateStack(stackName, restoredTemplate.replace("\"Resources\": {", """
+ "Resources": {
+ "Marker": {"Type": "AWS::SSM::Parameter",
+ "Properties": {"Name": "/r53-group-skip/%s", "Type": "String", "Value": "kept"}},
+ """.formatted(suffix)));
+ CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stackName);
+ assertEquals("UPDATE_COMPLETE", state.status(), state.reason());
+ assertTrue(listResourceRecordSets(oldZone).contains("192.0.2.1"), "the old record was deleted");
+ assertTrue(listResourceRecordSets(newZone).contains("192.0.2.2"), "the skipped leftover was deleted");
+ } finally {
+ deleteStack(stackName);
+ CfnStackWaits.awaitStackDeleted(stackName);
+ }
+ assertFalse(listResourceRecordSets(oldZone).contains(name));
+ assertTrue(listResourceRecordSets(newZone).contains("192.0.2.2"), "the stack delete took the skipped leftover");
+ changeRecordSet(newZone, "DELETE", name, "192.0.2.2");
+ changeRecordSet(oldZone, "DELETE", taken, "192.0.2.9");
+ deleteHostedZone(oldZone);
+ deleteHostedZone(newZone);
+ }
+
+ @Test
+ void reApplyingAMoveAfterASkippedRollbackKeepsTheRecordsItWrites() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "r53-group-reapply-" + suffix;
+ String zoneName = suffix + ".cfn-group-it.example.com";
+ String oldZone = createHostedZone(zoneName, "cfn-group-reapply-old-" + suffix);
+ String newZone = createHostedZone(zoneName, "cfn-group-reapply-new-" + suffix);
+ String name = "www." + zoneName;
+ createStack(stackName, groupTemplate(oldZone, groupRecord(name, "192.0.2.1")));
+ assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+ try {
+ refuseFirstGroupDeleteIn(newZone);
+ updateStack(stackName, groupTemplate(newZone, groupRecord(name, "192.0.2.2"), "", FAILS_AFTER_GROUP));
+ assertEquals("UPDATE_ROLLBACK_FAILED", CfnStackWaits.awaitTerminal(stackName).status());
+ continueUpdateRollback(stackName, "Group");
+ assertEquals("UPDATE_ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+
+ // The record the failed update left is deleted by hand, and the move is applied again.
+ changeRecordSet(newZone, "DELETE", name, "192.0.2.2");
+ updateStack(stackName, groupTemplate(newZone, groupRecord(name, "192.0.2.2")));
+ CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stackName);
+ assertEquals("UPDATE_COMPLETE", state.status(), state.reason());
+ assertTrue(listResourceRecordSets(newZone).contains("192.0.2.2"), "the cleanup deleted the moved record");
+ assertFalse(listResourceRecordSets(oldZone).contains(name), "the old record was left behind");
+ } finally {
+ deleteStack(stackName);
+ CfnStackWaits.awaitStackDeleted(stackName);
+ }
+ assertFalse(listResourceRecordSets(newZone).contains(name));
+ deleteHostedZone(oldZone);
+ deleteHostedZone(newZone);
+ }
+
+ @Test
+ void aRecordSetThatRewritesWhatASkippedGroupRollbackLeftKeepsIt() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "r53-group-sibling-" + suffix;
+ String zoneName = suffix + ".cfn-group-it.example.com";
+ String oldZone = createHostedZone(zoneName, "cfn-group-sibling-old-" + suffix);
+ String newZone = createHostedZone(zoneName, "cfn-group-sibling-new-" + suffix);
+ String name = "www." + zoneName;
+ String restoredTemplate = groupTemplate(oldZone, groupRecord(name, "192.0.2.1"));
+ createStack(stackName, restoredTemplate);
+ assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+ try {
+ refuseFirstGroupDeleteIn(newZone);
+ updateStack(stackName, groupTemplate(newZone, groupRecord(name, "192.0.2.2"), "", FAILS_AFTER_GROUP));
+ assertEquals("UPDATE_ROLLBACK_FAILED", CfnStackWaits.awaitTerminal(stackName).status());
+ continueUpdateRollback(stackName, "Group");
+ assertEquals("UPDATE_ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+
+ // The leftover is deleted by hand, and a record set of the same stack writes the same record.
+ changeRecordSet(newZone, "DELETE", name, "192.0.2.2");
+ updateStack(stackName, restoredTemplate.replace("\"Resources\": {", """
+ "Resources": {
+ "Sibling": {"Type": "AWS::Route53::RecordSet",
+ "Properties": {"HostedZoneId": "%s", "Name": "%s", "Type": "A", "TTL": "300",
+ "ResourceRecords": ["192.0.2.2"]}},
+ """.formatted(newZone, name)));
+ CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stackName);
+ assertEquals("UPDATE_COMPLETE", state.status(), state.reason());
+ assertTrue(listResourceRecordSets(newZone).contains("192.0.2.2"),
+ "the group's cleanup deleted the record set's record");
+ } finally {
+ deleteStack(stackName);
+ CfnStackWaits.awaitStackDeleted(stackName);
+ }
+ assertFalse(listResourceRecordSets(newZone).contains(name));
+ assertFalse(listResourceRecordSets(oldZone).contains(name));
+ deleteHostedZone(oldZone);
+ deleteHostedZone(newZone);
+ }
+
+ @Test
+ void deletingAStackWhoseRecordGroupMoveRollbackFailedDeletesBothZonesRecordsEvenUnderRetain() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "r53-group-failed-delete-" + suffix;
+ String zoneName = suffix + ".cfn-group-it.example.com";
+ String oldZone = createHostedZone(zoneName, "cfn-group-failed-delete-old-" + suffix);
+ String newZone = createHostedZone(zoneName, "cfn-group-failed-delete-new-" + suffix);
+ String name = "www." + zoneName;
+ String retain = ", \"UpdateReplacePolicy\": \"Retain\"";
+ createStack(stackName, groupTemplate(oldZone, groupRecord(name, "192.0.2.1"), retain, ""));
+ assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
+ refuseFirstGroupDeleteIn(newZone);
+ updateStack(stackName, groupTemplate(newZone, groupRecord(name, "192.0.2.2"), retain, FAILS_AFTER_GROUP));
+ assertEquals("UPDATE_ROLLBACK_FAILED", CfnStackWaits.awaitTerminal(stackName).status());
+ assertTrue(listResourceRecordSets(oldZone).contains("192.0.2.1"), "the old record was touched");
+ assertTrue(listResourceRecordSets(newZone).contains("192.0.2.2"), "the refused delete went through");
+
+ // Retain keeps what a committed replacement displaced; this move never committed.
+ deleteStack(stackName);
+ CfnStackWaits.awaitStackDeleted(stackName);
+ assertFalse(listResourceRecordSets(oldZone).contains(name), "the group's own record outlived the stack");
+ assertFalse(listResourceRecordSets(newZone).contains(name), "the failed update's record outlived the stack");
+ deleteHostedZone(oldZone);
+ deleteHostedZone(newZone);
+ }
+
+ private static String groupRecord(String name, String address) {
+ return """
+ {"Name": "%s", "Type": "A", "TTL": "300", "ResourceRecords": ["%s"]}""".formatted(name, address);
+ }
+
+ private static String groupTemplate(String zoneId, String recordSets) {
+ return groupTemplate(zoneId, recordSets, "", "");
+ }
+
+ /**
+ * One record group in {@code zoneId}, with outputs for its {@code Ref} and {@code Fn::GetAtt Id}.
+ * {@code policy} is added to the group's definition and {@code extra} after it, each led by a
+ * comma.
+ */
+ private static String groupTemplate(String zoneId, String recordSets, String policy, String extra) {
+ return """
+ {
+ "Resources": {
+ "Group": {
+ "Type": "AWS::Route53::RecordSetGroup"%s,
+ "Properties": {"HostedZoneId": "%s", "RecordSets": [%s]}
+ }%s
+ },
+ "Outputs": {
+ "GroupRef": {"Value": {"Ref": "Group"}},
+ "GroupChange": {"Value": {"Fn::GetAtt": ["Group", "Id"]}}
+ }
+ }
+ """.formatted(policy, zoneId, recordSets, extra);
+ }
+
+ private void parameterizedStackRequest(String stackName, String action, String template, String parameter,
+ String value) {
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", CFN_AUTH)
+ .formParam("Action", action)
+ .formParam("StackName", stackName)
+ .formParam("TemplateBody", template)
+ .formParam("Parameters.member.1.ParameterKey", parameter)
+ .formParam("Parameters.member.1.ParameterValue", value)
+ .when().post("/").then().statusCode(200);
+ }
+
+ private RequestSpecification changeSetRequest(String action, String stackName, String changeSetName) {
+ return given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", CFN_AUTH)
+ .formParam("Action", action)
+ .formParam("StackName", stackName)
+ .formParam("ChangeSetName", changeSetName);
+ }
+
+ private String stackOutput(String stackName, String key) {
+ return given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", CFN_AUTH)
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", stackName)
+ .when().post("/").then().statusCode(200)
+ .extract().xmlPath().getString("DescribeStacksResponse.DescribeStacksResult.Stacks.member.Outputs.member"
+ + ".find { it.OutputKey == '" + key + "' }.OutputValue");
+ }
+
+ private void assertChangeInSync(String changeId) {
+ given()
+ .header("Authorization", ROUTE53_AUTH)
+ .when().get("/2013-04-01/change/" + changeId.substring(changeId.lastIndexOf('/') + 1))
+ .then().statusCode(200)
+ .body(containsString("<Status>INSYNC</Status>"));
+ }
+
+ private void deleteHostedZone(String zoneId) {
+ given()
+ .header("Authorization", ROUTE53_AUTH)
+ .when().delete("/2013-04-01/hostedzone/" + zoneId)
+ .then().statusCode(200);
+ }
+
+ /**
+ * Refuses the first DELETE batch the group sends to {@code zoneId}, as Route 53 refuses one whose
+ * values no longer match a record changed outside the stack; later batches go through.
+ */
+ private void refuseFirstGroupDeleteIn(String zoneId) {
+ Mockito.doThrow(new AwsException("InvalidChangeBatch",
+ "Tried to delete resource record set but the values provided do not match the current values",
+ 400))
+ .doCallRealMethod()
+ .when(route53Service).changeResourceRecordSets(eq(zoneId), anyList(), eq("CloudFormation delete Group"));
+ }
+
+ private void continueUpdateRollback(String stackName, String... resourcesToSkip) {
+ RequestSpecification request = given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", CFN_AUTH)
+ .formParam("Action", "ContinueUpdateRollback")
+ .formParam("StackName", stackName);
+ for (int i = 0; i < resourcesToSkip.length; i++) {
+ request.formParam("ResourcesToSkip.member." + (i + 1), resourcesToSkip[i]);
+ }
+ request.when().post("/").then().statusCode(200);
+ }
+
/**
* One TCP health check; {@code failing} adds a record set in a zone that does not exist, created
* after the check, so the update fails once the check has been replaced.
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Route53CfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Route53CfnProvisionerTest.java
index 8113837576..857f3ca258 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Route53CfnProvisionerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Route53CfnProvisionerTest.java
@@ -6,6 +6,7 @@ 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.route53.Route53Service;
+import io.github.hectorvent.floci.services.route53.model.ChangeInfo;
import io.github.hectorvent.floci.services.route53.model.HealthCheck;
import io.github.hectorvent.floci.services.route53.model.HealthCheckConfig;
import io.github.hectorvent.floci.services.route53.model.HostedZone;
@@ -18,6 +19,7 @@ import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@@ -33,6 +35,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
@@ -743,6 +746,546 @@ class Route53CfnProvisionerTest {
+ "\"SetIdentifier\":\"a\",\"Weight\":\"heavy\"}");
}
+ @Test
+ void recordGroupWritesEveryRecordSetInOneBatchWithItsRoutingPolicy() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ when(service.changeResourceRecordSets(eq("Z1"), any(), any()))
+ .thenReturn(new ChangeInfo("C123", "2026-10-23T00:00:00Z", "web tier"));
+ JsonNode props = new ObjectMapper().readTree("""
+ {"HostedZoneId": "Z1", "Comment": "web tier", "RecordSets": [
+ {"Name": "www.example.test", "Type": "A", "TTL": "60", "SetIdentifier": "blue", "Weight": 70,
+ "HealthCheckId": "hc-1", "ResourceRecords": ["10.0.0.7"]},
+ {"Name": "www.example.test.", "Type": "A", "TTL": "60", "SetIdentifier": "green", "Weight": 30,
+ "ResourceRecords": ["10.0.0.8"]}
+ ]}
+ """);
+ StackResource resource = recordGroup();
+
+ new Route53CfnProvisioner(service).provision(resource, props, groupContext(null));
+
+ // Ref is the group's name, and Fn::GetAtt Id the change GetChange reads.
+ assertEquals("Group", resource.getPhysicalId());
+ assertEquals(Set.of("Id"), publicAttributes(resource));
+ assertEquals("C123", resource.getAttributes().get("Id"));
+ @SuppressWarnings("unchecked")
+ ArgumentCaptor<List<Map<String, Object>>> captor = ArgumentCaptor.forClass(List.class);
+ verify(service).changeResourceRecordSets(eq("Z1"), captor.capture(), eq("web tier"));
+ List<Map<String, Object>> changes = captor.getValue();
+ assertEquals(List.of("CREATE", "CREATE"), changes.stream().map(change -> change.get("action")).toList());
+ ResourceRecordSet blue = (ResourceRecordSet) changes.get(0).get("rrs");
+ assertEquals("www.example.test.", blue.getName());
+ assertEquals(Long.valueOf(60), blue.getTtl());
+ assertEquals("blue", blue.getSetIdentifier());
+ assertEquals(Long.valueOf(70), blue.getWeight());
+ assertEquals("hc-1", blue.getHealthCheckId());
+ assertEquals(List.of("10.0.0.7"), blue.getRecords().stream().map(ResourceRecord::getValue).toList());
+ ResourceRecordSet green = (ResourceRecordSet) changes.get(1).get("rrs");
+ assertEquals("green", green.getSetIdentifier());
+ assertEquals(Long.valueOf(30), green.getWeight());
+ }
+
+ @Test
+ void recordGroupValidatesEveryRecordSetBeforeChangingTheZone() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ JsonNode props = new ObjectMapper().readTree("""
+ {"HostedZoneId": "Z1", "RecordSets": [
+ {"Name": "valid.example.test", "Type": "A", "TTL": "60", "ResourceRecords": ["192.0.2.1"]},
+ {"Name": "invalid.example.test"}
+ ]}
+ """);
+ StackResource resource = recordGroup();
+
+ AwsException failure = assertThrows(AwsException.class,
+ () -> new Route53CfnProvisioner(service).provision(resource, props, groupContext(null)));
+
+ assertEquals("ValidationError", failure.getErrorCode());
+ assertTrue(failure.getMessage().contains("RecordSets[1]"), failure.getMessage());
+ verifyNoInteractions(service);
+ assertNull(resource.getPhysicalId());
+ assertTrue(resource.getAttributes().isEmpty());
+ }
+
+ @Test
+ void recordGroupWithoutRecordSetsIsRejected() throws Exception {
+ assertRecordGroupRejected("{\"HostedZoneId\": \"Z1\", \"RecordSets\": []}");
+ assertRecordGroupRejected("{\"HostedZoneId\": \"Z1\"}");
+ }
+
+ @Test
+ void recordGroupWithoutAZoneIsRejected() throws Exception {
+ assertRecordGroupRejected("""
+ {"RecordSets": [{"Name": "www.example.test", "Type": "A", "ResourceRecords": ["192.0.2.1"]}]}
+ """);
+ }
+
+ @Test
+ void recordGroupRejectsAHostedZoneIdOnBothTheGroupAndARecordSet() throws Exception {
+ // AWS documents that creation fails when HostedZoneId is defined on the group and a record set.
+ assertRecordGroupRejected("""
+ {"HostedZoneId": "Z1", "RecordSets": [
+ {"HostedZoneId": "Z1", "Name": "www.example.test", "Type": "A", "ResourceRecords": ["192.0.2.1"]}
+ ]}
+ """);
+ }
+
+ @Test
+ void recordGroupRejectsARecordSetInAnotherZone() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ when(service.listHostedZonesByName("other.example.test.", 0)).thenReturn(List.of(
+ new HostedZone("Z2", "other.example.test.", "caller", null, (VpcAssociation) null)));
+ JsonNode props = new ObjectMapper().readTree("""
+ {"HostedZoneId": "Z1", "RecordSets": [
+ {"HostedZoneName": "other.example.test.", "Name": "www.other.example.test", "Type": "A",
+ "ResourceRecords": ["192.0.2.1"]}
+ ]}
+ """);
+
+ AwsException failure = assertThrows(AwsException.class,
+ () -> new Route53CfnProvisioner(service).provision(recordGroup(), props, groupContext(null)));
+
+ assertEquals("ValidationError", failure.getErrorCode());
+ verify(service, never()).changeResourceRecordSets(any(), any(), any());
+ }
+
+ @Test
+ void recordGroupThatNamesNoZoneWritesIntoTheZoneItsRecordSetsName() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ when(service.changeResourceRecordSets(eq("Z2"), any(), any())).thenReturn(new ChangeInfo("C1", "now", null));
+ JsonNode props = new ObjectMapper().readTree("""
+ {"RecordSets": [
+ {"HostedZoneId": "Z2", "Name": "a.example.test", "Type": "A", "ResourceRecords": ["192.0.2.1"]},
+ {"Name": "b.example.test", "Type": "A", "ResourceRecords": ["192.0.2.2"]}
+ ]}
+ """);
+
+ new Route53CfnProvisioner(service).provision(recordGroup(), props, groupContext(null));
+
+ verify(service).changeResourceRecordSets(eq("Z2"), argThat(changes -> changes.size() == 2), any());
+ }
+
+ @Test
+ void recordGroupUpdateInItsZoneSwapsItsRecordsInOneBatchAndARollbackPutsThemBack() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ ResourceRecordSet kept = aRecord("old.example.test.", "192.0.2.1");
+ // Written by the last update, then deleted outside the stack: there is nothing to DELETE.
+ ResourceRecordSet gone = aRecord("gone.example.test.", "192.0.2.5");
+ when(service.listResourceRecordSets("Z1", null, null, 0)).thenReturn(List.of(kept));
+ when(service.changeResourceRecordSets(eq("Z1"), any(), any())).thenReturn(new ChangeInfo("C2", "now", null));
+ StackResource resource = existingGroup("Z1", "C1", kept, gone);
+ Map<String, String> before = Map.copyOf(resource.getAttributes());
+ JsonNode props = new ObjectMapper().readTree("""
+ {"HostedZoneId": "Z1", "RecordSets": [
+ {"Name": "new.example.test", "Type": "A", "TTL": "300", "ResourceRecords": ["192.0.2.2"]}
+ ]}
+ """);
+ Route53CfnProvisioner owner = new Route53CfnProvisioner(service);
+
+ owner.provision(resource, props, groupContext("Group"));
+
+ @SuppressWarnings("unchecked")
+ ArgumentCaptor<List<Map<String, Object>>> captor = ArgumentCaptor.forClass(List.class);
+ verify(service).changeResourceRecordSets(eq("Z1"), captor.capture(), any());
+ assertEquals(List.of("DELETE old.example.test.", "CREATE new.example.test."), describe(captor.getValue()));
+ assertEquals("C2", resource.getAttributes().get("Id"));
+ assertFalse(owner.hasReplacementUpdate(resource));
+ assertNull(owner.updateCleanupPhysicalId(resource));
+
+ ResourceRecordSet written = (ResourceRecordSet) captor.getValue().get(1).get("rrs");
+ when(service.listResourceRecordSets("Z1", null, null, 0)).thenReturn(List.of(written));
+ assertTrue(owner.rollbackUpdate(resource));
+
+ verify(service, times(2)).changeResourceRecordSets(eq("Z1"), captor.capture(), any());
+ assertEquals(List.of("DELETE new.example.test.", "CREATE old.example.test.", "CREATE gone.example.test."),
+ describe(captor.getValue()));
+ assertEquals(before, resource.getAttributes());
+ }
+
+ @Test
+ void recordGroupZoneChangeDeletesTheOldRecordsOnlyOnceTheUpdateCommits() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ ResourceRecordSet old = aRecord("www.example.test.", "192.0.2.1");
+ when(service.listResourceRecordSets("ZOLD", null, null, 0)).thenReturn(List.of(old));
+ when(service.changeResourceRecordSets(any(), any(), any())).thenReturn(new ChangeInfo("C2", "now", null));
+ StackResource resource = existingGroup("ZOLD", "C1", old);
+ JsonNode props = new ObjectMapper().readTree("""
+ {"HostedZoneId": "ZNEW", "RecordSets": [
+ {"Name": "www.example.test", "Type": "A", "TTL": "300", "ResourceRecords": ["192.0.2.2"]}
+ ]}
+ """);
+ Route53CfnProvisioner owner = new Route53CfnProvisioner(service);
+
+ owner.provision(resource, props, groupContext("Group"));
+
+ verify(service).changeResourceRecordSets(eq("ZNEW"), argThat(changes -> changes.size() == 1
+ && "CREATE".equals(changes.get(0).get("action"))), any());
+ verify(service, never()).changeResourceRecordSets(eq("ZOLD"), any(), any());
+ assertTrue(owner.hasReplacementUpdate(resource));
+ assertEquals("Group", owner.updateCleanupPhysicalId(resource));
+
+ UpdateCleanupResult cleanup = owner.completeUpdate(resource);
+
+ assertTrue(cleanup.applicable());
+ assertTrue(cleanup.complete());
+ verify(service).changeResourceRecordSets(eq("ZOLD"), argThat(changes -> changes.size() == 1
+ && "DELETE".equals(changes.get(0).get("action")) && changes.get(0).get("rrs") != null), any());
+ owner.clearUpdate(resource);
+ assertFalse(owner.hasReplacementUpdate(resource));
+ assertEquals(Set.of("Id"), publicAttributes(resource));
+ }
+
+ @Test
+ void recordGroupCleanupThatGivesUpForgetsTheOldRecords() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ ResourceRecordSet old = aRecord("www.example.test.", "192.0.2.1");
+ when(service.listResourceRecordSets("ZOLD", null, null, 0)).thenReturn(List.of(old));
+ when(service.listResourceRecordSets("ZNEW", null, null, 0)).thenReturn(List.of(movedRecord()));
+ when(service.changeResourceRecordSets(eq("ZOLD"), any(), any()))
+ .thenThrow(new AwsException("InvalidChangeBatch", "values do not match", 400));
+ Route53CfnProvisioner owner = new Route53CfnProvisioner(service);
+ StackResource resource = movedGroup(owner, service, old);
+
+ for (int attempt = 1; attempt <= 3; attempt++) {
+ UpdateCleanupResult cleanup = owner.completeUpdate(resource);
+ assertFalse(cleanup.complete());
+ assertEquals(attempt, cleanup.attempts());
+ assertEquals("values do not match", cleanup.failureReason());
+ }
+ owner.clearUpdate(resource);
+
+ assertFalse(owner.hasReplacementUpdate(resource));
+ assertNull(owner.updateCleanupPhysicalId(resource));
+ owner.delete(resource, "us-east-1");
+ verify(service, times(3)).changeResourceRecordSets(eq("ZOLD"), any(), any());
+ // The update's write and the delete.
+ verify(service, times(2)).changeResourceRecordSets(eq("ZNEW"), any(), any());
+ }
+
+ @Test
+ void deletingARecordGroupMidMoveDeletesTheOldRecordsFirstAndKeepsBothUntilTheyAreGone() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ ResourceRecordSet old = aRecord("www.example.test.", "192.0.2.1");
+ when(service.listResourceRecordSets("ZOLD", null, null, 0)).thenReturn(List.of(old));
+ when(service.listResourceRecordSets("ZNEW", null, null, 0)).thenReturn(List.of(movedRecord()));
+ when(service.changeResourceRecordSets(eq("ZOLD"), any(), any()))
+ .thenThrow(new AwsException("InvalidChangeBatch", "values do not match", 400))
+ .thenReturn(null);
+ Route53CfnProvisioner owner = new Route53CfnProvisioner(service);
+ StackResource resource = movedGroup(owner, service, old);
+
+ assertThrows(AwsException.class, () -> owner.delete(resource, "us-east-1"));
+ verify(service, never()).listResourceRecordSets(eq("ZNEW"), any(), any(), eq(0));
+ assertTrue(owner.hasReplacementUpdate(resource));
+
+ owner.delete(resource, "us-east-1");
+ verify(service, times(2)).changeResourceRecordSets(eq("ZOLD"), any(), any());
+ verify(service, times(2)).changeResourceRecordSets(eq("ZNEW"), any(), any());
+ assertFalse(owner.hasReplacementUpdate(resource));
+ }
+
+ @Test
+ void zoneChangeRollbackThatCannotDeleteTheNewRecordsPointsBackAtTheOldOnesAndRetries() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ Route53CfnProvisioner owner = new Route53CfnProvisioner(service);
+ ResourceRecordSet old = aRecord("www.example.test.", "192.0.2.1");
+ StackResource resource = failedMoveRollback(owner, service, old);
+
+ // The group already names its old records again, so a skip leaves it matching the template the
+ // stack rolls back to; only the new zone's records are owed, and Retain never keeps them.
+ assertEquals("Group", resource.getPhysicalId());
+ Map<String, String> restored = new HashMap<>(resource.getAttributes());
+ assertNotNull(restored.remove("__FlociRoute53GroupOwedDeletes"));
+ assertEquals(existingGroup("ZOLD", "C1", old).getAttributes(), restored);
+ // Owed records do not make the group a replacement, and no cleanup announces them: a commit
+ // never deletes them, and the engine announces before it says whether it commits or deletes.
+ assertFalse(owner.hasReplacementUpdate(resource));
+ assertNull(owner.updateCleanupPhysicalId(resource));
+
+ // ContinueUpdateRollback runs the rollback again, which now only owes that delete.
+ assertTrue(owner.rollbackUpdate(resource));
+
+ assertEquals(existingGroup("ZOLD", "C1", old).getAttributes(), resource.getAttributes());
+ assertFalse(owner.hasReplacementUpdate(resource));
+ verify(service, never()).changeResourceRecordSets(eq("ZOLD"), any(), any());
+ // The update's write, the failed delete and the retried one.
+ verify(service, times(3)).changeResourceRecordSets(eq("ZNEW"), any(), any());
+ }
+
+ @Test
+ void recordsAFailedRollbackCouldNotDeleteGoWithTheStackWhileTheRollbackIsUnfinished() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ Route53CfnProvisioner owner = new Route53CfnProvisioner(service);
+ ResourceRecordSet old = aRecord("www.example.test.", "192.0.2.1");
+ StackResource resource = failedMoveRollback(owner, service, old);
+
+ // DeleteStack from UPDATE_ROLLBACK_FAILED: the stack's cleanup runs first, under Retain too.
+ UpdateCleanupResult cleanup = owner.completeDeleteCleanup(resource);
+
+ assertTrue(cleanup.applicable());
+ assertTrue(cleanup.complete());
+ assertEquals("Group", cleanup.previousPhysicalId());
+ owner.clearDeleteCleanup(resource);
+ assertEquals(existingGroup("ZOLD", "C1", old).getAttributes(), resource.getAttributes());
+ // The failed delete and this one.
+ verify(service, times(2)).changeResourceRecordSets(eq("ZNEW"), any(), eq("CloudFormation delete Group"));
+ verify(service, never()).changeResourceRecordSets(eq("ZOLD"), any(), any());
+ }
+
+ @Test
+ void aCommitNeverDeletesWhatAFailedRollbackLeftEvenWhileTheGroupIsUpdateFailed() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ Route53CfnProvisioner owner = new Route53CfnProvisioner(service);
+ StackResource resource = failedMoveRollback(owner, service, aRecord("www.example.test.", "192.0.2.1"));
+
+ // A nested stack's child is updated while it is still UPDATE_ROLLBACK_FAILED, so its commit can
+ // meet a group whose rollback is unfinished.
+ assertFalse(owner.completeUpdate(resource).applicable());
+
+ verify(service, times(1)).changeResourceRecordSets(eq("ZNEW"), any(), eq("CloudFormation delete Group"));
+ assertFalse(resource.getAttributes().containsKey("__FlociRoute53GroupOwedDeletes"));
+ }
+
+ @Test
+ void removingAGroupNeverDeletesWhatAFailedRollbackLeft() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ Route53CfnProvisioner owner = new Route53CfnProvisioner(service);
+ ResourceRecordSet old = aRecord("www.example.test.", "192.0.2.1");
+ when(service.listResourceRecordSets("ZOLD", null, null, 0)).thenReturn(List.of(old));
+ StackResource resource = failedMoveRollback(owner, service, old);
+
+ owner.delete(resource, "us-east-1");
+
+ verify(service, times(1)).changeResourceRecordSets(eq("ZNEW"), any(), eq("CloudFormation delete Group"));
+ verify(service).changeResourceRecordSets(eq("ZOLD"), any(), eq("CloudFormation delete Group"));
+ }
+
+ @Test
+ void aSkippedRollbackLeavesTheRecordsItCouldNotDeleteInTheirZone() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ Route53CfnProvisioner owner = new Route53CfnProvisioner(service);
+ ResourceRecordSet old = aRecord("www.example.test.", "192.0.2.1");
+ when(service.listResourceRecordSets("ZOLD", null, null, 0)).thenReturn(List.of(old));
+ StackResource resource = failedMoveRollback(owner, service, old);
+ // ContinueUpdateRollback skipped the group.
+ resource.setStatus("UPDATE_COMPLETE");
+
+ // Nothing tells the leftover apart from an identical record written there later, so neither the
+ // next commit's cleanup nor a delete removes it.
+ assertNull(owner.updateCleanupPhysicalId(resource));
+ assertFalse(owner.completeUpdate(resource).applicable());
+ assertFalse(resource.getAttributes().containsKey("__FlociRoute53GroupOwedDeletes"));
+ owner.delete(resource, "us-east-1");
+
+ verify(service, times(1)).changeResourceRecordSets(eq("ZNEW"), any(), eq("CloudFormation delete Group"));
+ verify(service).changeResourceRecordSets(eq("ZOLD"), any(), eq("CloudFormation delete Group"));
+ }
+
+ @Test
+ void reApplyingAMoveAfterASkippedRollbackKeepsTheRecordItWrites() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ Route53CfnProvisioner owner = new Route53CfnProvisioner(service);
+ ResourceRecordSet old = aRecord("www.example.test.", "192.0.2.1");
+ StackResource resource = failedMoveRollback(owner, service, old);
+ when(service.listResourceRecordSets("ZOLD", null, null, 0)).thenReturn(List.of(old));
+ resource.setUpdateReplacePolicy(null);
+ resource.setStatus("UPDATE_COMPLETE");
+
+ // ContinueUpdateRollback skipped the group, the leftover record was deleted by hand, and the
+ // move is applied again, writing the same record into ZNEW.
+ owner.provision(resource, new ObjectMapper().readTree("""
+ {"HostedZoneId": "ZNEW", "RecordSets": [
+ {"Name": "www.example.test", "Type": "A", "TTL": "300", "ResourceRecords": ["192.0.2.2"]}
+ ]}
+ """), groupContext("Group"));
+ UpdateCleanupResult cleanup = owner.completeUpdate(resource);
+ owner.clearUpdate(resource);
+
+ assertTrue(cleanup.complete());
+ verify(service).changeResourceRecordSets(eq("ZOLD"), any(), eq("CloudFormation delete Group"));
+ // Only the failed rollback's delete went to ZNEW; the record the move wrote stays.
+ verify(service, times(1)).changeResourceRecordSets(eq("ZNEW"), any(), eq("CloudFormation delete Group"));
+ assertFalse(resource.getAttributes().containsKey("__FlociRoute53GroupOwedDeletes"));
+ assertFalse(owner.hasReplacementUpdate(resource));
+ }
+
+ @Test
+ void aLaterUpdateForgetsWhatASkippedRollbackLeftSoARetryNeedsOnlyItsOwn() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ Route53CfnProvisioner owner = new Route53CfnProvisioner(service);
+ StackResource resource = failedMoveRollback(owner, service, aRecord("www.example.test.", "192.0.2.1"));
+ // The ZNEW record stays changed outside the stack, so deleting it keeps failing.
+ doThrow(new AwsException("InvalidChangeBatch", "values do not match", 400))
+ .when(service).changeResourceRecordSets(eq("ZNEW"), any(), eq("CloudFormation delete Group"));
+
+ // After a skip, a second move, to Z3, fails too, and its rollback cannot delete Z3's record once.
+ resource.setStatus("UPDATE_COMPLETE");
+ when(service.changeResourceRecordSets(eq("Z3"), any(), any())).thenReturn(new ChangeInfo("C3", "now", null));
+ when(service.listResourceRecordSets("Z3", null, null, 0))
+ .thenReturn(List.of(aRecord("www.example.test.", "192.0.2.3")));
+ owner.provision(resource, new ObjectMapper().readTree("""
+ {"HostedZoneId": "Z3", "RecordSets": [
+ {"Name": "www.example.test", "Type": "A", "TTL": "300", "ResourceRecords": ["192.0.2.3"]}
+ ]}
+ """), groupContext("Group"));
+ doThrow(new AwsException("InvalidChangeBatch", "Z3 values do not match", 400)).doReturn(null)
+ .when(service).changeResourceRecordSets(eq("Z3"), any(), eq("CloudFormation delete Group"));
+ assertThrows(AwsException.class, () -> owner.rollbackUpdate(resource));
+ resource.setStatus("UPDATE_FAILED");
+
+ // The skipped rollback's ZNEW record was left behind by the second update, so the retry needs only
+ // Z3's record, which its own rollback could not delete.
+ String owed = resource.getAttributes().get("__FlociRoute53GroupOwedDeletes");
+ assertTrue(owed.contains("Z3") && !owed.contains("ZNEW"), owed);
+ assertTrue(owner.rollbackUpdate(resource));
+
+ verify(service, times(2)).changeResourceRecordSets(eq("Z3"), any(), eq("CloudFormation delete Group"));
+ verify(service, times(1)).changeResourceRecordSets(eq("ZNEW"), any(), eq("CloudFormation delete Group"));
+ assertFalse(resource.getAttributes().containsKey("__FlociRoute53GroupOwedDeletes"));
+ assertFalse(owner.completeUpdate(resource).applicable());
+ }
+
+ @Test
+ void replacingAStubAnOlderFlociLeftIsNotAZoneChangeAndARollbackPutsTheStubBack() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ when(service.changeResourceRecordSets(eq("Z1"), any(), any())).thenReturn(new ChangeInfo("C1", "now", null));
+ StackResource resource = recordGroup();
+ resource.setPhysicalId("Group-0a1b2c3d");
+ resource.getAttributes().put("Arn", "arn:aws:stub:::Group");
+ Map<String, String> stub = Map.copyOf(resource.getAttributes());
+ Route53CfnProvisioner owner = new Route53CfnProvisioner(service);
+
+ owner.provision(resource, new ObjectMapper().readTree("""
+ {"HostedZoneId": "Z1", "RecordSets": [
+ {"Name": "www.example.test", "Type": "A", "TTL": "300", "ResourceRecords": ["192.0.2.1"]}
+ ]}
+ """), groupContext("Group-0a1b2c3d"));
+
+ assertEquals("Group", resource.getPhysicalId());
+ assertFalse(owner.hasReplacementUpdate(resource));
+ assertNull(owner.updateCleanupPhysicalId(resource));
+
+ when(service.listResourceRecordSets("Z1", null, null, 0))
+ .thenReturn(List.of(aRecord("www.example.test.", "192.0.2.1")));
+ assertTrue(owner.rollbackUpdate(resource));
+
+ assertEquals("Group-0a1b2c3d", resource.getPhysicalId());
+ assertEquals(stub, resource.getAttributes());
+ verify(service).changeResourceRecordSets(eq("Z1"), argThat(changes -> describe(changes)
+ .equals(List.of("DELETE www.example.test."))), any());
+ }
+
+ @Test
+ void recordGroupRejectsTwoRecordSetsWithOneNameTypeAndSetIdentifier() throws Exception {
+ assertRecordGroupRejected("""
+ {"HostedZoneId": "Z1", "RecordSets": [
+ {"Name": "www.example.test", "Type": "A", "ResourceRecords": ["192.0.2.1"]},
+ {"Name": "www.example.test.", "Type": "A", "ResourceRecords": ["192.0.2.2"]}
+ ]}
+ """);
+ }
+
+ @Test
+ void deletingARecordGroupToleratesAZoneAlreadyGone() throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ when(service.listResourceRecordSets("Z1", null, null, 0))
+ .thenThrow(new AwsException("NoSuchHostedZone", "No hosted zone found with ID: Z1", 404));
+ StackResource resource = existingGroup("Z1", "C1", aRecord("www.example.test.", "192.0.2.1"));
+
+ new Route53CfnProvisioner(service).delete(resource, "us-east-1");
+
+ verify(service, never()).changeResourceRecordSets(any(), any(), any());
+ }
+
+ private void assertRecordGroupRejected(String json) throws Exception {
+ Route53Service service = mock(Route53Service.class);
+ JsonNode props = new ObjectMapper().readTree(json);
+
+ AwsException failure = assertThrows(AwsException.class,
+ () -> new Route53CfnProvisioner(service).provision(recordGroup(), props, groupContext(null)));
+
+ assertEquals("ValidationError", failure.getErrorCode());
+ verify(service, never()).changeResourceRecordSets(any(), any(), any());
+ }
+
+ private static StackResource recordGroup() {
+ StackResource resource = new StackResource();
+ resource.setLogicalId("Group");
+ resource.setResourceType("AWS::Route53::RecordSetGroup");
+ return resource;
+ }
+
+ /** A group a create or an earlier update wrote into {@code zoneId} with change {@code changeId}. */
+ private static StackResource existingGroup(String zoneId, String changeId, ResourceRecordSet... records)
+ throws Exception {
+ StackResource resource = recordGroup();
+ resource.setPhysicalId("Group");
+ resource.getAttributes().put("Id", changeId);
+ resource.getAttributes().put("__FlociRoute53RecordZoneId", zoneId);
+ resource.getAttributes().put("__FlociRoute53GroupRecords",
+ new ObjectMapper().writeValueAsString(List.of(records)));
+ return resource;
+ }
+
+ /**
+ * A group created in ZOLD as {@code old} that an update has just moved to ZNEW, writing
+ * {@link #movedRecord()} there; the update has neither committed nor rolled back.
+ */
+ private static StackResource movedGroup(Route53CfnProvisioner owner, Route53Service service,
+ ResourceRecordSet old) throws Exception {
+ StackResource resource = existingGroup("ZOLD", "C1", old);
+ when(service.changeResourceRecordSets(eq("ZNEW"), any(), any())).thenReturn(new ChangeInfo("C2", "now", null));
+ owner.provision(resource, new ObjectMapper().readTree("""
+ {"HostedZoneId": "ZNEW", "RecordSets": [
+ {"Name": "www.example.test", "Type": "A", "TTL": "300", "ResourceRecords": ["192.0.2.2"]}
+ ]}
+ """), groupContext("Group"));
+ return resource;
+ }
+
+ private static ResourceRecordSet movedRecord() {
+ return aRecord("www.example.test.", "192.0.2.2");
+ }
+
+ /**
+ * A moved group, under {@code UpdateReplacePolicy: Retain}, whose rollback could not delete the
+ * record it wrote into ZNEW; the next delete there succeeds.
+ */
+ private static StackResource failedMoveRollback(Route53CfnProvisioner owner, Route53Service service,
+ ResourceRecordSet old) throws Exception {
+ when(service.listResourceRecordSets("ZNEW", null, null, 0)).thenReturn(List.of(movedRecord()));
+ StackResource resource = movedGroup(owner, service, old);
+ resource.setUpdateReplacePolicy("Retain");
+ doThrow(new AwsException("InvalidChangeBatch", "values do not match", 400)).doReturn(null)
+ .when(service).changeResourceRecordSets(eq("ZNEW"), any(), eq("CloudFormation delete Group"));
+ AwsException failure = assertThrows(AwsException.class, () -> owner.rollbackUpdate(resource));
+ assertEquals("values do not match", failure.getMessage());
+ // As the rollback walker marks a resource whose rollback failed.
+ resource.setStatus("UPDATE_FAILED");
+ return resource;
+ }
+
+ private static ResourceRecordSet aRecord(String name, String address) {
+ ResourceRecordSet record = new ResourceRecordSet();
+ record.setName(name);
+ record.setType("A");
+ record.setTtl(300L);
+ record.setRecords(List.of(new ResourceRecord(address)));
+ return record;
+ }
+
+ private static List<String> describe(List<Map<String, Object>> changes) {
+ return changes.stream()
+ .map(change -> change.get("action") + " " + ((ResourceRecordSet) change.get("rrs")).getName())
+ .toList();
+ }
+
+ private static ProvisionContext groupContext(String priorPhysicalId) {
+ CloudFormationTemplateEngine engine = recordEngine();
+ when(engine.resolveNodeOmittingNoValue(any())).thenAnswer(invocation -> invocation.getArgument(0));
+ return priorPhysicalId == null
+ ? new ProvisionContext(engine, "us-east-1", "000000000000", "dns-stack")
+ : new ProvisionContext(engine, "us-east-1", "000000000000", "dns-stack", priorPhysicalId);
+ }
+
@Test
void healthCheckIsCreatedWithItsConfigTagsAndExactAttributes() throws Exception {
Route53Service service = mock(Route53Service.class);
diff --git a/src/test/resources/cloudformation/supported-resource-types.tsv b/src/test/resources/cloudformation/supported-resource-types.tsv
index 1cec1dc2cd..86e8a1349e 100644
--- a/src/test/resources/cloudformation/supported-resource-types.tsv
+++ b/src/test/resources/cloudformation/supported-resource-types.tsv
@@ -165,6 +165,7 @@ AWS::RedshiftServerless::Workgroup RedshiftServerlessCfnProvisioner
AWS::Route53::HealthCheck Route53CfnProvisioner
AWS::Route53::HostedZone Route53CfnProvisioner
AWS::Route53::RecordSet Route53CfnProvisioner
+AWS::Route53::RecordSetGroup Route53CfnProvisioner
AWS::S3::Bucket S3CfnProvisioner
AWS::S3::BucketPolicy S3CfnProvisioner
AWS::S3Vectors::Index S3VectorsCfnProvisioner
diff --git a/tools/docs/cfn_resource_types.yaml b/tools/docs/cfn_resource_types.yaml
index d62d3fb50f..8e19839c44 100644
--- a/tools/docs/cfn_resource_types.yaml
+++ b/tools/docs/cfn_resource_types.yaml
@@ -106,7 +106,7 @@ type_order:
AWS::ElasticLoadBalancingV2: [LoadBalancer, TargetGroup, Listener, ListenerRule]
AWS::ApplicationAutoScaling: [ScalableTarget, ScalingPolicy]
AWS::AutoScaling: [LaunchConfiguration, AutoScalingGroup, LifecycleHook]
- AWS::Route53: [HostedZone, RecordSet, HealthCheck]
+ AWS::Route53: [HostedZone, RecordSet, RecordSetGroup, HealthCheck]
AWS::ApiGateway: [RestApi, Resource, Authorizer, Method, Deployment, Stage, Account, DomainName, BasePathMapping, GatewayResponse]
AWS::ApiGatewayV2: [Api, Authorizer, Route, Integration, Stage, Deployment, DomainName, ApiMapping]
AWS::AppSync: [GraphQLApi, GraphQLSchema, DataSource, FunctionConfiguration, Resolver, ApiKey]
@@ -205,6 +205,7 @@ notes:
AWS::IAM::User: 'template `LoginProfile` and `PermissionsBoundary` are ignored; an API-created login profile is removed on delete'
AWS::IoT::DomainConfiguration: '`ServerCertificates` resolves to a JSON string'
AWS::IoT::Policy: 'deleted after detaching it from its principals; on AWS the delete fails with `DeleteConflictException` while the policy is attached'
+ AWS::Route53::RecordSetGroup: 'written in one change batch, so a record set that cannot be written leaves the zone unchanged; every record set must be in the group''s hosted zone'
Custom::DynamoDBReplica: applied natively against DynamoDB, not via a provider Lambda
AWS::DynamoDB::Table: '`TimeToLiveSpecification` applied on create and update; to rename an enabled TTL attribute, disable TTL in one update and enable the new attribute in a later one'
AWS::Cognito::UserPool: '`ProviderURL` is the local issuer of the tokens Floci mints, `<base-url>/<pool id>`'