@require_context
@require_volume_exists
def _volume_glance_metadata_get(context, volume_id, session=None):
- return model_query(context, models.VolumeGlanceMetadata, session=session).\
+ rows = model_query(context, models.VolumeGlanceMetadata, session=session).\
filter_by(volume_id=volume_id).\
filter_by(deleted=False).\
all()
+ if not rows:
+ raise exception.GlanceMetadataNotFound(id=volume_id)
+
+ return rows
+
@require_context
@require_volume_exists
@require_context
@require_snapshot_exists
def _volume_snapshot_glance_metadata_get(context, snapshot_id, session=None):
- return model_query(context, models.VolumeGlanceMetadata, session=session).\
+ rows = model_query(context, models.VolumeGlanceMetadata, session=session).\
filter_by(snapshot_id=snapshot_id).\
filter_by(deleted=False).\
all()
+ if not rows:
+ raise exception.GlanceMetadataNotFound(id=snapshot_id)
+
+ return rows
+
@require_context
@require_snapshot_exists
" exists for volume id %(volume_id)s")
+class GlanceMetadataNotFound(NotFound):
+ message = _("Glance metadata for volume/snapshot %(id)s cannot be found.")
+
+
class ExportFailure(Invalid):
message = _("Failed to export for volume: %(reason)s")
vol_metadata = db.volume_glance_metadata_create(ctxt, 1, 'key1',
'value1')
db.volume_glance_metadata_delete_by_volume(ctxt, 1)
- metadata = db.volume_glance_metadata_get(ctxt, 1)
- self.assertEqual(len(metadata), 0)
- db.volume_glance_metadata_delete_by_volume(ctxt, 1)
- metadata = db.volume_glance_metadata_get(ctxt, 1)
- self.assertEqual(len(metadata), 0)
+ self.assertRaises(exception.GlanceMetadataNotFound,
+ db.volume_glance_metadata_get, ctxt, 1)
def test_vol_glance_metadata_copy_to_snapshot(self):
ctxt = context.get_admin_context()
db.volume_create(ctxt, {'id': 100, 'source_volid': 1})
vol_meta = db.volume_glance_metadata_create(ctxt, 1, 'key1',
'value1')
- db.volume_glance_metadata_copy_from_volume_to_volume(ctxt, 100, 1)
+ db.volume_glance_metadata_copy_from_volume_to_volume(ctxt, 1, 100)
- expected_meta = {'id': '100',
- 'key': 'key1',
+ expected_meta = {'key': 'key1',
'value': 'value1'}
for meta in db.volume_glance_metadata_get(ctxt, 100):
raise exception.MetadataUpdateFailure(reason=ex)
if make_bootable:
self._enable_bootable_flag(context, volume_id)
- try:
- LOG.debug(_("Copying metadata from snapshot %(snap_volume_id)s"
- " to %(volume_id)s") % {'snap_volume_id': snapshot_id,
- 'volume_id': volume_id})
- self.db.volume_glance_metadata_copy_to_volume(context, volume_id,
- snapshot_id)
- except exception.CinderException as ex:
- LOG.exception(_("Failed updating volume %(volume_id)s metadata"
- " using the provided glance snapshot "
- "%(snapshot_id)s metadata") %
- {'volume_id': volume_id, 'snapshot_id': snapshot_id})
- raise exception.MetadataCopyFailure(reason=ex)
+ try:
+ LOG.debug(_("Copying metadata from snapshot "
+ "%(snap_volume_id)s to %(volume_id)s") %
+ {'snap_volume_id': snapshot_id,
+ 'volume_id': volume_id})
+ self.db.volume_glance_metadata_copy_to_volume(
+ context, volume_id, snapshot_id)
+ except exception.CinderException as ex:
+ LOG.exception(_("Failed updating volume %(volume_id)s "
+ "metadata using the provided glance "
+ "snapshot %(snapshot_id)s metadata") %
+ {'volume_id': volume_id,
+ 'snapshot_id': snapshot_id})
+ raise exception.MetadataCopyFailure(reason=ex)
return model_update
def _enable_bootable_flag(self, context, volume_id):
# will not destroy the volume (although they could in the future).
if srcvol_ref.bootable:
self._enable_bootable_flag(context, volume_ref['id'])
- try:
- msg = _('Copying metadata from source volume %(source_volid)s'
- ' to cloned volume %(clone_vol_id)s')
- LOG.debug(msg % {'source_volid': source_volid,
- 'clone_vol_id': volume_ref['id'], })
- self.db.volume_glance_metadata_copy_from_volume_to_volume(
- context,
- source_volid,
- volume_ref['id'])
- except exception.CinderException as ex:
- LOG.exception(_("Failed updating cloned volume %(volume_id)s"
- " metadata using the provided source volumes"
- " %(source_volid)s metadata") %
- {'volume_id': volume_ref['id'],
- 'source_volid': source_volid})
- raise exception.MetadataCopyFailure(reason=ex)
+ try:
+ LOG.debug(_('Copying metadata from source volume '
+ '%(source_volid)s to cloned volume '
+ '%(clone_vol_id)s') % {
+ 'source_volid': source_volid,
+ 'clone_vol_id': volume_ref['id'],
+ })
+ self.db.volume_glance_metadata_copy_from_volume_to_volume(
+ context,
+ source_volid,
+ volume_ref['id'])
+ except exception.CinderException as ex:
+ LOG.exception(_("Failed updating cloned volume %(volume_id)s"
+ " metadata using the provided source volumes"
+ " %(source_volid)s metadata") %
+ {'volume_id': volume_ref['id'],
+ 'source_volid': source_volid})
+ raise exception.MetadataCopyFailure(reason=ex)
return model_update
def _copy_image_to_volume(self, context, volume_ref,
self.db.snapshot_update(context,
snapshot_ref['id'], {'status': 'available',
'progress': '100%'})
- self.db.volume_glance_metadata_copy_to_snapshot(context,
- snapshot_ref['id'],
- volume_id)
+
+ vol_ref = self.db.volume_get(context, volume_id)
+ if vol_ref.bootable:
+ try:
+ self.db.volume_glance_metadata_copy_to_snapshot(
+ context, snapshot_ref['id'], volume_id)
+ except exception.CinderException as ex:
+ LOG.exception(_("Failed updating %(snapshot_id)s"
+ " metadata using the provided volumes"
+ " %(volume_id)s metadata") %
+ {'volume_id': volume_id,
+ 'snapshot_id': snapshot_id})
+ raise exception.MetadataCopyFailure(reason=ex)
LOG.info(_("snapshot %s: created successfully"), snapshot_ref['name'])
self._notify_about_snapshot_usage(context, snapshot_ref, "create.end")
return snapshot_id