fix(ec2): honour DryRun on the VPC, subnet, internet gateway and route table calls (#9018)

* fix(ec2): honour DryRun on the VPC, subnet, internet gateway and route table calls
This commit is contained in:
mrfelek authored and GitHub committed 2026-11-19 18:30:09 -04:00
1 parent 683cb84080
commit a283c7a49b
5 files changed
+359 -32

No files matched your search

@@ -1825,7 +1825,7 @@ public class Ec2QueryHandler {
}
private Response handleDeleteVpc(MultivaluedMap<String, String> p, String region) {
service.deleteVpc(region, p.getFirst("VpcId"));
service.deleteVpc(region, p.getFirst("VpcId"), dryRun(p));
return booleanResponse("DeleteVpc");
}
@@ -3501,6 +3501,7 @@ public class Ec2QueryHandler {
private Response handleCreateDefaultVpc(MultivaluedMap<String, String> p, String region) {
checkDryRun(p);
Vpc vpc = service.createDefaultVpc(region);
XmlBuilder xml = new XmlBuilder()
.start("CreateDefaultVpcResponse", AwsNamespaces.EC2)
@@ -3618,7 +3619,7 @@ public class Ec2QueryHandler {
}
private Response handleDeleteSubnet(MultivaluedMap<String, String> p, String region) {
service.deleteSubnet(region, p.getFirst("SubnetId"));
service.deleteSubnet(region, p.getFirst("SubnetId"), dryRun(p));
return booleanResponse("DeleteSubnet");
}
@@ -4344,6 +4345,7 @@ public class Ec2QueryHandler {
private Response handleCreateInternetGateway(MultivaluedMap<String, String> p, String region) {
List<Tag> resourceTags = parseTagsForResource(p, "internet-gateway");
checkDryRun(p);
InternetGateway igw = service.createInternetGateway(region);
applyResourceTags(resourceTags, region, igw.getInternetGatewayId());
XmlBuilder xml = new XmlBuilder()
@@ -4370,17 +4372,17 @@ public class Ec2QueryHandler {
}
private Response handleDeleteInternetGateway(MultivaluedMap<String, String> p, String region) {
service.deleteInternetGateway(region, p.getFirst("InternetGatewayId"));
service.deleteInternetGateway(region, p.getFirst("InternetGatewayId"), dryRun(p));
return booleanResponse("DeleteInternetGateway");
}
private Response handleAttachInternetGateway(MultivaluedMap<String, String> p, String region) {
service.attachInternetGateway(region, p.getFirst("InternetGatewayId"), p.getFirst("VpcId"));
service.attachInternetGateway(region, p.getFirst("InternetGatewayId"), p.getFirst("VpcId"), dryRun(p));
return booleanResponse("AttachInternetGateway");
}
private Response handleDetachInternetGateway(MultivaluedMap<String, String> p, String region) {
service.detachInternetGateway(region, p.getFirst("InternetGatewayId"), p.getFirst("VpcId"));
service.detachInternetGateway(region, p.getFirst("InternetGatewayId"), p.getFirst("VpcId"), dryRun(p));
return booleanResponse("DetachInternetGateway");
}
@@ -4389,7 +4391,7 @@ public class Ec2QueryHandler {
private Response handleCreateRouteTable(MultivaluedMap<String, String> p, String region) {
List<Tag> resourceTags = parseTagsForResource(p, "route-table");
String vpcId = p.getFirst("VpcId");
RouteTable rt = service.createRouteTable(region, vpcId);
RouteTable rt = service.createRouteTable(region, vpcId, dryRun(p));
applyResourceTags(resourceTags, region, rt.getRouteTableId());
XmlBuilder xml = new XmlBuilder()
.start("CreateRouteTableResponse", AwsNamespaces.EC2)
@@ -4420,14 +4422,14 @@ public class Ec2QueryHandler {
}
private Response handleDeleteRouteTable(MultivaluedMap<String, String> p, String region) {
service.deleteRouteTable(region, p.getFirst("RouteTableId"));
service.deleteRouteTable(region, p.getFirst("RouteTableId"), dryRun(p));
return booleanResponse("DeleteRouteTable");
}
private Response handleAssociateRouteTable(MultivaluedMap<String, String> p, String region) {
String rtId = p.getFirst("RouteTableId");
String subnetId = p.getFirst("SubnetId");
RouteTableAssociation assoc = service.associateRouteTable(region, rtId, subnetId);
RouteTableAssociation assoc = service.associateRouteTable(region, rtId, subnetId, dryRun(p));
XmlBuilder xml = new XmlBuilder()
.start("AssociateRouteTableResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
@@ -4440,6 +4442,7 @@ public class Ec2QueryHandler {
}
private Response handleDisassociateRouteTable(MultivaluedMap<String, String> p, String region) {
checkDryRun(p);
service.disassociateRouteTable(region, p.getFirst("AssociationId"));
return booleanResponse("DisassociateRouteTable");
}
@@ -4505,7 +4508,7 @@ public class Ec2QueryHandler {
String dest = p.getFirst("DestinationCidrBlock");
String destIpv6 = p.getFirst("DestinationIpv6CidrBlock");
String destPrefixList = p.getFirst("DestinationPrefixListId");
service.deleteRoute(region, rtId, dest, destIpv6, destPrefixList);
service.deleteRoute(region, rtId, dest, destIpv6, destPrefixList, dryRun(p));
return booleanResponse("DeleteRoute");
}
@@ -7027,6 +7027,14 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
public void deleteVpc(String region, String vpcId) {
deleteVpc(region, vpcId, false);
}
/**
* As above; with {@code dryRun} the VPC must exist and hold nothing DeleteVpc refuses to delete it
* with, and the request is then answered with DryRunOperation, the VPC and its default resources kept.
*/
public void deleteVpc(String region, String vpcId, boolean dryRun) {
ensureDefaultResources(region);
getRequiredVpc(region, vpcId);
synchronized (attachmentTopologyLock(region)) {
@@ -7036,6 +7044,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
requireNoProviderDependency(provider -> provider.hasVpcDependency(region, vpcId),
"The vpc '" + vpcId + "' has dependencies and cannot be deleted.");
requireNoVpcDependents(region, vpcId);
if (dryRun) {
throw dryRunOperation();
}
deleteVpcDefaultResources(region, vpcId);
vpcs.delete(key(region, vpcId));
removeTags(region, vpcId);
@@ -8301,6 +8312,14 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
public void deleteSubnet(String region, String subnetId) {
deleteSubnet(region, subnetId, false);
}
/**
* As above; with {@code dryRun} the subnet must exist and nothing may still use it, and the request is
* then answered with DryRunOperation, the subnet kept.
*/
public void deleteSubnet(String region, String subnetId, boolean dryRun) {
ensureDefaultResources(region);
if (subnets.get(key(region, subnetId)).isEmpty()) {
throw new AwsException("InvalidSubnetID.NotFound", "The subnet ID '" + subnetId + "' does not exist", 400);
@@ -8313,6 +8332,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
"The subnet '" + subnetId + "' has dependencies and cannot be deleted.");
requireNoProviderDependency(provider -> provider.hasSubnetDependency(region, subnetId),
"The subnet '" + subnetId + "' has dependencies and cannot be deleted.");
if (dryRun) {
throw dryRunOperation();
}
subnets.delete(key(region, subnetId));
}
if (vpcNetworkManager != null) {
@@ -11851,26 +11873,57 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
public void deleteInternetGateway(String region, String igwId) {
deleteInternetGateway(region, igwId, false);
}
/**
* As above; with {@code dryRun} the gateway must exist, and the request is then answered with
* DryRunOperation, the gateway kept.
*/
public void deleteInternetGateway(String region, String igwId, boolean dryRun) {
ensureDefaultResources(region);
if (internetGateways.get(key(region, igwId)).isEmpty()) {
throw new AwsException("InvalidInternetGatewayID.NotFound", "The internet gateway '" + igwId + "' does not exist", 400);
getRequiredInternetGateway(region, igwId);
if (dryRun) {
throw dryRunOperation();
}
internetGateways.delete(key(region, igwId));
}
public void attachInternetGateway(String region, String igwId, String vpcId) {
attachInternetGateway(region, igwId, vpcId, false);
}
/**
* As above; with {@code dryRun} the VPC and the gateway must exist, and the request is then answered
* with DryRunOperation, nothing attached.
*/
public void attachInternetGateway(String region, String igwId, String vpcId, boolean dryRun) {
ensureDefaultResources(region);
synchronized (attachmentTopologyLock(region)) {
getRequiredVpc(region, vpcId);
InternetGateway igw = getRequiredInternetGateway(region, igwId);
if (dryRun) {
throw dryRunOperation();
}
igw.getAttachments().add(new InternetGatewayAttachment(vpcId, "available"));
internetGateways.put(key(region, igwId), igw);
}
}
public void detachInternetGateway(String region, String igwId, String vpcId) {
detachInternetGateway(region, igwId, vpcId, false);
}
/**
* As above; with {@code dryRun} the gateway must exist, and the request is then answered with
* DryRunOperation, nothing detached.
*/
public void detachInternetGateway(String region, String igwId, String vpcId, boolean dryRun) {
ensureDefaultResources(region);
InternetGateway igw = getRequiredInternetGateway(region, igwId);
if (dryRun) {
throw dryRunOperation();
}
igw.getAttachments().removeIf(a -> a.getVpcId().equals(vpcId));
internetGateways.put(key(region, igwId), igw);
@@ -12635,8 +12688,19 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
// ─── Route Tables ──────────────────────────────────────────────────────────
public RouteTable createRouteTable(String region, String vpcId) {
return createRouteTable(region, vpcId, false);
}
/**
* As above; with {@code dryRun} the VPC must exist, and the request is then answered with
* DryRunOperation, no route table created.
*/
public RouteTable createRouteTable(String region, String vpcId, boolean dryRun) {
ensureDefaultResources(region);
Vpc vpc = getRequiredVpc(region, vpcId);
if (dryRun) {
throw dryRunOperation();
}
String rtId = "rtb-" + randomHex(8);
RouteTable rt = new RouteTable();
@@ -12688,18 +12752,38 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
public void deleteRouteTable(String region, String routeTableId) {
deleteRouteTable(region, routeTableId, false);
}
/**
* As above; with {@code dryRun} the route table must exist, and the request is then answered with
* DryRunOperation, the route table kept.
*/
public void deleteRouteTable(String region, String routeTableId, boolean dryRun) {
ensureDefaultResources(region);
RouteTable rt = routeTables.get(key(region, routeTableId)).orElse(null);
if (rt == null) {
throw new AwsException("InvalidRouteTableID.NotFound", "The route table '" + routeTableId + "' does not exist", 400);
RouteTable rt = getRequiredRouteTable(region, routeTableId);
if (dryRun) {
throw dryRunOperation();
}
routeTables.delete(key(region, routeTableId));
notifyRouteTableUpdated(region, rt);
}
public RouteTableAssociation associateRouteTable(String region, String routeTableId, String subnetId) {
return associateRouteTable(region, routeTableId, subnetId, false);
}
/**
* As above; with {@code dryRun} the route table must exist, and the request is then answered with
* DryRunOperation, nothing associated.
*/
public RouteTableAssociation associateRouteTable(String region, String routeTableId, String subnetId,
boolean dryRun) {
ensureDefaultResources(region);
RouteTable rt = getRequiredRouteTable(region, routeTableId);
if (dryRun) {
throw dryRunOperation();
}
String assocId = "rtbassoc-" + randomHex(8);
RouteTableAssociation assoc = new RouteTableAssociation();
@@ -13053,6 +13137,16 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
public void deleteRoute(String region, String routeTableId, String destinationCidrBlock,
String destinationIpv6CidrBlock, String destinationPrefixListId) {
deleteRoute(region, routeTableId, destinationCidrBlock, destinationIpv6CidrBlock, destinationPrefixListId,
false);
}
/**
* As above; with {@code dryRun} the route table must hold a route to the destination, and not the
* local route, and the request is then answered with DryRunOperation, the route kept.
*/
public void deleteRoute(String region, String routeTableId, String destinationCidrBlock,
String destinationIpv6CidrBlock, String destinationPrefixListId, boolean dryRun) {
requireExactlyOneDestination("DeleteRoute", destinationCidrBlock, destinationIpv6CidrBlock,
destinationPrefixListId);
final String canonicalDestinationCidrBlock = canonicalizeIpv4Cidr(destinationCidrBlock);
@@ -13078,6 +13172,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
+ " in route table " + routeTableId, 400);
}
}
if (dryRun) {
throw dryRunOperation();
}
next.removeAll(matched);
current.setRoutes(next);
routeTables.put(key(region, routeTableId), current);