diff options
| author | John Griffith <john.griffith@solidfire.com> | 2012-05-14 14:01:32 -0600 |
|---|---|---|
| committer | John Griffith <john.griffith@solidfire.com> | 2012-05-15 17:34:19 -0600 |
| commit | f0dd8b0af87cf5d838457100430b529379d70916 (patch) | |
| tree | dce3b7a4925ff3542f2ccdfd89135acb725aa5c6 /nova/api | |
| parent | caf7461aff60ef68a366ab329f136d09e19d9b0b (diff) | |
get instance details results in volumes key error
* fixes bug 999043
* this code was still looking for instance FK in volumes
* changed contrib/compute volume.api:attach to use bdm
* added minimal tests for changes in Attach class
Change-Id: I19c7aff25182aa55741ec4d80b7e30f7e66ca998
Diffstat (limited to 'nova/api')
| -rw-r--r-- | nova/api/ec2/cloud.py | 11 | ||||
| -rw-r--r-- | nova/api/openstack/compute/contrib/volumes.py | 90 |
2 files changed, 67 insertions, 34 deletions
diff --git a/nova/api/ec2/cloud.py b/nova/api/ec2/cloud.py index 797cf35bf..b077d55aa 100644 --- a/nova/api/ec2/cloud.py +++ b/nova/api/ec2/cloud.py @@ -850,11 +850,16 @@ class CloudController(object): def _format_volume(self, context, volume): instance_ec2_id = None instance_data = None - if volume.get('instance', None): - instance_id = volume['instance']['id'] + + if volume.get('instance_uuid', None): + instance_uuid = volume['instance_uuid'] + instance = db.instance_get_by_uuid(context.elevated(), + instance_uuid) + + instance_id = instance['id'] instance_ec2_id = ec2utils.id_to_ec2_id(instance_id) instance_data = '%s[%s]' % (instance_ec2_id, - volume['instance']['host']) + instance['host']) v = {} v['volumeId'] = ec2utils.id_to_ec2_vol_id(volume['id']) v['status'] = volume['status'] diff --git a/nova/api/openstack/compute/contrib/volumes.py b/nova/api/openstack/compute/contrib/volumes.py index e3357cf2b..a51ba9229 100644 --- a/nova/api/openstack/compute/contrib/volumes.py +++ b/nova/api/openstack/compute/contrib/volumes.py @@ -57,7 +57,9 @@ def _translate_volume_summary_view(context, vol): d['createdAt'] = vol['created_at'] if vol['attach_status'] == 'attached': - d['attachments'] = [_translate_attachment_detail_view(context, vol)] + d['attachments'] = [_translate_attachment_detail_view(vol['id'], + vol['instance_uuid'], + vol['mountpoint'])] else: d['attachments'] = [{}] @@ -222,30 +224,29 @@ class VolumeController(object): return {'volume': retval} -def _translate_attachment_detail_view(_context, vol): +def _translate_attachment_detail_view(volume_id, instance_uuid, mountpoint): """Maps keys for attachment details view.""" - d = _translate_attachment_summary_view(_context, vol) + d = _translate_attachment_summary_view(volume_id, + instance_uuid, + mountpoint) # No additional data / lookups at the moment - return d -def _translate_attachment_summary_view(_context, vol): +def _translate_attachment_summary_view(volume_id, instance_uuid, mountpoint): """Maps keys for attachment summary view.""" d = {} - volume_id = vol['id'] - # NOTE(justinsb): We use the volume id as the id of the attachment object d['id'] = volume_id d['volumeId'] = volume_id - if vol.get('instance'): - d['serverId'] = vol['instance']['uuid'] - if vol.get('mountpoint'): - d['device'] = vol['mountpoint'] + + d['serverId'] = instance_uuid + if mountpoint: + d['device'] = mountpoint return d @@ -284,7 +285,6 @@ class VolumeAttachmentController(object): def __init__(self): self.compute_api = compute.API() - self.volume_api = volume.API() super(VolumeAttachmentController, self).__init__() @wsgi.serializers(xml=VolumeAttachmentsTemplate) @@ -301,19 +301,31 @@ class VolumeAttachmentController(object): volume_id = id try: - vol = self.volume_api.get(context, volume_id) + instance = self.compute_api.get(context, server_id) except exception.NotFound: - LOG.debug("volume_id not found") raise exc.HTTPNotFound() - instance = vol['instance'] - if instance is None or str(instance['uuid']) != server_id: - LOG.debug("Instance not found (server_id=%(server_id)s)", - {'server_id': server_id}, instance=instance) + bdms = self.compute_api.get_instance_bdms(context, instance) + + if not bdms: + LOG.debug(_("Instance %s is not attached."), server_id) raise exc.HTTPNotFound() - return {'volumeAttachment': _translate_attachment_detail_view(context, - vol)} + assigned_mountpoint = None + + for bdm in bdms: + if bdm['volume_id'] == volume_id: + assigned_mountpoint = bdm['device_name'] + break + + if assigned_mountpoint is None: + LOG.debug("volume_id not found") + raise exc.HTTPNotFound() + + return {'volumeAttachment': _translate_attachment_detail_view( + volume_id, + instance['uuid'], + assigned_mountpoint)} @wsgi.serializers(xml=VolumeAttachmentTemplate) def create(self, req, server_id, body): @@ -367,19 +379,28 @@ class VolumeAttachmentController(object): LOG.audit(_("Detach volume %s"), volume_id, context=context) try: - vol = self.volume_api.get(context, volume_id) + instance = self.compute_api.get(context, server_id) except exception.NotFound: raise exc.HTTPNotFound() - instance_uuid = vol['instance_uuid'] - if instance_uuid is None or instance_uuid != server_id: - LOG.debug(_("Instance %s is not attached."), instance_uuid) + bdms = self.compute_api.get_instance_bdms(context, instance) + + if not bdms: + LOG.debug(_("Instance %s is not attached."), server_id) raise exc.HTTPNotFound() - self.compute_api.detach_volume(context, - volume_id=volume_id) + found = False + for bdm in bdms: + if bdm['volume_id'] == volume_id: + self.compute_api.detach_volume(context, + volume_id=volume_id) + found = True + break - return webob.Response(status_int=202) + if not found: + raise exc.HTTPNotFound() + else: + return webob.Response(status_int=202) def _items(self, req, server_id, entity_maker): """Returns a list of attachments, transformed through entity_maker.""" @@ -391,10 +412,17 @@ class VolumeAttachmentController(object): except exception.NotFound: raise exc.HTTPNotFound() - volumes = instance['volumes'] - limited_list = common.limited(volumes, req) - res = [entity_maker(context, vol) for vol in limited_list] - return {'volumeAttachments': res} + bdms = self.compute_api.get_instance_bdms(context, instance) + limited_list = common.limited(bdms, req) + results = [] + + for bdm in limited_list: + if bdm['volume_id']: + results.append(entity_maker(bdm['volume_id'], + bdm['instance_uuid'], + bdm['device_name'])) + + return {'volumeAttachments': results} class BootFromVolumeController(servers.Controller): |
