mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cur): keep report definition tags and serve the tag operations (#8095)
PutReportDefinition now stores its Tags on the report definition, and TagResource, UntagResource and ListTagsForResource read and change them by ReportName instead of answering empty stubs. ModifyReportDefinition keeps the tags, a missing report answers ResourceNotFoundException, and the TagList and TagKeyList constraints from the model are checked. Closes #7952 PutReportDefinition and TagResource now fail with ValidationException when the report would hold more than 50 tags, when Tags is not a list, or when a tag key, value or TagKeys entry is not a string. tools/aws/regen_cur_shapes.py vendors the TagResource, UntagResource and ListTagsForResource input constraints from botocore, and CurRequestShapes checks them, and the Tags of PutReportDefinition, through RequestConstraintCheck in place of the hand-written limits.
This commit is contained in:
1 parent
ca0d166a33
commit
efd8971241
11 files changed
+470
-17
No files matched your search
@@ -13,7 +13,9 @@ import jakarta.ws.rs.core.Response;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* JSON 1.1 handler for AWS CUR operations.
|
||||
@@ -38,19 +40,15 @@ public class CurJsonHandler {
|
||||
|
||||
public Response handle(String action, JsonNode request, String region) {
|
||||
LOG.debugv("CUR action: {0}", action);
|
||||
CurRequestShapes.check(action, request);
|
||||
return switch (action) {
|
||||
case "PutReportDefinition" -> handlePutReportDefinition(request, region);
|
||||
case "ModifyReportDefinition" -> handleModifyReportDefinition(request, region);
|
||||
case "DescribeReportDefinitions" -> handleDescribeReportDefinitions();
|
||||
case "DeleteReportDefinition" -> handleDeleteReportDefinition(request, region);
|
||||
// Tag endpoints are not yet emulated; respond with empty bodies so SDK
|
||||
// clients that probe for them don't trip on UnknownOperationException.
|
||||
case "TagResource", "UntagResource" -> Response.ok(objectMapper.createObjectNode()).build();
|
||||
case "ListTagsForResource" -> {
|
||||
ObjectNode body = objectMapper.createObjectNode();
|
||||
body.putArray("Tags");
|
||||
yield Response.ok(body).build();
|
||||
}
|
||||
case "TagResource" -> handleTagResource(request, region);
|
||||
case "UntagResource" -> handleUntagResource(request, region);
|
||||
case "ListTagsForResource" -> handleListTagsForResource(request, region);
|
||||
default -> Response.status(400)
|
||||
.entity(new AwsErrorResponse("UnknownOperationException",
|
||||
"Unknown operation: AWSOrigamiServiceGatewayService." + action))
|
||||
@@ -59,7 +57,9 @@ public class CurJsonHandler {
|
||||
}
|
||||
|
||||
private Response handlePutReportDefinition(JsonNode request, String region) {
|
||||
CurRequestShapes.checkTags(request.get("Tags"));
|
||||
ReportDefinition def = parseReportDefinition(request.path("ReportDefinition"));
|
||||
def.setTags(parseTags(request.path("Tags")));
|
||||
ReportDefinition created = service.putReportDefinition(def, region);
|
||||
// Emission failures must not roll back the management mutation — match AWS
|
||||
// semantics where the definition is created even if the first run errors.
|
||||
@@ -104,6 +104,37 @@ public class CurJsonHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleTagResource(JsonNode request, String region) {
|
||||
service.tagResource(stringOrNull(request, "ReportName"), parseTags(request.path("Tags")), region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleUntagResource(JsonNode request, String region) {
|
||||
service.untagResource(stringOrNull(request, "ReportName"), parseTagKeys(request.path("TagKeys")), region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleListTagsForResource(JsonNode request, String region) {
|
||||
Map<String, String> tags = service.listTags(stringOrNull(request, "ReportName"), region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode list = response.putArray("Tags");
|
||||
tags.forEach((key, value) -> list.addObject().put("Key", key).put("Value", value));
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
/** The tags of a request checked by {@link CurRequestShapes}; none when it has no {@code Tags}. */
|
||||
private static Map<String, String> parseTags(JsonNode node) {
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
node.forEach(tag -> tags.put(tag.get("Key").textValue(), tag.get("Value").textValue()));
|
||||
return tags;
|
||||
}
|
||||
|
||||
private static List<String> parseTagKeys(JsonNode node) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
node.forEach(key -> keys.add(key.textValue()));
|
||||
return keys;
|
||||
}
|
||||
|
||||
private ReportDefinition parseReportDefinition(JsonNode node) {
|
||||
if (node == null || !node.isObject() || node.isEmpty()) {
|
||||
throw new AwsException("ValidationException",
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package io.floci.aws.services.cur;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.floci.aws.core.common.RequestConstraintCheck;
|
||||
|
||||
/**
|
||||
* Checks the Cost and Usage Reports tag operations, and the {@code Tags} of {@code PutReportDefinition},
|
||||
* against the constraints generated by {@code tools/aws/regen_cur_shapes.py}.
|
||||
*/
|
||||
final class CurRequestShapes {
|
||||
|
||||
private static final RequestConstraintCheck CHECK = RequestConstraintCheck.load(
|
||||
"aws/cur-request-shapes.json", "CUR request shapes",
|
||||
RequestConstraintCheck.standardMessages("ValidationException"));
|
||||
|
||||
private CurRequestShapes() {
|
||||
}
|
||||
|
||||
static void check(String operation, JsonNode request) {
|
||||
if (CHECK.hasOperation(operation)) {
|
||||
CHECK.check(operation, request);
|
||||
}
|
||||
}
|
||||
|
||||
/** Checks a request's {@code Tags} member, when it has one, as a {@code TagList}. */
|
||||
static void checkTags(JsonNode tags) {
|
||||
if (tags != null && !tags.isNull()) {
|
||||
CHECK.checkShape("TagList", tags);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,7 @@ public class CurService {
|
||||
|
||||
/** AWS hard limit per account: 5 report definitions. */
|
||||
private static final int MAX_REPORTS_PER_ACCOUNT = 5;
|
||||
private static final int MAX_TAGS_PER_REPORT = 50;
|
||||
/** AWS allows the report name to contain alphanumerics, hyphens, and underscores. */
|
||||
private static final Pattern REPORT_NAME_PATTERN =
|
||||
Pattern.compile("[A-Za-z0-9_-]+");
|
||||
@@ -76,8 +77,9 @@ public class CurService {
|
||||
}
|
||||
|
||||
/** {@code PutReportDefinition} — creates a new report. Fails if one already exists. */
|
||||
public ReportDefinition putReportDefinition(ReportDefinition definition, String region) {
|
||||
public synchronized ReportDefinition putReportDefinition(ReportDefinition definition, String region) {
|
||||
validateReportDefinition(definition);
|
||||
requireTagLimit(definition.getTags());
|
||||
String key = compositeKey(region, definition.getReportName());
|
||||
|
||||
if (store.get(key).isPresent()) {
|
||||
@@ -101,8 +103,8 @@ public class CurService {
|
||||
return definition;
|
||||
}
|
||||
|
||||
/** {@code ModifyReportDefinition} — replaces an existing report's definition. */
|
||||
public ReportDefinition modifyReportDefinition(String reportName, ReportDefinition incoming, String region) {
|
||||
/** {@code ModifyReportDefinition}: replaces an existing report's definition, keeping its tags. */
|
||||
public synchronized ReportDefinition modifyReportDefinition(String reportName, ReportDefinition incoming, String region) {
|
||||
requireNonEmpty(reportName, "ReportName");
|
||||
if (!reportName.equals(incoming.getReportName())) {
|
||||
throw new AwsException("ValidationException",
|
||||
@@ -116,6 +118,7 @@ public class CurService {
|
||||
"Report " + reportName + " not found.", 400));
|
||||
|
||||
incoming.setCreatedDate(existing.getCreatedDate());
|
||||
incoming.setTags(existing.getTags());
|
||||
incoming.setLastUpdatedDate(Instant.now());
|
||||
incoming.setReportStatus(existing.getReportStatus() == null ? "PENDING" : existing.getReportStatus());
|
||||
incoming.setOwnerAccountId(existing.getOwnerAccountId() != null
|
||||
@@ -179,7 +182,7 @@ public class CurService {
|
||||
* success/error to {@code DescribeReportDefinitions} consumers. Silent
|
||||
* no-op if the report has been deleted in the meantime.
|
||||
*/
|
||||
public void markReportStatus(String reportName, String region, String status) {
|
||||
public synchronized void markReportStatus(String reportName, String region, String status) {
|
||||
if (reportName == null || region == null || status == null) {
|
||||
return;
|
||||
}
|
||||
@@ -197,7 +200,7 @@ public class CurService {
|
||||
* directly under {@code accountId} rather than relying on the request
|
||||
* context.
|
||||
*/
|
||||
public void markReportStatusForAccount(String accountId, String reportName, String region, String status) {
|
||||
public synchronized void markReportStatusForAccount(String accountId, String reportName, String region, String status) {
|
||||
if (accountId == null || reportName == null || region == null || status == null) {
|
||||
return;
|
||||
}
|
||||
@@ -216,8 +219,46 @@ public class CurService {
|
||||
});
|
||||
}
|
||||
|
||||
/** {@code ListTagsForResource}: the tags of the named report definition. */
|
||||
public Map<String, String> listTags(String reportName, String region) {
|
||||
return existingReport(reportName, region).getTags();
|
||||
}
|
||||
|
||||
/** {@code TagResource}: adds the tags, replacing the value of a key the report already has. */
|
||||
public synchronized void tagResource(String reportName, Map<String, String> tags, String region) {
|
||||
ReportDefinition report = existingReport(reportName, region);
|
||||
Map<String, String> merged = new LinkedHashMap<>(report.getTags());
|
||||
merged.putAll(tags);
|
||||
requireTagLimit(merged);
|
||||
report.setTags(merged);
|
||||
store.put(compositeKey(region, reportName), report);
|
||||
}
|
||||
|
||||
/** {@code UntagResource}: removes the keys; a key the report does not have is ignored. */
|
||||
public synchronized void untagResource(String reportName, List<String> tagKeys, String region) {
|
||||
ReportDefinition report = existingReport(reportName, region);
|
||||
Map<String, String> remaining = new LinkedHashMap<>(report.getTags());
|
||||
tagKeys.forEach(remaining::remove);
|
||||
report.setTags(remaining);
|
||||
store.put(compositeKey(region, reportName), report);
|
||||
}
|
||||
|
||||
private static void requireTagLimit(Map<String, String> tags) {
|
||||
if (tags.size() > MAX_TAGS_PER_REPORT) {
|
||||
throw new AwsException("ValidationException",
|
||||
"A report definition can have up to " + MAX_TAGS_PER_REPORT + " tags.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private ReportDefinition existingReport(String reportName, String region) {
|
||||
requireNonEmpty(reportName, "ReportName");
|
||||
return store.get(compositeKey(region, reportName)).orElseThrow(() -> new AwsException(
|
||||
"ResourceNotFoundException",
|
||||
"The specified report (" + reportName + ") in the request doesn't exist.", 400));
|
||||
}
|
||||
|
||||
/** {@code DeleteReportDefinition} — removes a report; idempotent (returns null when absent). */
|
||||
public ReportDefinition deleteReportDefinition(String reportName, String region) {
|
||||
public synchronized ReportDefinition deleteReportDefinition(String reportName, String region) {
|
||||
requireNonEmpty(reportName, "ReportName");
|
||||
String key = compositeKey(region, reportName);
|
||||
ReportDefinition existing = store.get(key).orElse(null);
|
||||
|
||||
@@ -5,7 +5,9 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* AWS CUR (Cost and Usage Report) {@code ReportDefinition} record.
|
||||
@@ -31,6 +33,8 @@ public class ReportDefinition {
|
||||
private Instant createdDate;
|
||||
private Instant lastUpdatedDate;
|
||||
private String reportStatus; // SUCCESS | ERROR | PENDING
|
||||
/** The report definition resource's tags. Not part of the {@code ReportDefinition} wire shape. */
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
/**
|
||||
* Snapshot of the account that owned the request that created this report.
|
||||
* Used by the background emission scheduler to locate the report under the
|
||||
@@ -87,6 +91,9 @@ public class ReportDefinition {
|
||||
public String getReportStatus() { return reportStatus; }
|
||||
public void setReportStatus(String v) { this.reportStatus = v; }
|
||||
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> v) { this.tags = v; }
|
||||
|
||||
public String getOwnerAccountId() { return ownerAccountId; }
|
||||
public void setOwnerAccountId(String v) { this.ownerAccountId = v; }
|
||||
}
|
||||
Reference in new issue
Block a user