mirror of
https://github.com/floci-io/floci.git
synced 2026-11-29 00:21:51 -05:00
fix(cloudwatch): paginate ListDashboards results (#7789)
* fix(cloudwatch): paginate ListDashboards results * fix(cloudwatch): reject empty ListDashboards tokens
This commit is contained in:
1 parent
c9fc00a8d5
commit
ddad6ddeb9
4 files changed
+131
-10
No files matched your search
+15
@@ -9,6 +9,8 @@ import com.fasterxml.jackson.databind.ObjectReader;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.AwsRegions;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
@@ -142,6 +144,19 @@ public class CloudWatchDashboardsService {
|
||||
return all;
|
||||
}
|
||||
|
||||
public PaginatedResult<Dashboard> listDashboardsPage(String dashboardNamePrefix,
|
||||
String nextToken, String region) {
|
||||
if (nextToken != null && nextToken.isEmpty()) {
|
||||
throw new AwsException("InvalidParameterValue", "Invalid NextToken.", 400);
|
||||
}
|
||||
String prefix = dashboardNamePrefix == null || dashboardNamePrefix.isBlank()
|
||||
? "" : dashboardNamePrefix;
|
||||
String scope = "cloudwatch-dashboards:" + regionResolver.getAccountId() + ":" + region + ":" + prefix;
|
||||
return Pagination.paginate(listDashboards(prefix, region), Dashboard::getDashboardName,
|
||||
1000, nextToken, scope,
|
||||
token -> new AwsException("InvalidParameterValue", "Invalid NextToken.", 400));
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes as many of the named dashboards as it can, then reports the first name that was not
|
||||
* there. AWS documents this as best effort, verbatim: "If there is an error during this call,
|
||||
|
||||
+6
-2
@@ -621,17 +621,21 @@ public class CloudWatchMetricsJsonHandler {
|
||||
private Response handleListDashboards(JsonNode request, String region) {
|
||||
String prefix = request.has("DashboardNamePrefix")
|
||||
? request.path("DashboardNamePrefix").asText() : null;
|
||||
List<Dashboard> dashboards = dashboardsService.listDashboards(prefix, region);
|
||||
PaginatedResult<Dashboard> page = dashboardsService.listDashboardsPage(prefix,
|
||||
Pagination.parseNextToken(request, "NextToken", "InvalidParameterValue"), region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode entries = response.putArray("DashboardEntries");
|
||||
for (Dashboard d : dashboards) {
|
||||
for (Dashboard d : page.items()) {
|
||||
ObjectNode node = entries.addObject();
|
||||
node.put("DashboardName", d.getDashboardName());
|
||||
node.put("DashboardArn", d.getDashboardArn());
|
||||
node.put("LastModified", d.getLastModified());
|
||||
node.put("Size", d.getSize());
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
|
||||
+6
-3
@@ -456,12 +456,12 @@ public class CloudWatchMetricsQueryHandler {
|
||||
}
|
||||
|
||||
private Response handleListDashboards(MultivaluedMap<String, String> params, String region) {
|
||||
List<Dashboard> dashboards =
|
||||
dashboardsService.listDashboards(params.getFirst("DashboardNamePrefix"), region);
|
||||
PaginatedResult<Dashboard> page = dashboardsService.listDashboardsPage(
|
||||
params.getFirst("DashboardNamePrefix"), params.getFirst("NextToken"), region);
|
||||
|
||||
DateTimeFormatter fmt = DateTimeFormatter.ISO_INSTANT;
|
||||
XmlBuilder xml = new XmlBuilder().start("DashboardEntries");
|
||||
for (Dashboard d : dashboards) {
|
||||
for (Dashboard d : page.items()) {
|
||||
xml.start("member")
|
||||
.elem("DashboardName", d.getDashboardName())
|
||||
.elem("DashboardArn", d.getDashboardArn())
|
||||
@@ -470,6 +470,9 @@ public class CloudWatchMetricsQueryHandler {
|
||||
.end("member");
|
||||
}
|
||||
xml.end("DashboardEntries");
|
||||
if (page.nextToken() != null) {
|
||||
xml.elem("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(AwsQueryResponse.envelope("ListDashboards", null, xml.build())).build();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user