Light, fluffy, and always free - The AWS Local Emulator alternative
feat(ec2): support DescribeIpamScopes (#4843)
Allen Sanborn6172451e0bd6
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2IpamService.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2IpamService.javaindex 690520fc01..956758201c 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2IpamService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2IpamService.java@@ -30,6 +30,7 @@ import java.util.Optional;import java.util.Set;import java.util.TreeSet;import java.util.concurrent.ThreadLocalRandom;+import java.util.stream.Collectors;/*** Amazon VPC IP Address Manager (IPAM) emulation: organization admin@@ -309,6 +310,46 @@ public class Ec2IpamService {return result;}+ /** A scope with the IPAM that owns it; {@link IpamScope} itself carries no ARN, region or owner. */+ public record ScopeOfIpam(Ipam ipam, IpamScope scope) {}++ public List<ScopeOfIpam> describeIpamScopes(String region, List<String> ipamScopeIds) {+ List<ScopeOfIpam> result = new ArrayList<>();+ for (Ipam ipam : describeIpams(region, null)) {+ for (IpamScope scope : ipam.getScopes()) {+ if (ipamScopeIds == null || ipamScopeIds.isEmpty() || ipamScopeIds.contains(scope.getIpamScopeId())) {+ result.add(new ScopeOfIpam(ipam, scope));+ }+ }+ }+ if (ipamScopeIds != null) {+ for (String id : ipamScopeIds) {+ if (result.stream().noneMatch(s -> id.equals(s.scope().getIpamScopeId()))) {+ throw scopeNotFound(id);+ }+ }+ }+ return result;+ }++ /** The scope and its IPAM, or empty when the scope is not visible (e.g. its IPAM was deleted). */+ public Optional<ScopeOfIpam> findScope(String ipamScopeId) {+ for (Ipam ipam : ipams.scan(k -> true)) {+ for (IpamScope scope : ipam.getScopes()) {+ if (scope.getIpamScopeId().equals(ipamScopeId)) {+ return Optional.of(new ScopeOfIpam(ipam, scope));+ }+ }+ }+ return Optional.empty();+ }++ /** Pool count per scope id, from a single pass over the pool store. */+ public Map<String, Long> poolCountsByScope() {+ return pools.scan(k -> true).stream()+ .collect(Collectors.groupingBy(IpamPool::getIpamScopeId, Collectors.counting()));+ }+public AsnAssociation associateIpamByoasn(String region, String asn, String cidr) {if (asn == null || asn.isBlank()) {throw new AwsException("MissingParameter", "Asn is required.", 400);@@ -511,15 +552,7 @@ public class Ec2IpamService {/** Resolves the IPAM owning a scope. {@code IpamScopeId} is a required member of* CreateIpamPool, so an unknown scope is an error rather than a null {@code ipamId}. */private String ipamIdOfScope(String ipamScopeId) {- for (Ipam ipam : ipams.scan(k -> true)) {- for (IpamScope scope : ipam.getScopes()) {- if (ipamScopeId.equals(scope.getIpamScopeId())) {- return ipam.getIpamId();- }- }- }- throw new AwsException("InvalidIpamScopeId.NotFound",- "IPAM scope " + ipamScopeId + " does not exist.", 400);+ return findScope(ipamScopeId).orElseThrow(() -> scopeNotFound(ipamScopeId)).ipam().getIpamId();}public List<IpamPool> describeIpamPools(String region, List<String> ipamPoolIds) {@@ -776,6 +809,11 @@ public class Ec2IpamService {ipams.delete(ipamKey);}+ private static AwsException scopeNotFound(String ipamScopeId) {+ return new AwsException("InvalidIpamScopeId.NotFound",+ "IPAM scope " + ipamScopeId + " does not exist.", 400);+ }+private static AwsException ipamNotFound(String ipamId) {return new AwsException("InvalidIpamId.NotFound", "IPAM " + ipamId + " does not exist.", 400);}diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.javaindex dbecdd5fcb..66b199cdf0 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java@@ -282,6 +282,7 @@ public class Ec2QueryHandler {case "DisableIpamOrganizationAdminAccount" -> handleDisableIpamOrgAdmin(params);case "CreateIpam" -> handleCreateIpam(params, region);case "DescribeIpams" -> handleDescribeIpams(params, region);+ case "DescribeIpamScopes" -> handleDescribeIpamScopes(params, region);case "DeleteIpam" -> handleDeleteIpam(params, region);case "ModifyIpam" -> handleModifyIpam(params, region);case "CreateIpamPool" -> handleCreateIpamPool(params, region);@@ -1815,6 +1816,35 @@ public class Ec2QueryHandler {return xmlResponse(xml.build());}+ private Response handleDescribeIpamScopes(MultivaluedMap<String, String> p, String region) {+ checkDryRun(p);+ List<String> ids = getList(p, "IpamScopeId");+ if (ids.isEmpty()) {+ ids = getList(p, "IpamScopeIds.member");+ }+ XmlBuilder xml = new XmlBuilder()+ .start("DescribeIpamScopesResponse", AwsNamespaces.EC2)+ .elem("requestId", UUID.randomUUID().toString())+ .start("ipamScopeSet");+ Map<String, Long> poolCounts = ipamService.poolCountsByScope();+ for (Ec2IpamService.ScopeOfIpam s : ipamService.describeIpamScopes(region, ids)) {+ IpamScope scope = s.scope();+ xml.start("item")+ .elem("ownerId", s.ipam().getOwnerId())+ .elem("ipamScopeId", scope.getIpamScopeId())+ .elem("ipamScopeArn", scope.getIpamScopeArn())+ .elem("ipamArn", s.ipam().getIpamArn())+ .elem("ipamRegion", s.ipam().getRegion())+ .elem("ipamScopeType", scope.getScopeType())+ .elem("isDefault", String.valueOf(scope.isDefault()))+ .elem("poolCount", String.valueOf(poolCounts.getOrDefault(scope.getIpamScopeId(), 0L)))+ .elem("state", scope.getState())+ .end("item");+ }+ xml.end("ipamScopeSet").end("DescribeIpamScopesResponse");+ return xmlResponse(xml.build());+ }+private Response handleDeleteIpam(MultivaluedMap<String, String> p, String region) {checkDryRun(p);Ipam ipam = ipamService.deleteIpam(region, p.getFirst("IpamId"));@@ -2080,6 +2110,11 @@ public class Ec2QueryHandler {.elem("addressFamily", pool.getAddressFamily()).elem("state", pool.getState()).elem("autoImport", String.valueOf(pool.isAutoImport()));+ ipamService.findScope(pool.getIpamScopeId()).ifPresent(s -> xml+ .elem("ipamScopeArn", s.scope().getIpamScopeArn())+ .elem("ipamScopeType", s.scope().getScopeType())+ .elem("ipamArn", s.ipam().getIpamArn())+ .elem("ipamRegion", s.ipam().getRegion()));if (pool.getSourceIpamPoolId() != null) {xml.elem("sourceIpamPoolId", pool.getSourceIpamPoolId());}diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.javaindex 60ad02179f..0fc16ced82 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java@@ -17,6 +17,7 @@ import static org.hamcrest.Matchers.nullValue;import static org.hamcrest.MatcherAssert.assertThat;import static org.hamcrest.Matchers.startsWith;+import io.restassured.path.xml.XmlPath;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.nio.charset.StandardCharsets;@@ -5011,6 +5012,82 @@ class Ec2IntegrationTest {.statusCode(400).body("Response.Errors.Error.Code", equalTo("InvalidIpamId.NotFound"));}++ @Test+ @Order(322)+ void describeIpamScopesReturnsTheDefaultScopesOverQuery() {+ // aws_vpc_ipam_pool reads its scope through DescribeIpamScopes before CreateIpamPool+ XmlPath created = given()+ .formParam("Action", "CreateIpam")+ .formParam("Description", "scope-ipam")+ .header("Authorization", AUTH_HEADER)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract().xmlPath();+ String ipamId = created.getString("CreateIpamResponse.ipam.ipamId");+ String ipamArn = created.getString("CreateIpamResponse.ipam.ipamArn");+ String scopeId = given()+ .formParam("Action", "DescribeIpams")+ .formParam("IpamId.1", ipamId)+ .header("Authorization", AUTH_HEADER)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract().path("DescribeIpamsResponse.ipamSet.item.privateDefaultScopeId");++ given()+ .formParam("Action", "DescribeIpamScopes")+ .formParam("IpamScopeId.1", scopeId)+ .header("Authorization", AUTH_HEADER)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("DescribeIpamScopesResponse.ipamScopeSet.item.size()", equalTo(1))+ .body("DescribeIpamScopesResponse.ipamScopeSet.item.ipamScopeId", equalTo(scopeId))+ .body("DescribeIpamScopesResponse.ipamScopeSet.item.ipamArn", equalTo(ipamArn))+ .body("DescribeIpamScopesResponse.ipamScopeSet.item.ipamScopeType", equalTo("private"))+ .body("DescribeIpamScopesResponse.ipamScopeSet.item.isDefault", equalTo("true"))+ .body("DescribeIpamScopesResponse.ipamScopeSet.item.ipamScopeArn", endsWith("ipam-scope/" + scopeId));++ // aws_vpc_ipam_pool's Read splits ipamScopeArn on "/" and reads ipamScopeType off the pool+ String poolId = given()+ .formParam("Action", "CreateIpamPool")+ .formParam("IpamScopeId", scopeId)+ .formParam("AddressFamily", "ipv4")+ .header("Authorization", AUTH_HEADER)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract().path("CreateIpamPoolResponse.ipamPool.ipamPoolId");+ given()+ .formParam("Action", "DescribeIpamPools")+ .formParam("IpamPoolId.1", poolId)+ .header("Authorization", AUTH_HEADER)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("DescribeIpamPoolsResponse.ipamPoolSet.item.ipamScopeArn", endsWith("ipam-scope/" + scopeId))+ .body("DescribeIpamPoolsResponse.ipamPoolSet.item.ipamScopeType", equalTo("private"))+ .body("DescribeIpamPoolsResponse.ipamPoolSet.item.ipamArn", equalTo(ipamArn))+ .body("DescribeIpamPoolsResponse.ipamPoolSet.item.ipamRegion", equalTo("us-east-1"));++ given()+ .formParam("Action", "DescribeIpamScopes")+ .formParam("IpamScopeId.1", "ipam-scope-doesnotexist")+ .header("Authorization", AUTH_HEADER)+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("Response.Errors.Error.Code", equalTo("InvalidIpamScopeId.NotFound"));+ }+@Test@Order(323)void associateIpamByoasnSuccess() {diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IpamServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IpamServiceTest.javaindex 9beb16dedb..b1a7ee0b7e 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IpamServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IpamServiceTest.java@@ -155,6 +155,20 @@ class Ec2IpamServiceTest {assertEquals("full", first.getTags().getFirst().getValue());}+ @Test+ void describeIpamScopesFindsTheDefaultScopesAndRejectsUnknownIds() {+ Ipam ipam = service.createIpam(REGION, null, List.of(REGION));+ assertEquals(2, service.describeIpamScopes(REGION, List.of()).size());+ List<Ec2IpamService.ScopeOfIpam> one =+ service.describeIpamScopes(REGION, List.of(ipam.getPublicDefaultScopeId()));+ assertEquals(1, one.size());+ assertEquals("public", one.get(0).scope().getScopeType());+ assertEquals(ipam.getIpamId(), one.get(0).ipam().getIpamId());+ AwsException e = assertThrows(AwsException.class,+ () -> service.describeIpamScopes(REGION, List.of("ipam-scope-doesnotexist")));+ assertEquals("InvalidIpamScopeId.NotFound", e.getErrorCode());+ }+@Testvoid deleteIpamRemovesItAndUnknownIdsThrow() {Ipam ipam = service.createIpam(REGION, null, List.of(REGION));