l3: don't begin db transaction in set_extra_attr_value

As ExtraAttributesMixin.set_extra_attr_value() is assumed to be called
by callback of PRECOMMIT. So there is no point to start nested
transaction. So don't start transaction and check if transaction is
already started.

Change-Id: I0e2707222b1f91199cf466c997c0e691679fd3a4
Partial-bug: #1745633
This commit is contained in:
Isaku Yamahata 2018-02-07 11:51:50 -08:00 committed by Isaku Yamahata
parent c7e144e9b4
commit 31a3f2ff45
2 changed files with 25 additions and 18 deletions

View File

@ -54,13 +54,16 @@ class ExtraAttributesMixin(object):
router_db['extra_attributes'] = new
def set_extra_attr_value(self, context, router_db, key, value):
if not context.session.is_active:
raise RuntimeError(_("set_extra_attr_value cannot be called "
"out of a transaction."))
# set a single value explicitly
with context.session.begin(subtransactions=True):
if key in get_attr_info():
info = get_attr_info()[key]
to_db = info.get('transform_to_db', lambda x: x)
self._ensure_extra_attr_model(context, router_db)
router_db['extra_attributes'].update({key: to_db(value)})
return
raise RuntimeError(_("Tried to set a key '%s' that doesn't exist "
"in the extra attributes table.") % key)
if key in get_attr_info():
info = get_attr_info()[key]
to_db = info.get('transform_to_db', lambda x: x)
self._ensure_extra_attr_model(context, router_db)
router_db['extra_attributes'].update({key: to_db(value)})
return
raise RuntimeError(_("Tried to set a key '%s' that doesn't exist "
"in the extra attributes table.") % key)

View File

@ -577,8 +577,9 @@ class ExtraAttributesMixinTestCase(testlib_api.SqlTestCase):
def test_set_extra_attr_key_bad(self):
with testtools.ExpectedException(RuntimeError):
self.mixin.set_extra_attr_value(self.ctx, self.router,
'bad', 'value')
with self.ctx.session.begin():
self.mixin.set_extra_attr_value(self.ctx, self.router,
'bad', 'value')
def test__extend_extra_router_dict_defaults(self):
rdict = {}
@ -586,19 +587,22 @@ class ExtraAttributesMixinTestCase(testlib_api.SqlTestCase):
self.assertEqual(self._get_default_api_values(), rdict)
def test_set_attrs_and_extend(self):
self.mixin.set_extra_attr_value(self.ctx, self.router, 'ha_vr_id', 99)
self.mixin.set_extra_attr_value(self.ctx, self.router,
'availability_zone_hints',
['x', 'y', 'z'])
with self.ctx.session.begin():
self.mixin.set_extra_attr_value(self.ctx, self.router,
'ha_vr_id', 99)
self.mixin.set_extra_attr_value(self.ctx, self.router,
'availability_zone_hints',
['x', 'y', 'z'])
expected = self._get_default_api_values()
expected.update({'ha_vr_id': 99,
'availability_zone_hints': ['x', 'y', 'z']})
rdict = {}
self.mixin._extend_extra_router_dict(rdict, self.router)
self.assertEqual(expected, rdict)
self.mixin.set_extra_attr_value(self.ctx, self.router,
'availability_zone_hints',
['z', 'y', 'z'])
with self.ctx.session.begin():
self.mixin.set_extra_attr_value(self.ctx, self.router,
'availability_zone_hints',
['z', 'y', 'z'])
expected['availability_zone_hints'] = ['z', 'y', 'z']
self.mixin._extend_extra_router_dict(rdict, self.router)
self.assertEqual(expected, rdict)