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

« back to all changes in this revision

Viewing changes to nova/api/openstack/compute/contrib/hypervisors.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:
20
20
from nova.api.openstack import extensions
21
21
from nova.api.openstack import wsgi
22
22
from nova.api.openstack import xmlutil
 
23
from nova.compute import api as compute_api
23
24
from nova import db
24
25
from nova import exception
25
26
from nova.openstack.common import log as logging
79
80
        return xmlutil.MasterTemplate(root, 1)
80
81
 
81
82
 
 
83
class HypervisorUptimeTemplate(xmlutil.TemplateBuilder):
 
84
    def construct(self):
 
85
        root = xmlutil.TemplateElement('hypervisor', selector='hypervisor')
 
86
        make_hypervisor(root, False)
 
87
        root.set('uptime')
 
88
        return xmlutil.MasterTemplate(root, 1)
 
89
 
 
90
 
82
91
class HypervisorServersTemplate(xmlutil.TemplateBuilder):
83
92
    def construct(self):
84
93
        root = xmlutil.TemplateElement('hypervisors')
95
104
        return xmlutil.MasterTemplate(root, 1)
96
105
 
97
106
 
 
107
class HypervisorStatisticsTemplate(xmlutil.TemplateBuilder):
 
108
    def construct(self):
 
109
        root = xmlutil.TemplateElement('hypervisor_statistics',
 
110
                                       selector='hypervisor_statistics')
 
111
        root.set('count')
 
112
        root.set('vcpus')
 
113
        root.set('memory_mb')
 
114
        root.set('local_gb')
 
115
        root.set('vcpus_used')
 
116
        root.set('memory_mb_used')
 
117
        root.set('local_gb_used')
 
118
        root.set('free_ram_mb')
 
119
        root.set('free_disk_gb')
 
120
        root.set('current_workload')
 
121
        root.set('running_vms')
 
122
        root.set('disk_available_least')
 
123
 
 
124
        return xmlutil.MasterTemplate(root, 1)
 
125
 
 
126
 
98
127
class HypervisorsController(object):
99
128
    """The Hypervisors API controller for the OpenStack API."""
100
129
 
101
 
    def _view_hypervisor(self, hypervisor, detail, servers=None):
 
130
    def __init__(self):
 
131
        self.api = compute_api.HostAPI()
 
132
        super(HypervisorsController, self).__init__()
 
133
 
 
134
    def _view_hypervisor(self, hypervisor, detail, servers=None, **kwargs):
102
135
        hyp_dict = {
103
136
            'id': hypervisor['id'],
104
137
            'hypervisor_hostname': hypervisor['hypervisor_hostname'],
121
154
            hyp_dict['servers'] = [dict(name=serv['name'], uuid=serv['uuid'])
122
155
                                   for serv in servers]
123
156
 
 
157
        # Add any additional info
 
158
        if kwargs:
 
159
            hyp_dict.update(kwargs)
 
160
 
124
161
        return hyp_dict
125
162
 
126
163
    @wsgi.serializers(xml=HypervisorIndexTemplate)
148
185
            raise webob.exc.HTTPNotFound(explanation=msg)
149
186
        return dict(hypervisor=self._view_hypervisor(hyp, True))
150
187
 
 
188
    @wsgi.serializers(xml=HypervisorUptimeTemplate)
 
189
    def uptime(self, req, id):
 
190
        context = req.environ['nova.context']
 
191
        authorize(context)
 
192
        try:
 
193
            hyp = db.compute_node_get(context, int(id))
 
194
        except (ValueError, exception.ComputeHostNotFound):
 
195
            msg = _("Hypervisor with ID '%s' could not be found.") % id
 
196
            raise webob.exc.HTTPNotFound(explanation=msg)
 
197
 
 
198
        # Get the uptime
 
199
        try:
 
200
            host = hyp['service']['host']
 
201
            uptime = self.api.get_host_uptime(context, host)
 
202
        except NotImplementedError:
 
203
            msg = _("Virt driver does not implement uptime function.")
 
204
            raise webob.exc.HTTPNotImplemented(explanation=msg)
 
205
 
 
206
        return dict(hypervisor=self._view_hypervisor(hyp, False,
 
207
                                                     uptime=uptime))
 
208
 
151
209
    @wsgi.serializers(xml=HypervisorIndexTemplate)
152
210
    def search(self, req, id):
153
211
        context = req.environ['nova.context']
174
232
            msg = _("No hypervisor matching '%s' could be found.") % id
175
233
            raise webob.exc.HTTPNotFound(explanation=msg)
176
234
 
 
235
    @wsgi.serializers(xml=HypervisorStatisticsTemplate)
 
236
    def statistics(self, req):
 
237
        context = req.environ['nova.context']
 
238
        authorize(context)
 
239
        stats = db.compute_node_statistics(context)
 
240
        return dict(hypervisor_statistics=stats)
 
241
 
177
242
 
178
243
class Hypervisors(extensions.ExtensionDescriptor):
179
244
    """Admin-only hypervisor administration"""
186
251
    def get_resources(self):
187
252
        resources = [extensions.ResourceExtension('os-hypervisors',
188
253
                HypervisorsController(),
189
 
                collection_actions={'detail': 'GET'},
190
 
                member_actions={'search': 'GET', 'servers': 'GET'})]
 
254
                collection_actions={'detail': 'GET',
 
255
                                    'statistics': 'GET'},
 
256
                member_actions={'uptime': 'GET',
 
257
                                'search': 'GET',
 
258
                                'servers': 'GET'})]
191
259
 
192
260
        return resources