~ubuntu-branches/ubuntu/saucy/nova/saucy-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-05-24 13:12:53 UTC
  • mfrom: (1.1.55)
  • Revision ID: package-import@ubuntu.com-20120524131253-ommql08fg1en06ut
Tags: 2012.2~f1-0ubuntu1
* New upstream release.
* Prepare for quantal:
  - Dropped debian/patches/upstream/0006-Use-project_id-in-ec2.cloud._format_image.patch
  - Dropped debian/patches/upstream/0005-Populate-image-properties-with-project_id-again.patch
  - Dropped debian/patches/upstream/0004-Fixed-bug-962840-added-a-test-case.patch
  - Dropped debian/patches/upstream/0003-Allow-unprivileged-RADOS-users-to-access-rbd-volumes.patch
  - Dropped debian/patches/upstream/0002-Stop-libvirt-test-from-deleting-instances-dir.patch
  - Dropped debian/patches/upstream/0001-fix-bug-where-nova-ignores-glance-host-in-imageref.patch 
  - Dropped debian/patches/0001-fix-useexisting-deprecation-warnings.patch
* debian/control: Add python-keystone as a dependency. (LP: #907197)
* debian/patches/kombu_tests_timeout.patch: Refreshed.
* debian/nova.conf, debian/nova-common.postinst: Convert to new ini
  file configuration
* debian/patches/nova-manage_flagfile_location.patch: Refreshed

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
"""The volumes extension."""
17
17
 
 
18
import webob
18
19
from webob import exc
19
 
import webob
20
20
 
21
21
from nova.api.openstack import common
 
22
from nova.api.openstack.compute import servers
22
23
from nova.api.openstack import extensions
23
 
from nova.api.openstack.compute import servers
24
24
from nova.api.openstack import wsgi
25
25
from nova.api.openstack import xmlutil
26
26
from nova import compute
57
57
    d['createdAt'] = vol['created_at']
58
58
 
59
59
    if vol['attach_status'] == 'attached':
60
 
        d['attachments'] = [_translate_attachment_detail_view(context, vol)]
 
60
        d['attachments'] = [_translate_attachment_detail_view(vol['id'],
 
61
            vol['instance_uuid'],
 
62
            vol['mountpoint'])]
61
63
    else:
62
64
        d['attachments'] = [{}]
63
65
 
222
224
        return {'volume': retval}
223
225
 
224
226
 
225
 
def _translate_attachment_detail_view(_context, vol):
 
227
def _translate_attachment_detail_view(volume_id, instance_uuid, mountpoint):
226
228
    """Maps keys for attachment details view."""
227
229
 
228
 
    d = _translate_attachment_summary_view(_context, vol)
 
230
    d = _translate_attachment_summary_view(volume_id,
 
231
            instance_uuid,
 
232
            mountpoint)
229
233
 
230
234
    # No additional data / lookups at the moment
231
 
 
232
235
    return d
233
236
 
234
237
 
235
 
def _translate_attachment_summary_view(_context, vol):
 
238
def _translate_attachment_summary_view(volume_id, instance_uuid, mountpoint):
236
239
    """Maps keys for attachment summary view."""
237
240
    d = {}
238
241
 
239
 
    volume_id = vol['id']
240
 
 
241
242
    # NOTE(justinsb): We use the volume id as the id of the attachment object
242
243
    d['id'] = volume_id
243
244
 
244
245
    d['volumeId'] = volume_id
245
 
    if vol.get('instance'):
246
 
        d['serverId'] = vol['instance']['uuid']
247
 
    if vol.get('mountpoint'):
248
 
        d['device'] = vol['mountpoint']
 
246
 
 
247
    d['serverId'] = instance_uuid
 
248
    if mountpoint:
 
249
        d['device'] = mountpoint
249
250
 
250
251
    return d
251
252
 
284
285
 
285
286
    def __init__(self):
286
287
        self.compute_api = compute.API()
287
 
        self.volume_api = volume.API()
288
288
        super(VolumeAttachmentController, self).__init__()
289
289
 
290
290
    @wsgi.serializers(xml=VolumeAttachmentsTemplate)
301
301
 
302
302
        volume_id = id
303
303
        try:
304
 
            vol = self.volume_api.get(context, volume_id)
 
304
            instance = self.compute_api.get(context, server_id)
305
305
        except exception.NotFound:
 
306
            raise exc.HTTPNotFound()
 
307
 
 
308
        bdms = self.compute_api.get_instance_bdms(context, instance)
 
309
 
 
310
        if not bdms:
 
311
            LOG.debug(_("Instance %s is not attached."), server_id)
 
312
            raise exc.HTTPNotFound()
 
313
 
 
314
        assigned_mountpoint = None
 
315
 
 
316
        for bdm in bdms:
 
317
            if bdm['volume_id'] == volume_id:
 
318
                assigned_mountpoint = bdm['device_name']
 
319
                break
 
320
 
 
321
        if assigned_mountpoint is None:
306
322
            LOG.debug("volume_id not found")
307
323
            raise exc.HTTPNotFound()
308
324
 
309
 
        instance = vol['instance']
310
 
        if instance is None or str(instance['uuid']) != server_id:
311
 
            LOG.debug("instance_id != server_id")
312
 
            raise exc.HTTPNotFound()
313
 
 
314
 
        return {'volumeAttachment': _translate_attachment_detail_view(context,
315
 
                                                                      vol)}
 
325
        return {'volumeAttachment': _translate_attachment_detail_view(
 
326
            volume_id,
 
327
            instance['uuid'],
 
328
            assigned_mountpoint)}
316
329
 
317
330
    @wsgi.serializers(xml=VolumeAttachmentTemplate)
318
331
    def create(self, req, server_id, body):
366
379
        LOG.audit(_("Detach volume %s"), volume_id, context=context)
367
380
 
368
381
        try:
369
 
            vol = self.volume_api.get(context, volume_id)
 
382
            instance = self.compute_api.get(context, server_id)
370
383
        except exception.NotFound:
371
384
            raise exc.HTTPNotFound()
372
385
 
373
 
        instance = vol['instance']
374
 
        if instance is None or str(instance['uuid']) != server_id:
375
 
            LOG.debug("instance_id != server_id")
376
 
            raise exc.HTTPNotFound()
377
 
 
378
 
        self.compute_api.detach_volume(context,
379
 
                                       volume_id=volume_id)
380
 
 
381
 
        return webob.Response(status_int=202)
 
386
        bdms = self.compute_api.get_instance_bdms(context, instance)
 
387
 
 
388
        if not bdms:
 
389
            LOG.debug(_("Instance %s is not attached."), server_id)
 
390
            raise exc.HTTPNotFound()
 
391
 
 
392
        found = False
 
393
        for bdm in bdms:
 
394
            if bdm['volume_id'] == volume_id:
 
395
                self.compute_api.detach_volume(context,
 
396
                    volume_id=volume_id)
 
397
                found = True
 
398
                break
 
399
 
 
400
        if not found:
 
401
            raise exc.HTTPNotFound()
 
402
        else:
 
403
            return webob.Response(status_int=202)
382
404
 
383
405
    def _items(self, req, server_id, entity_maker):
384
406
        """Returns a list of attachments, transformed through entity_maker."""
390
412
        except exception.NotFound:
391
413
            raise exc.HTTPNotFound()
392
414
 
393
 
        volumes = instance['volumes']
394
 
        limited_list = common.limited(volumes, req)
395
 
        res = [entity_maker(context, vol) for vol in limited_list]
396
 
        return {'volumeAttachments': res}
 
415
        bdms = self.compute_api.get_instance_bdms(context, instance)
 
416
        limited_list = common.limited(bdms, req)
 
417
        results = []
 
418
 
 
419
        for bdm in limited_list:
 
420
            if bdm['volume_id']:
 
421
                results.append(entity_maker(bdm['volume_id'],
 
422
                        bdm['instance_uuid'],
 
423
                        bdm['device_name']))
 
424
 
 
425
        return {'volumeAttachments': results}
397
426
 
398
427
 
399
428
class BootFromVolumeController(servers.Controller):