如何进行nova.api.openstack.APIRouterV21模块的解析,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
nova.api.openstack.APIRouterV21模块的解析:
from nova import wsgi as base_wsgi
class APIRouterV21(base_wsgi.Router):""" 将OpenStack v2.1 API上的请求路由到适当的控制器和方法。 """ @classmethod def factory(cls, global_config, **local_config):"""paste factory 方法, 在/etc/nova/api-paste.ini里的[app:osapi_compute_app_v21]调用""" return cls()@staticmethod def api_extension_namespace(): """定义插件命名空间 """ return 'nova.api.v21.extensions' def __init__(self):def _check_load_extension(ext):return self._register_extension(ext)
#通过stevedore 动态加载插件self.api_extension_manager = stevedore.enabled.EnabledExtensionManager(namespace=self.api_extension_namespace(),check_func=_check_load_extension,invoke_on_load=True,invoke_kwds={"extension_info": self.loaded_extension_info})
mapper = ProjectMapper()#Routes映射器,此类是nova对routes.Mapper的继承重载self.resources = {}# 核心API支持被重写为扩展, 但在概念上仍然具有核心性 if list(self.api_extension_manager):# 如果没有检测到插件,Stevedore将引发异常。 self._register_resources_check_inherits(mapper)self.api_extension_manager.map(self._register_controllers)
LOG.info(_LI("Loaded extensions: %s"), sorted(self.loaded_extension_info.get_extensions().keys()))super(APIRouterV21, self).__init__(mapper)def _register_resources_list(self, ext_list, mapper):for ext in ext_list:self._register_resources(ext, mapper)def _register_resources_check_inherits(self, mapper):
ext_has_inherits = []
ext_no_inherits = []for ext in self.api_extension_manager:for resource in ext.obj.get_resources():if resource.inherits:
ext_has_inherits.append(ext)break else:
ext_no_inherits.append(ext)self._register_resources_list(ext_no_inherits, mapper)self._register_resources_list(ext_has_inherits, mapper)@property def loaded_extension_info(self):raise NotImplementedError()def _register_extension(self, ext):raise NotImplementedError()def _register_resources(self, ext, mapper):""" 注册由extensions定义的资源 Extensions通过get_resources函数定义要添加的资源 """ handler = ext.obj
LOG.debug("Running _register_resources on %s", ext.obj)for resource in handler.get_resources():
LOG.debug('Extended resource: %s', resource.collection)
inherits = None if resource.inherits:
inherits = self.resources.get(resource.inherits)if not resource.controller:
resource.controller = inherits.controller
wsgi_resource = wsgi.ResourceV21(resource.controller, inherits=inherits)self.resources[resource.collection] = wsgi_resource
kargs = dict(controller=wsgi_resource,collection=resource.collection_actions,member=resource.member_actions)if resource.parent:
kargs['parent_resource'] = resource.parent# 非核心api插件使用collection名称作为member名称, # 但是core-API插件对member/collection名称使用单复数约定 if resource.member_name:
member_name = resource.member_nameelse:
member_name = resource.collection
mapper.resource(member_name, resource.collection,
**kargs)if resource.custom_routes_fn:
resource.custom_routes_fn(mapper, wsgi_resource)def _register_controllers(self, ext):""" 注册由extensions定义的控制器 通过get_controller_extensions函数Extensions定义要添加的资源""" handler = ext.obj
LOG.debug("Running _register_controllers on %s", ext.obj)for extension in handler.get_controller_extensions():
ext_name = extension.extension.name
collection = extension.collection
controller = extension.controllerif collection not in self.resources:
LOG.warning(_LW('Extension %(ext_name)s: Cannot extend ' 'resource %(collection)s: No such resource'),
{'ext_name': ext_name, 'collection': collection})continue LOG.debug('Extension %(ext_name)s extending resource: ' '%(collection)s',
{'ext_name': ext_name, 'collection': collection})
resource = self.resources[collection]
resource.register_actions(controller)
resource.register_extensions(controller)
关于如何进行nova.api.openstack.APIRouterV21模块的解析问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注天达云行业资讯频道了解更多相关知识。