fix(servicequotas): filter ListServiceQuotas by QuotaAppliedAtLevel (#7238)

This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-11-02 15:08:13 -04:00
1 parent d44e96eab8
commit be696ff51d
4 files changed
+114 -16

No files matched your search

@@ -53,9 +53,12 @@ public class ServiceQuotasJsonHandler {
String quotaCode = stringOrNull(request, "QuotaCode");
String nextToken = stringOrNull(request, "NextToken");
Integer maxResults = integerOrNull(request, "MaxResults");
return Response.ok(awsDefault
? service.listAwsDefaultServiceQuotas(serviceCode, quotaCode, nextToken, maxResults, region, accountId)
: service.listServiceQuotas(serviceCode, quotaCode, nextToken, maxResults, region, accountId)).build();
if (awsDefault) {
// ListAWSDefaultServiceQuotas does not have QuotaAppliedAtLevel in the service model
return Response.ok(service.listAwsDefaultServiceQuotas(serviceCode, quotaCode, nextToken, maxResults, region, accountId)).build();
}
String appliedAtLevel = stringOrNull(request, "QuotaAppliedAtLevel");
return Response.ok(service.listServiceQuotas(serviceCode, quotaCode, appliedAtLevel, nextToken, maxResults, region, accountId)).build();
}
private Response handleGetServiceQuota(JsonNode request, String region, String accountId, boolean awsDefault) {
@@ -18,6 +18,7 @@ import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;
@@ -97,23 +98,35 @@ public class ServiceQuotasService {
new TypeReference<Map<String, ObjectNode>>() {});
}
public ObjectNode listServiceQuotas(String serviceCode, String quotaCodeFilter, String nextToken,
Integer maxResults, String region, String accountId) {
return listQuotas(serviceCode, quotaCodeFilter, nextToken, maxResults, region, accountId, true);
public ObjectNode listServiceQuotas(String serviceCode, String quotaCodeFilter, String appliedAtLevel,
String nextToken, Integer maxResults, String region, String accountId) {
return listQuotas(serviceCode, quotaCodeFilter, appliedAtLevel, nextToken, maxResults, region, accountId, true);
}
public ObjectNode listAwsDefaultServiceQuotas(String serviceCode, String quotaCodeFilter, String nextToken,
Integer maxResults, String region, String accountId) {
return listQuotas(serviceCode, quotaCodeFilter, nextToken, maxResults, region, accountId, false);
return listQuotas(serviceCode, quotaCodeFilter, null, nextToken, maxResults, region, accountId, false);
}
private ObjectNode listQuotas(String serviceCode, String quotaCodeFilter, String nextToken,
Integer maxResults, String region, String accountId, boolean applied) {
private ObjectNode listQuotas(String serviceCode, String quotaCodeFilter, String appliedAtLevel,
String nextToken, Integer maxResults, String region, String accountId, boolean applied) {
requireServiceCode(serviceCode);
List<QuotaDefinition> quotas = quotasFor(serviceCode);
if (quotaCodeFilter != null && !quotaCodeFilter.isEmpty()) {
quotas = quotas.stream().filter(q -> q.quotaCode().equals(quotaCodeFilter)).toList();
}
// Filter by QuotaAppliedAtLevel: ACCOUNT, RESOURCE, or ALL (exact match per AppliedLevelEnum)
if (appliedAtLevel != null && !appliedAtLevel.isBlank()) {
if (!"ALL".equals(appliedAtLevel) && !"ACCOUNT".equals(appliedAtLevel) && !"RESOURCE".equals(appliedAtLevel)) {
throw new AwsException("IllegalArgumentException",
"Invalid value for QuotaAppliedAtLevel: " + appliedAtLevel + ". Valid values: ACCOUNT, RESOURCE, ALL.", 400);
}
if (!"ALL".equals(appliedAtLevel)) {
quotas = quotas.stream()
.filter(q -> appliedAtLevel.equalsIgnoreCase(q.appliedAtLevel()))
.toList();
}
}
Page<QuotaDefinition> page = paginate(quotas, nextToken, maxResults);
ObjectNode response = objectMapper.createObjectNode();
@@ -186,7 +199,7 @@ public class ServiceQuotasService {
node.put("Unit", "None");
node.put("Adjustable", true);
node.put("GlobalQuota", quota.globalQuota());
node.put("QuotaAppliedAtLevel", "ACCOUNT");
node.put("QuotaAppliedAtLevel", quota.appliedAtLevel());
return node;
}
@@ -269,7 +282,10 @@ public class ServiceQuotasService {
return response;
}
record QuotaDefinition(String quotaCode, String quotaName, double value, boolean globalQuota) {
record QuotaDefinition(String quotaCode, String quotaName, double value, boolean globalQuota, String appliedAtLevel) {
public QuotaDefinition(String quotaCode, String quotaName, double value, boolean globalQuota) {
this(quotaCode, quotaName, value, globalQuota, "ACCOUNT");
}
}
private record Page<T>(List<T> items, String nextToken) {
@@ -12,6 +12,7 @@ import org.junit.jupiter.params.provider.CsvSource;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.notNullValue;
@@ -536,4 +537,82 @@ class ServiceQuotasIntegrationTest {
.statusCode(400)
.body("__type", equalTo("UnknownOperationException"));
}
@Test
void listServiceQuotas_filterByQuotaAppliedAtLevelResource() {
// EC2 quotas are ACCOUNT-level; RESOURCE filter should return empty
given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "ServiceQuotasV20190624.ListServiceQuotas")
.header("Authorization", AUTH_HEADER)
.body("{\"ServiceCode\":\"ec2\",\"QuotaAppliedAtLevel\":\"RESOURCE\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("Quotas.size()", equalTo(0));
}
@Test
void listServiceQuotas_filterByQuotaAppliedAtLevelAll() {
// ALL should return all quotas (4 for EC2)
given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "ServiceQuotasV20190624.ListServiceQuotas")
.header("Authorization", AUTH_HEADER)
.body("{\"ServiceCode\":\"ec2\",\"QuotaAppliedAtLevel\":\"ALL\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("Quotas.size()", equalTo(4))
.body("Quotas.find { it.QuotaCode == 'L-1216C47A' }.QuotaAppliedAtLevel", equalTo("ACCOUNT"));
}
@Test
void listServiceQuotas_filterByQuotaAppliedAtLevelAccount() {
// ACCOUNT (default) should return ACCOUNT-level quotas
given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "ServiceQuotasV20190624.ListServiceQuotas")
.header("Authorization", AUTH_HEADER)
.body("{\"ServiceCode\":\"ec2\",\"QuotaAppliedAtLevel\":\"ACCOUNT\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("Quotas.size()", equalTo(4));
}
@Test
void listAwsDefaultServiceQuotas_noQuotaAppliedAtLevelFilter() {
// ListAWSDefaultServiceQuotas does not have QuotaAppliedAtLevel parameter
// Should return same as default (all quotas for EC2)
given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "ServiceQuotasV20190624.ListAWSDefaultServiceQuotas")
.header("Authorization", AUTH_HEADER)
.body("{\"ServiceCode\":\"ec2\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("Quotas.size()", equalTo(4));
}
@Test
void listServiceQuotas_filterByQuotaAppliedAtLevelInvalid() {
// Invalid value should return IllegalArgumentException
given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "ServiceQuotasV20190624.ListServiceQuotas")
.header("Authorization", AUTH_HEADER)
.body("{\"ServiceCode\":\"ec2\",\"QuotaAppliedAtLevel\":\"ACOUNT\"}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("IllegalArgumentException"))
.body("message", containsString("Invalid value for QuotaAppliedAtLevel"));
}
}
@@ -60,7 +60,7 @@ class ServiceQuotasServiceTest {
String code = "L-1216C47A";
assertEquals((double) value, configured.getServiceQuota("ec2", code, "us-east-1", ACCOUNT_ID)
.path("Quota").path("Value").asDouble());
assertEquals((double) value, configured.listServiceQuotas("ec2", code, null, null, "us-east-1", ACCOUNT_ID)
assertEquals((double) value, configured.listServiceQuotas("ec2", code, null, null, null, "us-east-1", ACCOUNT_ID)
.path("Quotas").get(0).path("Value").asDouble());
assertEquals(5.0, configured.getAwsDefaultServiceQuota("ec2", code, "us-east-1", ACCOUNT_ID)
.path("Quota").path("Value").asDouble());
@@ -102,7 +102,7 @@ class ServiceQuotasServiceTest {
assertEquals("ACCOUNT", quota.path("QuotaAppliedAtLevel").asText());
assertEquals("arn:aws:servicequotas:us-east-1:" + ACCOUNT_ID + ":ec2/" + code,
quota.path("QuotaArn").asText());
JsonNode listed = service.listServiceQuotas("ec2", null, null, 100, "us-east-1", ACCOUNT_ID).path("Quotas");
JsonNode listed = service.listServiceQuotas("ec2", null, null, null, 100, "us-east-1", ACCOUNT_ID).path("Quotas");
assertEquals(4, listed.size());
int matching = 0;
for (JsonNode entry : listed) {
@@ -136,7 +136,7 @@ class ServiceQuotasServiceTest {
assertEquals("Amazon Virtual Private Cloud (Amazon VPC)", quota.path("ServiceName").asText());
}
Map<String, String> listed = new HashMap<>();
service.listServiceQuotas("vpc", null, null, 100, "us-east-1", "000000000000").withArray("Quotas")
service.listServiceQuotas("vpc", null, null, null, 100, "us-east-1", "000000000000").withArray("Quotas")
.forEach(q -> listed.put(q.path("QuotaCode").asText(), q.path("QuotaName").asText()));
want.forEach((code, name) -> assertEquals(name, listed.get(code), listed.toString()));
assertEquals(want.size() + 3, listed.size(), "synthetic quotas still appended: " + listed);
@@ -166,7 +166,7 @@ class ServiceQuotasServiceTest {
void malformedServiceCodeIsRejected() {
for (String bad : List.of("!!!", "1codebuild", "-codebuild", "code build", "c", "a".repeat(64))) {
AwsException listing = assertThrows(AwsException.class,
() -> service.listServiceQuotas(bad, null, null, null, "us-east-1", "000000000000"),
() -> service.listServiceQuotas(bad, null, null, null, null, "us-east-1", "000000000000"),
"expected ListServiceQuotas to reject " + bad);
assertEquals("IllegalArgumentException", listing.getErrorCode(), bad);
assertEquals(400, listing.getHttpStatus(), bad);
@@ -182,7 +182,7 @@ class ServiceQuotasServiceTest {
void wellFormedServiceCodesAreStillAccepted() {
// Including an unknown-but-valid code: generating a catalog for those is deliberate.
for (String good : List.of("codebuild", "widgetfactory", "elasticloadbalancing", "AWS-Thing", "ec")) {
assertTrue(service.listServiceQuotas(good, null, null, null, "us-east-1", ACCOUNT_ID)
assertTrue(service.listServiceQuotas(good, null, null, null, null, "us-east-1", ACCOUNT_ID)
.withArray("Quotas").size() > 0, good);
}
}