Skip to content

Commit

Permalink
EC2: update_route() now correctly handles DestinationPreflixListId (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Zethex authored Dec 15, 2024
1 parent ea13ff2 commit 82c0c63
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
4 changes: 1 addition & 3 deletions moto/ec2/models/route_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,7 @@ def replace_route(
if destination_prefix_list_id:
cidr = destination_prefix_list_id
route_table = self.get_route_table(route_table_id)
route_id = generate_route_id(
route_table.id, destination_cidr_block, destination_ipv6_cidr_block
)
route_id = generate_route_id(route_table.id, cidr, destination_ipv6_cidr_block)
try:
route = route_table.routes[route_id]
except KeyError:
Expand Down
33 changes: 32 additions & 1 deletion tests/test_ec2/test_route_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,14 @@ def test_routes_replace():
]
)["RouteTables"][0]["RouteTableId"]
main_route_table = ec2.RouteTable(main_route_table_id)

# Various route sources
ROUTE_CIDR = "10.0.0.4/24"

prefix_list = client.create_managed_prefix_list(
PrefixListName="examplelist", MaxEntries=2, AddressFamily="?"
)["PrefixList"]

# Various route targets
igw = ec2.create_internet_gateway()

Expand All @@ -626,7 +632,8 @@ def get_target_route():
routes = [
route
for route in route_table["Routes"]
if route["DestinationCidrBlock"] != vpc.cidr_block
if "DestinationCidrBlock" not in route
or route["DestinationCidrBlock"] != vpc.cidr_block
]
assert len(routes) == 1
return routes[0]
Expand Down Expand Up @@ -692,6 +699,30 @@ def get_target_route():
# equivalent, but for some reason AWS returns InvalidParameterValue instead.
assert ex.value.response["Error"]["Code"] == "InvalidParameterValue"

# Remove route
client.delete_route(
RouteTableId=main_route_table.id, DestinationCidrBlock=ROUTE_CIDR
)

# Create prefix list source route
main_route_table.create_route(
DestinationPrefixListId=prefix_list["PrefixListId"], GatewayId=igw.id
)

# Replace...
client.replace_route(
RouteTableId=main_route_table.id,
DestinationPrefixListId=prefix_list["PrefixListId"],
InstanceId=instance.id,
)

target_route = get_target_route()
assert "GatewayId" not in target_route
assert target_route["InstanceId"] == instance.id
assert "NetworkInterfaceId" not in target_route
assert target_route["State"] == "active"
assert target_route["DestinationPrefixListId"] == prefix_list["PrefixListId"]


@mock_aws
def test_routes_already_exist():
Expand Down

0 comments on commit 82c0c63

Please sign in to comment.