1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2012 OpenStack LLC.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
18
"""Config Drive extension"""
20
from nova.api.openstack.compute import servers
21
from nova.api.openstack import extensions
22
from nova.api.openstack import wsgi
23
from nova.api.openstack import xmlutil
24
from nova import flags
28
authorize = extensions.soft_extension_authorizer('compute', 'config_drive')
31
class ServerConfigDriveTemplate(xmlutil.TemplateBuilder):
33
root = xmlutil.TemplateElement('server')
34
root.set('config_drive', 'config_drive')
35
return xmlutil.SlaveTemplate(root, 1)
38
class ServersConfigDriveTemplate(xmlutil.TemplateBuilder):
40
root = xmlutil.TemplateElement('servers')
41
elem = xmlutil.SubTemplateElement(root, 'server', selector='servers')
42
elem.set('config_drive', 'config_drive')
43
return xmlutil.SlaveTemplate(root, 1)
46
class Controller(servers.Controller):
48
def _add_config_drive(self, req, servers):
49
for server in servers:
50
db_server = req.get_db_instance(server['id'])
51
# server['id'] is guaranteed to be in the cache due to
52
# the core API adding it in its 'show'/'detail' methods.
53
server['config_drive'] = db_server['config_drive']
55
def _show(self, req, resp_obj):
56
if 'server' in resp_obj.obj:
57
resp_obj.attach(xml=ServerConfigDriveTemplate())
58
server = resp_obj.obj['server']
59
self._add_config_drive(req, [server])
62
def show(self, req, resp_obj, id):
63
context = req.environ['nova.context']
64
if authorize(context):
65
self._show(req, resp_obj)
68
def detail(self, req, resp_obj):
69
context = req.environ['nova.context']
70
if 'servers' in resp_obj.obj and authorize(context):
71
resp_obj.attach(xml=ServersConfigDriveTemplate())
72
servers = resp_obj.obj['servers']
73
self._add_config_drive(req, servers)
76
class Config_drive(extensions.ExtensionDescriptor):
77
"""Config Drive Extension"""
80
alias = "os-config-drive"
81
namespace = "http://docs.openstack.org/compute/ext/config_drive/api/v1.1"
82
updated = "2012-07-16T00:00:00+00:00"
84
def get_controller_extensions(self):
85
controller = Controller(self.ext_mgr)
86
extension = extensions.ControllerExtension(self, 'servers', controller)