Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(rds,docdb,neptune): extract shared DescribeGlobalClusters into RdsFamilyQuerySupport (#3648)
Priamo Germosende27308bd95d
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/RdsFamilyQuerySupport.java b/src/main/java/io/github/hectorvent/floci/core/common/RdsFamilyQuerySupport.javanew file mode 100644index 0000000000..72d2b334af--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/core/common/RdsFamilyQuerySupport.java@@ -0,0 +1,55 @@+package io.github.hectorvent.floci.core.common;++import jakarta.ws.rs.core.MultivaluedMap;+import jakarta.ws.rs.core.Response;+import org.jboss.logging.Logger;++/**+ * Query-protocol operations that RDS, DocumentDB, and Neptune answer identically, since+ * DocumentDB and Neptune both accept the RDS-family wire protocol and claim compatibility for+ * these operations. Real SDKs sign DocumentDB and Neptune requests with the "rds" scope and can+ * land on any of the three handlers, so a correction here reaches all of them at once.+ */+public final class RdsFamilyQuerySupport {++ private RdsFamilyQuerySupport() {+ }++ /**+ * Answers DescribeGlobalClusters the same way for all three services: global clusters are+ * not modeled, so the response is always an empty list, once MaxRecords,+ * GlobalClusterIdentifier, and Marker have been validated the way a live account would.+ *+ * <p>MaxRecords is rejected before the identifier is looked up, and a marker after it, the+ * order a live account applies them in. Naming a specific identifier is a different question+ * from listing none, and AWS errors on it. Filters are accepted without validation, since the+ * answer is empty for every name AWS accepts, and a partial list of accepted names would+ * reject filters a live account allows.+ */+ public static Response handleDescribeGlobalClusters(Logger log, MultivaluedMap<String, String> params) {+ String maxRecords = params.getFirst("MaxRecords");+ if (maxRecords != null && !maxRecords.isBlank()) {+ int max = -1;+ try {+ max = Integer.parseInt(maxRecords.trim());+ } catch (NumberFormatException e) {+ log.debugv("Non-numeric MaxRecords {0} on DescribeGlobalClusters", maxRecords);+ }+ if (max < 20 || max > 100) {+ throw new AwsException("InvalidParameterValue",+ "Invalid value " + maxRecords + " for MaxRecords. Must be between 20 and 100", 400);+ }+ }+ String identifier = params.getFirst("GlobalClusterIdentifier");+ if (identifier != null && !identifier.isBlank()) {+ throw new AwsException("GlobalClusterNotFoundFault",+ "Global cluster '" + identifier + "' not found", 404);+ }+ String marker = params.getFirst("Marker");+ if (marker != null && !marker.isBlank()) {+ throw new AwsException("InvalidParameterValue", "The request token is invalid.", 400);+ }+ String xml = new XmlBuilder().start("GlobalClusters").end("GlobalClusters").build();+ return Response.ok(AwsQueryResponse.envelope("DescribeGlobalClusters", AwsNamespaces.RDS, xml)).build();+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbQueryHandler.javaindex 940649502d..bcc1e40e28 100644--- a/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbQueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbQueryHandler.java@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.AwsNamespaces;import io.github.hectorvent.floci.core.common.AwsQueryResponse;+import io.github.hectorvent.floci.core.common.RdsFamilyQuerySupport;import io.github.hectorvent.floci.core.common.XmlBuilder;import io.github.hectorvent.floci.core.common.BackupWindows;import io.github.hectorvent.floci.services.docdb.model.DocDbCluster;@@ -124,39 +125,7 @@ public class DocDbQueryHandler {}private Response handleDescribeGlobalClusters(MultivaluedMap<String, String> params) {- // Global clusters are not modeled; an empty list is what completes the provider's read.- // Real SDKs sign DocumentDB with the "rds" scope and land on RdsQueryHandler instead;- // this serves the "docdb" scope Floci also accepts, and must answer the same way.- // MaxRecords is rejected before the identifier is looked up, and a marker after it —- // the order a live account applies them in.- String maxRecords = params.getFirst("MaxRecords");- if (maxRecords != null && !maxRecords.isBlank()) {- int max = -1;- try {- max = Integer.parseInt(maxRecords.trim());- } catch (NumberFormatException e) {- LOG.debugv("Non-numeric MaxRecords {0} on DescribeGlobalClusters", maxRecords);- }- if (max < 20 || max > 100) {- throw new AwsException("InvalidParameterValue",- "Invalid value " + maxRecords + " for MaxRecords. Must be between 20 and 100", 400);- }- }- String identifier = params.getFirst("GlobalClusterIdentifier");- if (identifier != null && !identifier.isBlank()) {- // Naming one is a different question from listing none, and AWS errors on it.- throw new AwsException("GlobalClusterNotFoundFault",- "Global cluster '" + identifier + "' not found", 404);- }- // No page is ever handed out, so any marker a caller presents came from somewhere else.- String marker = params.getFirst("Marker");- if (marker != null && !marker.isBlank()) {- throw new AwsException("InvalidParameterValue", "The request token is invalid.", 400);- }- // Filters are not validated: the answer is empty for every name AWS accepts, and a partial- // list of accepted names would reject filters a live account allows.- XmlBuilder xml = new XmlBuilder().start("GlobalClusters").end("GlobalClusters");- return Response.ok(AwsQueryResponse.envelope("DescribeGlobalClusters", AwsNamespaces.RDS, xml.build())).build();+ return RdsFamilyQuerySupport.handleDescribeGlobalClusters(LOG, params);}private Response handleDescribeDbClusterSnapshots(MultivaluedMap<String, String> params) {diff --git a/src/main/java/io/github/hectorvent/floci/services/neptune/NeptuneQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/neptune/NeptuneQueryHandler.javaindex 28cc04db0e..3a2c3029bf 100644--- a/src/main/java/io/github/hectorvent/floci/services/neptune/NeptuneQueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/neptune/NeptuneQueryHandler.java@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.AwsNamespaces;import io.github.hectorvent.floci.core.common.AwsQueryResponse;import io.github.hectorvent.floci.core.common.BackupWindows;+import io.github.hectorvent.floci.core.common.RdsFamilyQuerySupport;import io.github.hectorvent.floci.core.common.XmlBuilder;import io.github.hectorvent.floci.services.neptune.model.NeptuneCluster;import io.github.hectorvent.floci.services.neptune.model.NeptuneClusterSettings;@@ -188,30 +189,7 @@ public class NeptuneQueryHandler {}private Response handleDescribeGlobalClusters(MultivaluedMap<String, String> params) {- String maxRecords = params.getFirst("MaxRecords");- if (maxRecords != null && !maxRecords.isBlank()) {- int max = -1;- try {- max = Integer.parseInt(maxRecords.trim());- } catch (NumberFormatException e) {- LOG.debugv("Non-numeric MaxRecords {0} on DescribeGlobalClusters", maxRecords);- }- if (max < 20 || max > 100) {- throw new AwsException("InvalidParameterValue",- "Invalid value " + maxRecords + " for MaxRecords. Must be between 20 and 100", 400);- }- }- String identifier = params.getFirst("GlobalClusterIdentifier");- if (identifier != null && !identifier.isBlank()) {- throw new AwsException("GlobalClusterNotFoundFault",- "Global cluster '" + identifier + "' not found", 404);- }- String marker = params.getFirst("Marker");- if (marker != null && !marker.isBlank()) {- throw new AwsException("InvalidParameterValue", "The request token is invalid.", 400);- }- XmlBuilder xml = new XmlBuilder().start("GlobalClusters").end("GlobalClusters");- return Response.ok(AwsQueryResponse.envelope("DescribeGlobalClusters", AwsNamespaces.RDS, xml.build())).build();+ return RdsFamilyQuerySupport.handleDescribeGlobalClusters(LOG, params);}// ── Instances ─────────────────────────────────────────────────────────────diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.javaindex 6b18fd9bd6..9ae843bb99 100644--- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.AwsNamespaces;import io.github.hectorvent.floci.core.common.AwsQueryResponse;+import io.github.hectorvent.floci.core.common.RdsFamilyQuerySupport;import io.github.hectorvent.floci.core.common.XmlBuilder;import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;import io.github.hectorvent.floci.services.rds.model.DbCluster;@@ -1411,38 +1412,7 @@ public class RdsQueryHandler {}private Response handleDescribeGlobalClusters(MultivaluedMap<String, String> params) {- // Global clusters are not modeled. Both providers read this on every cluster read, and- // DocumentDB signs with the "rds" scope, so this handler answers for either service.- // MaxRecords is rejected before the identifier is looked up, and a marker after it —- // the order a live account applies them in.- String maxRecords = params.getFirst("MaxRecords");- if (maxRecords != null && !maxRecords.isBlank()) {- int max = -1;- try {- max = Integer.parseInt(maxRecords.trim());- } catch (NumberFormatException e) {- LOG.debugv("Non-numeric MaxRecords {0} on DescribeGlobalClusters", maxRecords);- }- if (max < 20 || max > 100) {- throw new AwsException("InvalidParameterValue",- "Invalid value " + maxRecords + " for MaxRecords. Must be between 20 and 100", 400);- }- }- String identifier = params.getFirst("GlobalClusterIdentifier");- if (identifier != null && !identifier.isBlank()) {- // Naming one is a different question from listing none, and AWS errors on it.- throw new AwsException("GlobalClusterNotFoundFault",- "Global cluster '" + identifier + "' not found", 404);- }- // No page is ever handed out, so any marker a caller presents came from somewhere else.- String marker = params.getFirst("Marker");- if (marker != null && !marker.isBlank()) {- throw new AwsException("InvalidParameterValue", "The request token is invalid.", 400);- }- // Filters are not validated: the answer is empty for every name AWS accepts, and a partial- // list of accepted names would reject filters a live account allows.- String result = new XmlBuilder().start("GlobalClusters").end("GlobalClusters").build();- return Response.ok(AwsQueryResponse.envelope("DescribeGlobalClusters", AwsNamespaces.RDS, result)).build();+ return RdsFamilyQuerySupport.handleDescribeGlobalClusters(LOG, params);}private Response handleDescribeDbClusterSnapshots(MultivaluedMap<String, String> params) {