Merge "objects: Add update_fields method in base class."

This commit is contained in:
Jenkins 2016-07-26 12:28:03 +00:00 committed by Gerrit Code Review
commit f7ec19ad01
5 changed files with 26 additions and 21 deletions

View File

@ -1041,11 +1041,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
address_scope_changed = (
orig_sp.address_scope_id != reader.address_scope_id)
for k, v in dict(reader.subnetpool).items():
# TODO(ihrachys): we should probably have some helper method in
# base object to update just updatable fields
if k not in subnetpool_obj.SubnetPool.fields_no_update:
orig_sp[k] = v
orig_sp.update_fields(reader.subnetpool)
orig_sp.update()
if address_scope_changed:

View File

@ -221,7 +221,7 @@ class DeclarativeObject(abc.ABCMeta):
if hasattr(base, 'obj_extra_fields'):
keys_set.update(base.obj_extra_fields)
for key in keys_set:
if key in cls.fields:
if key in cls.fields or key in cls.obj_extra_fields:
fields_no_update_set.add(key)
cls.fields_no_update = list(fields_no_update_set)
@ -482,8 +482,8 @@ class NeutronDbObject(NeutronObject):
keys[key] = getattr(self, key)
return keys
def update_nonidentifying_fields(self, obj_data, reset_changes=False):
"""Updates non-identifying fields of an object.
def update_fields(self, obj_data, reset_changes=False):
"""Updates fields of an object that are not forbidden to be updated.
:param obj_data: the full set of object data
:type obj_data: dict
@ -493,11 +493,10 @@ class NeutronDbObject(NeutronObject):
:returns: None
"""
if reset_changes:
self.obj_reset_changes()
for k, v in obj_data.items():
if k not in self.primary_keys:
if k not in self.fields_no_update:
setattr(self, k, v)
def update(self):

View File

@ -72,8 +72,7 @@ class QoSPlugin(qos.QoSPluginBase):
"""
policy_data = policy['policy']
policy_obj = policy_object.QosPolicy(context, id=policy_id)
policy_obj.update_nonidentifying_fields(policy_data,
reset_changes=True)
policy_obj.update_fields(policy_data, reset_changes=True)
policy_obj.update()
self.notification_driver_manager.update_policy(context, policy_obj)
return policy_obj
@ -203,7 +202,7 @@ class QoSPlugin(qos.QoSPluginBase):
# Ensure the rule belongs to the policy.
policy.get_rule_by_id(rule_id)
rule = rule_obj(context, id=rule_id)
rule.update_nonidentifying_fields(rule_data, reset_changes=True)
rule.update_fields(rule_data, reset_changes=True)
rule.update()
policy.reload_rules()
self.notification_driver_manager.update_policy(context, policy)

View File

@ -131,8 +131,7 @@ class TrunkPlugin(service_base.ServicePluginBase,
trunk_data = trunk['trunk']
with db_api.autonested_transaction(context.session):
trunk_obj = self._get_trunk(context, trunk_id)
trunk_obj.update_nonidentifying_fields(
trunk_data, reset_changes=True)
trunk_obj.update_fields(trunk_data, reset_changes=True)
trunk_obj.update()
return trunk_obj

View File

@ -561,7 +561,7 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase):
obj = self._test_class(self.context, **self.obj_fields[0])
self.assertRaises(base.NeutronDbObjectDuplicateEntry, obj.create)
def test_update_nonidentifying_fields(self):
def test_update_fields(self):
if not self._test_class.primary_keys:
self.skipTest(
'Test class %r has no primary keys' % self._test_class)
@ -569,17 +569,16 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase):
with mock.patch.object(obj_base.VersionedObject, 'obj_reset_changes'):
expected = self._test_class(self.context, **self.obj_fields[0])
for key, val in self.obj_fields[1].items():
if key not in expected.primary_keys:
if key not in expected.fields_no_update:
setattr(expected, key, val)
observed = self._test_class(self.context, **self.obj_fields[0])
observed.update_nonidentifying_fields(self.obj_fields[1],
reset_changes=True)
observed.update_fields(self.obj_fields[1], reset_changes=True)
self.assertEqual(expected, observed)
self.assertTrue(observed.obj_reset_changes.called)
with mock.patch.object(obj_base.VersionedObject, 'obj_reset_changes'):
obj = self._test_class(self.context, **self.obj_fields[0])
obj.update_nonidentifying_fields(self.obj_fields[1])
obj.update_fields(self.obj_fields[1])
self.assertFalse(obj.obj_reset_changes.called)
def test_extra_fields(self):
@ -804,10 +803,23 @@ class BaseDbObjectRenamedFieldTestCase(BaseObjectIfaceTestCase):
_test_class = FakeNeutronObjectRenamedField
class BaseObjectIfaceWithProjectId(BaseObjectIfaceTestCase):
class BaseObjectIfaceWithProjectIdTestCase(BaseObjectIfaceTestCase):
_test_class = FakeNeutronObjectWithProjectId
def test_update_fields_using_tenant_id(self):
obj = self._test_class(self.context, **self.obj_fields[0])
obj.obj_reset_changes()
tenant_id = obj['tenant_id']
new_obj_fields = dict()
new_obj_fields['tenant_id'] = uuidutils.generate_uuid()
new_obj_fields['field2'] = uuidutils.generate_uuid()
obj.update_fields(new_obj_fields)
self.assertEqual(set(['field2']), obj.obj_what_changed())
self.assertEqual(tenant_id, obj.project_id)
class BaseDbObjectMultipleForeignKeysTestCase(_BaseObjectTestCase,
test_base.BaseTestCase):