Refactor refresh method in objects for reuse

This change refactors the "refresh" method in ironic.objects.chassis,
ironic.objects.conductor, ironic.objects.node and ironic.objects.port to
reuse a more generic method added to ironic.objects.base.IronicObject
for identifying and applying changes of fields. It make the code in
refresh method more simpler to understand by removing details of
implementation, in addition decreases the code replication in four
classes.

Change-Id: I6171203690cfd282fa1250dbe0204151f5c665fb
This commit is contained in:
Sinval Vieira 2015-07-21 01:23:15 -03:00
parent 57c3065eb8
commit 1a76311f07
6 changed files with 30 additions and 16 deletions

View File

@ -399,6 +399,18 @@ class IronicObject(ovo_base.VersionedObjectDictCompat):
for k in self.fields
if hasattr(self, k))
def obj_refresh(self, loaded_object):
"""Applies updates for objects that inherit from base.IronicObject.
Checks for updated attributes in an object. Updates are applied from
the loaded object column by column in comparison with the current
object.
"""
for field in self.fields:
if (hasattr(self, get_attrname(field)) and
self[field] != loaded_object[field]):
self[field] = loaded_object[field]
class ObjectListBase(object):
"""Mixin class for lists of objects.

View File

@ -180,7 +180,4 @@ class Chassis(base.IronicObject):
object, e.g.: Chassis(context)
"""
current = self.__class__.get_by_uuid(self._context, uuid=self.uuid)
for field in self.fields:
if (hasattr(self, base.get_attrname(field)) and
self[field] != current[field]):
self[field] = current[field]
self.obj_refresh(current)

View File

@ -72,10 +72,7 @@ class Conductor(base.IronicObject):
"""
current = self.__class__.get_by_hostname(self._context,
hostname=self.hostname)
for field in self.fields:
if (hasattr(self, base.get_attrname(field)) and
self[field] != current[field]):
self[field] = current[field]
self.obj_refresh(current)
@base.remotable
def touch(self, context):

View File

@ -282,10 +282,7 @@ class Node(base.IronicObject):
object, e.g.: Node(context)
"""
current = self.__class__.get_by_uuid(self._context, self.uuid)
for field in self.fields:
if (hasattr(self, base.get_attrname(field)) and
self[field] != current[field]):
self[field] = current[field]
self.obj_refresh(current)
@base.remotable
def touch_provisioning(self, context=None):

View File

@ -211,7 +211,4 @@ class Port(base.IronicObject):
object, e.g.: Port(context)
"""
current = self.__class__.get_by_uuid(self._context, uuid=self.uuid)
for field in self.fields:
if (hasattr(self, base.get_attrname(field)) and
self[field] != current[field]):
self[field] = current[field]
self.obj_refresh(current)

View File

@ -444,6 +444,20 @@ class _TestObject(object):
self.assertEqual(set(['created_at', 'updated_at', 'foo', 'bar']),
set(obj.obj_fields))
def test_refresh_object(self):
class TestObj(base.IronicObject):
fields = {'foo': int, 'bar': str}
obj = TestObj(self.context)
current_obj = TestObj(self.context)
obj.foo = 10
obj.bar = 'obj.bar'
current_obj.foo = 2
current_obj.bar = 'current.bar'
obj.obj_refresh(current_obj)
self.assertEqual(obj.foo, 2)
self.assertEqual(obj.bar, 'current.bar')
def test_obj_constructor(self):
obj = MyObj(self.context, foo=123, bar='abc')
self.assertEqual(123, obj.foo)