~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/api/openstack/compute/contrib/config_drive.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2012 OpenStack LLC.
 
4
# All Rights Reserved.
 
5
#
 
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
 
9
#
 
10
#         http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
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
 
16
#    under the License.
 
17
 
 
18
"""Config Drive extension"""
 
19
 
 
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
 
25
 
 
26
 
 
27
FLAGS = flags.FLAGS
 
28
authorize = extensions.soft_extension_authorizer('compute', 'config_drive')
 
29
 
 
30
 
 
31
class ServerConfigDriveTemplate(xmlutil.TemplateBuilder):
 
32
    def construct(self):
 
33
        root = xmlutil.TemplateElement('server')
 
34
        root.set('config_drive', 'config_drive')
 
35
        return xmlutil.SlaveTemplate(root, 1)
 
36
 
 
37
 
 
38
class ServersConfigDriveTemplate(xmlutil.TemplateBuilder):
 
39
    def construct(self):
 
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)
 
44
 
 
45
 
 
46
class Controller(servers.Controller):
 
47
 
 
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']
 
54
 
 
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])
 
60
 
 
61
    @wsgi.extends
 
62
    def show(self, req, resp_obj, id):
 
63
        context = req.environ['nova.context']
 
64
        if authorize(context):
 
65
            self._show(req, resp_obj)
 
66
 
 
67
    @wsgi.extends
 
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)
 
74
 
 
75
 
 
76
class Config_drive(extensions.ExtensionDescriptor):
 
77
    """Config Drive Extension"""
 
78
 
 
79
    name = "ConfigDrive"
 
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"
 
83
 
 
84
    def get_controller_extensions(self):
 
85
        controller = Controller(self.ext_mgr)
 
86
        extension = extensions.ControllerExtension(self, 'servers', controller)
 
87
        return [extension]