mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(ec2): support DescribeIpamScopes (#4843)
* feat(ec2): support DescribeIpamScopes CreateIpam already creates the private and public default scopes, but DescribeIpamScopes answered UnsupportedOperation. Terraform's aws_vpc_ipam_pool reads its scope through DescribeIpamScopes before CreateIpamPool, so no IPAM pool could be created. Accepts IpamScopeId.N (and IpamScopeIds.member.N, as DescribeIpams does) and returns InvalidIpamScopeId.NotFound for unknown ids. Filter.N is not supported. * fix(ec2): report scope and IPAM on IPAM pools The IpamPool shape carries ipamScopeArn, ipamScopeType, ipamArn and ipamRegion, but pools were written without them. Terraform's aws_vpc_ipam_pool Read splits ipamScopeArn on "/" and reads ipamScopeType off the pool, so every refresh of a pool broke. They are derived from the pool's scope and its IPAM, and omitted when that scope is no longer visible. * fix(ec2): count IPAM pools per scope in one pass; import XmlPath in test * refactor(ec2): resolve IPAM scope lookup and its not-found error in one place
This commit is contained in:
1 parent
548f4698c5
commit
6172451e0b
4 files changed
+173
-9
No files matched your search
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteIpamRemovesItAndUnknownIdsThrow() {
|
||||
Ipam ipam = service.createIpam(REGION, null, List.of(REGION));
|
||||
|
||||
Reference in new issue
Block a user