Skip to content

Commit

Permalink
EC2: Implement ReplaceRoute to NetworkInterfaceId (#6236)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjohnes authored Apr 21, 2023
1 parent c6b0486 commit 462ff57
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
5 changes: 1 addition & 4 deletions moto/ec2/models/route_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,6 @@ def replace_route(
)
route = route_table.routes[route_id]

if interface_id:
self.raise_not_implemented_error("ReplaceRoute to NetworkInterfaceId") # type: ignore[attr-defined]

route.gateway = None
route.nat_gateway = None
route.egress_only_igw = None
Expand All @@ -466,7 +463,7 @@ def replace_route(
)

route.instance = self.get_instance(instance_id) if instance_id else None # type: ignore[attr-defined]
route.interface = None
route.interface = self.get_network_interface(interface_id) if interface_id else None # type: ignore[attr-defined]
route.vpc_pcx = (
self.get_vpc_peering_connection(vpc_peering_connection_id) # type: ignore[attr-defined]
if vpc_peering_connection_id
Expand Down
36 changes: 18 additions & 18 deletions tests/test_ec2/test_route_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ def test_routes_replace():
client = boto3.client("ec2", region_name="us-east-1")
ec2 = boto3.resource("ec2", region_name="us-east-1")
vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
subnet = ec2.create_subnet(VpcId=vpc.id, CidrBlock="10.0.0.0/24")

main_route_table_id = client.describe_route_tables(
Filters=[
Expand All @@ -575,6 +576,8 @@ def test_routes_replace():

instance = ec2.create_instances(ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1)[0]

eni = ec2.create_network_interface(SubnetId=subnet.id)

# Create initial route
main_route_table.create_route(DestinationCidrBlock=ROUTE_CIDR, GatewayId=igw.id)

Expand All @@ -600,6 +603,7 @@ def get_target_route():
target_route = get_target_route()
target_route.shouldnt.have.key("GatewayId")
target_route["InstanceId"].should.equal(instance.id)
target_route.shouldnt.have.key("NetworkInterfaceId")
target_route["State"].should.equal("active")
target_route["DestinationCidrBlock"].should.equal(ROUTE_CIDR)

Expand All @@ -612,6 +616,20 @@ def get_target_route():
target_route = get_target_route()
target_route["GatewayId"].should.equal(igw.id)
target_route.shouldnt.have.key("InstanceId")
target_route.shouldnt.have.key("NetworkInterfaceId")
target_route["State"].should.equal("active")
target_route["DestinationCidrBlock"].should.equal(ROUTE_CIDR)

client.replace_route(
RouteTableId=main_route_table.id,
DestinationCidrBlock=ROUTE_CIDR,
NetworkInterfaceId=eni.id,
)

target_route = get_target_route()
target_route.shouldnt.have.key("GatewayId")
target_route.shouldnt.have.key("InstanceId")
target_route["NetworkInterfaceId"].should.equal(eni.id)
target_route["State"].should.equal("active")
target_route["DestinationCidrBlock"].should.equal(ROUTE_CIDR)

Expand Down Expand Up @@ -681,11 +699,9 @@ def test_routes_already_exist():
@mock_ec2
def test_routes_not_supported():
client = boto3.client("ec2", region_name="us-east-1")
ec2 = boto3.resource("ec2", region_name="us-east-1")
main_route_table_id = client.describe_route_tables()["RouteTables"][0][
"RouteTableId"
]
main_route_table = ec2.RouteTable(main_route_table_id)

ROUTE_CIDR = "10.0.0.4/24"

Expand All @@ -702,22 +718,6 @@ def test_routes_not_supported():
"InvalidNetworkInterfaceID.NotFound"
)

igw = ec2.create_internet_gateway()
client.create_route(
RouteTableId=main_route_table_id,
DestinationCidrBlock=ROUTE_CIDR,
GatewayId=igw.id,
)

# Replace
if not settings.TEST_SERVER_MODE:
args = {
"RouteTableId": main_route_table.id,
"DestinationCidrBlock": ROUTE_CIDR,
"NetworkInterfaceId": "eni-1234abcd",
}
client.replace_route.when.called_with(**args).should.throw(NotImplementedError)


@mock_ec2
def test_routes_vpc_peering_connection():
Expand Down

0 comments on commit 462ff57

Please sign in to comment.