Neutron API 流转过程
程序员文章站
2024-03-08 19:34:22
...
1、Neutron core server
加载api接口,源码路径:/neutron/service.py
def _run_wsgi(app_name):
app = config.load_paste_app(app_name)
if not app:
LOG.error('No known API applications configured.')
return
return run_wsgi_app(app)
load_paste_app代码如下:/neutron/common/config.py
def load_paste_app(app_name):
"""Builds and returns a WSGI app from a paste config file.
:param app_name: Name of the application to load
"""
loader = wsgi.Loader(cfg.CONF)
app = loader.load_app(app_name)
return app
load_app代码路径:oslo_service/wsgi.py
配置文件路径:/usr/share/neutron/api-paste.ini
[composite:neutron]
use = egg:Paste#urlmap
/: neutronversions_composite
/v2.0: neutronapi_v2_0
[composite:neutronapi_v2_0]
use = call:neutron.auth:pipeline_factory
noauth = cors http_proxy_to_wsgi request_id catch_errors extensions neutronapiapp_v2_0
keystone = cors http_proxy_to_wsgi request_id catch_errors authtoken keystonecontext extensions neutronapiapp_v2_0
[composite:neutron]
use = egg:Paste#urlmap
/: neutronversions_composite
/v2.0: neutronapi_v2_0
[composite:neutronapi_v2_0]
use = call:neutron.auth:pipeline_factory
noauth = cors http_proxy_to_wsgi request_id catch_errors extensions neutronapiapp_v2_0
keystone = cors http_proxy_to_wsgi request_id catch_errors authtoken keystonecontext extensions neutronapiapp_v2_0
省略。。。
[app:neutronversions]
paste.app_factory = neutron.api.versions:Versions.factory
[app:neutronapiapp_v2_0]
paste.app_factory = neutron.api.v2.router:APIRouter.factory
[filter:osprofiler]
paste.filter_factory = osprofiler.web:WsgiMiddleware.factory
Core service 的WSGI Application由:neutron.api.v2.router:APIRouter.factory创建,代码路径:/neutron/api/v2/router.py
这个是关键,这里定义了URL到处理函数的映射关系@@@@
class APIRouter(base_wsgi.Router):
@classmethod
def factory(cls, global_config, **local_config):
if cfg.CONF.web_framework == 'pecan':
return pecan_app.v2_factory(global_config, **local_config)
return cls(**local_config)
码路径:/neutron/api/v2/attributes.py
这里定义了资源中字段的取值类型,是否是必须字段等信息。
总结:
资源名字定义:neutron_lib/api/definitions/资源.py (CORE_RESOURCES)
资源属性定义:/neutron/api/v2/attributes.py (RESOURCE_ATTRIBUTE_MAP)
资源对应的处理函数定义:/neutron/api/v2/base.py
把上面两个对象CORE_RESOURCES和RESOURCE_ATTRIBUTE_MAP传入到_map_resource中,构建api与处理函数之间的对应关系,如下:
class APIRouter(base_wsgi.Router):
def __init__(self, **local_config):
for resource in RESOURCES:
_map_resource(RESOURCES[resource], resource,
attributes.RESOURCE_ATTRIBUTE_MAP.get(
RESOURCES[resource], dict()))
resource_registry.register_resource_by_name(resource)
/neutron/api/v2/base.py
每个URL对应的处理函数构建
def create_resource(collection, resource, plugin, params, allow_bulk=False,
member_actions=None, parent=None, allow_pagination=False,
allow_sorting=False):
controller = Controller(plugin, collection, resource, params, allow_bulk,
member_actions=member_actions, parent=parent,
allow_pagination=allow_pagination,
allow_sorting=allow_sorting)
return wsgi_resource.Resource(controller, FAULT_MAP)
/neutron/api/v2/resource.py中
result = method(request=request, **args)
2、Neutron extension server
3、agent