diff --git a/nova/compute/cells_api.py b/nova/compute/cells_api.py index c8d13bba42b3..f8825bbb8cba 100644 --- a/nova/compute/cells_api.py +++ b/nova/compute/cells_api.py @@ -22,6 +22,7 @@ from nova.cells import utils as cells_utils from nova.compute import api as compute_api from nova.compute import rpcapi as compute_rpcapi from nova import exception +from nova.objects import base as obj_base from nova.objects import service as service_obj from nova.openstack.common import excutils from nova import rpcclient @@ -480,7 +481,7 @@ class HostAPI(compute_api.HostAPI): # NOTE(danms): Currently cells does not support objects as # return values, so just convert the db-formatted service objects # to new-world objects here - return service_obj._make_list(context, + return obj_base.obj_make_list(context, service_obj.ServiceList(), service_obj.Service, services) diff --git a/nova/objects/aggregate.py b/nova/objects/aggregate.py index bbb35e4f8bc5..382082290c8e 100644 --- a/nova/objects/aggregate.py +++ b/nova/objects/aggregate.py @@ -144,22 +144,15 @@ class Aggregate(base.NovaObject): return self.metadata.get('availability_zone', None) -def _make_list(context, list_obj, item_cls, db_list): - list_obj.objects = [] - for db_item in db_list: - item = item_cls._from_db_object(context, item_cls(), db_item) - list_obj.objects.append(item) - list_obj.obj_reset_changes() - return list_obj - - class AggregateList(base.ObjectListBase, base.NovaObject): @base.remotable_classmethod def get_all(cls, context): db_aggregates = db.aggregate_get_all(context) - return _make_list(context, AggregateList(), Aggregate, db_aggregates) + return base.obj_make_list(context, AggregateList(), Aggregate, + db_aggregates) @base.remotable_classmethod def get_by_host(cls, context, host): db_aggregates = db.aggregate_get_by_host(context, host) - return _make_list(context, AggregateList(), Aggregate, db_aggregates) + return base.obj_make_list(context, AggregateList(), Aggregate, + db_aggregates) diff --git a/nova/objects/base.py b/nova/objects/base.py index 14f8909b2f72..6d2e7ee7d476 100644 --- a/nova/objects/base.py +++ b/nova/objects/base.py @@ -528,3 +528,26 @@ def obj_to_primitive(obj): return result else: return obj + + +def obj_make_list(context, list_obj, item_cls, db_list, **extra_args): + """Construct an object list from a list of primitives. + + This calls item_cls._from_db_object() on each item of db_list, and + adds the resulting object to list_obj. + + :param:context: Request contextr + :param:list_obj: An ObjectListBase object + :param:item_cls: The NovaObject class of the objects within the list + :param:db_list: The list of primitives to convert to objects + :param:extra_args: Extra arguments to pass to _from_db_object() + :returns: list_obj + """ + list_obj.objects = [] + for db_item in db_list: + item = item_cls._from_db_object(context, item_cls(), db_item, + **extra_args) + list_obj.objects.append(item) + list_obj._context = context + list_obj.obj_reset_changes() + return list_obj diff --git a/nova/objects/compute_node.py b/nova/objects/compute_node.py index 833be7731dfa..104d0bb866e5 100644 --- a/nova/objects/compute_node.py +++ b/nova/objects/compute_node.py @@ -92,23 +92,16 @@ class ComputeNode(base.NovaObject): return self._cached_service -def _make_list(context, list_obj, item_cls, db_list): - list_obj.objects = [] - for db_item in db_list: - item = item_cls._from_db_object(context, item_cls(), db_item) - list_obj.objects.append(item) - list_obj.obj_reset_changes() - return list_obj - - class ComputeNodeList(base.ObjectListBase, base.NovaObject): @base.remotable_classmethod def get_all(cls, context): db_computes = db.compute_node_get_all(context) - return _make_list(context, ComputeNodeList(), ComputeNode, db_computes) + return base.obj_make_list(context, ComputeNodeList(), ComputeNode, + db_computes) @base.remotable_classmethod def get_by_hypervisor(cls, context, hypervisor_match): db_computes = db.compute_node_search_by_hypervisor(context, hypervisor_match) - return _make_list(context, ComputeNodeList(), ComputeNode, db_computes) + return base.obj_make_list(context, ComputeNodeList(), ComputeNode, + db_computes) diff --git a/nova/objects/instance_action.py b/nova/objects/instance_action.py index 404572974421..eaa4b0096745 100644 --- a/nova/objects/instance_action.py +++ b/nova/objects/instance_action.py @@ -77,20 +77,11 @@ class InstanceAction(base.NovaObject): self._from_db_object(context, self, db_action) -def _make_list(context, list_obj, item_cls, db_list): - list_obj.objects = [] - for db_item in db_list: - item = item_cls._from_db_object(context, item_cls(), db_item) - list_obj.objects.append(item) - list_obj.obj_reset_changes() - return list_obj - - class InstanceActionList(base.ObjectListBase, base.NovaObject): @base.remotable_classmethod def get_by_instance_uuid(cls, context, instance_uuid): db_actions = db.actions_get(context, instance_uuid) - return _make_list(context, cls(), InstanceAction, db_actions) + return base.obj_make_list(context, cls(), InstanceAction, db_actions) class InstanceActionEvent(base.NovaObject): @@ -168,4 +159,5 @@ class InstanceActionEventList(base.ObjectListBase, base.NovaObject): @base.remotable_classmethod def get_by_action(cls, context, action_id): db_events = db.action_events_get(context, action_id) - return _make_list(context, cls(), InstanceActionEvent, db_events) + return base.obj_make_list(context, cls(), InstanceActionEvent, + db_events) diff --git a/nova/objects/keypair.py b/nova/objects/keypair.py index 77877694ae21..e44f9b720225 100644 --- a/nova/objects/keypair.py +++ b/nova/objects/keypair.py @@ -56,20 +56,11 @@ class KeyPair(base.NovaObject): db.key_pair_destroy(context, self.user_id, self.name) -def _make_list(context, list_obj, item_cls, db_list): - list_obj.objects = [] - for db_item in db_list: - item = item_cls._from_db_object(context, item_cls(), db_item) - list_obj.objects.append(item) - list_obj.obj_reset_changes() - return list_obj - - class KeyPairList(base.ObjectListBase, base.NovaObject): @base.remotable_classmethod def get_by_user(cls, context, user_id): db_keypairs = db.key_pair_get_all_by_user(context, user_id) - return _make_list(context, KeyPairList(), KeyPair, db_keypairs) + return base.obj_make_list(context, KeyPairList(), KeyPair, db_keypairs) @base.remotable_classmethod def get_count_by_user(cls, context, user_id): diff --git a/nova/objects/service.py b/nova/objects/service.py index 5b5a16d952d9..1cb5af4b9dae 100644 --- a/nova/objects/service.py +++ b/nova/objects/service.py @@ -119,25 +119,16 @@ class Service(base.NovaObject): db.service_destroy(context, self.id) -def _make_list(context, list_obj, item_cls, db_list): - list_obj.objects = [] - for db_item in db_list: - item = item_cls._from_db_object(context, item_cls(), db_item) - list_obj.objects.append(item) - list_obj.obj_reset_changes() - return list_obj - - class ServiceList(base.ObjectListBase, base.NovaObject): @base.remotable_classmethod def get_by_topic(cls, context, topic): db_services = db.service_get_all_by_topic(context, topic) - return _make_list(context, ServiceList(), Service, db_services) + return base.obj_make_list(context, ServiceList(), Service, db_services) @base.remotable_classmethod def get_by_host(cls, context, host): db_services = db.service_get_all_by_host(context, host) - return _make_list(context, ServiceList(), Service, db_services) + return base.obj_make_list(context, ServiceList(), Service, db_services) @base.remotable_classmethod def get_all(cls, context, disabled=None, set_zones=False): @@ -145,4 +136,4 @@ class ServiceList(base.ObjectListBase, base.NovaObject): if set_zones: db_services = availability_zones.set_availability_zones( context, db_services) - return _make_list(context, ServiceList(), Service, db_services) + return base.obj_make_list(context, ServiceList(), Service, db_services) diff --git a/nova/tests/objects/test_objects.py b/nova/tests/objects/test_objects.py index 8aa31cbd5447..8b7b8301708b 100644 --- a/nova/tests/objects/test_objects.py +++ b/nova/tests/objects/test_objects.py @@ -33,6 +33,14 @@ class MyObj(base.NovaObject): 'missing': str, } + @staticmethod + def _from_db_object(context, obj, db_obj): + self = MyObj() + self.foo = db_obj['foo'] + self.bar = db_obj['bar'] + self.missing = db_obj['missing'] + return self + def obj_load_attr(self, attrname): setattr(self, attrname, 'loaded!') @@ -234,6 +242,21 @@ class TestUtils(test.TestCase): self.assertEqual([{'foo': 0}, {'foo': 1}], base.obj_to_primitive(mylist)) + def test_obj_make_list(self): + class MyList(base.ObjectListBase, base.NovaObject): + pass + + db_objs = [{'foo': 1, 'bar': 'baz', 'missing': 'banana'}, + {'foo': 2, 'bar': 'bat', 'missing': 'apple'}, + ] + mylist = base.obj_make_list('ctxt', MyList(), MyObj, db_objs) + self.assertEqual(2, len(mylist)) + self.assertEqual('ctxt', mylist._context) + for index, item in enumerate(mylist): + self.assertEqual(db_objs[index]['foo'], item.foo) + self.assertEqual(db_objs[index]['bar'], item.bar) + self.assertEqual(db_objs[index]['missing'], item.missing) + class _BaseTestCase(test.TestCase): def setUp(self):

AltStyle によって変換されたページ (->オリジナル) /