Light, fluffy, and always free - The AWS Local Emulator alternative
feat(cloudformation): write Route53 RecordSet records into the zone (#4132)
Hector Ventura32856bfee018
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.javaindex a492aa8637..f253e779b5 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@@ -4,33 +4,45 @@ import com.fasterxml.jackson.databind.JsonNode;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.HostedZone;+import io.github.hectorvent.floci.services.route53.model.ResourceRecord;+import io.github.hectorvent.floci.services.route53.model.ResourceRecordSet;import io.github.hectorvent.floci.services.route53.model.VpcAssociation;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import org.jboss.logging.Logger;import java.util.ArrayList;+import java.util.HashMap;import java.util.List;import java.util.Map;+import java.util.Objects;import java.util.Set;-import java.util.UUID;/*** Provisions {@code AWS::Route53::HostedZone} and {@code AWS::Route53::RecordSet}.*- * <p>A record set is a name-only resource here, as it was in the legacy- * {@code CloudFormationResourceProvisioner} it moved out of: nothing is written to the zone,- * {@code Ref} is the record name (or a generated {@code record-} id when the template gives- * none), and no {@code Fn::GetAtt} attribute is published. The registry schema lists {@code Id}- * as read-only, but the type has no registry handlers and the CloudFormation resource- * specification gives it no attributes, so {@code Ref} is its only reference. Writing the record- * through {@code ChangeResourceRecordSets} is a follow-up.+ * <p>A record set is written into its hosted zone through {@code ChangeResourceRecordSets}: the+ * declared record is applied as an UPSERT so create and update both converge, {@code Ref} is the+ * record name, and stack delete removes the record with a matching DELETE change. The schema-required+ * {@code Type} and {@code Name} are validated, and the zone is taken from {@code HostedZoneId} (or+ * resolved from {@code HostedZoneName}), since a record cannot be 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.*/@ApplicationScopedpublic class Route53CfnProvisioner implements CfnResourceProvisioner {static final String HOSTED_ZONE = "AWS::Route53::HostedZone";static final String RECORD_SET = "AWS::Route53::RecordSet";+ // Create-time values a record's delete needs to build a matching DELETE change; kept off the+ // published attributes by the __Floci prefix, as the other provisioners do.+ 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";+private static final Logger LOG = Logger.getLogger(Route53CfnProvisioner.class);private final Route53Service route53Service;@@ -56,9 +68,149 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner {}private void provisionRecordSet(StackResource resource, JsonNode props, ProvisionContext ctx) {+ Map<String, String> priorAttributes = new HashMap<>(resource.getAttributes());String name = ctx.resolveOptional(props, "Name");- String id = name != null ? name : "record-" + UUID.randomUUID().toString().substring(0, 8);- resource.setPhysicalId(id);+ 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);+ }+ if (type == null || type.isBlank()) {+ throw new AwsException("ValidationError", "AWS::Route53::RecordSet requires Type.", 400);+ }+ String zoneId = resolveZoneId(props, ctx);++ ResourceRecordSet rrs = new ResourceRecordSet();+ rrs.setName(name);+ rrs.setType(type);+ String ttl = ctx.resolveOptional(props, "TTL");+ if (ttl != null && !ttl.isBlank()) {+ // TTL is a string in the schema, so a non-numeric value is valid template JSON; reject it+ // as a ValidationError like Name and Type rather than letting a raw NumberFormatException+ // escape.+ try {+ rrs.setTtl(Long.parseLong(ttl.trim()));+ } catch (NumberFormatException e) {+ throw new AwsException("ValidationError",+ "AWS::Route53::RecordSet TTL must be an integer: " + ttl, 400);+ }+ }+ String setIdentifier = ctx.resolveOptional(props, "SetIdentifier");+ rrs.setSetIdentifier(setIdentifier);+ rrs.setRecords(parseResourceRecords(props, ctx));+ rrs.setAliasTarget(parseAliasTarget(props, ctx));++ // UPSERT: provision serves create and update, and re-applying the same record must not fail+ // with "already exists" the way a CREATE would on the second UpdateStack.+ Map<String, Object> change = new HashMap<>();+ change.put("action", "UPSERT");+ change.put("rrs", rrs);+ route53Service.changeResourceRecordSets(zoneId, List.of(change),+ "CloudFormation " + ctx.stackName() + "/" + resource.getLogicalId());++ // The record's identity is Name + Type + SetIdentifier within a zone. When an update changes+ // any of them, the UPSERT writes a new record and the prior one is orphaned, so remove it,+ // otherwise it lingers in the zone and later blocks the zone's own delete.+ removeSupersededRecord(ctx, priorAttributes, zoneId, name, type, setIdentifier);++ 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);+ }+ }++ /**+ * Removes the record a prior provision wrote when this update changed the record's identity+ * (zone, Name, Type or SetIdentifier). Reads the prior identity from the attributes the last+ * provision recorded plus the prior physical id (the prior Name). A no-op on create, and when+ * the identity is unchanged the UPSERT already replaced the record in place.+ */+ private void removeSupersededRecord(ProvisionContext ctx, Map<String, String> priorAttributes,+ String zoneId, String name, String type, String setIdentifier) {+ if (!ctx.isUpdate()) {+ return;+ }+ String priorZoneId = priorAttributes.get(RECORD_ZONE_ATTR);+ String priorName = ctx.priorPhysicalId();+ String priorType = priorAttributes.get(RECORD_TYPE_ATTR);+ String priorSetId = priorAttributes.get(RECORD_SET_ID_ATTR);+ if (priorZoneId == null || priorZoneId.isBlank() || priorName == null || priorName.isBlank()+ || priorType == null || priorType.isBlank()) {+ return;+ }+ boolean sameIdentity = priorZoneId.equals(zoneId) && priorName.equals(name)+ && priorType.equals(type) && Objects.equals(priorSetId, setIdentifier);+ if (sameIdentity) {+ return;+ }+ removeRecord(priorZoneId, priorName, priorType, priorSetId, "CloudFormation supersede");+ }++ /**+ * 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.+ */+ private String resolveZoneId(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);+ }+ throw new AwsException("ValidationError",+ "AWS::Route53::RecordSet requires HostedZoneId or HostedZoneName.", 400);+ }++ /**+ * The id of the single hosted zone whose name equals {@code zoneName}. {@code listHostedZonesByName}+ * is a starts-at paginator, so it must be filtered to an exact match rather than trusting the first+ * result, or a record lands in whatever zone happens to sort at or after the name. AWS rejects the+ * 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) {+ String normalized = zoneName.endsWith(".") ? zoneName : zoneName + ".";+ List<HostedZone> matches = route53Service.listHostedZonesByName(zoneName, 0).stream()+ .filter(zone -> normalized.equals(zone.getName()))+ .toList();+ if (matches.size() == 1) {+ 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);+ }++ private List<ResourceRecord> parseResourceRecords(JsonNode props, ProvisionContext ctx) {+ List<ResourceRecord> records = new ArrayList<>();+ for (String value : ctx.resolveStringList(props, "ResourceRecords")) {+ if (value != null && !value.isBlank()) {+ records.add(new ResourceRecord(value));+ }+ }+ return records;+ }++ private AliasTarget parseAliasTarget(JsonNode props, ProvisionContext ctx) {+ JsonNode node = ctx.engine().resolveNode(props != null ? props.get("AliasTarget") : null);+ if (node == null || node.isNull() || !node.isObject()) {+ return null;+ }+ AliasTarget target = new AliasTarget();+ target.setDnsName(node.path("DNSName").asText(null));+ target.setHostedZoneId(node.path("HostedZoneId").asText(null));+ target.setEvaluateTargetHealth(node.path("EvaluateTargetHealth").asBoolean(false));+ return target;}private void provisionHostedZone(StackResource resource, JsonNode props, ProvisionContext ctx) {@@ -157,20 +309,80 @@ 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.+ */+ @Override+ public void delete(StackResource resource, String region) {+ if (RECORD_SET.equals(resource.getResourceType())) {+ deleteRecordSet(resource);+ } else {+ delete(resource.getResourceType(), resource.getPhysicalId(), region);+ }+ }+@Overridepublic void delete(String resourceType, String physicalId, String region) {switch (resourceType) {case "AWS::Route53::HostedZone" -> route53Service.deleteHostedZone(physicalId);- // Nothing was written for the record, so there is nothing to remove; the warning- // mirrors the legacy dispatcher's default arm so the gap stays visible in the log.+ // 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(- "No delete implemented for resource type {0}: {1} is not removed here.",- resourceType, physicalId);+ "No delete from the physical id alone for resource type {0}: {1} is removed via "+ + "delete(resource, region).", resourceType, physicalId);default -> throw new IllegalStateException("Route53CfnProvisioner received an unsupported type: " + resourceType);}}+ private void deleteRecordSet(StackResource resource) {+ Map<String, String> attributes = resource.getAttributes() != null+ ? resource.getAttributes() : Map.of();+ String zoneId = attributes.get(RECORD_ZONE_ATTR);+ String type = attributes.get(RECORD_TYPE_ATTR);+ String name = resource.getPhysicalId();+ if (zoneId == null || zoneId.isBlank() || type == null || type.isBlank()+ || name == null || name.isBlank()) {+ // A record from before this write path stored no zone, so there is nothing to remove.+ LOG.warnv("No stored zone for Route53 record {0}; nothing to delete.", resource.getLogicalId());+ return;+ }+ String setIdentifier = attributes.get(RECORD_SET_ID_ATTR);+ // Removing a record the zone no longer has is a no-op, and a zone already gone tolerated; any+ // other failure must reach the stack as DELETE_FAILED.+ removeRecord(zoneId, name, type, setIdentifier, "CloudFormation delete " + resource.getLogicalId());+ }++ /**+ * Removes a record by its identity, tolerating a zone or record already gone. A zone still+ * holding the record blocks its own delete, so a superseded or deleted record must actually go.+ */+ private void removeRecord(String zoneId, String name, String type, String setIdentifier, String comment) {+ CfnDeletes.safeDelete("Route53 record set", name + " " + type, () -> {+ ResourceRecordSet existing = findRecord(zoneId, name, type, setIdentifier);+ 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);+ }, "NoSuchHostedZone", "InvalidChangeBatch");+ }++ private ResourceRecordSet findRecord(String zoneId, String name, String type, String setIdentifier) {+ for (ResourceRecordSet rrs : route53Service.listResourceRecordSets(zoneId, null, null, 0)) {+ if (name.equals(rrs.getName()) && type.equals(rrs.getType())+ && Objects.equals(setIdentifier, rrs.getSetIdentifier())) {+ return rrs;+ }+ }+ return null;+ }+private List<VpcAssociation> parseVpcs(JsonNode node) {List<VpcAssociation> vpcs = new ArrayList<>();if (node.isArray()) {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.javaindex 885b85f50b..55706e6a19 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@@ -27,6 +27,8 @@ class CloudFormationRoute53RecordSetIntegrationTest {"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";private static final String SSM_AUTH ="AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ssm/aws4_request";+ private static final String ROUTE53_AUTH =+ "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);@@ -60,21 +62,40 @@ class CloudFormationRoute53RecordSetIntegrationTest {"RefParam": {"Type": "AWS::SSM::Parameter","Properties": {"Name": "/r53-recordset/%s/ref", "Type": "String", "Value": {"Ref": "Www"}}+ },+ "ZoneParam": {+ "Type": "AWS::SSM::Parameter",+ "Properties": {"Name": "/r53-recordset/%s/zone", "Type": "String", "Value": {"Ref": "Zone"}}}}}- """.formatted(suffix, recordName, suffix);+ """.formatted(suffix, recordName, suffix, suffix);createStack(stackName, template);assertStackStatus(stackName, "CREATE_COMPLETE");try {assertEquals(recordName, parameterValue("/r53-recordset/" + suffix + "/ref"));++ // A stack status proves nothing on its own: read the record back from the zone to+ // confirm the provisioner wrote it (not stubbed) rather than leaving the zone empty.+ String zoneId = parameterValue("/r53-recordset/" + suffix + "/zone");+ String records = listResourceRecordSets(zoneId);+ assertTrue(records.contains(recordName), "record not written to zone: " + records);+ assertTrue(records.contains("10.0.0.1"), "record value not written to zone: " + records);} finally {deleteStack(stackName);awaitStackGone(stackName);}}+ private String listResourceRecordSets(String zoneId) {+ return given()+ .header("Authorization", ROUTE53_AUTH)+ .when().get("/2013-04-01/hostedzone/" + zoneId + "/rrset")+ .then().statusCode(200)+ .extract().asString();+ }+private void createStack(String stackName, String template) {given().contentType("application/x-www-form-urlencoded")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.javaindex 8bf4de5547..2fe9eddcde 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@@ -7,21 +7,28 @@ import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateimport io.github.hectorvent.floci.services.cloudformation.model.StackResource;import io.github.hectorvent.floci.services.route53.Route53Service;import io.github.hectorvent.floci.services.route53.model.HostedZone;+import io.github.hectorvent.floci.services.route53.model.ResourceRecord;+import io.github.hectorvent.floci.services.route53.model.ResourceRecordSet;import io.github.hectorvent.floci.services.route53.model.VpcAssociation;import org.junit.jupiter.api.Test;+import org.mockito.ArgumentCaptor;+import java.util.ArrayList;import java.util.List;+import java.util.Map;import java.util.Set;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertThrows;import static org.junit.jupiter.api.Assertions.assertNotNull;+import static org.junit.jupiter.api.Assertions.assertSame;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.mock;import static org.mockito.Mockito.never;+import static org.mockito.Mockito.times;import static org.mockito.Mockito.verify;import static org.mockito.Mockito.verifyNoInteractions;import static org.mockito.Mockito.when;@@ -370,49 +377,204 @@ class Route53CfnProvisionerTest {}@Test- void recordSetUsesItsNameAsRefPublishesNoAttributesAndWritesNothingToTheZone() {+ void recordSetIsUpsertedIntoItsZoneAndRefIsTheRecordName() {Route53Service service = mock(Route53Service.class);ObjectMapper mapper = new ObjectMapper();JsonNode props = mapper.createObjectNode().put("HostedZoneId", "Z123456789").put("Name", "www.example.com")- .put("Type", "A");- CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);- when(engine.resolve(any())).thenAnswer(invocation -> invocation.<JsonNode>getArgument(0).asText());+ .put("Type", "A")+ .put("TTL", "300");+ ((com.fasterxml.jackson.databind.node.ObjectNode) props).set("ResourceRecords",+ mapper.createArrayNode().add("10.0.0.1"));+ CloudFormationTemplateEngine engine = recordEngine();ProvisionContext context = new ProvisionContext(engine, "us-east-1", "623666680275", "dns-stack");StackResource resource = new StackResource();- resource.setLogicalId("Alias");+ resource.setLogicalId("Www");resource.setResourceType("AWS::Route53::RecordSet");new Route53CfnProvisioner(service).provision(resource, props, context);assertEquals("www.example.com", resource.getPhysicalId());- assertEquals(Set.of(), resource.getAttributes().keySet());- verifyNoInteractions(service);+ // The internal zone/type record survives for delete, but no schema attribute is published.+ assertEquals(Set.of("__FlociRoute53RecordZoneId", "__FlociRoute53RecordType"),+ resource.getAttributes().keySet());++ @SuppressWarnings("unchecked")+ ArgumentCaptor<List<Map<String, Object>>> captor = ArgumentCaptor.forClass(List.class);+ verify(service).changeResourceRecordSets(eq("Z123456789"), captor.capture(), any());+ Map<String, Object> change = captor.getValue().get(0);+ assertEquals("UPSERT", change.get("action"));+ ResourceRecordSet rrs = (ResourceRecordSet) change.get("rrs");+ assertEquals("www.example.com", rrs.getName());+ assertEquals("A", rrs.getType());+ assertEquals(Long.valueOf(300), rrs.getTtl());+ assertEquals(List.of("10.0.0.1"), rrs.getRecords().stream().map(ResourceRecord::getValue).toList());}@Test- void recordSetWithoutANameGetsAGeneratedRecordId() {+ void recordSetResolvesTheZoneFromHostedZoneNameByExactMatch() {Route53Service service = mock(Route53Service.class);+ // listHostedZonesByName is a starts-at paginator: it also returns zones that sort after the+ // requested name, so the provisioner must pick the exact match, not the first result.+ when(service.listHostedZonesByName("example.com.", 0)).thenReturn(List.of(+ new HostedZone("Z999", "example.com.", "caller", null, (VpcAssociation) null),+ new HostedZone("Zzzz", "zzz.internal.", "caller", null, (VpcAssociation) null)));ObjectMapper mapper = new ObjectMapper();- JsonNode props = mapper.createObjectNode().put("Type", "A");- CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);- when(engine.resolve(any())).thenAnswer(invocation -> invocation.<JsonNode>getArgument(0).asText());- ProvisionContext context = new ProvisionContext(engine, "us-east-1", "623666680275", "dns-stack");+ JsonNode props = mapper.createObjectNode()+ .put("HostedZoneName", "example.com.")+ .put("Name", "www.example.com")+ .put("Type", "A");+ ProvisionContext context = new ProvisionContext(recordEngine(), "us-east-1", "623666680275", "dns-stack");StackResource resource = new StackResource();- resource.setLogicalId("Alias");+ resource.setLogicalId("Www");resource.setResourceType("AWS::Route53::RecordSet");new Route53CfnProvisioner(service).provision(resource, props, context);- assertNotNull(resource.getPhysicalId());- assertTrue(resource.getPhysicalId().matches("record-[0-9a-f]{8}"), resource.getPhysicalId());- assertTrue(resource.getAttributes().isEmpty(), resource.getAttributes().toString());- verifyNoInteractions(service);+ verify(service).changeResourceRecordSets(eq("Z999"), any(), any());+ }++ @Test+ void recordSetRejectsAHostedZoneNameThatMatchesNoZone() {+ Route53Service service = mock(Route53Service.class);+ // The paginator returns a later-sorting zone, but none whose name actually equals the request.+ when(service.listHostedZonesByName("app.example.com.", 0)).thenReturn(List.of(+ new HostedZone("Zzzz", "zzz.internal.", "caller", null, (VpcAssociation) null)));+ ObjectMapper mapper = new ObjectMapper();+ JsonNode props = mapper.createObjectNode()+ .put("HostedZoneName", "app.example.com.")+ .put("Name", "www.app.example.com")+ .put("Type", "A");+ ProvisionContext context = new ProvisionContext(recordEngine(), "us-east-1", "623666680275", "dns-stack");+ StackResource resource = new StackResource();+ resource.setLogicalId("Www");+ resource.setResourceType("AWS::Route53::RecordSet");++ AwsException failure = assertThrows(AwsException.class,+ () -> new Route53CfnProvisioner(service).provision(resource, props, context));+ assertEquals("InvalidChangeBatch", failure.getErrorCode());+ verify(service, never()).changeResourceRecordSets(any(), any(), any());+ }++ @Test+ void recordSetWithANonNumericTtlIsRejected() {+ assertRecordSetRejected("{\"HostedZoneId\": \"Z1\", \"Name\": \"www.example.com\", "+ + "\"Type\": \"A\", \"TTL\": \"5 minutes\"}");+ }++ @Test+ void recordSetWithoutANameIsRejected() {+ assertRecordSetRejected("{\"HostedZoneId\": \"Z1\", \"Type\": \"A\"}");+ }++ @Test+ void recordSetWithoutATypeIsRejected() {+ assertRecordSetRejected("{\"HostedZoneId\": \"Z1\", \"Name\": \"www.example.com\"}");+ }++ @Test+ void recordSetWithoutAZoneIsRejected() {+ assertRecordSetRejected("{\"Name\": \"www.example.com\", \"Type\": \"A\"}");+ }++ private void assertRecordSetRejected(String json) {+ Route53Service service = mock(Route53Service.class);+ ObjectMapper mapper = new ObjectMapper();+ JsonNode props;+ try {+ props = mapper.readTree(json);+ } catch (Exception e) {+ throw new RuntimeException(e);+ }+ ProvisionContext context = new ProvisionContext(recordEngine(), "us-east-1", "623666680275", "dns-stack");+ StackResource resource = new StackResource();+ resource.setLogicalId("Www");+ resource.setResourceType("AWS::Route53::RecordSet");++ AwsException failure = assertThrows(AwsException.class,+ () -> new Route53CfnProvisioner(service).provision(resource, props, context));+ assertEquals("ValidationError", failure.getErrorCode());+ verify(service, never()).changeResourceRecordSets(any(), any(), any());+ }++ @Test+ void renamingARecordSetOnUpdateRemovesTheSupersededRecord() {+ Route53Service service = mock(Route53Service.class);+ ResourceRecordSet priorRecord = new ResourceRecordSet();+ priorRecord.setName("auth.example.com");+ priorRecord.setType("A");+ when(service.listResourceRecordSets("Z1", null, null, 0)).thenReturn(List.of(priorRecord));+ ObjectMapper mapper = new ObjectMapper();+ JsonNode props = mapper.createObjectNode()+ .put("HostedZoneId", "Z1")+ .put("Name", "login.example.com")+ .put("Type", "A");+ ProvisionContext context = new ProvisionContext(recordEngine(), "us-east-1", "623666680275",+ "dns-stack", "auth.example.com");+ StackResource resource = new StackResource();+ resource.setLogicalId("Www");+ resource.setResourceType("AWS::Route53::RecordSet");+ resource.setPhysicalId("auth.example.com");+ resource.getAttributes().put("__FlociRoute53RecordZoneId", "Z1");+ resource.getAttributes().put("__FlociRoute53RecordType", "A");++ new Route53CfnProvisioner(service).provision(resource, props, context);++ assertEquals("login.example.com", resource.getPhysicalId());+ // Two changes: an UPSERT for the new name and a DELETE removing the record left under the old.+ @SuppressWarnings("unchecked")+ ArgumentCaptor<List<Map<String, Object>>> captor = ArgumentCaptor.forClass(List.class);+ verify(service, times(2)).changeResourceRecordSets(eq("Z1"), captor.capture(), any());+ List<String> actions = captor.getAllValues().stream()+ .map(changes -> (String) changes.get(0).get("action")).toList();+ assertTrue(actions.contains("UPSERT"), actions.toString());+ assertTrue(actions.contains("DELETE"), actions.toString());+ }++ @Test+ void deletingARecordSetViaTheResourceIssuesAMatchingDeleteChange() {+ Route53Service service = mock(Route53Service.class);+ ResourceRecordSet existing = new ResourceRecordSet();+ existing.setName("www.example.com");+ existing.setType("A");+ when(service.listResourceRecordSets("Z123456789", null, null, 0)).thenReturn(List.of(existing));+ StackResource resource = new StackResource();+ resource.setLogicalId("Www");+ resource.setResourceType("AWS::Route53::RecordSet");+ resource.setPhysicalId("www.example.com");+ resource.getAttributes().put("__FlociRoute53RecordZoneId", "Z123456789");+ resource.getAttributes().put("__FlociRoute53RecordType", "A");++ new Route53CfnProvisioner(service).delete(resource, "us-east-1");++ @SuppressWarnings("unchecked")+ ArgumentCaptor<List<Map<String, Object>>> captor = ArgumentCaptor.forClass(List.class);+ verify(service).changeResourceRecordSets(eq("Z123456789"), captor.capture(), any());+ Map<String, Object> change = captor.getValue().get(0);+ assertEquals("DELETE", change.get("action"));+ assertSame(existing, change.get("rrs"));+ }++ @Test+ void deletingARecordSetToleratesAZoneAlreadyGone() {+ Route53Service service = mock(Route53Service.class);+ when(service.listResourceRecordSets("Z123456789", null, null, 0))+ .thenThrow(new AwsException("NoSuchHostedZone", "gone", 404));+ StackResource resource = new StackResource();+ resource.setLogicalId("Www");+ resource.setResourceType("AWS::Route53::RecordSet");+ resource.setPhysicalId("www.example.com");+ resource.getAttributes().put("__FlociRoute53RecordZoneId", "Z123456789");+ resource.getAttributes().put("__FlociRoute53RecordType", "A");++ new Route53CfnProvisioner(service).delete(resource, "us-east-1");++ verify(service, never()).changeResourceRecordSets(any(), any(), any());}@Test- void deletingARecordSetLeavesHostedZonesAlone() {+ void deletingARecordSetByIdAloneLeavesTheZoneUntouched() {Route53Service service = mock(Route53Service.class);new Route53CfnProvisioner(service).delete("AWS::Route53::RecordSet", "www.example.com", "us-east-1");@@ -420,6 +582,23 @@ class Route53CfnProvisionerTest {verifyNoInteractions(service);}+ private static CloudFormationTemplateEngine recordEngine() {+ CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);+ when(engine.resolve(any())).thenAnswer(invocation -> {+ JsonNode node = invocation.getArgument(0);+ return node == null ? null : node.asText();+ });+ when(engine.resolveStringList(any())).thenAnswer(invocation -> {+ JsonNode node = invocation.getArgument(0);+ List<String> out = new ArrayList<>();+ if (node != null && node.isArray()) {+ node.forEach(element -> out.add(element.asText()));+ }+ return out;+ });+ return engine;+ }+@Testvoid deletingAHostedZoneStillDeletesTheZone() {Route53Service service = mock(Route53Service.class);