Skip to content

armgit / floci

Public Imported from GitHub
0

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

feat(route53): implement UpdateHostedZoneComment (#3906)

Matthew Ingle037236daf8ab
diff --git a/docs/services/route53.md b/docs/services/route53.md
index 3eac9c549d..04046e76c2 100644
--- a/docs/services/route53.md
+++ b/docs/services/route53.md
@@ -9,6 +9,7 @@ Route53 management-plane emulation. Supports hosted zones, resource record sets,
| --- | --- |
| `CreateHostedZone` | `POST /2013-04-01/hostedzone`. A `<VPC>` element in the request creates a private zone and records the first VPC association. |
| `GetHostedZone` | `GET /2013-04-01/hostedzone/{Id}`. Private zones include their `<VPCs>` list. |
+| `UpdateHostedZoneComment` | `POST /2013-04-01/hostedzone/{Id}` |
| `DeleteHostedZone` | `DELETE /2013-04-01/hostedzone/{Id}` |
| `ListHostedZones` | `GET /2013-04-01/hostedzone` |
| `ListHostedZonesByName` | `GET /2013-04-01/hostedzonesbyname` |
diff --git a/src/main/java/io/github/hectorvent/floci/services/route53/Route53Controller.java b/src/main/java/io/github/hectorvent/floci/services/route53/Route53Controller.java
index ae120172a2..43a4eb4a66 100644
--- a/src/main/java/io/github/hectorvent/floci/services/route53/Route53Controller.java
+++ b/src/main/java/io/github/hectorvent/floci/services/route53/Route53Controller.java
@@ -122,6 +122,28 @@ public class Route53Controller {
}
}
+ @POST
+ @Path("/hostedzone/{Id}")
+ public Response updateHostedZoneComment(@PathParam("Id") String id, String body) {
+ try {
+ if (body != null && !body.isBlank()
+ && !"UpdateHostedZoneCommentRequest".equals(XmlParser.rootElementName(body))) {
+ throw new AwsException("InvalidInput",
+ "The request body must be an UpdateHostedZoneCommentRequest document.", 400);
+ }
+ String comment = XmlParser.extractFirst(body, "Comment", null);
+ HostedZone zone = service.updateHostedZoneComment(id, comment);
+ String xml = new XmlBuilder()
+ .start("UpdateHostedZoneCommentResponse", NS)
+ .raw(xmlHostedZone(zone))
+ .end("UpdateHostedZoneCommentResponse")
+ .build();
+ return Response.ok(xml, XML).build();
+ } catch (AwsException e) {
+ return xmlErrorResponse(e);
+ }
+ }
+
@DELETE
@Path("/hostedzone/{Id}")
public Response deleteHostedZone(@PathParam("Id") String id) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/route53/Route53Service.java b/src/main/java/io/github/hectorvent/floci/services/route53/Route53Service.java
index db5b2f2a41..d589fc2fca 100644
--- a/src/main/java/io/github/hectorvent/floci/services/route53/Route53Service.java
+++ b/src/main/java/io/github/hectorvent/floci/services/route53/Route53Service.java
@@ -137,6 +137,17 @@ public class Route53Service {
return newChange(null);
}
+ /**
+ * The comment is cleared when the request carries none, which is what AWS does rather than
+ * leaving the previous comment in place.
+ */
+ public synchronized HostedZone updateHostedZoneComment(String id, String comment) {
+ HostedZone zone = getHostedZoneOwnedByCaller(id);
+ zone.setComment(comment);
+ zoneStore.put(id, zone);
+ return zone;
+ }
+
public List<HostedZone> listHostedZones(String marker, int maxItems) {
List<HostedZone> all = new ArrayList<>(zoneStore.scan(k -> true));
all.sort((a, b) -> a.getName().compareTo(b.getName()));
diff --git a/src/test/java/io/github/hectorvent/floci/services/route53/Route53IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/route53/Route53IntegrationTest.java
index e7c3ad83c0..411f921fbd 100644
--- a/src/test/java/io/github/hectorvent/floci/services/route53/Route53IntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/route53/Route53IntegrationTest.java
@@ -537,4 +537,113 @@ class Route53IntegrationTest {
.body("GetAccountLimitResponse.Limit.Type", equalTo("MAX_HOSTED_ZONES_BY_OWNER"))
.body("GetAccountLimitResponse.Limit.Value", equalTo("500"));
}
+
+ @Test
+ @Order(22)
+ void updateHostedZoneComment_replacesTheCommentAndClearsItWhenOmitted() {
+ String ownZoneId = createZoneForCommentTest("comment-zone-1", "before the import");
+
+ given()
+ .contentType(XML)
+ .body("""
+ <?xml version="1.0" encoding="UTF-8"?>
+ <UpdateHostedZoneCommentRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
+ <Comment>after the import</Comment>
+ </UpdateHostedZoneCommentRequest>
+ """)
+ .when().post("/2013-04-01/hostedzone/" + ownZoneId)
+ .then()
+ .statusCode(200)
+ .contentType(XML)
+ .body("UpdateHostedZoneCommentResponse.HostedZone.Id", equalTo("/hostedzone/" + ownZoneId))
+ .body("UpdateHostedZoneCommentResponse.HostedZone.Config.Comment", equalTo("after the import"));
+
+ given()
+ .when().get("/2013-04-01/hostedzone/" + ownZoneId)
+ .then()
+ .statusCode(200)
+ .body("GetHostedZoneResponse.HostedZone.Config.Comment", equalTo("after the import"));
+
+ // AWS deletes the existing comment when the request carries none.
+ given()
+ .contentType(XML)
+ .body("""
+ <?xml version="1.0" encoding="UTF-8"?>
+ <UpdateHostedZoneCommentRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/"/>
+ """)
+ .when().post("/2013-04-01/hostedzone/" + ownZoneId)
+ .then()
+ .statusCode(200)
+ .body(not(containsString("<Comment>")));
+
+ // Cleanup
+ given().when().delete("/2013-04-01/hostedzone/" + ownZoneId).then().statusCode(200);
+ }
+
+ @Test
+ @Order(23)
+ void updateHostedZoneComment_unknownZoneIsNoSuchHostedZone() {
+ given()
+ .contentType(XML)
+ .body("""
+ <?xml version="1.0" encoding="UTF-8"?>
+ <UpdateHostedZoneCommentRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
+ <Comment>nope</Comment>
+ </UpdateHostedZoneCommentRequest>
+ """)
+ .when().post("/2013-04-01/hostedzone/ZNOPE000000000")
+ .then()
+ .statusCode(404)
+ .body(containsString("NoSuchHostedZone"));
+ }
+
+ @Test
+ @Order(24)
+ void updateHostedZoneComment_rejectsABodyThatIsNotAnUpdateRequest() {
+ String zoneId = createZoneForCommentTest("comment-zone-2", "keep me");
+
+ // A truncated document parses to no root, so it must not read as "no Comment given".
+ given()
+ .contentType(XML)
+ .body("<UpdateHostedZoneCommentRequest><Comment>gone</Comment>")
+ .when().post("/2013-04-01/hostedzone/" + zoneId)
+ .then()
+ .statusCode(400)
+ .body(containsString("InvalidInput"));
+
+ given()
+ .contentType(XML)
+ .body("<DeleteHostedZoneRequest/>")
+ .when().post("/2013-04-01/hostedzone/" + zoneId)
+ .then()
+ .statusCode(400)
+ .body(containsString("InvalidInput"));
+
+ given()
+ .when().get("/2013-04-01/hostedzone/" + zoneId)
+ .then()
+ .statusCode(200)
+ .body("GetHostedZoneResponse.HostedZone.Config.Comment", equalTo("keep me"));
+
+ // Cleanup
+ given().when().delete("/2013-04-01/hostedzone/" + zoneId).then().statusCode(200);
+ }
+
+ private String createZoneForCommentTest(String callerReference, String comment) {
+ String location = given()
+ .contentType(XML)
+ .body("""
+ <?xml version="1.0" encoding="UTF-8"?>
+ <CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
+ <Name>comments.example.com</Name>
+ <CallerReference>%s</CallerReference>
+ <HostedZoneConfig>
+ <Comment>%s</Comment>
+ </HostedZoneConfig>
+ </CreateHostedZoneRequest>
+ """.formatted(callerReference, comment))
+ .when().post("/2013-04-01/hostedzone")
+ .then().statusCode(201).extract().header("Location");
+ return location.substring(location.lastIndexOf('/') + 1);
+ }
}