From bb10721ffc14abb86ab4d58b2b30fc676e88d394 Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Fri, 13 Jan 2012 11:22:50 -0800 Subject: Convert nova.volume.api.API to use volume objects Change-Id: If6b78f7de814116bc93b273ec300dba02e63593d --- nova/api/ec2/cloud.py | 51 +++++++++++----------- .../compute/contrib/virtual_storage_arrays.py | 7 +-- nova/api/openstack/compute/contrib/volumes.py | 12 ++++- nova/api/openstack/volume/snapshots.py | 12 ++--- nova/api/openstack/volume/volumes.py | 37 ++++++++++------ 5 files changed, 70 insertions(+), 49 deletions(-) (limited to 'nova/api') diff --git a/nova/api/ec2/cloud.py b/nova/api/ec2/cloud.py index 646fd42bd..8049d3bd7 100644 --- a/nova/api/ec2/cloud.py +++ b/nova/api/ec2/cloud.py @@ -204,9 +204,8 @@ class CloudController(object): self.image_service = s3.S3ImageService() self.network_api = network.API() self.volume_api = volume.API() - self.compute_api = compute.API( - network_api=self.network_api, - volume_api=self.volume_api) + self.compute_api = compute.API(network_api=self.network_api, + volume_api=self.volume_api) self.setup() def __str__(self): @@ -360,16 +359,18 @@ class CloudController(object): LOG.audit(_("Create snapshot of volume %s"), volume_id, context=context) volume_id = ec2utils.ec2_id_to_id(volume_id) + volume = self.volume_api.get(context, volume_id) snapshot = self.volume_api.create_snapshot( context, - volume_id=volume_id, - name=kwargs.get('display_name'), - description=kwargs.get('display_description')) + volume, + kwargs.get('display_name'), + kwargs.get('display_description')) return self._format_snapshot(context, snapshot) def delete_snapshot(self, context, snapshot_id, **kwargs): snapshot_id = ec2utils.ec2_id_to_id(snapshot_id) - self.volume_api.delete_snapshot(context, snapshot_id=snapshot_id) + snapshot = self.volume_api.get_snapshot(context, snapshot_id) + self.volume_api.delete_snapshot(context, snapshot) return True def describe_key_pairs(self, context, key_name=None, **kwargs): @@ -857,7 +858,7 @@ class CloudController(object): volumes = [] for ec2_id in volume_id: internal_id = ec2utils.ec2_id_to_id(ec2_id) - volume = self.volume_api.get(context, volume_id=internal_id) + volume = self.volume_api.get(context, internal_id) volumes.append(volume) else: volumes = self.volume_api.get_all(context) @@ -907,18 +908,18 @@ class CloudController(object): size = kwargs.get('size') if kwargs.get('snapshot_id') is not None: snapshot_id = ec2utils.ec2_id_to_id(kwargs['snapshot_id']) + snapshot = self.volume_api.get_snapshot(context, snapshot_id) LOG.audit(_("Create volume from snapshot %s"), snapshot_id, context=context) else: - snapshot_id = None + snapshot = None LOG.audit(_("Create volume of %s GB"), size, context=context) - volume = self.volume_api.create( - context, - size=size, - snapshot_id=snapshot_id, - name=kwargs.get('display_name'), - description=kwargs.get('display_description')) + volume = self.volume_api.create(context, + size, + kwargs.get('display_name'), + kwargs.get('display_description'), + snapshot) # TODO(vish): Instance should be None at db layer instead of # trying to lazy load, but for now we turn it into # a dict to avoid an error. @@ -926,7 +927,8 @@ class CloudController(object): def delete_volume(self, context, volume_id, **kwargs): volume_id = ec2utils.ec2_id_to_id(volume_id) - self.volume_api.delete(context, volume_id=volume_id) + volume = self.volume_api.get(context, volume_id) + self.volume_api.delete(context, volume) return True def update_volume(self, context, volume_id, **kwargs): @@ -937,9 +939,8 @@ class CloudController(object): if field in kwargs: changes[field] = kwargs[field] if changes: - self.volume_api.update(context, - volume_id=volume_id, - fields=changes) + volume = self.volume_api.get(context, volume_id) + self.volume_api.update(context, volume, fields=changes) return True def attach_volume(self, context, volume_id, instance_id, device, **kwargs): @@ -950,7 +951,7 @@ class CloudController(object): " at %(device)s") % locals() LOG.audit(msg, context=context) self.compute_api.attach_volume(context, instance, volume_id, device) - volume = self.volume_api.get(context, volume_id=volume_id) + volume = self.volume_api.get(context, volume_id) return {'attachTime': volume['attach_time'], 'device': volume['mountpoint'], 'instanceId': ec2utils.id_to_ec2_id(instance_id), @@ -961,7 +962,7 @@ class CloudController(object): def detach_volume(self, context, volume_id, **kwargs): volume_id = ec2utils.ec2_id_to_id(volume_id) LOG.audit(_("Detach volume %s"), volume_id, context=context) - volume = self.volume_api.get(context, volume_id=volume_id) + volume = self.volume_api.get(context, volume_id) instance = self.compute_api.detach_volume(context, volume_id=volume_id) return {'attachTime': volume['attach_time'], 'device': volume['mountpoint'], @@ -1089,7 +1090,7 @@ class CloudController(object): assert not bdm['virtual_name'] root_device_type = 'ebs' - vol = self.volume_api.get(context, volume_id=volume_id) + vol = self.volume_api.get(context, volume_id) LOG.debug(_("vol = %s\n"), vol) # TODO(yamahata): volume attach time ebs = {'volumeId': volume_id, @@ -1624,13 +1625,13 @@ class CloudController(object): volume_id = m.get('volume_id') if m.get('snapshot_id') and volume_id: # create snapshot based on volume_id - vol = self.volume_api.get(context, volume_id=volume_id) + volume = self.volume_api.get(context, volume_id) # NOTE(yamahata): Should we wait for snapshot creation? # Linux LVM snapshot creation completes in # short time, it doesn't matter for now. snapshot = self.volume_api.create_snapshot_force( - context, volume_id=volume_id, name=vol['display_name'], - description=vol['display_description']) + context, volume, volume['display_name'], + volume['display_description']) m['snapshot_id'] = snapshot['id'] del m['volume_id'] diff --git a/nova/api/openstack/compute/contrib/virtual_storage_arrays.py b/nova/api/openstack/compute/contrib/virtual_storage_arrays.py index 440be6ad4..10e8a1001 100644 --- a/nova/api/openstack/compute/contrib/virtual_storage_arrays.py +++ b/nova/api/openstack/compute/contrib/virtual_storage_arrays.py @@ -278,7 +278,7 @@ class VsaVolumeDriveController(volumes.VolumeController): def _check_volume_ownership(self, context, vsa_id, id): obj = self.object try: - volume_ref = self.volume_api.get(context, volume_id=id) + volume_ref = self.volume_api.get(context, id) except exception.NotFound: LOG.error(_("%(obj)s with ID %(id)s not found"), locals()) raise @@ -333,9 +333,9 @@ class VsaVolumeDriveController(volumes.VolumeController): new_volume = self.volume_api.create(context, size, - None, vol.get('displayName'), vol.get('displayDescription'), + None, volume_type=volume_type, metadata=dict(from_vsa_id=str(vsa_id))) @@ -371,7 +371,8 @@ class VsaVolumeDriveController(volumes.VolumeController): locals(), context=context) try: - self.volume_api.update(context, volume_id=id, fields=changes) + volume = self.volume_api.get(context, id) + self.volume_api.update(context, volume, fields=changes) except exception.NotFound: raise exc.HTTPNotFound() return webob.Response(status_int=202) diff --git a/nova/api/openstack/compute/contrib/volumes.py b/nova/api/openstack/compute/contrib/volumes.py index 972c000ef..4509a4e1f 100644 --- a/nova/api/openstack/compute/contrib/volumes.py +++ b/nova/api/openstack/compute/contrib/volumes.py @@ -145,7 +145,8 @@ class VolumeController(object): LOG.audit(_("Delete volume with id: %s"), id, context=context) try: - self.volume_api.delete(context, volume_id=id) + volume = self.volume_api.get(context, id) + self.volume_api.delete(context, volume) except exception.NotFound: raise exc.HTTPNotFound() return webob.Response(status_int=202) @@ -191,10 +192,17 @@ class VolumeController(object): metadata = vol.get('metadata', None) + snapshot_id = vol.get('snapshot_id'), + + if snapshot_id is not None: + snapshot = self.volume_api.get_snapshot(context, snapshot_id) + else: + snapshot = None + new_volume = self.volume_api.create(context, size, - vol.get('snapshot_id'), vol.get('display_name'), vol.get('display_description'), + snapshot=snapshot, volume_type=vol_type, metadata=metadata) diff --git a/nova/api/openstack/volume/snapshots.py b/nova/api/openstack/volume/snapshots.py index d86332ba8..ec12c052b 100644 --- a/nova/api/openstack/volume/snapshots.py +++ b/nova/api/openstack/volume/snapshots.py @@ -82,7 +82,8 @@ class SnapshotsController(object): LOG.audit(_("Delete snapshot with id: %s"), id, context=context) try: - self.volume_api.delete_snapshot(context, snapshot_id=id) + snapshot = self.volume_api.get_snapshot(context, id) + self.volume_api.delete_snapshot(context, snapshot) except exception.NotFound: return exc.HTTPNotFound() return webob.Response(status_int=202) @@ -113,18 +114,19 @@ class SnapshotsController(object): snapshot = body['snapshot'] volume_id = snapshot['volume_id'] + volume = self.volume_api.get(context, volume_id) force = snapshot.get('force', False) - LOG.audit(_("Create snapshot from volume %s"), volume_id, - context=context) + msg = _("Create snapshot from volume %s") + LOG.audit(msg, volume_id, context=context) if force: new_snapshot = self.volume_api.create_snapshot_force(context, - volume_id, + volume, snapshot.get('display_name'), snapshot.get('display_description')) else: new_snapshot = self.volume_api.create_snapshot(context, - volume_id, + volume, snapshot.get('display_name'), snapshot.get('display_description')) diff --git a/nova/api/openstack/volume/volumes.py b/nova/api/openstack/volume/volumes.py index cdecb967a..6783244d6 100644 --- a/nova/api/openstack/volume/volumes.py +++ b/nova/api/openstack/volume/volumes.py @@ -106,7 +106,8 @@ class VolumeController(object): LOG.audit(_("Delete volume with id: %s"), id, context=context) try: - self.volume_api.delete(context, volume_id=id) + volume = self.volume_api.get(context, id) + self.volume_api.delete(context, volume) except exception.NotFound: raise exc.HTTPNotFound() return webob.Response(status_int=202) @@ -135,26 +136,34 @@ class VolumeController(object): if not body: raise exc.HTTPUnprocessableEntity() - vol = body['volume'] - size = vol['size'] + volume = body['volume'] + size = volume['size'] LOG.audit(_("Create volume of %s GB"), size, context=context) - vol_type = vol.get('volume_type', None) - if vol_type: + kwargs = {} + + req_volume_type = volume.get('volume_type', None) + if req_volume_type: try: - vol_type = volume_types.get_volume_type_by_name(context, - vol_type) + kwargs['volume_type'] = volume_types.get_volume_type_by_name( + context, req_volume_type) except exception.NotFound: raise exc.HTTPNotFound() - metadata = vol.get('metadata', None) + kwargs['metadata'] = volume.get('metadata', None) + + snapshot_id = volume.get('snapshot_id') + if snapshot_id is not None: + snapshot = self.volume_api.get_snapshot(context, snapshot_id) + else: + snapshot = None - new_volume = self.volume_api.create(context, size, - vol.get('snapshot_id'), - vol.get('display_name'), - vol.get('display_description'), - volume_type=vol_type, - metadata=metadata) + new_volume = self.volume_api.create(context, + size, + snapshot, + volume.get('display_name'), + volume.get('display_description'), + **kwargs) # Work around problem that instance is lazy-loaded... new_volume = self.volume_api.get(context, new_volume['id']) -- cgit