From b513d69313152419639c517d88a11e163ba2d802 Mon Sep 17 00:00:00 2001 From: Tyler Sanders Date: Tue, 9 Dec 2014 12:07:14 -0600 Subject: [PATCH] Add support for route table routes to target virtual private gateways. --- moto/ec2/models.py | 21 +++++++++++++++++---- moto/ec2/responses/route_tables.py | 12 ++++++------ tests/test_ec2/test_route_tables.py | 23 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 9c30cb119..1a5f0a21e 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -1817,12 +1817,12 @@ class RouteTableBackend(object): class Route(object): def __init__(self, route_table, destination_cidr_block, local=False, - internet_gateway=None, instance=None, interface=None, vpc_pcx=None): + gateway=None, instance=None, interface=None, vpc_pcx=None): self.id = generate_route_id(route_table.id, destination_cidr_block) self.route_table = route_table self.destination_cidr_block = destination_cidr_block self.local = local - self.internet_gateway = internet_gateway + self.gateway = gateway self.instance = instance self.interface = interface self.vpc_pcx = vpc_pcx @@ -1861,8 +1861,15 @@ class RouteBackend(object): if interface_id: self.raise_not_implemented_error("CreateRoute to NetworkInterfaceId") + gateway = None + if gateway_id: + if EC2_RESOURCE_TO_PREFIX['vpn-gateway'] in gateway_id: + gateway = self.get_vpn_gateway(gateway_id) + elif EC2_RESOURCE_TO_PREFIX['internet-gateway'] in gateway_id: + gateway = self.get_internet_gateway(gateway_id) + route = Route(route_table, destination_cidr_block, local=local, - internet_gateway=self.get_internet_gateway(gateway_id) if gateway_id else None, + gateway=gateway, instance=self.get_instance(instance_id) if instance_id else None, interface=None, vpc_pcx=self.get_vpc_peering_connection(vpc_peering_connection_id) if vpc_peering_connection_id else None) @@ -1879,7 +1886,13 @@ class RouteBackend(object): if interface_id: self.raise_not_implemented_error("ReplaceRoute to NetworkInterfaceId") - route.internet_gateway = self.get_internet_gateway(gateway_id) if gateway_id else None + route.gateway = None + if gateway_id: + if EC2_RESOURCE_TO_PREFIX['vpn-gateway'] in gateway_id: + route.gateway = self.get_vpn_gateway(gateway_id) + elif EC2_RESOURCE_TO_PREFIX['internet-gateway'] in gateway_id: + route.gateway = self.get_internet_gateway(gateway_id) + route.instance = self.get_instance(instance_id) if instance_id else None route.interface = None route.vpc_pcx = self.get_vpc_peering_connection(vpc_peering_connection_id) if vpc_peering_connection_id else None diff --git a/moto/ec2/responses/route_tables.py b/moto/ec2/responses/route_tables.py index f583cbbfb..632c671a8 100644 --- a/moto/ec2/responses/route_tables.py +++ b/moto/ec2/responses/route_tables.py @@ -17,13 +17,13 @@ class RouteTables(BaseResponse): route_table_id = self.querystring.get('RouteTableId')[0] destination_cidr_block = self.querystring.get('DestinationCidrBlock')[0] - internet_gateway_id = optional_from_querystring('GatewayId', self.querystring) + gateway_id = optional_from_querystring('GatewayId', self.querystring) instance_id = optional_from_querystring('InstanceId', self.querystring) interface_id = optional_from_querystring('NetworkInterfaceId', self.querystring) pcx_id = optional_from_querystring('VpcPeeringConnectionId', self.querystring) self.ec2_backend.create_route(route_table_id, destination_cidr_block, - gateway_id=internet_gateway_id, + gateway_id=gateway_id, instance_id=instance_id, interface_id=interface_id, vpc_peering_connection_id=pcx_id) @@ -67,13 +67,13 @@ class RouteTables(BaseResponse): route_table_id = self.querystring.get('RouteTableId')[0] destination_cidr_block = self.querystring.get('DestinationCidrBlock')[0] - internet_gateway_id = optional_from_querystring('GatewayId', self.querystring) + gateway_id = optional_from_querystring('GatewayId', self.querystring) instance_id = optional_from_querystring('InstanceId', self.querystring) interface_id = optional_from_querystring('NetworkInterfaceId', self.querystring) pcx_id = optional_from_querystring('VpcPeeringConnectionId', self.querystring) self.ec2_backend.replace_route(route_table_id, destination_cidr_block, - gateway_id=internet_gateway_id, + gateway_id=gateway_id, instance_id=instance_id, interface_id=interface_id, vpc_peering_connection_id=pcx_id) @@ -152,8 +152,8 @@ DESCRIBE_ROUTE_TABLES_RESPONSE = """ CreateRouteTable active {% endif %} - {% if route.internet_gateway %} - {{ route.internet_gateway.id }} + {% if route.gateway %} + {{ route.gateway.id }} CreateRoute active {% endif %} diff --git a/tests/test_ec2/test_route_tables.py b/tests/test_ec2/test_route_tables.py index e31d34125..38ccd09c8 100644 --- a/tests/test_ec2/test_route_tables.py +++ b/tests/test_ec2/test_route_tables.py @@ -419,6 +419,29 @@ def test_routes_vpc_peering_connection(): new_route.destination_cidr_block.should.equal(ROUTE_CIDR) +@requires_boto_gte("2.34.0") +@mock_ec2 +def test_routes_vpn_gateway(): + + conn = boto.connect_vpc('the_key', 'the_secret') + vpc = conn.create_vpc("10.0.0.0/16") + main_route_table = conn.get_all_route_tables(filters={'association.main':'true','vpc-id':vpc.id})[0] + ROUTE_CIDR = "10.0.0.4/24" + + vpn_gw = conn.create_vpn_gateway(type="ipsec.1") + + conn.create_route(main_route_table.id, ROUTE_CIDR, gateway_id=vpn_gw.id) + + main_route_table = conn.get_all_route_tables(main_route_table.id)[0] + new_routes = [route for route in main_route_table.routes if route.destination_cidr_block != vpc.cidr_block] + new_routes.should.have.length_of(1) + + new_route = new_routes[0] + new_route.gateway_id.should.equal(vpn_gw.id) + new_route.instance_id.should.be.none + new_route.vpc_peering_connection_id.should.be.none + + @mock_ec2 def test_network_acl_tagging():