<s> import netaddr <EOL> import netaddr . core as netexc <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> import six <EOL> from webob import exc <EOL> from nova . api . openstack . compute . schemas import tenant_networks as schema <EOL> from nova . api . openstack import extensions <EOL> from nova . api . openstack import wsgi <EOL> from nova . api import validation <EOL> from nova import context as nova_context <EOL> from nova import exception <EOL> from nova . i18n import _ <EOL> from nova . i18n import _LE <EOL> import nova . network <EOL> from nova import quota <EOL> CONF = cfg . CONF <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> ALIAS = '<STR_LIT>' <EOL> QUOTAS = quota . QUOTAS <EOL> LOG = logging . getLogger ( __name__ ) <EOL> authorize = extensions . os_compute_authorizer ( ALIAS ) <EOL> def network_dict ( network ) : <EOL> return { "<STR_LIT:id>" : network . get ( "<STR_LIT>" , None ) or network . get ( "<STR_LIT:id>" , None ) , <EOL> "<STR_LIT>" : str ( network . get ( "<STR_LIT>" , None ) ) , <EOL> "<STR_LIT:label>" : network . get ( "<STR_LIT:label>" , None ) } <EOL> class TenantNetworkController ( wsgi . Controller ) : <EOL> def __init__ ( self , network_api = None ) : <EOL> self . network_api = nova . network . API ( skip_policy_check = True ) <EOL> self . _default_networks = [ ] <EOL> def _refresh_default_networks ( self ) : <EOL> self . _default_networks = [ ] <EOL> if CONF . use_neutron_default_nets == "<STR_LIT:True>" : <EOL> try : <EOL> self . _default_networks = self . _get_default_networks ( ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) ) <EOL> def _get_default_networks ( self ) : <EOL> project_id = CONF . neutron_default_tenant_id <EOL> ctx = nova_context . RequestContext ( user_id = None , <EOL> project_id = project_id ) <EOL> networks = { } <EOL> for n in self . network_api . get_all ( ctx ) : <EOL> networks [ n [ '<STR_LIT:id>' ] ] = n [ '<STR_LIT:label>' ] <EOL> return [ { '<STR_LIT:id>' : k , '<STR_LIT:label>' : v } for k , v in six . iteritems ( networks ) ] <EOL> @ extensions . expected_errors ( ( ) ) <EOL> def index ( self , req ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> networks = list ( self . network_api . get_all ( context ) ) <EOL> if not self . _default_networks : <EOL> self . _refresh_default_networks ( ) <EOL> networks . extend ( self . _default_networks ) <EOL> return { '<STR_LIT>' : [ network_dict ( n ) for n in networks ] } <EOL> @ extensions . expected_errors ( <NUM_LIT> ) <EOL> def show ( self , req , id ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> try : <EOL> network = self . network_api . get ( context , id ) <EOL> except exception . NetworkNotFound : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPNotFound ( explanation = msg ) <EOL> return { '<STR_LIT>' : network_dict ( network ) } <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ wsgi . response ( <NUM_LIT> ) <EOL> def delete ( self , req , id ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> reservation = None <EOL> try : <EOL> if CONF . enable_network_quota : <EOL> reservation = QUOTAS . reserve ( context , networks = - <NUM_LIT:1> ) <EOL> except Exception : <EOL> reservation = None <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> def _rollback_quota ( reservation ) : <EOL> if CONF . enable_network_quota and reservation : <EOL> QUOTAS . rollback ( context , reservation ) <EOL> try : <EOL> self . network_api . disassociate ( context , id ) <EOL> self . network_api . delete ( context , id ) <EOL> except exception . PolicyNotAuthorized as e : <EOL> _rollback_quota ( reservation ) <EOL> raise exc . HTTPForbidden ( explanation = six . text_type ( e ) ) <EOL> except exception . NetworkInUse as e : <EOL> _rollback_quota ( reservation ) <EOL> raise exc . HTTPConflict ( explanation = e . format_message ( ) ) <EOL> except exception . NetworkNotFound : <EOL> _rollback_quota ( reservation ) <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPNotFound ( explanation = msg ) <EOL> if CONF . enable_network_quota and reservation : <EOL> QUOTAS . commit ( context , reservation ) <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ validation . schema ( schema . create ) <EOL> def create ( self , req , body ) : <EOL> context = req . environ [ "<STR_LIT>" ] <EOL> authorize ( context ) <EOL> network = body [ "<STR_LIT>" ] <EOL> keys = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> kwargs = { k : network . get ( k ) for k in keys } <EOL> label = network [ "<STR_LIT:label>" ] <EOL> if kwargs [ "<STR_LIT>" ] : <EOL> try : <EOL> net = netaddr . IPNetwork ( kwargs [ "<STR_LIT>" ] ) <EOL> if net . size < <NUM_LIT:4> : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> except netexc . AddrConversionError : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> networks = [ ] <EOL> try : <EOL> if CONF . enable_network_quota : <EOL> reservation = QUOTAS . reserve ( context , networks = <NUM_LIT:1> ) <EOL> except exception . OverQuota : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> kwargs [ '<STR_LIT>' ] = context . project_id <EOL> try : <EOL> networks = self . network_api . create ( context , <EOL> label = label , ** kwargs ) <EOL> if CONF . enable_network_quota : <EOL> QUOTAS . commit ( context , reservation ) <EOL> except exception . PolicyNotAuthorized as e : <EOL> raise exc . HTTPForbidden ( explanation = six . text_type ( e ) ) <EOL> except exception . CidrConflict as e : <EOL> raise exc . HTTPConflict ( explanation = e . format_message ( ) ) <EOL> except Exception : <EOL> if CONF . enable_network_quota : <EOL> QUOTAS . rollback ( context , reservation ) <EOL> msg = _ ( "<STR_LIT>" ) <EOL> LOG . exception ( msg , extra = network ) <EOL> raise exc . HTTPServiceUnavailable ( explanation = msg ) <EOL> return { "<STR_LIT>" : network_dict ( networks [ <NUM_LIT:0> ] ) } <EOL> class TenantNetworks ( extensions . V21APIExtensionBase ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> alias = ALIAS <EOL> version = <NUM_LIT:1> <EOL> def get_resources ( self ) : <EOL> ext = extensions . ResourceExtension ( ALIAS , TenantNetworkController ( ) ) <EOL> return [ ext ] <EOL> def get_controller_extensions ( self ) : <EOL> return [ ] <EOL> def _sync_networks ( context , project_id , session ) : <EOL> ctx = nova_context . RequestContext ( user_id = None , project_id = project_id ) <EOL> ctx = ctx . elevated ( ) <EOL> networks = nova . network . api . API ( ) . get_all ( ctx ) <EOL> return dict ( networks = len ( networks ) ) <EOL> if CONF . enable_network_quota : <EOL> QUOTAS . register_resource ( quota . ReservableResource ( '<STR_LIT>' , <EOL> _sync_networks , <EOL> '<STR_LIT>' ) ) </s>
