openstack框架可以很容易地扩展api,可以自定义一些api。本文以nova-api为例,进行api扩展。
nova的扩展都放在nova/api/openstack/compute/contrib/ 目录下,比如我们扩展一个注入文件的api。
nova/api/openstack/compute/contrib/inject_file.py
import webob
from webob import exc
from nova import db
from nova import exception
from nova import compute
from nova.api.openstack import extensions
from nova.api.openstack import common
from nova.api.openstack import wsgi
from nova.compute import power_state
from oslo_log import log as logging
LOG = logging.getLogger(__name__)
# 这个操作需要认证
authorize = extensions.extension_authorizer('compute', 'inject_file')
class InjectFileController(object):
def __init__(self):
self.compute_api = compute.API()
@wsgi.response(202)
@wsgi.action('injectFile')
def update(self, req, id, body):
context = req.environ['nova.context']
authorize(context)
instance = common.get_instance(self.compute_api, context, id)
req.cache_db_instance(instance)
LOG.error("instance=%s", instance)
if 'inject_file' in body:
inject_files = []
for f in body['inject_file'</