mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat: DynamoDB support ReturnValuesOnConditionCheckFailure (#505)
This commit is contained in:
1 parent
7f82a28abb
commit
9b5f364e47
8 files changed
+392
-35
No files matched your search
@@ -0,0 +1,10 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
public record AwsErrorResponseWithItem(@JsonProperty("__type") String type, @JsonProperty("message") String message,
|
||||
@JsonProperty("Item") JsonNode item) {
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.ConditionalCheckFailedException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
@@ -9,22 +11,32 @@ public class JsonErrorResponseUtils {
|
||||
}
|
||||
|
||||
public static Response createErrorResponse(Exception e) {
|
||||
return JsonErrorResponseUtils.createErrorResponse(500, "InternalFailure", "InternalFailure", e.getMessage());
|
||||
return JsonErrorResponseUtils.createErrorResponse(500, "InternalFailure", "InternalFailure", e.getMessage(), null);
|
||||
}
|
||||
|
||||
public static Response createErrorResponse(AwsException e) {
|
||||
return createErrorResponse(e.getHttpStatus(), e.getErrorCode(), e.jsonType(), e.getMessage());
|
||||
JsonNode item = null;
|
||||
if (e instanceof ConditionalCheckFailedException){
|
||||
item = ((ConditionalCheckFailedException) e).getItem();
|
||||
}
|
||||
return createErrorResponse(e.getHttpStatus(), e.getErrorCode(), e.jsonType(), e.getMessage(), item);
|
||||
}
|
||||
|
||||
public static Response createUnknownOperationErrorResponse(String target) {
|
||||
return createErrorResponse(404,
|
||||
"UnknownOperationException",
|
||||
"UnknownOperationException",
|
||||
"Unknown operation: " + target);
|
||||
"Unknown operation: " + target, null);
|
||||
}
|
||||
|
||||
public static Response createErrorResponse(int httpStatusCode, String queryError, String errorType, String errorMessage) {
|
||||
public static Response createErrorResponse(int httpStatusCode, String queryError, String errorType, String errorMessage, JsonNode item) {
|
||||
String queryErrorFault = (httpStatusCode < 500) ? "Sender" : "Receiver";
|
||||
if (item != null) {
|
||||
return Response.status(httpStatusCode)
|
||||
.header("x-amzn-query-error", queryError + ";" + queryErrorFault)
|
||||
.entity(new AwsErrorResponseWithItem(errorType, errorMessage, item))
|
||||
.build();
|
||||
}
|
||||
return Response.status(httpStatusCode)
|
||||
.header("x-amzn-query-error", queryError + ";" + queryErrorFault)
|
||||
.entity(new AwsErrorResponse(errorType, errorMessage))
|
||||
|
||||
@@ -207,6 +207,7 @@ public class DynamoDbJsonHandler {
|
||||
String tableName = request.path("TableName").asText();
|
||||
JsonNode item = request.path("Item");
|
||||
String returnValues = request.path("ReturnValues").asText("NONE");
|
||||
String returnValuesOnConditionCheckFailure = request.path("ReturnValuesOnConditionCheckFailure").asText("NONE");
|
||||
String conditionExpression = request.has("ConditionExpression")
|
||||
? request.get("ConditionExpression").asText() : null;
|
||||
JsonNode exprAttrNames = request.has("ExpressionAttributeNames")
|
||||
@@ -220,7 +221,7 @@ public class DynamoDbJsonHandler {
|
||||
oldItem = dynamoDbService.getItem(tableName, item, region);
|
||||
}
|
||||
|
||||
dynamoDbService.putItem(tableName, item, conditionExpression, exprAttrNames, exprAttrValues, region);
|
||||
dynamoDbService.putItem(tableName, item, conditionExpression, exprAttrNames, exprAttrValues, region, returnValuesOnConditionCheckFailure);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
if ("ALL_OLD" .equals(returnValues) && oldItem != null) {
|
||||
@@ -247,7 +248,8 @@ public class DynamoDbJsonHandler {
|
||||
private Response handleDeleteItem(JsonNode request, String region) {
|
||||
String tableName = request.path("TableName").asText();
|
||||
JsonNode key = request.path("Key");
|
||||
String returnValues = request.path("ReturnValues").asText("NONE");
|
||||
String returnValues = request.path("ReturnValues").asText("NONE");
|
||||
String returnValuesOnConditionCheckFailure = request.path("ReturnValuesOnConditionCheckFailure").asText("NONE");
|
||||
String conditionExpression = request.has("ConditionExpression")
|
||||
? request.get("ConditionExpression").asText() : null;
|
||||
JsonNode exprAttrNames = request.has("ExpressionAttributeNames")
|
||||
@@ -256,7 +258,7 @@ public class DynamoDbJsonHandler {
|
||||
? request.get("ExpressionAttributeValues") : null;
|
||||
|
||||
JsonNode oldItem = dynamoDbService.deleteItem(tableName, key, conditionExpression,
|
||||
exprAttrNames, exprAttrValues, region);
|
||||
exprAttrNames, exprAttrValues, region, returnValuesOnConditionCheckFailure);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
if ("ALL_OLD" .equals(returnValues) && oldItem != null) {
|
||||
@@ -279,12 +281,13 @@ public class DynamoDbJsonHandler {
|
||||
String conditionExpression = request.has("ConditionExpression")
|
||||
? request.get("ConditionExpression").asText() : null;
|
||||
String returnValues = request.path("ReturnValues").asText("NONE");
|
||||
String returnValuesOnConditionCheckFailure = request.path("ReturnValuesOnConditionCheckFailure").asText("NONE");
|
||||
|
||||
JsonNode updateData = attributeUpdates.isMissingNode() ? null : attributeUpdates;
|
||||
|
||||
DynamoDbService.UpdateResult result = dynamoDbService.updateItem(
|
||||
tableName, key, updateData, updateExpression, exprAttrNames, exprAttrValues,
|
||||
returnValues, conditionExpression, region);
|
||||
returnValues, conditionExpression, region, returnValuesOnConditionCheckFailure);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
if ("ALL_NEW" .equals(returnValues) && result.newItem() != null) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.LocalSecondaryIndex;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.ConditionalCheckFailedException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
@@ -221,17 +222,17 @@ public class DynamoDbService {
|
||||
}
|
||||
|
||||
public void putItem(String tableName, JsonNode item) {
|
||||
putItem(tableName, item, null, null, null, regionResolver.getDefaultRegion());
|
||||
putItem(tableName, item, null, null, null, regionResolver.getDefaultRegion(), "NONE");
|
||||
}
|
||||
|
||||
public void putItem(String tableName, JsonNode item, String region) {
|
||||
putItem(tableName, item, null, null, null, region);
|
||||
putItem(tableName, item, null, null, null, region, "NONE");
|
||||
}
|
||||
|
||||
public void putItem(String tableName, JsonNode item,
|
||||
String conditionExpression,
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues,
|
||||
String region) {
|
||||
String region, String returnValuesOnConditionCheckFailure) {
|
||||
String storageKey = regionKey(region, tableName);
|
||||
TableDefinition table = tableStore.get(storageKey)
|
||||
.orElseThrow(() -> resourceNotFoundException(tableName));
|
||||
@@ -242,7 +243,7 @@ public class DynamoDbService {
|
||||
JsonNode existing = tableItems.get(itemKey);
|
||||
|
||||
if (conditionExpression != null) {
|
||||
evaluateCondition(existing, conditionExpression, exprAttrNames, exprAttrValues);
|
||||
evaluateCondition(existing, conditionExpression, exprAttrNames, exprAttrValues, returnValuesOnConditionCheckFailure);
|
||||
}
|
||||
|
||||
tableItems.put(itemKey, item);
|
||||
@@ -276,17 +277,17 @@ public class DynamoDbService {
|
||||
}
|
||||
|
||||
public JsonNode deleteItem(String tableName, JsonNode key) {
|
||||
return deleteItem(tableName, key, null, null, null, regionResolver.getDefaultRegion());
|
||||
return deleteItem(tableName, key, null, null, null, regionResolver.getDefaultRegion(), "NONE");
|
||||
}
|
||||
|
||||
public JsonNode deleteItem(String tableName, JsonNode key, String region) {
|
||||
return deleteItem(tableName, key, null, null, null, region);
|
||||
return deleteItem(tableName, key, null, null, null, region, "NONE");
|
||||
}
|
||||
|
||||
public JsonNode deleteItem(String tableName, JsonNode key,
|
||||
String conditionExpression,
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues,
|
||||
String region) {
|
||||
String region, String returnValuesOnConditionCheckFailure) {
|
||||
String storageKey = regionKey(region, tableName);
|
||||
TableDefinition table = tableStore.get(storageKey)
|
||||
.orElseThrow(() -> resourceNotFoundException(tableName));
|
||||
@@ -297,7 +298,7 @@ public class DynamoDbService {
|
||||
|
||||
if (conditionExpression != null) {
|
||||
JsonNode existing = items.get(itemKey);
|
||||
evaluateCondition(existing, conditionExpression, exprAttrNames, exprAttrValues);
|
||||
evaluateCondition(existing, conditionExpression, exprAttrNames, exprAttrValues, returnValuesOnConditionCheckFailure);
|
||||
}
|
||||
|
||||
JsonNode removed = items.remove(itemKey);
|
||||
@@ -317,11 +318,11 @@ public class DynamoDbService {
|
||||
}
|
||||
|
||||
public UpdateResult updateItem(String tableName, JsonNode key, JsonNode attributeUpdates,
|
||||
String updateExpression,
|
||||
JsonNode expressionAttrNames, JsonNode expressionAttrValues,
|
||||
String returnValues) {
|
||||
String updateExpression,
|
||||
JsonNode expressionAttrNames, JsonNode expressionAttrValues,
|
||||
String returnValues) {
|
||||
return updateItem(tableName, key, attributeUpdates, updateExpression, expressionAttrNames,
|
||||
expressionAttrValues, returnValues, null, regionResolver.getDefaultRegion());
|
||||
expressionAttrValues, returnValues, null, regionResolver.getDefaultRegion(), "NONE");
|
||||
}
|
||||
|
||||
public UpdateResult updateItem(String tableName, JsonNode key, JsonNode attributeUpdates,
|
||||
@@ -329,13 +330,14 @@ public class DynamoDbService {
|
||||
JsonNode expressionAttrNames, JsonNode expressionAttrValues,
|
||||
String returnValues, String region) {
|
||||
return updateItem(tableName, key, attributeUpdates, updateExpression, expressionAttrNames,
|
||||
expressionAttrValues, returnValues, null, region);
|
||||
expressionAttrValues, returnValues, null, region, "NONE");
|
||||
}
|
||||
|
||||
public UpdateResult updateItem(String tableName, JsonNode key, JsonNode attributeUpdates,
|
||||
String updateExpression,
|
||||
JsonNode expressionAttrNames, JsonNode expressionAttrValues,
|
||||
String returnValues, String conditionExpression, String region) {
|
||||
String returnValues, String conditionExpression, String region,
|
||||
String returnValuesOnConditionCheckFailure) {
|
||||
String storageKey = regionKey(region, tableName);
|
||||
TableDefinition table = tableStore.get(storageKey)
|
||||
.orElseThrow(() -> resourceNotFoundException(tableName));
|
||||
@@ -347,7 +349,7 @@ public class DynamoDbService {
|
||||
JsonNode existing = items.get(itemKey);
|
||||
|
||||
if (conditionExpression != null) {
|
||||
evaluateCondition(existing, conditionExpression, expressionAttrNames, expressionAttrValues);
|
||||
evaluateCondition(existing, conditionExpression, expressionAttrNames, expressionAttrValues, returnValuesOnConditionCheckFailure);
|
||||
}
|
||||
|
||||
ObjectNode item;
|
||||
@@ -684,8 +686,9 @@ public class DynamoDbService {
|
||||
String updateExpression = upd.has("UpdateExpression") ? upd.get("UpdateExpression").asText() : null;
|
||||
JsonNode exprAttrNames = upd.has("ExpressionAttributeNames") ? upd.get("ExpressionAttributeNames") : null;
|
||||
JsonNode exprAttrValues = upd.has("ExpressionAttributeValues") ? upd.get("ExpressionAttributeValues") : null;
|
||||
//there is no ConditionExpression, so setting returnValuesOnConditionCheckFailure = "NONE"
|
||||
updateItem(tableName, key, null, updateExpression, exprAttrNames, exprAttrValues,
|
||||
"NONE", null, region);
|
||||
"NONE", null, region, "NONE");
|
||||
}
|
||||
// ConditionCheck-only items are handled in the first pass only
|
||||
}
|
||||
@@ -710,6 +713,8 @@ public class DynamoDbService {
|
||||
if (conditionExpression == null) {
|
||||
return null;
|
||||
}
|
||||
String returnValuesOnConditionCheckFailure = target.has("ReturnValuesOnConditionCheckFailure")
|
||||
? target.get("ReturnValuesOnConditionCheckFailure").asText() : null;
|
||||
|
||||
String tableName = target.path("TableName").asText();
|
||||
JsonNode key = transactItem.has("Put") ? target.get("Item") : target.get("Key");
|
||||
@@ -725,7 +730,7 @@ public class DynamoDbService {
|
||||
JsonNode existing = tableItems != null ? tableItems.get(itemKey) : null;
|
||||
|
||||
try {
|
||||
evaluateCondition(existing, conditionExpression, exprAttrNames, exprAttrValues);
|
||||
evaluateCondition(existing, conditionExpression, exprAttrNames, exprAttrValues, returnValuesOnConditionCheckFailure);
|
||||
return null;
|
||||
} catch (AwsException e) {
|
||||
return e.getMessage();
|
||||
@@ -908,10 +913,14 @@ public class DynamoDbService {
|
||||
// --- Condition expression evaluation ---
|
||||
|
||||
private void evaluateCondition(JsonNode existingItem, String conditionExpression,
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues) {
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues, String returnValuesOnConditionCheckFailure) {
|
||||
if (!matchesFilterExpression(existingItem, conditionExpression, exprAttrNames, exprAttrValues)) {
|
||||
throw new AwsException("ConditionalCheckFailedException",
|
||||
"The conditional request failed", 400);
|
||||
if ("ALL_OLD".equals(returnValuesOnConditionCheckFailure)){
|
||||
throw new ConditionalCheckFailedException(existingItem);
|
||||
}
|
||||
else {
|
||||
throw new ConditionalCheckFailedException(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb.model;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class ConditionalCheckFailedException extends io.github.hectorvent.floci.core.common.AwsException {
|
||||
|
||||
private final JsonNode item;
|
||||
|
||||
public ConditionalCheckFailedException(JsonNode Item) {
|
||||
super("ConditionalCheckFailedException", "The conditional request failed", 400);
|
||||
this.item = Item;
|
||||
}
|
||||
|
||||
public JsonNode getItem() {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
@@ -464,7 +464,7 @@ public class AslExecutor {
|
||||
? input.get("ExpressionAttributeNames") : null;
|
||||
JsonNode exprAttrValues = input.has("ExpressionAttributeValues")
|
||||
? input.get("ExpressionAttributeValues") : null;
|
||||
dynamoDbService.putItem(tableName, item, conditionExpr, exprAttrNames, exprAttrValues, region);
|
||||
dynamoDbService.putItem(tableName, item, conditionExpr, exprAttrNames, exprAttrValues, region, "NONE");
|
||||
return objectMapper.createObjectNode();
|
||||
}
|
||||
case "getItem" -> {
|
||||
@@ -484,7 +484,7 @@ public class AslExecutor {
|
||||
? input.get("ExpressionAttributeNames") : null;
|
||||
JsonNode exprAttrValues = input.has("ExpressionAttributeValues")
|
||||
? input.get("ExpressionAttributeValues") : null;
|
||||
dynamoDbService.deleteItem(tableName, key, conditionExpr, exprAttrNames, exprAttrValues, region);
|
||||
dynamoDbService.deleteItem(tableName, key, conditionExpr, exprAttrNames, exprAttrValues, region, "NONE");
|
||||
return objectMapper.createObjectNode();
|
||||
}
|
||||
case "scan" -> {
|
||||
@@ -523,7 +523,7 @@ public class AslExecutor {
|
||||
DynamoDbService.UpdateResult result = dynamoDbService.updateItem(
|
||||
tableName, key, attributeUpdates, updateExpression,
|
||||
exprAttrNames, exprAttrValues, returnValues,
|
||||
conditionExpression, region);
|
||||
conditionExpression, region, "NONE");
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
if ("ALL_NEW".equals(returnValues) && result.newItem() != null) {
|
||||
|
||||
Reference in new issue
Block a user