同事整理了一份“如何创建Cinder-API核心资源”的步骤,请原谅我的拿来主义:
核心资源的python文件在 /cinder/api/v2/ 目录下面。Cinder-API的核心资源包括:volumes,types,snapshots,limits等。核心资源在Cinder-API启动时通过/etc/cinder/api-paste.ini文件中定义的APIRouter类进行加载。
新增核心资源:
1.创建核心资源的Controller类
# -*- coding:utf-8 -*-
from oslo_log import log as logging
import webob.exc
from cinder.api.openstack import wsgi
LOG = logging.getLogger(__name__)
class RuijieController(wsgi.Controller):
def __init__(self, ext_mgr):
self.ext_mgr = ext_mgr
super(RuijieController, self).__init__()
def index(self, req):
"""对应GET /v2/< tenant_id >/ruijies"""
LOG.debug("GET Request : {0}".format(req))
return webob.Response(status_int=202)
2.在对应的python文件中创建静态方法create_resource,该方法返回一个Controller实例。
def create_resource(ext_mgr):
return wsgi.Resource(RuijieController(ext_mgr))
3.创建新的Router类,该类型用来追加我们新添加的核心资源
# -*- coding:utf-8 -*-
from cinder.api.v2 import router
from cinder.api.v2 import ruijie_demo
class RuijieAPIRouter(router.APIRouter):
"""定义新的Router类型,该类型用来追加我们新添加的核心资源"""
def _setup_routes(self, mapper, ext_mgr):
super(RuijieAPIRouter,self)._setup_routes(mapper=mapper,ext_mgr=ext_mgr)
self.resources['ruijies'] = ruijie_demo.create_resource(ext_mgr)
mapper.resource("ruijie", "ruijies",
controller=self.resources['ruijies'],
collection={'detail': 'GET'},
member={'action': 'POST'}
)
4.修改配置文件/etc/cinder/api-paste.ini,用新定义的RuijieAPIRouter替换原有的cinder.api.v2.router.APIRouter
[app:apiv2]
#paste.app_factory = cinder.api.v2.router:APIRouter.factory
paste.app_factory = cinder.api.v2.ruijie_router:RuijieAPIRouter.factory