Reduce complexity of _from_db_object
This patch reduces the complexity of the helper method _from_db_object by creating a second helper method to handle processing the instance's extra attributes. Although this reduces the complexity of nova/object/instance.py, it is not possible to reduce the max-complexity in tox.ini, since there are still other files with a complexity of 34. Reducing the complexity of _from_db_object means that it is now possible to add new extra attributes without increasing the overall complexity of the nova code. Change-Id: Ide987bb5e15f1fe2eb61ad0b218d1d5cc6bf49a2
This commit is contained in:
parent
81f0ca1352
commit
46ecf410d4
|
@ -353,13 +353,6 @@ class Instance(base.NovaPersistentObject, base.NovaObject,
|
|||
else:
|
||||
instance[field] = db_inst[field]
|
||||
|
||||
# NOTE(danms): We can be called with a dict instead of a
|
||||
# SQLAlchemy object, so we have to be careful here
|
||||
if hasattr(db_inst, '__dict__'):
|
||||
have_extra = 'extra' in db_inst.__dict__ and db_inst['extra']
|
||||
else:
|
||||
have_extra = 'extra' in db_inst and db_inst['extra']
|
||||
|
||||
if 'metadata' in expected_attrs:
|
||||
instance['metadata'] = utils.instance_meta(db_inst)
|
||||
if 'system_metadata' in expected_attrs:
|
||||
|
@ -368,41 +361,8 @@ class Instance(base.NovaPersistentObject, base.NovaObject,
|
|||
instance['fault'] = (
|
||||
objects.InstanceFault.get_latest_for_instance(
|
||||
context, instance.uuid))
|
||||
if 'numa_topology' in expected_attrs:
|
||||
if have_extra:
|
||||
instance._load_numa_topology(
|
||||
db_inst['extra'].get('numa_topology'))
|
||||
else:
|
||||
instance.numa_topology = None
|
||||
if 'pci_requests' in expected_attrs:
|
||||
if have_extra:
|
||||
instance._load_pci_requests(
|
||||
db_inst['extra'].get('pci_requests'))
|
||||
else:
|
||||
instance.pci_requests = None
|
||||
if 'device_metadata' in expected_attrs:
|
||||
if have_extra:
|
||||
instance._load_device_metadata(
|
||||
db_inst['extra'].get('device_metadata'))
|
||||
else:
|
||||
instance.device_metadata = None
|
||||
if 'vcpu_model' in expected_attrs:
|
||||
if have_extra:
|
||||
instance._load_vcpu_model(
|
||||
db_inst['extra'].get('vcpu_model'))
|
||||
else:
|
||||
instance.vcpu_model = None
|
||||
if 'ec2_ids' in expected_attrs:
|
||||
instance._load_ec2_ids()
|
||||
if 'migration_context' in expected_attrs:
|
||||
if have_extra:
|
||||
instance._load_migration_context(
|
||||
db_inst['extra'].get('migration_context'))
|
||||
else:
|
||||
instance.migration_context = None
|
||||
if 'keypairs' in expected_attrs:
|
||||
if have_extra:
|
||||
instance._load_keypairs(db_inst['extra'].get('keypairs'))
|
||||
if 'info_cache' in expected_attrs:
|
||||
if db_inst.get('info_cache') is None:
|
||||
instance.info_cache = None
|
||||
|
@ -415,12 +375,6 @@ class Instance(base.NovaPersistentObject, base.NovaObject,
|
|||
instance.info_cache,
|
||||
db_inst['info_cache'])
|
||||
|
||||
if any([x in expected_attrs for x in ('flavor',
|
||||
'old_flavor',
|
||||
'new_flavor')]):
|
||||
if have_extra and db_inst['extra'].get('flavor'):
|
||||
instance._flavor_from_db(db_inst['extra']['flavor'])
|
||||
|
||||
# TODO(danms): If we are updating these on a backlevel instance,
|
||||
# we'll end up sending back new versions of these objects (see
|
||||
# above note for new info_caches
|
||||
|
@ -447,9 +401,65 @@ class Instance(base.NovaPersistentObject, base.NovaObject,
|
|||
objects.Service, db_inst['services'])
|
||||
instance['services'] = services
|
||||
|
||||
instance._extra_attributes_from_db_object(instance, db_inst,
|
||||
expected_attrs)
|
||||
|
||||
instance.obj_reset_changes()
|
||||
return instance
|
||||
|
||||
@staticmethod
|
||||
def _extra_attributes_from_db_object(instance, db_inst,
|
||||
expected_attrs=None):
|
||||
"""Method to help with migration of extra attributes to objects.
|
||||
"""
|
||||
if expected_attrs is None:
|
||||
expected_attrs = []
|
||||
# NOTE(danms): We can be called with a dict instead of a
|
||||
# SQLAlchemy object, so we have to be careful here
|
||||
if hasattr(db_inst, '__dict__'):
|
||||
have_extra = 'extra' in db_inst.__dict__ and db_inst['extra']
|
||||
else:
|
||||
have_extra = 'extra' in db_inst and db_inst['extra']
|
||||
|
||||
if 'numa_topology' in expected_attrs:
|
||||
if have_extra:
|
||||
instance._load_numa_topology(
|
||||
db_inst['extra'].get('numa_topology'))
|
||||
else:
|
||||
instance.numa_topology = None
|
||||
if 'pci_requests' in expected_attrs:
|
||||
if have_extra:
|
||||
instance._load_pci_requests(
|
||||
db_inst['extra'].get('pci_requests'))
|
||||
else:
|
||||
instance.pci_requests = None
|
||||
if 'device_metadata' in expected_attrs:
|
||||
if have_extra:
|
||||
instance._load_device_metadata(
|
||||
db_inst['extra'].get('device_metadata'))
|
||||
else:
|
||||
instance.device_metadata = None
|
||||
if 'vcpu_model' in expected_attrs:
|
||||
if have_extra:
|
||||
instance._load_vcpu_model(
|
||||
db_inst['extra'].get('vcpu_model'))
|
||||
else:
|
||||
instance.vcpu_model = None
|
||||
if 'migration_context' in expected_attrs:
|
||||
if have_extra:
|
||||
instance._load_migration_context(
|
||||
db_inst['extra'].get('migration_context'))
|
||||
else:
|
||||
instance.migration_context = None
|
||||
if 'keypairs' in expected_attrs:
|
||||
if have_extra:
|
||||
instance._load_keypairs(db_inst['extra'].get('keypairs'))
|
||||
if any([x in expected_attrs for x in ('flavor',
|
||||
'old_flavor',
|
||||
'new_flavor')]):
|
||||
if have_extra and db_inst['extra'].get('flavor'):
|
||||
instance._flavor_from_db(db_inst['extra']['flavor'])
|
||||
|
||||
@staticmethod
|
||||
@db.select_db_reader_mode
|
||||
def _db_instance_get_by_uuid(context, uuid, columns_to_join,
|
||||
|
|
Loading…
Reference in New Issue