Keystone controller.py & routers.py代码解析
2016-07-29 19:12
337 查看
目录
目录Keystone WSGI 实现
controllerspy
routerspy
参考文档
Keystone WSGI 实现
Keystone 项目把每个功能都分到单独的目录下,EXAMPLE:token 相关的功能 ===> keystone/token/
assignment 相关的功能 ===> keystone/assignment/
auth 相关的功能 ===> keystone/auth/
catalog 相关的功能 ===> keystone/catalog/
credential 相关的功能 ===> keystone/credential/
identity 相关的功能 ===> keystone/identity/
policy 相关的功能 ===> keystone/policy/
resource 相关的功能 ===> keystone/resource/
这些功能目录下都一般会有三个文件:routers.py/ controllers.py/ core.py
controllers.py
controllers.py 中的 action 操作函数调用了 core.py 中的底层接口实现来 RESTful API 对应的操作功能。EXAMPLE:这个例子的代码非官方源码,用于理解。
# controller.py class AccountController(wsgi.Controller): def __init__(self, ext_mgr): self.ext_mgr = ext_mgr self.account_api = account_api.API() super(AccountController, self).__init__() @wsgi.serializers() def index(self, req): #Action:index ==> HTTP:Get(all) context = req.environ['duckbill.context'] params = req.GET qs = {} qs['page_count'] = params.get('page_count', DEFAULT_PAGE_COUNT) qs['page_num'] = params.get('page_num', 0) qs['account_name'] = params.get('account_name', None) qs['pubcloud'] = params.get('pubcloud', None) qs['user'] = params.get('user', None) accounts = self.account_api.list_account(context, qs) #account_api.list_account()是从core.py中实现的函数 return {'accounts': accounts} # core.py class API(base.Base): """API for handling account resources.""" def __init__(self): super(API, self).__init__() def list_account(self, context, filter=None): """Get account list. :param context: class:`RequestContext` instance :param filter: select data by filter :type: ``dict`` :return: return a list of class:`AccountInfo` instance. """ return self.db.account_get_all(context, filter) #调用 db Module 的方法实现对数据库的操作, db Module 使用了 ORM # db.py def account_get_all(context, filter=None): """Get account list. :param context: class:`RequestContext` instance :param filter: select data by filter :type: ''dict'' :return: return a list of class:`AccountInfo` instance. """ return IMPL.account_get_all(context, filter=filter) #IMPL 主要在 oslo.db 通用库中实现
routers.py
routers.py 中实现了 URL 路由,把 URL 和 controllers.py 中的 action 对应起来。EXAMPLE:
URl == http://hostname:35357/v3/auth/tokens 对 Keystone 而言,/v3 开头的请求会交给
keystone.service.v3_app_factory这个函数生成的 application 来处理。
routes 的一般用法是创建一个 mapper 对象,然后调用该 Mapper 对象的 connect() 方法把 URL_Path(这里是 /auth/tokens )和 HTTP 内建方法映射到一个 controller 的某个 action 上。如果是这样的话,那么我们就需要先实现 controller 和其 action 操作函数。然后使用 mapper.connect() 将上述的几个关键的参数映射到一起。使得一个请求,在 Client 看起来是 URL ,在程序内部看起来就是一个 Action 操作函数。
keystone.service.v3_app_factory这个函数说明了路由转发的原理,我们来看代码:
def v3_app_factory(global_conf, **local_conf): #v3_app_factory()函数中先遍历了所有的模块,将每个模块的路由都添加到同一个mapper对象中 ... mapper = routes.Mapper() # 创建一个 mapper 对象 ... router_modules = [auth, # Keystone 功能模块列表 assignment, catalog, credential, identity, policy, resource] ... for module in router_modules: routers_instance = module.routers.Routers() _routers.append(routers_instance) routers_instance.append_v3_routers(mapper, sub_routers) #将每个 keystone 功能模块的路由都添加到同一个 mapper 对象中,这样的话每个模块的 routes 都拥有了 mapper 的能力。 # Add in the v3 version api sub_routers.append(routers.VersionV3('public', _routers)) return wsgi.ComposingRouter(mapper, sub_routers) #然后把 mapper 对象作为参数用于初始化 wsgi.ComposingRouter 对象 # ComposingRouter 对象(在其父类Router中实现)被调用时,会 Return 一个 WSGI application 。 # 即当调用 keystone.service.v3_app_factory 这个函数时会返回一个 application 对象,用于连接 WSGI Server 和 Application(application参数传递请求)。
这个 wsgi.ComposingRouter 对象一定是一个 WSGI application,我们看看代码就知道了:
class Router(object): """WSGI middleware that maps incoming requests to WSGI apps.""" def __init__(self, mapper): self.map = mapper self._router = routes.middleware.RoutesMiddleware(self._dispatch, self.map) #这个 application 中则使用了 routes 模块的中间件来实现了请求路由 (在routes.middleware.RoutesMiddleware中实现)。 #这里对 Path 进行路由的结果就是返回各个模块的 controllers.py 中定义的 controller (路由的结果就是将请求中的URL的资源和一个 controller 对应起来)。 #各个模块的 controller 都是一个WSGI application,这个你可以通过这些 controller 的类继承关系看出来 @webob.dec.wsgify() def __call__(self, req): return self._router ... class ComposingRouter(Router): def __init__(self, mapper=None, routers=None): ...
routes 模块把 URL_Path 映射到了一个 controller,但是如何把对 URL_Path 的处理(HTTP方法)映射到 controller 的操作函数(Action)呢?
这个可以从 controller 的父类 keystone.common.wsgi.Application 的实现看出来。
这个 Application 类中使用了 environ[‘wsgiorg.routing_args’] 中的数据来确定调用 controller 的哪个方法,这些数据是由上面提到的routes.middleware.RoutesMiddleware 设置的。所以最近调用哪一个 Controller 的 Action 还是由 routes.middleware.RoutesMiddleware 来决定的。
class Application(BaseApplication): @webob.dec.wsgify() def __call__(self, req): arg_dict = req.environ['wsgiorg.routing_args'][1] action = arg_dict.pop('action')
参考文档
routes 库的项目官网Python Paste 库项目官网
通过demo学习OpenStack开发–API服务
Openstack Restful API 开发框架 Paste + PasteDeploy + Routes + WebOb
相关文章推荐
- 趣味100道C语言编程题num seven
- 制作基于pbr的python可发布package
- Java.lang.Math.ceil()方法
- 对webservice 的理解 以及示例
- git:代码改完了才发现分支错了,怎么办?
- 【步兵 lua】利用脚本的优势
- java 集合list和map的用法
- 七牛-预转(缩略图)持久化(java)
- c语言:逆序的三位数
- Arrays类应用·使用Arrays类操作Java中的数组
- c++计算器后续(4)
- Qt中mouseMoveEvent无效
- 用python对指定目录下的所有文件(夹)大小进行排序
- C++中的Lambda表达式
- 大数幂 C语言
- Qt窗口大小设置
- 【Delphi】支付宝RSA签名之Delphi实现
- javaweb学习总结(三十八)——事务
- 7.29的小记录
- 修改hosts无法访问google