mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix: DynamoDB table creation compatibility with Terraform AWS provider v6 (#89)
Hey @kurtmc, you're amazing! Thank you so much --- The Terraform AWS provider v6 calls waitTableWarmThroughputActive after creating a DynamoDB table, which checks DescribeTable for WarmThroughput.Status == "ACTIVE". Without this field the waiter retried for ~2.5 minutes then failed with "couldn't find resource (21 retries)". - Add WarmThroughput with Status ACTIVE to DescribeTable/CreateTable responses - Add BillingModeSummary for PAY_PER_REQUEST tables - Parse BillingMode and Tags from CreateTable/UpdateTable requests - Add DeletionProtectionEnabled field to table responses - Extend CBOR protocol handlers to support DynamoDB, SQS, SNS, StepFunctions
This commit is contained in:
1 parent
f82b370ab2
commit
7b87bf2c1f
4 files changed
+304
-37
No files matched your search
@@ -195,8 +195,9 @@ public class AwsJsonController {
|
||||
|
||||
/**
|
||||
* Handles AWS smithy-rpc-v2-cbor protocol requests.
|
||||
* Go SDK CloudWatch v1.55.1+ sends to POST /service/{serviceId}/operation/{op}
|
||||
* AWS SDK v2 sends to POST /service/{sdkId}/operation/{op}
|
||||
* with Content-Type: application/cbor and no X-Amz-Target header.
|
||||
* Supported services: DynamoDB, SQS, SNS, StepFunctions, CloudWatch.
|
||||
*/
|
||||
@POST
|
||||
@Path("service/{serviceId}/operation/{operation}")
|
||||
@@ -208,11 +209,7 @@ public class AwsJsonController {
|
||||
@Context HttpHeaders httpHeaders,
|
||||
byte[] body) {
|
||||
|
||||
if (!CLOUDWATCH_SERVICE_ID.equals(serviceId)) {
|
||||
return Response.status(404).build();
|
||||
}
|
||||
|
||||
LOG.debugv("CloudWatch RPC v2 CBOR action: {0}", operation);
|
||||
LOG.debugv("Smithy RPC v2 CBOR: service={0}, operation={1}", serviceId, operation);
|
||||
|
||||
try {
|
||||
JsonNode request = (body != null && body.length > 0)
|
||||
@@ -220,28 +217,21 @@ public class AwsJsonController {
|
||||
: objectMapper.createObjectNode();
|
||||
String region = regionResolver.resolveRegion(httpHeaders);
|
||||
|
||||
Response delegated = cloudWatchMetricsJsonHandler.handle(operation, request, region);
|
||||
byte[] cborBytes = nodeToSmithyCbor((JsonNode) delegated.getEntity());
|
||||
Response delegated = dispatchCbor(serviceId, operation, request, region);
|
||||
if (delegated == null) {
|
||||
return Response.status(404).build();
|
||||
}
|
||||
|
||||
byte[] cborBytes = nodeToSmithyCbor((JsonNode) delegated.getEntity());
|
||||
return Response.status(delegated.getStatus())
|
||||
.header("Smithy-Protocol", "rpc-v2-cbor")
|
||||
.type("application/cbor")
|
||||
.entity(cborBytes)
|
||||
.build();
|
||||
} catch (AwsException e) {
|
||||
try {
|
||||
byte[] errBytes = CBOR_MAPPER.writeValueAsBytes(
|
||||
new AwsErrorResponse(e.getErrorCode(), e.getMessage()));
|
||||
return Response.status(e.getHttpStatus())
|
||||
.header("Smithy-Protocol", "rpc-v2-cbor")
|
||||
.type("application/cbor")
|
||||
.entity(errBytes)
|
||||
.build();
|
||||
} catch (Exception ex) {
|
||||
return Response.status(e.getHttpStatus()).build();
|
||||
}
|
||||
return cborErrorResponse(e, "Smithy-Protocol");
|
||||
} catch (Exception e) {
|
||||
LOG.error("Error processing CloudWatch CBOR request", e);
|
||||
LOG.error("Error processing Smithy CBOR request: " + serviceId + "." + operation, e);
|
||||
return Response.status(500).build();
|
||||
}
|
||||
}
|
||||
@@ -258,12 +248,37 @@ public class AwsJsonController {
|
||||
@Context HttpHeaders httpHeaders,
|
||||
byte[] body) {
|
||||
|
||||
if (target == null || !target.startsWith(CLOUDWATCH_TARGET_PREFIX)) {
|
||||
if (target == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String action = target.substring(CLOUDWATCH_TARGET_PREFIX.length());
|
||||
LOG.debugv("CloudWatch CBOR action: {0}", action);
|
||||
String prefix;
|
||||
String serviceName;
|
||||
|
||||
if (target.startsWith(DYNAMODB_STREAMS_TARGET_PREFIX)) {
|
||||
prefix = DYNAMODB_STREAMS_TARGET_PREFIX;
|
||||
serviceName = "DynamoDBStreams";
|
||||
} else if (target.startsWith(DYNAMODB_TARGET_PREFIX)) {
|
||||
prefix = DYNAMODB_TARGET_PREFIX;
|
||||
serviceName = "DynamoDB";
|
||||
} else if (target.startsWith(SQS_TARGET_PREFIX)) {
|
||||
prefix = SQS_TARGET_PREFIX;
|
||||
serviceName = "SQS";
|
||||
} else if (target.startsWith(SNS_TARGET_PREFIX)) {
|
||||
prefix = SNS_TARGET_PREFIX;
|
||||
serviceName = "SNS";
|
||||
} else if (target.startsWith(STEPFUNCTIONS_TARGET_PREFIX)) {
|
||||
prefix = STEPFUNCTIONS_TARGET_PREFIX;
|
||||
serviceName = "StepFunctions";
|
||||
} else if (target.startsWith(CLOUDWATCH_TARGET_PREFIX)) {
|
||||
prefix = CLOUDWATCH_TARGET_PREFIX;
|
||||
serviceName = "CloudWatch";
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
String action = target.substring(prefix.length());
|
||||
LOG.debugv("{0} CBOR action: {1}", serviceName, action);
|
||||
|
||||
try {
|
||||
JsonNode request = (body != null && body.length > 0)
|
||||
@@ -271,29 +286,57 @@ public class AwsJsonController {
|
||||
: objectMapper.createObjectNode();
|
||||
String region = regionResolver.resolveRegion(httpHeaders);
|
||||
|
||||
Response delegated = cloudWatchMetricsJsonHandler.handle(action, request, region);
|
||||
byte[] cborBytes = nodeToSmithyCbor((JsonNode) delegated.getEntity());
|
||||
Response delegated = switch (serviceName) {
|
||||
case "DynamoDB" -> dynamoDbJsonHandler.handle(action, request, region);
|
||||
case "DynamoDBStreams" -> dynamoDbStreamsJsonHandler.handle(action, request, region);
|
||||
case "SQS" -> sqsJsonHandler.handle(action, request, region);
|
||||
case "SNS" -> snsJsonHandler.handle(action, request, region);
|
||||
case "StepFunctions" -> sfnJsonHandler.handle(action, request, region);
|
||||
case "CloudWatch" -> cloudWatchMetricsJsonHandler.handle(action, request, region);
|
||||
default -> null;
|
||||
};
|
||||
if (delegated == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] cborBytes = nodeToSmithyCbor((JsonNode) delegated.getEntity());
|
||||
return Response.status(delegated.getStatus())
|
||||
.header("smithy-protocol", "rpc-v2-cbor")
|
||||
.type("application/cbor")
|
||||
.entity(cborBytes)
|
||||
.build();
|
||||
} catch (AwsException e) {
|
||||
try {
|
||||
byte[] errBytes = CBOR_MAPPER.writeValueAsBytes(
|
||||
new AwsErrorResponse(e.getErrorCode(), e.getMessage()));
|
||||
return Response.status(e.getHttpStatus())
|
||||
.header("smithy-protocol", "rpc-v2-cbor")
|
||||
.type("application/cbor")
|
||||
.entity(errBytes)
|
||||
.build();
|
||||
} catch (Exception ex) {
|
||||
return Response.status(e.getHttpStatus()).build();
|
||||
}
|
||||
return cborErrorResponse(e, "smithy-protocol");
|
||||
} catch (Exception e) {
|
||||
LOG.error("Error processing CloudWatch CBOR request", e);
|
||||
LOG.error("Error processing CBOR request: " + serviceName + "." + action, e);
|
||||
return Response.status(500).build();
|
||||
}
|
||||
}
|
||||
|
||||
/** Dispatches a CBOR request to the appropriate service handler by SDK service ID. */
|
||||
private Response dispatchCbor(String serviceId, String operation, JsonNode request, String region) throws Exception {
|
||||
return switch (serviceId) {
|
||||
case "DynamoDB" -> dynamoDbJsonHandler.handle(operation, request, region);
|
||||
case "DynamoDB Streams" -> dynamoDbStreamsJsonHandler.handle(operation, request, region);
|
||||
case "SQS" -> sqsJsonHandler.handle(operation, request, region);
|
||||
case "SNS" -> snsJsonHandler.handle(operation, request, region);
|
||||
case "SFN" -> sfnJsonHandler.handle(operation, request, region);
|
||||
case CLOUDWATCH_SERVICE_ID -> cloudWatchMetricsJsonHandler.handle(operation, request, region);
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
private Response cborErrorResponse(AwsException e, String protocolHeader) {
|
||||
try {
|
||||
byte[] errBytes = CBOR_MAPPER.writeValueAsBytes(
|
||||
new AwsErrorResponse(e.getErrorCode(), e.getMessage()));
|
||||
return Response.status(e.getHttpStatus())
|
||||
.header(protocolHeader, "rpc-v2-cbor")
|
||||
.type("application/cbor")
|
||||
.entity(errBytes)
|
||||
.build();
|
||||
} catch (Exception ex) {
|
||||
return Response.status(e.getHttpStatus()).build();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -114,9 +114,28 @@ public class DynamoDbJsonHandler {
|
||||
}
|
||||
}
|
||||
|
||||
String billingMode = request.has("BillingMode")
|
||||
? request.get("BillingMode").asText() : null;
|
||||
|
||||
TableDefinition table = dynamoDbService.createTable(tableName, keySchema, attrDefs,
|
||||
readCapacity, writeCapacity, gsis, lsis, region);
|
||||
|
||||
if ("PAY_PER_REQUEST".equals(billingMode)) {
|
||||
table.setBillingMode("PAY_PER_REQUEST");
|
||||
table.getProvisionedThroughput().setReadCapacityUnits(0L);
|
||||
table.getProvisionedThroughput().setWriteCapacityUnits(0L);
|
||||
} else {
|
||||
table.setBillingMode("PROVISIONED");
|
||||
}
|
||||
|
||||
// Store tags from CreateTable request
|
||||
JsonNode tagsNode = request.path("Tags");
|
||||
if (tagsNode.isArray()) {
|
||||
for (JsonNode tag : tagsNode) {
|
||||
table.getTags().put(tag.path("Key").asText(), tag.path("Value").asText());
|
||||
}
|
||||
}
|
||||
|
||||
JsonNode streamSpec = request.path("StreamSpecification");
|
||||
if (!streamSpec.isMissingNode() && streamSpec.path("StreamEnabled").asBoolean(false)) {
|
||||
String viewType = streamSpec.path("StreamViewType").asText("NEW_AND_OLD_IMAGES");
|
||||
@@ -375,6 +394,16 @@ public class DynamoDbJsonHandler {
|
||||
|
||||
TableDefinition table = dynamoDbService.updateTable(tableName, readCapacity, writeCapacity, region);
|
||||
|
||||
String billingMode = request.has("BillingMode")
|
||||
? request.get("BillingMode").asText() : null;
|
||||
if (billingMode != null) {
|
||||
table.setBillingMode(billingMode);
|
||||
if ("PAY_PER_REQUEST".equals(billingMode)) {
|
||||
table.getProvisionedThroughput().setReadCapacityUnits(0L);
|
||||
table.getProvisionedThroughput().setWriteCapacityUnits(0L);
|
||||
}
|
||||
}
|
||||
|
||||
JsonNode streamSpec = request.path("StreamSpecification");
|
||||
if (!streamSpec.isMissingNode()) {
|
||||
boolean streamEnabled = streamSpec.path("StreamEnabled").asBoolean(false);
|
||||
@@ -525,6 +554,21 @@ public class DynamoDbJsonHandler {
|
||||
node.put("CreationDateTime", table.getCreationDateTime().getEpochSecond());
|
||||
node.put("ItemCount", table.getItemCount());
|
||||
node.put("TableSizeBytes", table.getTableSizeBytes());
|
||||
node.put("DeletionProtectionEnabled", false);
|
||||
|
||||
if ("PAY_PER_REQUEST".equals(table.getBillingMode())) {
|
||||
ObjectNode billing = objectMapper.createObjectNode();
|
||||
billing.put("BillingMode", "PAY_PER_REQUEST");
|
||||
billing.put("LastUpdateToPayPerRequestDateTime",
|
||||
table.getCreationDateTime().getEpochSecond());
|
||||
node.set("BillingModeSummary", billing);
|
||||
}
|
||||
|
||||
ObjectNode warmThroughput = objectMapper.createObjectNode();
|
||||
warmThroughput.put("Status", "ACTIVE");
|
||||
warmThroughput.put("ReadUnitsPerSecond", 0);
|
||||
warmThroughput.put("WriteUnitsPerSecond", 0);
|
||||
node.set("WarmThroughput", warmThroughput);
|
||||
|
||||
ArrayNode keySchemaArray = objectMapper.createArrayNode();
|
||||
for (var ks : table.getKeySchema()) {
|
||||
|
||||
@@ -29,6 +29,7 @@ public class TableDefinition {
|
||||
private Map<String, String> tags;
|
||||
private List<GlobalSecondaryIndex> globalSecondaryIndexes;
|
||||
private List<LocalSecondaryIndex> localSecondaryIndexes;
|
||||
private String billingMode; // "PROVISIONED" or "PAY_PER_REQUEST"
|
||||
private String ttlAttributeName;
|
||||
private boolean ttlEnabled;
|
||||
private boolean streamEnabled;
|
||||
@@ -107,6 +108,9 @@ public class TableDefinition {
|
||||
this.localSecondaryIndexes = localSecondaryIndexes != null ? localSecondaryIndexes : new ArrayList<>();
|
||||
}
|
||||
|
||||
public String getBillingMode() { return billingMode; }
|
||||
public void setBillingMode(String billingMode) { this.billingMode = billingMode; }
|
||||
|
||||
public String getTtlAttributeName() { return ttlAttributeName; }
|
||||
public void setTtlAttributeName(String ttlAttributeName) { this.ttlAttributeName = ttlAttributeName; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user