fixed route-table vpc-endpoint integration (#4285)

This commit is contained in:
Macwan Nevil 2021-09-17 03:19:49 +05:30 committed by GitHub
parent f84ba7d6ec
commit cb43134d44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 85 additions and 37 deletions

View File

@ -136,7 +136,6 @@ from .utils import (
random_transit_gateway_attachment_id,
random_transit_gateway_route_table_id,
random_vpc_ep_id,
randor_ipv4_cidr,
random_launch_template_id,
random_nat_gateway_id,
random_transit_gateway_id,
@ -3746,8 +3745,8 @@ class VPCBackend(object):
# validates if vpc is present or not.
self.get_vpc(vpc_id)
destination_prefix_list_id = None
service_destination_cidr = None
if type and type.lower() == "interface":
network_interface_ids = []
@ -3760,10 +3759,10 @@ class VPCBackend(object):
else:
# considering gateway if type is not mentioned.
service_destination_cidr = randor_ipv4_cidr()
for prefix_list in self.managed_prefix_lists.values():
if prefix_list.prefix_list_name == service_name:
destination_prefix_list_id = prefix_list.id
for route_table_id in route_table_ids:
self.create_route(route_table_id, service_destination_cidr)
if dns_entries:
dns_entries = [dns_entries]
@ -3782,15 +3781,23 @@ class VPCBackend(object):
security_group_ids,
tags,
private_dns_enabled,
service_destination_cidr,
destination_prefix_list_id,
)
self.vpc_end_points[vpc_endpoint_id] = vpc_end_point
if destination_prefix_list_id:
for route_table_id in route_table_ids:
self.create_route(
route_table_id,
None,
gateway_id=vpc_endpoint_id,
destination_prefix_list_id=destination_prefix_list_id,
)
return vpc_end_point
def delete_vpc_endpoints(self, vpce_ids=[]):
vpce_ids
for vpce_id in vpce_ids:
vpc_endpoint = self.vpc_end_points.get(vpce_id, None)
if vpc_endpoint:
@ -3800,7 +3807,7 @@ class VPCBackend(object):
else:
for route_table_id in vpc_endpoint.route_table_ids:
self.delete_route(
route_table_id, vpc_endpoint.service_destination_cidr
route_table_id, vpc_endpoint.destination_prefix_list_id
)
vpc_endpoint.state = "deleted"
return True
@ -3839,6 +3846,12 @@ class VPCBackend(object):
"availability_zones": availability_zones,
}
def get_vpc_end_point(self, vpc_end_point_id):
vpc_end_point = self.vpc_end_points.get(vpc_end_point_id)
if not vpc_end_point:
raise InvalidVpcEndPointIdError(vpc_end_point_id)
return vpc_end_point
class PeeringConnectionStatus(object):
def __init__(self, code="initiating-request", message=""):
@ -4863,7 +4876,7 @@ class Route(CloudFormationModel):
route_table,
destination_cidr_block,
destination_ipv6_cidr_block,
prefix_list=None,
destination_prefix_list=None,
local=False,
gateway=None,
instance=None,
@ -4875,12 +4888,15 @@ class Route(CloudFormationModel):
carrier_gateway=None,
):
self.id = generate_route_id(
route_table.id, destination_cidr_block, destination_ipv6_cidr_block
route_table.id,
destination_cidr_block,
destination_ipv6_cidr_block,
destination_prefix_list.id if destination_prefix_list else None,
)
self.route_table = route_table
self.destination_cidr_block = destination_cidr_block
self.destination_ipv6_cidr_block = destination_ipv6_cidr_block
self.prefix_list = prefix_list
self.destination_prefix_list = destination_prefix_list
self.local = local
self.gateway = gateway
self.instance = instance
@ -4951,7 +4967,7 @@ class VPCEndPoint(TaggedEC2Resource):
security_group_ids=None,
tags=None,
private_dns_enabled=None,
service_destination_cidr=None,
destination_prefix_list_id=None,
):
self.ec2_backend = ec2_backend
self.id = id
@ -4966,10 +4982,9 @@ class VPCEndPoint(TaggedEC2Resource):
self.client_token = client_token
self.security_group_ids = security_group_ids
self.private_dns_enabled = private_dns_enabled
# self.created_at = utc_date_and_time()
self.dns_entries = dns_entries
self.add_tags(tags or {})
self.service_destination_cidr = service_destination_cidr
self.destination_prefix_list_id = destination_prefix_list_id
@property
def owner_id(self):
@ -5169,7 +5184,7 @@ class RouteBackend(object):
transit_gateway = None
egress_only_igw = None
interface = None
prefix_list = None
destination_prefix_list = None
carrier_gateway = None
route_table = self.get_route_table(route_table_id)
@ -5184,6 +5199,8 @@ class RouteBackend(object):
gateway = self.get_vpn_gateway(gateway_id)
elif EC2_RESOURCE_TO_PREFIX["internet-gateway"] in gateway_id:
gateway = self.get_internet_gateway(gateway_id)
elif EC2_RESOURCE_TO_PREFIX["vpc-endpoint"] in gateway_id:
gateway = self.get_vpc_end_point(gateway_id)
try:
if destination_cidr_block:
@ -5198,7 +5215,9 @@ class RouteBackend(object):
if transit_gateway_id is not None:
transit_gateway = self.transit_gateways.get(transit_gateway_id)
if destination_prefix_list_id is not None:
prefix_list = self.managed_prefix_lists.get(destination_prefix_list_id)
destination_prefix_list = self.managed_prefix_lists.get(
destination_prefix_list_id
)
if carrier_gateway_id is not None:
carrier_gateway = self.carrier_gateways.get(carrier_gateway_id)
@ -5206,7 +5225,7 @@ class RouteBackend(object):
route_table,
destination_cidr_block,
destination_ipv6_cidr_block,
prefix_list,
destination_prefix_list,
local=local,
gateway=gateway,
instance=self.get_instance(instance_id) if instance_id else None,
@ -5283,12 +5302,18 @@ class RouteBackend(object):
return route_table.get(route_id)
def delete_route(
self, route_table_id, destination_cidr_block, destination_ipv6_cidr_block=None
self,
route_table_id,
destination_cidr_block,
destination_ipv6_cidr_block=None,
destination_prefix_list_id=None,
):
cidr = destination_cidr_block
route_table = self.get_route_table(route_table_id)
if destination_ipv6_cidr_block:
cidr = destination_ipv6_cidr_block
if destination_prefix_list_id:
cidr = destination_prefix_list_id
route_id = generate_route_id(route_table_id, cidr)
deleted = route_table.routes.pop(route_id, None)
if not deleted:

