]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
[qos] cleanup _find_object from neutron.db.api
authorIhar Hrachyshka <ihrachys@redhat.com>
Tue, 21 Jul 2015 13:05:33 +0000 (15:05 +0200)
committerIhar Hrachyshka <ihrachys@redhat.com>
Thu, 23 Jul 2015 11:49:49 +0000 (13:49 +0200)
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

neutron/db/api.py
neutron/objects/base.py
neutron/objects/qos/policy.py
neutron/objects/qos/rule.py
neutron/tests/unit/objects/qos/test_rule.py
neutron/tests/unit/objects/test_base.py

index ba9c70a3a14186def6be740fe2625380667ae519..2c438055ccc5e75cfa8aacd65d836eff907b3f0e 100644 (file)
@@ -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)
index e41ac9ec4d93700284caf3fce346dffd94a36a75..4fe8431d60282bcd9ac30a649ad9a14f15640407 100644 (file)
@@ -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()
index 3f0ba35cef7d245a233565e1b5154c5accfc5c1a..0c1718ef4865fb50e570e27b941eaeb302c3412f 100644 (file)
@@ -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'])
index efe8c5335457d8c81092a2cd041a7a63e2cc6aaf..6269e8dbb225b759a2ab22fad81db9be0706d6b5 100644 (file)
@@ -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])
 
index 53024b281331be7179fc3f6a9460fa8253482e4b..6a3736e1756280de16e1015751834951ca48b85a 100644 (file)
@@ -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)
index 45725c52975377756203f19130b0b571b2d878d3..5e15dc79717b6a3c178509a622fc84b2ac8824fe 100644 (file)
@@ -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):