<s> """<STR_LIT>""" <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> import routes <EOL> import six <EOL> import stevedore <EOL> import webob . dec <EOL> import webob . exc <EOL> from nova . api . openstack import extensions <EOL> from nova . api . openstack import wsgi <EOL> from nova import exception <EOL> from nova . i18n import _ <EOL> from nova . i18n import _LC <EOL> from nova . i18n import _LE <EOL> from nova . i18n import _LI <EOL> from nova . i18n import _LW <EOL> from nova . i18n import translate <EOL> from nova import notifications <EOL> from nova import utils <EOL> from nova import wsgi as base_wsgi <EOL> api_opts = [ <EOL> cfg . ListOpt ( '<STR_LIT>' , <EOL> default = [ ] , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> deprecated_for_removal = True , deprecated_group = '<STR_LIT>' ) , <EOL> cfg . ListOpt ( '<STR_LIT>' , <EOL> default = [ ] , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> deprecated_for_removal = True , deprecated_group = '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> deprecated_for_removal = True , deprecated_group = '<STR_LIT>' ) <EOL> ] <EOL> api_opts_group = cfg . OptGroup ( name = '<STR_LIT>' , title = '<STR_LIT>' ) <EOL> LOG = logging . getLogger ( __name__ ) <EOL> CONF = cfg . CONF <EOL> CONF . register_group ( api_opts_group ) <EOL> CONF . register_opts ( api_opts , api_opts_group ) <EOL> API_V21_CORE_EXTENSIONS = set ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> class FaultWrapper ( base_wsgi . Middleware ) : <EOL> """<STR_LIT>""" <EOL> _status_to_type = { } <EOL> @ staticmethod <EOL> def status_to_type ( status ) : <EOL> if not FaultWrapper . _status_to_type : <EOL> for clazz in utils . walk_class_hierarchy ( webob . exc . HTTPError ) : <EOL> FaultWrapper . _status_to_type [ clazz . code ] = clazz <EOL> return FaultWrapper . _status_to_type . get ( <EOL> status , webob . exc . HTTPInternalServerError ) ( ) <EOL> def _error ( self , inner , req ) : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) , six . text_type ( inner ) ) <EOL> safe = getattr ( inner , '<STR_LIT>' , False ) <EOL> headers = getattr ( inner , '<STR_LIT>' , None ) <EOL> status = getattr ( inner , '<STR_LIT:code>' , <NUM_LIT> ) <EOL> if status is None : <EOL> status = <NUM_LIT> <EOL> msg_dict = dict ( url = req . url , status = status ) <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , msg_dict ) <EOL> outer = self . status_to_type ( status ) <EOL> if headers : <EOL> outer . headers = headers <EOL> if safe : <EOL> user_locale = req . best_match_language ( ) <EOL> inner_msg = translate ( inner . message , user_locale ) <EOL> outer . explanation = '<STR_LIT>' % ( inner . __class__ . __name__ , <EOL> inner_msg ) <EOL> notifications . send_api_fault ( req . url , status , inner ) <EOL> return wsgi . Fault ( outer ) <EOL> @ webob . dec . wsgify ( RequestClass = wsgi . Request ) <EOL> def __call__ ( self , req ) : <EOL> try : <EOL> return req . get_response ( self . application ) <EOL> except Exception as ex : <EOL> return self . _error ( ex , req ) <EOL> class LegacyV2CompatibleWrapper ( base_wsgi . Middleware ) : <EOL> def _filter_request_headers ( self , req ) : <EOL> """<STR_LIT>""" <EOL> if wsgi . API_VERSION_REQUEST_HEADER in req . headers : <EOL> del req . headers [ wsgi . API_VERSION_REQUEST_HEADER ] <EOL> return req <EOL> def _filter_response_headers ( self , response ) : <EOL> """<STR_LIT>""" <EOL> if wsgi . API_VERSION_REQUEST_HEADER in response . headers : <EOL> del response . headers [ wsgi . API_VERSION_REQUEST_HEADER ] <EOL> if '<STR_LIT>' in response . headers : <EOL> vary_headers = response . headers [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> filtered_vary = [ ] <EOL> for vary in vary_headers : <EOL> vary = vary . strip ( ) <EOL> if vary == wsgi . API_VERSION_REQUEST_HEADER : <EOL> continue <EOL> filtered_vary . append ( vary ) <EOL> if filtered_vary : <EOL> response . headers [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( filtered_vary ) <EOL> else : <EOL> del response . headers [ '<STR_LIT>' ] <EOL> return response <EOL> @ webob . dec . wsgify ( RequestClass = wsgi . Request ) <EOL> def __call__ ( self , req ) : <EOL> req . set_legacy_v2 ( ) <EOL> req = self . _filter_request_headers ( req ) <EOL> response = req . get_response ( self . application ) <EOL> return self . _filter_response_headers ( response ) <EOL> class APIMapper ( routes . Mapper ) : <EOL> def routematch ( self , url = None , environ = None ) : <EOL> if url == "<STR_LIT>" : <EOL> result = self . _match ( "<STR_LIT>" , environ ) <EOL> return result [ <NUM_LIT:0> ] , result [ <NUM_LIT:1> ] <EOL> return routes . Mapper . routematch ( self , url , environ ) <EOL> def connect ( self , * args , ** kargs ) : <EOL> kargs . setdefault ( '<STR_LIT>' , { } ) <EOL> if not kargs [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) : <EOL> kargs [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return routes . Mapper . connect ( self , * args , ** kargs ) <EOL> class ProjectMapper ( APIMapper ) : <EOL> def resource ( self , member_name , collection_name , ** kwargs ) : <EOL> project_id_regex = '<STR_LIT>' <EOL> if CONF . osapi_v21 . project_id_regex : <EOL> project_id_regex = CONF . osapi_v21 . project_id_regex <EOL> project_id_token = '<STR_LIT>' % project_id_regex <EOL> if '<STR_LIT>' not in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' % project_id_token <EOL> else : <EOL> parent_resource = kwargs [ '<STR_LIT>' ] <EOL> p_collection = parent_resource [ '<STR_LIT>' ] <EOL> p_member = parent_resource [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' % ( <EOL> project_id_token , <EOL> p_collection , <EOL> p_member ) <EOL> routes . Mapper . resource ( <EOL> self , <EOL> member_name , <EOL> collection_name , <EOL> ** kwargs ) <EOL> if '<STR_LIT>' not in kwargs : <EOL> del kwargs [ '<STR_LIT>' ] <EOL> else : <EOL> parent_resource = kwargs [ '<STR_LIT>' ] <EOL> p_collection = parent_resource [ '<STR_LIT>' ] <EOL> p_member = parent_resource [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' % ( p_collection , <EOL> p_member ) <EOL> routes . Mapper . resource ( self , member_name , <EOL> collection_name , <EOL> ** kwargs ) <EOL> class PlainMapper ( APIMapper ) : <EOL> def resource ( self , member_name , collection_name , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs : <EOL> parent_resource = kwargs [ '<STR_LIT>' ] <EOL> p_collection = parent_resource [ '<STR_LIT>' ] <EOL> p_member = parent_resource [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' % ( p_collection , p_member ) <EOL> routes . Mapper . resource ( self , member_name , <EOL> collection_name , <EOL> ** kwargs ) <EOL> class APIRouter ( base_wsgi . Router ) : <EOL> """<STR_LIT>""" <EOL> ExtensionManager = None <EOL> @ classmethod <EOL> def factory ( cls , global_config , ** local_config ) : <EOL> """<STR_LIT>""" <EOL> return cls ( ) <EOL> def __init__ ( self , ext_mgr = None , init_only = None ) : <EOL> if ext_mgr is None : <EOL> if self . ExtensionManager : <EOL> ext_mgr = self . ExtensionManager ( ) <EOL> else : <EOL> raise Exception ( _ ( "<STR_LIT>" ) ) <EOL> mapper = ProjectMapper ( ) <EOL> self . resources = { } <EOL> self . _setup_routes ( mapper , ext_mgr , init_only ) <EOL> self . _setup_ext_routes ( mapper , ext_mgr , init_only ) <EOL> self . _setup_extensions ( ext_mgr ) <EOL> super ( APIRouter , self ) . __init__ ( mapper ) <EOL> def _setup_ext_routes ( self , mapper , ext_mgr , init_only ) : <EOL> for resource in ext_mgr . get_resources ( ) : <EOL> LOG . debug ( '<STR_LIT>' , <EOL> resource . collection ) <EOL> if init_only is not None and resource . collection not in init_only : <EOL> continue <EOL> inherits = None <EOL> if resource . inherits : <EOL> inherits = self . resources . get ( resource . inherits ) <EOL> if not resource . controller : <EOL> resource . controller = inherits . controller <EOL> wsgi_resource = wsgi . Resource ( resource . controller , <EOL> inherits = inherits ) <EOL> self . resources [ resource . collection ] = wsgi_resource <EOL> kargs = dict ( <EOL> controller = wsgi_resource , <EOL> collection = resource . collection_actions , <EOL> member = resource . member_actions ) <EOL> if resource . parent : <EOL> kargs [ '<STR_LIT>' ] = resource . parent <EOL> mapper . resource ( resource . collection , resource . collection , ** kargs ) <EOL> if resource . custom_routes_fn : <EOL> resource . custom_routes_fn ( mapper , wsgi_resource ) <EOL> def _setup_extensions ( self , ext_mgr ) : <EOL> for extension in ext_mgr . get_controller_extensions ( ) : <EOL> collection = extension . collection <EOL> controller = extension . controller <EOL> msg_format_dict = { '<STR_LIT>' : collection , <EOL> '<STR_LIT>' : extension . extension . name } <EOL> if collection not in self . resources : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> msg_format_dict ) <EOL> continue <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> msg_format_dict ) <EOL> resource = self . resources [ collection ] <EOL> resource . register_actions ( controller ) <EOL> resource . register_extensions ( controller ) <EOL> def _setup_routes ( self , mapper , ext_mgr , init_only ) : <EOL> raise NotImplementedError ( ) <EOL> class APIRouterV21 ( base_wsgi . Router ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def factory ( cls , global_config , ** local_config ) : <EOL> """<STR_LIT>""" <EOL> return cls ( ) <EOL> @ staticmethod <EOL> def api_extension_namespace ( ) : <EOL> return '<STR_LIT>' <EOL> def __init__ ( self , init_only = None , v3mode = False ) : <EOL> def _check_load_extension ( ext ) : <EOL> if ( self . init_only is None or ext . obj . alias in <EOL> self . init_only ) and isinstance ( ext . obj , <EOL> extensions . V21APIExtensionBase ) : <EOL> if ( not CONF . osapi_v21 . extensions_whitelist or <EOL> ext . obj . alias in CONF . osapi_v21 . extensions_whitelist ) : <EOL> blacklist = CONF . osapi_v21 . extensions_blacklist <EOL> if ext . obj . alias not in blacklist : <EOL> return self . _register_extension ( ext ) <EOL> return False <EOL> if ( CONF . osapi_v21 . extensions_blacklist or <EOL> CONF . osapi_v21 . extensions_whitelist ) : <EOL> LOG . warning ( <EOL> _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> self . init_only = init_only <EOL> LOG . debug ( "<STR_LIT>" , <EOL> CONF . osapi_v21 . extensions_blacklist ) <EOL> LOG . debug ( "<STR_LIT>" , <EOL> CONF . osapi_v21 . extensions_whitelist ) <EOL> in_blacklist_and_whitelist = set ( <EOL> CONF . osapi_v21 . extensions_whitelist ) . intersection ( <EOL> CONF . osapi_v21 . extensions_blacklist ) <EOL> if len ( in_blacklist_and_whitelist ) != <NUM_LIT:0> : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , <EOL> list ( in_blacklist_and_whitelist ) ) <EOL> self . api_extension_manager = stevedore . enabled . EnabledExtensionManager ( <EOL> namespace = self . api_extension_namespace ( ) , <EOL> check_func = _check_load_extension , <EOL> invoke_on_load = True , <EOL> invoke_kwds = { "<STR_LIT>" : self . loaded_extension_info } ) <EOL> if v3mode : <EOL> mapper = PlainMapper ( ) <EOL> else : <EOL> mapper = ProjectMapper ( ) <EOL> self . resources = { } <EOL> if list ( self . api_extension_manager ) : <EOL> self . _register_resources_check_inherits ( mapper ) <EOL> self . api_extension_manager . map ( self . _register_controllers ) <EOL> missing_core_extensions = self . get_missing_core_extensions ( <EOL> self . loaded_extension_info . get_extensions ( ) . keys ( ) ) <EOL> if not self . init_only and missing_core_extensions : <EOL> LOG . critical ( _LC ( "<STR_LIT>" ) , <EOL> missing_core_extensions ) <EOL> raise exception . CoreAPIMissing ( <EOL> missing_apis = missing_core_extensions ) <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , <EOL> sorted ( self . loaded_extension_info . get_extensions ( ) . keys ( ) ) ) <EOL> super ( APIRouterV21 , self ) . __init__ ( mapper ) <EOL> def _register_resources_list ( self , ext_list , mapper ) : <EOL> for ext in ext_list : <EOL> self . _register_resources ( ext , mapper ) <EOL> def _register_resources_check_inherits ( self , mapper ) : <EOL> ext_has_inherits = [ ] <EOL> ext_no_inherits = [ ] <EOL> for ext in self . api_extension_manager : <EOL> for resource in ext . obj . get_resources ( ) : <EOL> if resource . inherits : <EOL> ext_has_inherits . append ( ext ) <EOL> break <EOL> else : <EOL> ext_no_inherits . append ( ext ) <EOL> self . _register_resources_list ( ext_no_inherits , mapper ) <EOL> self . _register_resources_list ( ext_has_inherits , mapper ) <EOL> @ staticmethod <EOL> def get_missing_core_extensions ( extensions_loaded ) : <EOL> extensions_loaded = set ( extensions_loaded ) <EOL> missing_extensions = API_V21_CORE_EXTENSIONS - extensions_loaded <EOL> return list ( missing_extensions ) <EOL> @ property <EOL> def loaded_extension_info ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def _register_extension ( self , ext ) : <EOL> raise NotImplementedError ( ) <EOL> def _register_resources ( self , ext , mapper ) : <EOL> """<STR_LIT>""" <EOL> handler = ext . obj <EOL> LOG . debug ( "<STR_LIT>" , ext . obj ) <EOL> for resource in handler . get_resources ( ) : <EOL> LOG . debug ( '<STR_LIT>' , resource . collection ) <EOL> inherits = None <EOL> if resource . inherits : <EOL> inherits = self . resources . get ( resource . inherits ) <EOL> if not resource . controller : <EOL> resource . controller = inherits . controller <EOL> wsgi_resource = wsgi . ResourceV21 ( resource . controller , <EOL> inherits = inherits ) <EOL> self . resources [ resource . collection ] = wsgi_resource <EOL> kargs = dict ( <EOL> controller = wsgi_resource , <EOL> collection = resource . collection_actions , <EOL> member = resource . member_actions ) <EOL> if resource . parent : <EOL> kargs [ '<STR_LIT>' ] = resource . parent <EOL> if resource . member_name : <EOL> member_name = resource . member_name <EOL> else : <EOL> member_name = resource . collection <EOL> mapper . resource ( member_name , resource . collection , <EOL> ** kargs ) <EOL> if resource . custom_routes_fn : <EOL> resource . custom_routes_fn ( mapper , wsgi_resource ) <EOL> def _register_controllers ( self , ext ) : <EOL> """<STR_LIT>""" <EOL> handler = ext . obj <EOL> LOG . debug ( "<STR_LIT>" , ext . obj ) <EOL> for extension in handler . get_controller_extensions ( ) : <EOL> ext_name = extension . extension . name <EOL> collection = extension . collection <EOL> controller = extension . controller <EOL> if collection not in self . resources : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : ext_name , '<STR_LIT>' : collection } ) <EOL> continue <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : ext_name , '<STR_LIT>' : collection } ) <EOL> resource = self . resources [ collection ] <EOL> resource . register_actions ( controller ) <EOL> resource . register_extensions ( controller ) </s>