View File

@ -59,8 +59,12 @@ class RouteTables(BaseResponse):
route_table_id = self._get_param("RouteTableId")
destination_cidr_block = self._get_param("DestinationCidrBlock")
destination_ipv6_cidr_block = self._get_param("DestinationIpv6CidrBlock")
destination_prefix_list_id = self._get_param("DestinationPrefixListId")
self.ec2_backend.delete_route(
route_table_id, destination_cidr_block, destination_ipv6_cidr_block
route_table_id,
destination_cidr_block,
destination_ipv6_cidr_block,
destination_prefix_list_id,
)
template = self.response_template(DELETE_ROUTE_RESPONSE)
return template.render()
@ -151,9 +155,13 @@ CREATE_ROUTE_TABLE_RESPONSE = """
<item>
{% if route.destination_ipv6_cidr_block %}
<destinationIpv6CidrBlock>{{ route.destination_ipv6_cidr_block }}</destinationIpv6CidrBlock>
{% else %}
{% endif %}
{% if route.destination_cidr_block %}
<destinationCidrBlock>{{ route.destination_cidr_block }}</destinationCidrBlock>
{% endif %}
{% if route.destination_prefix_list_id %}
<destinationPrefixListId>{{ route.destination_prefix_list_id }}</destinationPrefixListId>
{% endif %}
<gatewayId>local</gatewayId>
<state>active</state>
</item>
@ -189,19 +197,18 @@ DESCRIBE_ROUTE_TABLES_RESPONSE = """
<item>
{% if route.destination_ipv6_cidr_block %}
<destinationIpv6CidrBlock>{{ route.destination_ipv6_cidr_block }}</destinationIpv6CidrBlock>
{% else %}
<destinationCidrBlock>{{ route.destination_cidr_block or "" }}</destinationCidrBlock>
{% endif %}
{% if route.destination_cidr_block %}
<destinationCidrBlock>{{ route.destination_cidr_block }}</destinationCidrBlock>
{% endif %}
{% if route.destination_prefix_list %}
<destinationPrefixListId>{{ route.destination_prefix_list.id }}</destinationPrefixListId>
{% endif %}
{% if route.local %}
<gatewayId>local</gatewayId>
<origin>CreateRouteTable</origin>
<state>active</state>
{% endif %}
{% if route.prefix_list %}
<destinationPrefixListId>{{ route.prefix_list.id }}</destinationPrefixListId>
<origin>CreateRoute</origin>
<state>active</state>
{% endif %}
{% if route.gateway %}
<gatewayId>{{ route.gateway.id }}</gatewayId>
<origin>CreateRoute</origin>
@ -215,27 +222,31 @@ DESCRIBE_ROUTE_TABLES_RESPONSE = """
{% if route.vpc_pcx %}
<vpcPeeringConnectionId>{{ route.vpc_pcx.id }}</vpcPeeringConnectionId>
<origin>CreateRoute</origin>
<state>blackhole</state>
<state>active</state>
{% endif %}
{% if route.carrier_gateway %}
<carrierGatewayId>{{ route.carrier_gateway.id }}</carrierGatewayId>
<origin>CreateRoute</origin>
<state>blackhole</state>
<state>active</state>
{% endif %}
{% if route.nat_gateway %}
<natGatewayId>{{ route.nat_gateway.id }}</natGatewayId>
<origin>CreateRoute</origin>
<state>active</state>
{% endif %}
{% if route.egress_only_igw %}
<egressOnlyInternetGatewayId>{{ route.egress_only_igw.id }}</egressOnlyInternetGatewayId>
<origin>CreateRoute</origin>
<state>active</state>
{% endif %}
{% if route.transit_gateway %}
<transitGatewayId>{{ route.transit_gateway.id }}</transitGatewayId>
<origin>CreateRoute</origin>
<state>active</state>
{% endif %}
{% if route.interface %}
<networkInterfaceId>{{ route.interface.id }}</networkInterfaceId>
<origin>CreateRoute</origin>
<state>active</state>
{% endif %}
</item>

View File

@ -257,9 +257,13 @@ def random_ipv6_cidr():
return "2400:6500:{}:{}::/56".format(random_resource_id(4), random_resource_id(4))
def generate_route_id(route_table_id, cidr_block, ipv6_cidr_block=None):
def generate_route_id(
route_table_id, cidr_block, ipv6_cidr_block=None, prefix_list=None
):
if ipv6_cidr_block and not cidr_block:
cidr_block = ipv6_cidr_block
if prefix_list and not cidr_block:
cidr_block = prefix_list
return "%s~%s" % (route_table_id, cidr_block)

View File

@ -115,6 +115,8 @@ TestAccAWSRouteTable_MultipleRoutes
TestAccAWSRouteTable_PrefixList_To_InternetGateway
TestAccAWSRouteTable_VpcMultipleCidrs
TestAccAWSRouteTable_IPv4_To_CarrierGateway
TestAccAWSRouteTable_IPv4_To_InternetGateway
TestAccAWSRouteTable_GatewayVpcEndpoint
TestAccAWSSsmDocumentDataSource
TestAccAwsEc2ManagedPrefixList
TestAccAWSEgressOnlyInternetGateway

View File

@ -502,7 +502,7 @@ def test_routes_vpc_peering_connection():
new_route.gateway_id.should.be.none
new_route.instance_id.should.be.none
new_route.vpc_peering_connection_id.should.equal(vpc_pcx.id)
new_route.state.should.equal("blackhole")
new_route.state.should.equal("active")
new_route.destination_cidr_block.should.equal(ROUTE_CIDR)
@ -748,13 +748,19 @@ def test_create_route_with_egress_only_igw():
route_table = ec2.create_route_table(VpcId=vpc.id)
ec2_client.create_route(
RouteTableId=route_table.id, EgressOnlyInternetGatewayId=eigw_id
RouteTableId=route_table.id,
EgressOnlyInternetGatewayId=eigw_id,
DestinationIpv6CidrBlock="::/0",
)
route_table.reload()
eigw_route = [r for r in route_table.routes if r.destination_cidr_block == ""][0]
eigw_route.egress_only_internet_gateway_id.should.equal(eigw_id)
eigw_route.state.should.equal("active")
eigw_route = [
r
for r in route_table.routes_attribute
if r.get("DestinationIpv6CidrBlock") == "::/0"
][0]
eigw_route.get("EgressOnlyInternetGatewayId").should.equal(eigw_id)
eigw_route.get("State").should.equal("active")
@mock_ec2

View File

@ -955,13 +955,13 @@ def test_delete_vpc_end_points():
route_table = ec2.create_route_table(VpcId=vpc["Vpc"]["VpcId"])
vpc_end_point1 = ec2.create_vpc_endpoint(
VpcId=vpc["Vpc"]["VpcId"],
ServiceName="com.amazonaws.us-east-1.s3",
ServiceName="com.amazonaws.us-west-1.s3",
RouteTableIds=[route_table["RouteTable"]["RouteTableId"]],
VpcEndpointType="gateway",
)["VpcEndpoint"]
vpc_end_point2 = ec2.create_vpc_endpoint(
VpcId=vpc["Vpc"]["VpcId"],
ServiceName="com.amazonaws.us-east-2.s3",
ServiceName="com.amazonaws.us-west-1.s3",
RouteTableIds=[route_table["RouteTable"]["RouteTableId"]],
VpcEndpointType="gateway",
)