From: Ihar Hrachyshka Date: Tue, 21 Jul 2015 13:05:33 +0000 (+0200) Subject: [qos] cleanup _find_object from neutron.db.api X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=69f4b813e8a086512f39ee3ef5b8f3354f9af8c0;p=openstack-build%2Fneutron-build.git [qos] cleanup _find_object from neutron.db.api Instead of having a separate function for id-based search, make get_object accept any kwargs, including id=, and reuse it everywhere where we used _find_object before. Change-Id: Ibd94c41fb847d67aabb94172d0117bafc0fdbdf6 --- diff --git a/neutron/db/api.py b/neutron/db/api.py index ba9c70a3a..2c438055c 100644 --- a/neutron/db/api.py +++ b/neutron/db/api.py @@ -94,23 +94,13 @@ class convert_db_exception_to_retry(object): # Common database operation implementations -# TODO(QoS): consider reusing get_objects below -# TODO(QoS): consider changing the name and making it public, officially -def _find_object(context, model, **kwargs): +def get_object(context, model, **kwargs): with context.session.begin(subtransactions=True): return (common_db_mixin.model_query(context, model) .filter_by(**kwargs) .first()) -def get_object(context, model, id): - # TODO(QoS): consider reusing get_objects below - with context.session.begin(subtransactions=True): - return (common_db_mixin.model_query(context, model) - .filter_by(id=id) - .first()) - - def get_objects(context, model, **kwargs): with context.session.begin(subtransactions=True): return (common_db_mixin.model_query(context, model) @@ -129,7 +119,7 @@ def create_object(context, model, values): def update_object(context, model, id, values): with context.session.begin(subtransactions=True): - db_obj = get_object(context, model, id) + db_obj = get_object(context, model, id=id) db_obj.update(values) db_obj.save(session=context.session) return db_obj.__dict__ @@ -137,5 +127,5 @@ def update_object(context, model, id, values): def delete_object(context, model, id): with context.session.begin(subtransactions=True): - db_obj = get_object(context, model, id) + db_obj = get_object(context, model, id=id) context.session.delete(db_obj) diff --git a/neutron/objects/base.py b/neutron/objects/base.py index e41ac9ec4..4fe8431d6 100644 --- a/neutron/objects/base.py +++ b/neutron/objects/base.py @@ -48,7 +48,7 @@ class NeutronObject(obj_base.VersionedObject, @classmethod def get_by_id(cls, context, id): - db_obj = db_api.get_object(context, cls.db_model, id) + db_obj = db_api.get_object(context, cls.db_model, id=id) if db_obj: obj = cls(context, **db_obj) obj.obj_reset_changes() diff --git a/neutron/objects/qos/policy.py b/neutron/objects/qos/policy.py index 3f0ba35ce..0c1718ef4 100644 --- a/neutron/objects/qos/policy.py +++ b/neutron/objects/qos/policy.py @@ -77,8 +77,7 @@ class QosPolicy(base.NeutronObject): @classmethod def _get_object_policy(cls, context, model, **kwargs): - # TODO(QoS): we should make sure we use public functions - binding_db_obj = db_api._find_object(context, model, **kwargs) + binding_db_obj = db_api.get_object(context, model, **kwargs) # TODO(QoS): rethink handling missing binding case if binding_db_obj: return cls.get_by_id(context, binding_db_obj['policy_id']) diff --git a/neutron/objects/qos/rule.py b/neutron/objects/qos/rule.py index efe8c5335..6269e8dbb 100644 --- a/neutron/objects/qos/rule.py +++ b/neutron/objects/qos/rule.py @@ -89,7 +89,7 @@ class QosRule(base.NeutronObject): if obj: # the object above does not contain fields from base QosRule yet, # so fetch it and mix its fields into the object - base_db_obj = db_api.get_object(context, cls.base_db_model, id) + base_db_obj = db_api.get_object(context, cls.base_db_model, id=id) for field in cls._core_fields: setattr(obj, field, base_db_obj[field]) diff --git a/neutron/tests/unit/objects/qos/test_rule.py b/neutron/tests/unit/objects/qos/test_rule.py index 53024b281..6a3736e17 100644 --- a/neutron/tests/unit/objects/qos/test_rule.py +++ b/neutron/tests/unit/objects/qos/test_rule.py @@ -54,7 +54,7 @@ class QosBandwidthLimitRuleObjectTestCase(test_base.BaseObjectIfaceTestCase): self.assertTrue(self._is_test_class(obj)) self.assertEqual(self.db_obj, test_base.get_obj_db_fields(obj)) get_object_mock.assert_has_calls([ - mock.call(self.context, model, 'fake_id') + mock.call(self.context, model, id='fake_id') for model in (self._test_class.db_model, self._test_class.base_db_model) ], any_order=True) diff --git a/neutron/tests/unit/objects/test_base.py b/neutron/tests/unit/objects/test_base.py index 45725c529..5e15dc797 100644 --- a/neutron/tests/unit/objects/test_base.py +++ b/neutron/tests/unit/objects/test_base.py @@ -95,7 +95,7 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase): self.assertTrue(self._is_test_class(obj)) self.assertEqual(self.db_obj, get_obj_db_fields(obj)) get_object_mock.assert_called_once_with( - self.context, self._test_class.db_model, 'fake_id') + self.context, self._test_class.db_model, id='fake_id') def test_get_by_id_missing_object(self): with mock.patch.object(db_api, 'get_object', return_value=None):