Save model_update as admin in create_volume

admin_metadata are not saved by the object without an admin context.
Turns out Huawei driver returns admin_metadata in model_update. To make
sure admin_metadata will be saved we should save the volume to the DB
with admin context. This commit implements that.

Change-Id: Ia48498734db61474009c8854a353c67cbdfccc0e
Closes-Bug: 1656867
This commit is contained in:
Michał Dulko 2017-01-16 15:53:31 +01:00
parent d9e48eb1a4
commit beccf8b671
2 changed files with 11 additions and 2 deletions

View File

@ -1054,6 +1054,14 @@ class VolumeTestCase(base.BaseVolumeTestCase):
self.volume.create_volume(self.context, volume) self.volume.create_volume(self.context, volume)
self.assertEqual(fake.PROVIDER_ID, volume['provider_id']) self.assertEqual(fake.PROVIDER_ID, volume['provider_id'])
def test_create_volume_with_admin_metadata(self):
with mock.patch.object(
self.volume.driver, 'create_volume',
return_value={'admin_metadata': {'foo': 'bar'}}):
volume = tests_utils.create_volume(self.user_context)
self.volume.create_volume(self.user_context, volume)
self.assertEqual({'foo': 'bar'}, volume['admin_metadata'])
@mock.patch.object(key_manager, 'API', new=fake_keymgr.fake_api) @mock.patch.object(key_manager, 'API', new=fake_keymgr.fake_api)
def test_create_delete_volume_with_encrypted_volume_type(self): def test_create_delete_volume_with_encrypted_volume_type(self):
cipher = 'aes-xts-plain64' cipher = 'aes-xts-plain64'

View File

@ -857,8 +857,9 @@ class CreateVolumeFromSpecTask(flow_utils.CinderTask):
# Persist any model information provided on creation. # Persist any model information provided on creation.
try: try:
if model_update: if model_update:
volume.update(model_update) with volume.obj_as_admin():
volume.save() volume.update(model_update)
volume.save()
except exception.CinderException: except exception.CinderException:
# If somehow the update failed we want to ensure that the # If somehow the update failed we want to ensure that the
# failure is logged (but not try rescheduling since the volume at # failure is logged (but not try rescheduling since the volume at