<s> from neutronclient . common import exceptions as neutron_exception <EOL> from oslo_log import log as logging <EOL> from ec2api . api import common <EOL> from ec2api . api import ec2utils <EOL> from ec2api . api import vpn_connection as vpn_connection_api <EOL> from ec2api import clients <EOL> from ec2api . db import api as db_api <EOL> from ec2api import exception <EOL> from ec2api . i18n import _ <EOL> LOG = logging . getLogger ( __name__ ) <EOL> """<STR_LIT>""" <EOL> Validator = common . Validator <EOL> def create_vpn_gateway ( context , type , availability_zone = None ) : <EOL> vpn_gateway = db_api . add_item ( context , '<STR_LIT>' , { } ) <EOL> return { '<STR_LIT>' : _format_vpn_gateway ( vpn_gateway ) } <EOL> def attach_vpn_gateway ( context , vpc_id , vpn_gateway_id ) : <EOL> vpn_gateway = ec2utils . get_db_item ( context , vpn_gateway_id ) <EOL> vpc = ec2utils . get_db_item ( context , vpc_id ) <EOL> if vpn_gateway [ '<STR_LIT>' ] and vpn_gateway [ '<STR_LIT>' ] != vpc [ '<STR_LIT:id>' ] : <EOL> raise exception . VpnGatewayAttachmentLimitExceeded ( ) <EOL> attached_vgw = ec2utils . get_attached_gateway ( context , vpc [ '<STR_LIT:id>' ] , '<STR_LIT>' ) <EOL> if attached_vgw and attached_vgw [ '<STR_LIT:id>' ] != vpn_gateway [ '<STR_LIT:id>' ] : <EOL> raise exception . InvalidVpcState ( vpc_id = vpc [ '<STR_LIT:id>' ] , <EOL> vgw_id = attached_vgw [ '<STR_LIT:id>' ] ) <EOL> subnets = [ subnet for subnet in db_api . get_items ( context , '<STR_LIT>' ) <EOL> if subnet [ '<STR_LIT>' ] == vpc [ '<STR_LIT:id>' ] ] <EOL> if not vpn_gateway [ '<STR_LIT>' ] : <EOL> external_network_id = None <EOL> if not ec2utils . get_attached_gateway ( context , vpc [ '<STR_LIT:id>' ] , '<STR_LIT>' ) : <EOL> external_network_id = ec2utils . get_os_public_network ( context ) [ '<STR_LIT:id>' ] <EOL> neutron = clients . neutron ( context ) <EOL> with common . OnCrashCleaner ( ) as cleaner : <EOL> _attach_vpn_gateway_item ( context , vpn_gateway , vpc [ '<STR_LIT:id>' ] ) <EOL> cleaner . addCleanup ( _detach_vpn_gateway_item , context , vpn_gateway ) <EOL> if external_network_id : <EOL> neutron . add_gateway_router ( vpc [ '<STR_LIT>' ] , <EOL> { '<STR_LIT>' : external_network_id } ) <EOL> cleaner . addCleanup ( neutron . remove_gateway_router , vpc [ '<STR_LIT>' ] ) <EOL> for subnet in subnets : <EOL> _create_subnet_vpnservice ( context , neutron , cleaner , <EOL> subnet , vpc ) <EOL> vpn_connection_api . _reset_vpn_connections ( <EOL> context , neutron , cleaner , vpn_gateway , subnets = subnets ) <EOL> return { '<STR_LIT>' : _format_attachment ( vpn_gateway ) } <EOL> def detach_vpn_gateway ( context , vpc_id , vpn_gateway_id ) : <EOL> vpn_gateway = ec2utils . get_db_item ( context , vpn_gateway_id ) <EOL> if vpn_gateway [ '<STR_LIT>' ] != vpc_id : <EOL> raise exception . InvalidVpnGatewayAttachmentNotFound ( <EOL> vgw_id = vpn_gateway_id , vpc_id = vpc_id ) <EOL> vpc = db_api . get_item_by_id ( context , vpc_id ) <EOL> neutron = clients . neutron ( context ) <EOL> remove_os_gateway_router = ( <EOL> ec2utils . get_attached_gateway ( context , vpc_id , '<STR_LIT>' ) is None ) <EOL> subnets = [ subnet for subnet in db_api . get_items ( context , '<STR_LIT>' ) <EOL> if subnet [ '<STR_LIT>' ] == vpc [ '<STR_LIT:id>' ] ] <EOL> with common . OnCrashCleaner ( ) as cleaner : <EOL> _detach_vpn_gateway_item ( context , vpn_gateway ) <EOL> cleaner . addCleanup ( _attach_vpn_gateway_item , context , vpn_gateway , <EOL> vpc_id ) <EOL> vpn_connection_api . _stop_gateway_vpn_connections ( <EOL> context , neutron , cleaner , vpn_gateway ) <EOL> for subnet in subnets : <EOL> _delete_subnet_vpnservice ( context , neutron , cleaner , subnet ) <EOL> if remove_os_gateway_router : <EOL> try : <EOL> neutron . remove_gateway_router ( vpc [ '<STR_LIT>' ] ) <EOL> except neutron_exception . NotFound : <EOL> pass <EOL> return True <EOL> def delete_vpn_gateway ( context , vpn_gateway_id ) : <EOL> vpn_gateway = ec2utils . get_db_item ( context , vpn_gateway_id ) <EOL> vpn_connections = db_api . get_items ( context , '<STR_LIT>' ) <EOL> if vpn_gateway [ '<STR_LIT>' ] or any ( vpn [ '<STR_LIT>' ] == vpn_gateway [ '<STR_LIT:id>' ] <EOL> for vpn in vpn_connections ) : <EOL> raise exception . IncorrectState ( reason = _ ( '<STR_LIT>' ) ) <EOL> db_api . delete_item ( context , vpn_gateway [ '<STR_LIT:id>' ] ) <EOL> return True <EOL> def describe_vpn_gateways ( context , vpn_gateway_id = None , filter = None ) : <EOL> formatted_vgws = VpnGatewayDescriber ( ) . describe ( <EOL> context , ids = vpn_gateway_id , filter = filter ) <EOL> return { '<STR_LIT>' : formatted_vgws } <EOL> class VpnGatewayDescriber ( common . TaggableItemsDescriber , <EOL> common . NonOpenstackItemsDescriber ) : <EOL> KIND = '<STR_LIT>' <EOL> FILTER_MAP = { '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:state>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT:state>' : '<STR_LIT:state>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:type>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> def format ( self , vpn_gateway ) : <EOL> return _format_vpn_gateway ( vpn_gateway ) <EOL> def _format_vpn_gateway ( vpn_gateway ) : <EOL> ec2_vgw = { '<STR_LIT>' : vpn_gateway [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:state>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ ] } <EOL> if vpn_gateway [ '<STR_LIT>' ] : <EOL> ec2_vgw [ '<STR_LIT>' ] . append ( _format_attachment ( vpn_gateway ) ) <EOL> return ec2_vgw <EOL> def _format_attachment ( vpn_gateway ) : <EOL> return { '<STR_LIT:state>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : vpn_gateway [ '<STR_LIT>' ] } <EOL> def _start_vpn_in_subnet ( context , neutron , cleaner , subnet , vpc , route_table ) : <EOL> vpn_gateway = ec2utils . get_attached_gateway ( context , vpc [ '<STR_LIT:id>' ] , '<STR_LIT>' ) <EOL> if not vpn_gateway : <EOL> return <EOL> _create_subnet_vpnservice ( context , neutron , cleaner , subnet , vpc ) <EOL> vpn_connection_api . _reset_vpn_connections ( context , neutron , cleaner , <EOL> vpn_gateway , subnets = [ subnet ] , <EOL> route_tables = [ route_table ] ) <EOL> def _stop_vpn_in_subnet ( context , neutron , cleaner , subnet ) : <EOL> os_vpnservice_id = subnet . get ( '<STR_LIT>' ) <EOL> if not os_vpnservice_id : <EOL> return <EOL> for vpn in db_api . get_items ( context , '<STR_LIT>' ) : <EOL> vpn_connection_api . _delete_subnet_vpn ( context , neutron , cleaner , <EOL> subnet , vpn ) <EOL> _safe_delete_vpnservice ( neutron , os_vpnservice_id , subnet [ '<STR_LIT:id>' ] ) <EOL> def _create_subnet_vpnservice ( context , neutron , cleaner , subnet , vpc ) : <EOL> os_vpnservice = { '<STR_LIT>' : subnet [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : vpc [ '<STR_LIT>' ] , <EOL> '<STR_LIT:name>' : subnet [ '<STR_LIT:id>' ] } <EOL> os_vpnservice = neutron . create_vpnservice ( <EOL> { '<STR_LIT>' : os_vpnservice } ) [ '<STR_LIT>' ] <EOL> cleaner . addCleanup ( neutron . delete_vpnservice , os_vpnservice [ '<STR_LIT:id>' ] ) <EOL> _set_vpnservice_in_subnet_item ( context , subnet , os_vpnservice [ '<STR_LIT:id>' ] ) <EOL> cleaner . addCleanup ( _clear_vpnservice_in_subnet_item , <EOL> context , subnet ) <EOL> def _delete_subnet_vpnservice ( context , neutron , cleaner , subnet ) : <EOL> os_vpnservice_id = subnet [ '<STR_LIT>' ] <EOL> _clear_vpnservice_in_subnet_item ( context , subnet ) <EOL> cleaner . addCleanup ( _set_vpnservice_in_subnet_item , <EOL> context , subnet , os_vpnservice_id ) <EOL> _safe_delete_vpnservice ( neutron , os_vpnservice_id , subnet [ '<STR_LIT:id>' ] ) <EOL> def _safe_delete_vpnservice ( neutron , os_vpnservice_id , subnet_id ) : <EOL> try : <EOL> neutron . delete_vpnservice ( os_vpnservice_id ) <EOL> except neutron_exception . NotFound : <EOL> pass <EOL> except neutron_exception . Conflict as ex : <EOL> LOG . warning ( <EOL> _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT:id>' : subnet_id , <EOL> '<STR_LIT>' : os_vpnservice_id , <EOL> '<STR_LIT>' : ex . message } ) <EOL> def _attach_vpn_gateway_item ( context , vpn_gateway , vpc_id ) : <EOL> vpn_gateway [ '<STR_LIT>' ] = vpc_id <EOL> db_api . update_item ( context , vpn_gateway ) <EOL> def _detach_vpn_gateway_item ( context , vpn_gateway ) : <EOL> vpn_gateway [ '<STR_LIT>' ] = None <EOL> db_api . update_item ( context , vpn_gateway ) <EOL> def _set_vpnservice_in_subnet_item ( context , subnet , os_vpnservice_id ) : <EOL> subnet [ '<STR_LIT>' ] = os_vpnservice_id <EOL> db_api . update_item ( context , subnet ) <EOL> def _clear_vpnservice_in_subnet_item ( context , subnet ) : <EOL> del subnet [ '<STR_LIT>' ] <EOL> db_api . update_item ( context , subnet ) </s>
