From 8fb6ab6518d2132750888c00e669d1308a39ba95 Mon Sep 17 00:00:00 2001 From: yanyanhu Date: Thu, 2 Jun 2016 03:08:51 -0400 Subject: [PATCH] Minor tweak versioned object implementation This patch defines "_from_db_object" method in base class of Senlin versioned object. Subclasses which have special cases to deal with can override this function. Change-Id: Ic14e572654f3583bf64ffe7575e8bf7d6c22efb3 --- senlin/objects/action.py | 12 --------- senlin/objects/base.py | 15 +++++++++++ senlin/objects/cluster.py | 16 ------------ senlin/objects/credential.py | 12 --------- senlin/objects/event.py | 15 ----------- senlin/objects/health_registry.py | 10 ------- senlin/objects/node.py | 16 ------------ senlin/objects/policy.py | 13 ---------- senlin/objects/profile.py | 15 ----------- senlin/objects/receiver.py | 13 ---------- senlin/objects/service.py | 12 --------- senlin/tests/unit/objects/test_base.py | 36 ++++++++++++++++++++++++++ 12 files changed, 51 insertions(+), 134 deletions(-) diff --git a/senlin/objects/action.py b/senlin/objects/action.py index 621794a00..4c1e0d335 100644 --- a/senlin/objects/action.py +++ b/senlin/objects/action.py @@ -46,18 +46,6 @@ class Action(base.SenlinObject, base.VersionedObjectDictCompat): 'domain': fields.StringField(nullable=True), } - @staticmethod - def _from_db_object(context, action, db_obj): - if db_obj is None: - return None - for field in action.fields: - action[field] = db_obj[field] - - action._context = context - action.obj_reset_changes() - - return action - @classmethod def create(cls, context, values): obj = db_api.action_create(context, values) diff --git a/senlin/objects/base.py b/senlin/objects/base.py index 6261c5fa8..3d344b2ef 100644 --- a/senlin/objects/base.py +++ b/senlin/objects/base.py @@ -32,6 +32,21 @@ class SenlinObject(base.VersionedObject): OBJ_PROJECT_NAMESPACE = 'senlin' VERSION = '1.0' + @staticmethod + def _from_db_object(context, obj, db_obj): + if db_obj is None: + return None + for field in obj.fields: + if field == 'metadata': + obj['metadata'] = db_obj['meta_data'] + else: + obj[field] = db_obj[field] + + obj._context = context + obj.obj_reset_changes() + + return obj + class SenlinObjectRegistry(base.VersionedObjectRegistry): diff --git a/senlin/objects/cluster.py b/senlin/objects/cluster.py index 94ef15d13..ffca8c0b1 100644 --- a/senlin/objects/cluster.py +++ b/senlin/objects/cluster.py @@ -43,22 +43,6 @@ class Cluster(base.SenlinObject, base.VersionedObjectDictCompat): 'domain': fields.StringField(nullable=True), } - @staticmethod - def _from_db_object(context, cluster, db_obj): - if db_obj is None: - return None - - for field in cluster.fields: - if field == 'metadata': - cluster['metadata'] = db_obj['meta_data'] - else: - cluster[field] = db_obj[field] - - cluster._context = context - cluster.obj_reset_changes() - - return cluster - @classmethod def create(cls, context, values): obj = db_api.cluster_create(context, values) diff --git a/senlin/objects/credential.py b/senlin/objects/credential.py index e26f3f0cb..483d29823 100644 --- a/senlin/objects/credential.py +++ b/senlin/objects/credential.py @@ -28,18 +28,6 @@ class Credential(base.SenlinObject, base.VersionedObjectDictCompat): 'data': fields.JsonField(nullable=True), } - @staticmethod - def _from_db_object(context, credential, db_obj): - if db_obj is None: - return None - for field in credential.fields: - credential[field] = db_obj[field] - - credential._context = context - credential.obj_reset_changes() - - return credential - @classmethod def create(cls, context, values): obj = db_api.cred_create(context, values) diff --git a/senlin/objects/event.py b/senlin/objects/event.py index 8c1fb8855..d76de5c0d 100644 --- a/senlin/objects/event.py +++ b/senlin/objects/event.py @@ -37,21 +37,6 @@ class Event(base.SenlinObject, base.VersionedObjectDictCompat): 'metadata': fields.JsonField(nullable=True), } - @staticmethod - def _from_db_object(context, event, db_obj): - if db_obj is None: - return None - for field in event.fields: - if field == 'metadata': - event['metadata'] = db_obj['meta_data'] - else: - event[field] = db_obj[field] - - event._context = context - event.obj_reset_changes() - - return event - @classmethod def create(cls, context, values): obj = db_api.event_create(context, values) diff --git a/senlin/objects/health_registry.py b/senlin/objects/health_registry.py index 577956bb6..08759ed8c 100644 --- a/senlin/objects/health_registry.py +++ b/senlin/objects/health_registry.py @@ -30,16 +30,6 @@ class HealthRegistry(base.SenlinObject, base.VersionedObjectDictCompat): 'engine_id': fields.UUIDField(), } - @staticmethod - def _from_db_object(context, registry, db_obj): - for field in registry.fields: - registry[field] = db_obj[field] - - registry._context = context - registry.obj_reset_changes() - - return registry - @classmethod def create(cls, context, cluster_id, check_type, interval, params, engine_id): diff --git a/senlin/objects/node.py b/senlin/objects/node.py index 7c6f94c47..d4277a254 100644 --- a/senlin/objects/node.py +++ b/senlin/objects/node.py @@ -42,22 +42,6 @@ class Node(base.SenlinObject, base.VersionedObjectDictCompat): 'domain': fields.StringField(nullable=True), } - @staticmethod - def _from_db_object(context, node, db_obj): - if db_obj is None: - return None - - for field in node.fields: - if field == 'metadata': - node['metadata'] = db_obj['meta_data'] - else: - node[field] = db_obj[field] - - node._context = context - node.obj_reset_changes() - - return node - @classmethod def create(cls, context, values): obj = db_api.node_create(context, values) diff --git a/senlin/objects/policy.py b/senlin/objects/policy.py index 727cb5c8a..99fffa5f8 100644 --- a/senlin/objects/policy.py +++ b/senlin/objects/policy.py @@ -36,19 +36,6 @@ class Policy(base.SenlinObject, base.VersionedObjectDictCompat): 'domain': fields.StringField(nullable=True), } - @staticmethod - def _from_db_object(context, policy, db_obj): - if db_obj is None: - return None - - for field in policy.fields: - policy[field] = db_obj[field] - - policy._context = context - policy.obj_reset_changes() - - return policy - @classmethod def create(cls, context, values): obj = db_api.policy_create(context, values) diff --git a/senlin/objects/profile.py b/senlin/objects/profile.py index d7a82f416..6c20e4314 100644 --- a/senlin/objects/profile.py +++ b/senlin/objects/profile.py @@ -36,21 +36,6 @@ class Profile(base.SenlinObject, base.VersionedObjectDictCompat): 'metadata': fields.JsonField(nullable=True), } - @staticmethod - def _from_db_object(context, profile, db_profile): - if db_profile is None: - return None - for field in profile.fields: - if field == 'metadata': - profile['metadata'] = db_profile['meta_data'] - else: - profile[field] = db_profile[field] - - profile._context = context - profile.obj_reset_changes() - - return profile - @classmethod def create(cls, context, values): obj = db_api.profile_create(context, values) diff --git a/senlin/objects/receiver.py b/senlin/objects/receiver.py index ea3c12a69..73ebb4a74 100644 --- a/senlin/objects/receiver.py +++ b/senlin/objects/receiver.py @@ -37,19 +37,6 @@ class Receiver(base.SenlinObject, base.VersionedObjectDictCompat): 'domain': fields.StringField(nullable=True), } - @staticmethod - def _from_db_object(context, receiver, db_obj): - if db_obj is None: - return None - - for field in receiver.fields: - receiver[field] = db_obj[field] - - receiver._context = context - receiver.obj_reset_changes() - - return receiver - @classmethod def create(cls, context, values): obj = db_api.receiver_create(context, values) diff --git a/senlin/objects/service.py b/senlin/objects/service.py index 88430948c..1fafa6224 100644 --- a/senlin/objects/service.py +++ b/senlin/objects/service.py @@ -32,18 +32,6 @@ class Service(base.SenlinObject, base.VersionedObjectDictCompat): 'updated_at': fields.DateTimeField(), } - @staticmethod - def _from_db_object(context, service, db_obj): - if db_obj is None: - return None - for field in service.fields: - service[field] = db_obj[field] - - service._context = context - service.obj_reset_changes() - - return service - @classmethod def create(cls, context, service_id, host=None, binary=None, topic=None): obj = db_api.service_create(context, service_id=service_id, host=host, diff --git a/senlin/tests/unit/objects/test_base.py b/senlin/tests/unit/objects/test_base.py index 9cd581456..9c57c2b42 100644 --- a/senlin/tests/unit/objects/test_base.py +++ b/senlin/tests/unit/objects/test_base.py @@ -10,7 +10,10 @@ # License for the specific language governing permissions and limitations # under the License. +import mock + from senlin.objects import base as obj_base +from senlin.objects import fields as obj_fields from senlin.tests.unit.common import base @@ -22,3 +25,36 @@ class TestBaseObject(base.SenlinTestCase): obj.OBJ_PROJECT_NAMESPACE) self.assertEqual(obj_base.SenlinObject.VERSION, obj.VERSION) + + @mock.patch.object(obj_base.SenlinObject, "obj_reset_changes") + def test_from_db_object(self, mock_obj_reset_ch): + class TestSenlinObject(obj_base.SenlinObject, + obj_base.VersionedObjectDictCompat): + fields = { + "key1": obj_fields.StringField(), + "key2": obj_fields.StringField(), + "metadata": obj_fields.JsonField() + } + + obj = TestSenlinObject() + context = mock.Mock() + db_obj = { + "key1": "value1", + "key2": "value2", + "meta_data": {"key3": "value3"} + } + res = obj_base.SenlinObject._from_db_object(context, obj, db_obj) + self.assertIsNotNone(res) + self.assertEqual("value1", obj["key1"]) + self.assertEqual("value2", obj["key2"]) + self.assertEqual({"key3": "value3"}, obj["metadata"]) + self.assertEqual(obj._context, context) + mock_obj_reset_ch.assert_called_once_with() + + def test_from_db_object_none(self): + obj = obj_base.SenlinObject() + db_obj = None + context = mock.Mock() + + res = obj_base.SenlinObject._from_db_object(context, obj, db_obj) + self.assertIsNone(res)