天天看點

Keystone controller.py & routers.py代碼解析目錄Keystone WSGI 實作參考文檔

<a href="#%E7%9B%AE%E5%BD%95">目錄</a>

<a href="#keystone-wsgi-%E5%AE%9E%E7%8E%B0">Keystone WSGI 實作</a>

<a href="#controllerspy">controllerspy</a>

<a href="#routerspy">routerspy</a>

<a href="#%E5%8F%82%E8%80%83%E6%96%87%E6%A1%A3">參考文檔</a>

Keystone 項目把每個功能都分到單獨的目錄下,EXAMPLE:

token 相關的功能 ===&gt; keystone/token/

assignment 相關的功能 ===&gt; keystone/assignment/

auth 相關的功能 ===&gt; keystone/auth/

catalog 相關的功能 ===&gt; keystone/catalog/

credential 相關的功能 ===&gt; keystone/credential/

identity 相關的功能 ===&gt; keystone/identity/

policy 相關的功能 ===&gt; keystone/policy/

resource 相關的功能 ===&gt; keystone/resource/

這些功能目錄下都一般會有三個檔案:routers.py/ controllers.py/ core.py

controllers.py 中的 action 操作函數調用了 core.py 中的底層接口實作來 RESTful API 對應的操作功能。

EXAMPLE:這個例子的代碼非官方源碼,用于了解。

routers.py 中實作了 URL 路由,把 URL 和 controllers.py 中的 action 對應起來。

EXAMPLE:

routes 的一般用法是建立一個 mapper 對象,然後調用該 Mapper 對象的 connect() 方法把 URL_Path(這裡是 /auth/tokens )和 HTTP 内建方法映射到一個 controller 的某個 action 上。如果是這樣的話,那麼我們就需要先實作 controller 和其 action 操作函數。然後使用 mapper.connect() 将上述的幾個關鍵的參數映射到一起。使得一個請求,在 Client 看起來是 URL ,在程式内部看起來就是一個 Action 操作函數。

<code>keystone.service.v3_app_factory</code> 這個函數說明了路由轉發的原理,我們來看代碼:

這個 wsgi.ComposingRouter 對象一定是一個 WSGI application,我們看看代碼就知道了:

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 來決定的。

<a href="https://routes.readthedocs.io/en/latest/">routes 庫的項目官網</a>

<a href="http://pythonpaste.org/">Python Paste 庫項目官網</a>

<a href="http://www.infoq.com/cn/articles/OpenStack-UnitedStack-API2">通過demo學習OpenStack開發–API服務</a>

<a href="http://blog.csdn.net/jmilk/article/details/52046914">Openstack Restful API 開發架構 Paste + PasteDeploy + Routes + WebOb</a>