Nova v3 API如何添加新资源
程序员文章站
2022-05-11 17:25:23
...
对于V3 API添加新资源,以现有扩展资源keypairs为例进行说明
#nova/api/openstack/compute/plugins/v3/keypairs.py
ALIAS = 'os-keypairs'
class Keypairs(extensions.V3APIExtensionBase):
"""Keypair Support."""
name = "Keypairs"
alias = ALIAS
version = 1
def get_resources(self):
resources = [
extensions.ResourceExtension(ALIAS,
KeypairController())]
return resources
#对于v3 API,对核心资源的扩展,所需要做的仅仅是在get_controller_extensions()
#中返回该核心资源的扩展,并不需要对核心资源的实现做任何修改。对于核心资源,
#其get_controller_extensions()会返回空
def get_controller_extensions(self):
controller = Controller()
extension = extensions.ControllerExtension(self, 'servers', controller)
return [extension]
# use nova.api.extensions.server.extensions entry point to modify
# server create kwargs
# NOTE(gmann): This function is not supposed to use 'body_deprecated_param'
# parameter as this is placed to handle scheduler_hint extension for V2.1.
def server_create(self, server_dict, create_kwargs, body_deprecated_param):
create_kwargs['key_name'] = server_dict.get('key_name')
那么,v3 API如何避免对核心资源servers的create操作的修改?
#nova/api/openstack/compute/plugins/v3/servers.py
class ServersController(wsgi.Controller):
"""The Server API base controller class for the OpenStack API."""
#在核心资源servers对应的Controller中,引入了三个entry points的命名空间,
#分别对应三个将被扩展资源所修改的操作create、rebuild、update
EXTENSION_CREATE_NAMESPACE = 'nova.api.v3.extensions.server.create'
EXTENSION_REBUILD_NAMESPACE = 'nova.api.v3.extensions.server.rebuild'
EXTENSION_UPDATE_NAMESPACE = 'nova.api.v3.extensions.server.update'
def __init__(self, **kwargs):
#初始化时,根据setup.cfg的定义,从之前引入的命名空间中加载所有的资源
self.create_extension_manager = \
stevedore.enabled.EnabledExtensionManager(
namespace=self.EXTENSION_CREATE_NAMESPACE,
check_func=_check_load_extension('server_create'),
invoke_on_load=True,
invoke_kwds={"extension_info": self.extension_info},
propagate_map_exceptions=True)
if not list(self.create_extension_manager):
LOG.debug("Did not find any server create extensions")
def create(self, req, body):
#servers的create操作使用map()函数依次调用各个扩展资源的server_create()函数
#去修改create_kwargs,最终的create_kwargs会被用来创建虚拟机
if list(self.create_extension_manager):
self.create_extension_manager.map(self._create_extension_point,
server_dict, create_kwargs, body)
此外,因为v3使用stevdore加载各个API插件,我们还需要在setup.cfg文件中增加相应的entry point:
nova.api.v3.extensions =
keypairs = nova.api.openstack.compute.plugins.v3.keypairs:Keypairs
nova.api.v3.extensions.server.create =
keypairs_create = nova.api.openstack.compute.plugins.v3.keypairs:Keypairs