feat(servicequotas): implement ListServices action (#7254)

This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-11-04 13:52:00 -04:00
1 parent fab3e7b923
commit e95af9cbf6
6 files changed
+205 -3

No files matched your search

@@ -3,11 +3,15 @@ package com.floci.test;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.servicequotas.ServiceQuotasClient;
import software.amazon.awssdk.services.servicequotas.model.InvalidPaginationTokenException;
import software.amazon.awssdk.services.servicequotas.model.ServiceInfo;
import software.amazon.awssdk.services.servicequotas.model.ServiceQuota;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
class ServiceQuotasTest {
@@ -63,4 +67,55 @@ class ServiceQuotasTest {
assertThat(quota.adjustable()).isTrue();
assertThat(quota.globalQuota()).isFalse();
}
@Test
@DisplayName("ListServices rejects empty NextToken")
void listServicesRejectsEmptyNextToken() {
assumeFalse(TestFixtures.isRealAws(), "Tests the emulated service catalog");
try (ServiceQuotasClient client = TestFixtures.serviceQuotasClient()) {
assertThatThrownBy(
() -> client.listServices(request -> request.nextToken("")))
.isInstanceOf(InvalidPaginationTokenException.class);
}
}
@Test
@DisplayName("ListServices rejects fabricated NextToken")
void listServicesRejectsFabricatedNextToken() {
assumeFalse(TestFixtures.isRealAws(), "Tests the emulated service catalog");
try (ServiceQuotasClient client = TestFixtures.serviceQuotasClient()) {
assertThatThrownBy(
() -> client.listServices(request -> request.nextToken("!!!")))
.isInstanceOf(InvalidPaginationTokenException.class);
}
}
@Test
@DisplayName("ListServices returns services with code and name, supports pagination")
void listServicesReturnsServicesAndPaginates() {
assumeFalse(TestFixtures.isRealAws(), "Tests the emulated service catalog");
try (ServiceQuotasClient client = TestFixtures.serviceQuotasClient()) {
// List all services
List<ServiceInfo> allServices = client.listServicesPaginator(request -> request.maxResults(100))
.stream().flatMap(page -> page.services().stream()).toList();
assertThat(allServices).hasSizeGreaterThanOrEqualTo(10);
assertThat(allServices).anyMatch(s -> s.serviceCode().equals("ec2") && s.serviceName().equals("Amazon Elastic Compute Cloud (Amazon EC2)"));
assertThat(allServices).anyMatch(s -> s.serviceCode().equals("lambda") && s.serviceName().equals("AWS Lambda"));
assertThat(allServices).anyMatch(s -> s.serviceCode().equals("s3") && s.serviceName().equals("Amazon Simple Storage Service (Amazon S3)"));
// Pagination
List<ServiceInfo> firstPage = client.listServices(request -> request.maxResults(2)).services();
assertThat(firstPage).hasSize(2);
String nextToken = client.listServices(request -> request.maxResults(2)).nextToken();
assertThat(nextToken).isNotNull();
List<ServiceInfo> secondPage = client.listServices(request -> request.nextToken(nextToken)).services();
assertThat(secondPage.size()).isGreaterThanOrEqualTo(1);
// Services sorted by ServiceCode (case-insensitive)
List<String> codes = allServices.stream().map(ServiceInfo::serviceCode).toList();
List<String> sorted = new ArrayList<>(codes);
sorted.sort(String::compareToIgnoreCase);
assertThat(codes).isEqualTo(sorted);
}
}
}
+1 -1
View File
@@ -116,7 +116,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
| [CodePipeline](codepipeline.md) | `POST /` + `X-Amz-Target: CodePipeline_20150709.*` | JSON 1.1 | 44 |
| [AWS Network Firewall](network-firewall.md) | `POST /` + `X-Amz-Target: NetworkFirewall_20201112.*` | JSON 1.0 | 27 |
| [AWS Service Catalog](service-catalog.md) | `POST /` + `X-Amz-Target: AWS242ServiceCatalogService.*` | JSON 1.1 | 89 |
| [Service Quotas](servicequotas.md) | `POST /` + `X-Amz-Target: ServiceQuotasV20190624.*` | JSON 1.1 | 7 |
| [Service Quotas](servicequotas.md) | `POST /` + `X-Amz-Target: ServiceQuotasV20190624.*` | JSON 1.1 | 8 |
| [AWS Budgets](budgets.md) | `POST /` + `X-Amz-Target: AWSBudgetServiceGateway.*` | JSON 1.1 | 26 |
| [AWS RAM](ram.md) | `POST /{operationname}` (lowercase), `DELETE /deleteresourceshare` | REST JSON | 16 |
| [Control Catalog](controlcatalog.md) | `/get-control`, `/list-controls` | REST JSON | 2 |
+1
View File
@@ -41,6 +41,7 @@ Limitations.
| `RequestServiceQuotaIncrease` | Validates and stores an increase request as `PENDING`; a second open request for the same quota is refused with `ResourceAlreadyExistsException` |
| `GetRequestedServiceQuotaChange` | Returns a stored request by id, else `NoSuchResourceException` |
| `ListRequestedServiceQuotaChangeHistoryByQuota` | Lists the stored requests for a service/quota pair, newest first |
| `ListServices` | Lists the available services with their codes and names, supports pagination via MaxResults and NextToken |
<!-- floci:actions:end -->
## Limitations
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.servicequotas;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.AwsException;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
@@ -41,6 +42,7 @@ public class ServiceQuotasJsonHandler {
stringOrNull(request, "ServiceCode"), stringOrNull(request, "QuotaCode"),
stringOrNull(request, "NextToken"), integerOrNull(request, "MaxResults"),
region)).build();
case "ListServices" -> handleListServices(request, region, accountId);
default -> Response.status(400)
.entity(new AwsErrorResponse("UnknownOperationException",
"Unknown operation: ServiceQuotasV20190624." + action))
@@ -48,6 +50,12 @@ public class ServiceQuotasJsonHandler {
};
}
private Response handleListServices(JsonNode request, String region, String accountId) {
String nextToken = stringOrNull(request, "NextToken");
Integer maxResults = integerOrNull(request, "MaxResults");
return Response.ok(service.listServices(nextToken, maxResults)).build();
}
private Response handleListServiceQuotas(JsonNode request, String region, String accountId, boolean awsDefault) {
String serviceCode = stringOrNull(request, "ServiceCode");
String quotaCode = stringOrNull(request, "QuotaCode");
@@ -76,7 +84,14 @@ public class ServiceQuotasJsonHandler {
private static Integer integerOrNull(JsonNode node, String field) {
JsonNode value = node == null ? null : node.get(field);
return (value != null && value.isNumber()) ? value.asInt() : null;
if (value == null || value.isNull()) {
return null;
}
if (!value.isInt()) {
throw new AwsException("ValidationException",
"1 validation error detected: Value '" + value.asText() + "' at 'maxResults' failed to satisfy constraint: Member must be an integer.", 400);
}
return value.asInt();
}
private Response handleRequestServiceQuotaIncrease(JsonNode request, String region, String accountId) {
@@ -108,6 +108,25 @@ public class ServiceQuotasService {
return listQuotas(serviceCode, quotaCodeFilter, null, nextToken, maxResults, region, accountId, false);
}
public ObjectNode listServices(String nextToken, Integer maxResults) {
List<String> serviceCodes = new ArrayList<>(SERVICE_NAMES.keySet());
serviceCodes.sort(String::compareToIgnoreCase);
Page<String> page = paginate(serviceCodes, nextToken, maxResults);
ObjectNode response = objectMapper.createObjectNode();
ArrayNode array = response.putArray("Services");
for (String code : page.items()) {
ObjectNode service = objectMapper.createObjectNode();
service.put("ServiceCode", code);
service.put("ServiceName", SERVICE_NAMES.getOrDefault(code, code));
array.add(service);
}
if (page.nextToken() != null) {
response.put("NextToken", page.nextToken());
}
return response;
}
private ObjectNode listQuotas(String serviceCode, String quotaCodeFilter, String appliedAtLevel,
String nextToken, Integer maxResults, String region, String accountId, boolean applied) {
requireServiceCode(serviceCode);
@@ -241,9 +260,12 @@ public class ServiceQuotasService {
}
private static int decodeToken(String token) {
if (token == null || token.isEmpty()) {
if (token == null) {
return 0;
}
if (token.isEmpty()) {
return -1;
}
try {
return Integer.parseInt(Base64Url.decode(token));
} catch (IllegalArgumentException e) {
@@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import java.util.ArrayList;
import java.util.List;
import static io.restassured.RestAssured.given;
@@ -615,4 +616,112 @@ class ServiceQuotasIntegrationTest {
.body("__type", equalTo("IllegalArgumentException"))
.body("message", containsString("Invalid value for QuotaAppliedAtLevel"));
}
@Test
void listServices_returnsKnownServicesWithServiceCodeAndName() {
given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "ServiceQuotasV20190624.ListServices")
.header("Authorization", AUTH_HEADER)
.body("{}")
.when()
.post("/")
.then()
.statusCode(200)
.body("Services.size()", greaterThanOrEqualTo(10))
.body("Services.find { it.ServiceCode == 'ec2' }.ServiceName", equalTo("Amazon Elastic Compute Cloud (Amazon EC2)"))
.body("Services.find { it.ServiceCode == 'lambda' }.ServiceName", equalTo("AWS Lambda"))
.body("Services.find { it.ServiceCode == 's3' }.ServiceName", equalTo("Amazon Simple Storage Service (Amazon S3)"))
.body("Services.find { it.ServiceCode == 'sqs' }.ServiceName", equalTo("Amazon Simple Queue Service (Amazon SQS)"))
.body("Services.find { it.ServiceCode == 'sns' }.ServiceName", equalTo("Amazon Simple Notification Service (Amazon SNS)"));
}
@Test
void listServices_paginatesWithMaxResultsAndNextToken() {
Response first = given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "ServiceQuotasV20190624.ListServices")
.header("Authorization", AUTH_HEADER)
.body("{\"MaxResults\":2}")
.when()
.post("/")
.then()
.statusCode(200)
.body("Services.size()", equalTo(2))
.body("NextToken", notNullValue())
.extract().response();
String firstCode = first.path("Services[0].ServiceCode");
String nextToken = first.path("NextToken");
assertNotNull(firstCode);
assertNotNull(nextToken);
Response second = given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "ServiceQuotasV20190624.ListServices")
.header("Authorization", AUTH_HEADER)
.body("{\"NextToken\":\"" + nextToken + "\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("Services.size()", greaterThanOrEqualTo(1))
.extract().response();
assertNull(second.path("NextToken"));
}
@Test
void listServices_invalidMaxResults_returnsIllegalArgument() {
given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "ServiceQuotasV20190624.ListServices")
.header("Authorization", AUTH_HEADER)
.body("{\"MaxResults\":101}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("IllegalArgumentException"));
}
@Test
void listServices_invalidNextToken_returnsInvalidPaginationToken() {
given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "ServiceQuotasV20190624.ListServices")
.header("Authorization", AUTH_HEADER)
.body("{\"NextToken\":\"not_a_token!\"}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidPaginationTokenException"));
}
@Test
void listServices_servicesAreSortedByCode() {
given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "ServiceQuotasV20190624.ListServices")
.header("Authorization", AUTH_HEADER)
.body("{\"MaxResults\":100}")
.when()
.post("/")
.then()
.statusCode(200)
.body("Services.ServiceCode", notNullValue());
// Services should be sorted alphabetically by ServiceCode (case-insensitive)
List<String> codes = given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "ServiceQuotasV20190624.ListServices")
.header("Authorization", AUTH_HEADER)
.body("{\"MaxResults\":100}")
.when()
.post("/")
.then()
.statusCode(200)
.extract().path("Services.ServiceCode");
List<String> sortedCodes = new ArrayList<>(codes);
sortedCodes.sort(String::compareToIgnoreCase);
assertEquals(sortedCodes, codes, "Services must be sorted by ServiceCode (case-insensitive)");
}
}