from ._base_response import EC2BaseResponse class RouteTables(EC2BaseResponse): def associate_route_table(self): route_table_id = self._get_param("RouteTableId") gateway_id = self._get_param("GatewayId") subnet_id = self._get_param("SubnetId") association_id = self.ec2_backend.associate_route_table( route_table_id, gateway_id, subnet_id ) template = self.response_template(ASSOCIATE_ROUTE_TABLE_RESPONSE) return template.render(association_id=association_id) def create_route(self): 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") gateway_id = self._get_param("GatewayId") instance_id = self._get_param("InstanceId") nat_gateway_id = self._get_param("NatGatewayId") egress_only_igw_id = self._get_param("EgressOnlyInternetGatewayId") transit_gateway_id = self._get_param("TransitGatewayId") interface_id = self._get_param("NetworkInterfaceId") pcx_id = self._get_param("VpcPeeringConnectionId") carrier_gateway_id = self._get_param("CarrierGatewayId") self.ec2_backend.create_route( route_table_id, destination_cidr_block, destination_ipv6_cidr_block, destination_prefix_list_id, gateway_id=gateway_id, instance_id=instance_id, nat_gateway_id=nat_gateway_id, egress_only_igw_id=egress_only_igw_id, transit_gateway_id=transit_gateway_id, interface_id=interface_id, vpc_peering_connection_id=pcx_id, carrier_gateway_id=carrier_gateway_id, ) template = self.response_template(CREATE_ROUTE_RESPONSE) return template.render() def create_route_table(self): vpc_id = self._get_param("VpcId") tags = self._get_multi_param("TagSpecification", skip_result_conversion=True) if tags: tags = tags[0].get("Tag") or [] route_table = self.ec2_backend.create_route_table(vpc_id, tags) template = self.response_template(CREATE_ROUTE_TABLE_RESPONSE) return template.render(route_table=route_table) def delete_route(self): 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, destination_prefix_list_id, ) template = self.response_template(DELETE_ROUTE_RESPONSE) return template.render() def delete_route_table(self): route_table_id = self._get_param("RouteTableId") self.ec2_backend.delete_route_table(route_table_id) template = self.response_template(DELETE_ROUTE_TABLE_RESPONSE) return template.render() def describe_route_tables(self): route_table_ids = self._get_multi_param("RouteTableId") filters = self._filters_from_querystring() route_tables = self.ec2_backend.describe_route_tables(route_table_ids, filters) template = self.response_template(DESCRIBE_ROUTE_TABLES_RESPONSE) return template.render(route_tables=route_tables) def disassociate_route_table(self): association_id = self._get_param("AssociationId") self.ec2_backend.disassociate_route_table(association_id) template = self.response_template(DISASSOCIATE_ROUTE_TABLE_RESPONSE) return template.render() def replace_route(self): 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") gateway_id = self._get_param("GatewayId") instance_id = self._get_param("InstanceId") interface_id = self._get_param("NetworkInterfaceId") pcx_id = self._get_param("VpcPeeringConnectionId") nat_gateway_id = self._get_param("NatGatewayId") egress_only_igw_id = self._get_param("EgressOnlyInternetGatewayId") transit_gateway_id = self._get_param("TransitGatewayId") self.ec2_backend.replace_route( route_table_id, destination_cidr_block, destination_ipv6_cidr_block, destination_prefix_list_id, nat_gateway_id, egress_only_igw_id, transit_gateway_id, gateway_id=gateway_id, instance_id=instance_id, interface_id=interface_id, vpc_peering_connection_id=pcx_id, ) template = self.response_template(REPLACE_ROUTE_RESPONSE) return template.render() def replace_route_table_association(self): route_table_id = self._get_param("RouteTableId") association_id = self._get_param("AssociationId") new_association_id = self.ec2_backend.replace_route_table_association( association_id, route_table_id ) template = self.response_template(REPLACE_ROUTE_TABLE_ASSOCIATION_RESPONSE) return template.render(association_id=new_association_id) CREATE_ROUTE_RESPONSE = """ 59dbff89-35bd-4eac-99ed-be587EXAMPLE true """ REPLACE_ROUTE_RESPONSE = """ 59dbff89-35bd-4eac-99ed-be587EXAMPLE true """ CREATE_ROUTE_TABLE_RESPONSE = """ 59dbff89-35bd-4eac-99ed-be587EXAMPLE {{ route_table.id }} {{ route_table.vpc_id }} {{ route_table.owner_id }} {% for route in route_table.routes.values() %} {% if route.local %} {% if route.destination_ipv6_cidr_block %} {{ route.destination_ipv6_cidr_block }} {% endif %} {% if route.destination_cidr_block %} {{ route.destination_cidr_block }} {% endif %} {% if route.destination_prefix_list_id %} {{ route.destination_prefix_list_id }} {% endif %} local active {% endif %} {% endfor %} {% for tag in route_table.get_tags() %} {{ tag.resource_id }} {{ tag.resource_type }} {{ tag.key }} {{ tag.value }} {% endfor %} """ DESCRIBE_ROUTE_TABLES_RESPONSE = """ 6f570b0b-9c18-4b07-bdec-73740dcf861a {% for route_table in route_tables %} {{ route_table.id }} {{ route_table.vpc_id }} {{ route_table.owner_id }} {% for route in route_table.routes.values() %} {% if route.destination_ipv6_cidr_block %} {{ route.destination_ipv6_cidr_block }} {% endif %} {% if route.destination_cidr_block %} {{ route.destination_cidr_block }} {% endif %} {% if route.destination_prefix_list %} {{ route.destination_prefix_list.id }} {% endif %} {% if route.local %} local CreateRouteTable active {% endif %} {% if route.gateway %} {{ route.gateway.id }} CreateRoute active {% endif %} {% if route.instance %} {{ route.instance.id }} CreateRoute active {% endif %} {% if route.vpc_pcx %} {{ route.vpc_pcx.id }} CreateRoute active {% endif %} {% if route.carrier_gateway %} {{ route.carrier_gateway.id }} CreateRoute active {% endif %} {% if route.nat_gateway %} {{ route.nat_gateway.id }} CreateRoute active {% endif %} {% if route.egress_only_igw %} {{ route.egress_only_igw.id }} CreateRoute active {% endif %} {% if route.transit_gateway %} {{ route.transit_gateway.id }} CreateRoute active {% endif %} {% if route.interface %} {{ route.interface.id }} CreateRoute active {% endif %} {% endfor %} {{ route_table.main_association }} {{ route_table.id }}
true
associated
{% for association_id,subnet_id in route_table.associations.items() %} {{ association_id }} {{ route_table.id }}
false
{% if subnet_id.startswith("igw") %} {{ subnet_id }} {% endif %} {% if subnet_id.startswith("subnet") %} {{ subnet_id }} {% endif %} associated
{% endfor %}
{% for tag in route_table.get_tags() %} {{ tag.resource_id }} {{ tag.resource_type }} {{ tag.key }} {{ tag.value }} {% endfor %}
{% endfor %}
""" DELETE_ROUTE_RESPONSE = """ 59dbff89-35bd-4eac-99ed-be587EXAMPLE true """ DELETE_ROUTE_TABLE_RESPONSE = """ 59dbff89-35bd-4eac-99ed-be587EXAMPLE true """ ASSOCIATE_ROUTE_TABLE_RESPONSE = """ 59dbff89-35bd-4eac-99ed-be587EXAMPLE {{ association_id }} """ DISASSOCIATE_ROUTE_TABLE_RESPONSE = """ 59dbff89-35bd-4eac-99ed-be587EXAMPLE true """ REPLACE_ROUTE_TABLE_ASSOCIATION_RESPONSE = """ 59dbff89-35bd-4eac-99ed-be587EXAMPLE {{ association_id }} """