From 5b605b6b8ae00f51deac5e0531f7aeda9bd11019 Mon Sep 17 00:00:00 2001 From: John Griffith Date: Fri, 27 Jul 2012 22:30:47 -0600 Subject: [PATCH] Include volume_metadata with object on vol create Fix for Bug 1029762 The symptom of this bug is that the response data of an OSAPI create call always shows and empty dict for volume_metadata regardless of what was passed in to created and actually used. Upon the db create_volume call a reference to the volume object is all that was being returned. Since metadata is specified and set, it should also be returned with the create reference object. This will result in the the osapi create call returning a body with correct metdata info rather than always showing and empty dict as it was previously. Change-Id: I9ae9c737bd2aa5bfa14c19fe8b8b2a7a5aa4d43a --- cinder/db/sqlalchemy/api.py | 11 ++++++++++- cinder/tests/test_volume.py | 20 +++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/cinder/db/sqlalchemy/api.py b/cinder/db/sqlalchemy/api.py index a9d074141..6249e01d7 100644 --- a/cinder/db/sqlalchemy/api.py +++ b/cinder/db/sqlalchemy/api.py @@ -434,7 +434,16 @@ def volume_create(context, values): with session.begin(): volume_ref.save(session=session) - return volume_ref + meta = volume_metadata_get(context, volume_ref.id) + volume_ref.metadata = meta + + result = model_query(context, models.Volume, read_deleted="no").\ + options(joinedload('volume_metadata')).\ + filter_by(id=volume_ref['id']).first() + if not result: + raise exception.VolumeNotFound(volume_id=volume_ref['id']) + + return result @require_admin_context diff --git a/cinder/tests/test_volume.py b/cinder/tests/test_volume.py index 065272382..33271b8d9 100644 --- a/cinder/tests/test_volume.py +++ b/cinder/tests/test_volume.py @@ -53,7 +53,7 @@ class VolumeTestCase(test.TestCase): super(VolumeTestCase, self).tearDown() @staticmethod - def _create_volume(size='0', snapshot_id=None): + def _create_volume(size='0', snapshot_id=None, metadata=None): """Create a volume object.""" vol = {} vol['size'] = size @@ -63,6 +63,8 @@ class VolumeTestCase(test.TestCase): vol['availability_zone'] = FLAGS.storage_availability_zone vol['status'] = "creating" vol['attach_status'] = "detached" + if metadata is not None: + vol['metadata'] = metadata return db.volume_create(context.get_admin_context(), vol) def test_create_delete_volume(self): @@ -79,6 +81,22 @@ class VolumeTestCase(test.TestCase): self.context, volume_id) + def test_create_delete_volume_with_metadata(self): + """Test volume can be created and deleted.""" + test_meta = {'fake_key': 'fake_value'} + volume = self._create_volume('0', None, test_meta) + volume_id = volume['id'] + self.volume.create_volume(self.context, volume_id) + result_meta = { + volume.volume_metadata[0].key: volume.volume_metadata[0].value} + self.assertEqual(result_meta, test_meta) + + self.volume.delete_volume(self.context, volume_id) + self.assertRaises(exception.NotFound, + db.volume_get, + self.context, + volume_id) + def test_delete_busy_volume(self): """Test volume survives deletion if driver reports it as busy.""" volume = self._create_volume() -- 2.45.2