From b36b7b3a66a0768e448655fd438a67bb667a7081 Mon Sep 17 00:00:00 2001 From: Balazs Gibizer Date: Mon, 9 Sep 2024 11:50:46 +0200 Subject: [PATCH] Refactor obj_make_compatible to reduce complexity Our tox.ini sets the maximum complexity of a function to be 40. Adding anything new to the ImageMetaProps.obj_make_compatible push it over the limit. So this patch factors out common new field value checking logic to a helper function to reduce the complexity before adding new supported values. As there was a small amount of this check in other ovos this patch does the refactor across all nova ovos. Change-Id: I828d91196ea06f4dc2823218f036960123446255 --- nova/objects/base.py | 10 +++++++ nova/objects/image_meta.py | 51 +++++++++++++---------------------- nova/objects/instance_numa.py | 13 +++------ nova/objects/pci_device.py | 17 ++++-------- 4 files changed, 37 insertions(+), 54 deletions(-) diff --git a/nova/objects/base.py b/nova/objects/base.py index bc00003481..d2a3a1a576 100644 --- a/nova/objects/base.py +++ b/nova/objects/base.py @@ -26,6 +26,7 @@ from oslo_utils import versionutils from oslo_versionedobjects import base as ovoo_base from oslo_versionedobjects import exception as ovoo_exc +from nova import exception from nova import objects from nova.objects import fields as obj_fields from nova import utils @@ -58,6 +59,15 @@ def get_attrname(name): return '_obj_' + name +def raise_on_too_new_values(version, primitive, field, new_values): + value = primitive.get(field, None) + if value in new_values: + raise exception.ObjectActionError( + action='obj_make_compatible', + reason='%s=%s not supported in version %s' % + (field, value, version)) + + class NovaObjectRegistry(ovoo_base.VersionedObjectRegistry): notification_classes = [] diff --git a/nova/objects/image_meta.py b/nova/objects/image_meta.py index 4c3589aaa4..f27cb94a5d 100644 --- a/nova/objects/image_meta.py +++ b/nova/objects/image_meta.py @@ -16,7 +16,6 @@ import copy from oslo_utils import versionutils -from nova import exception from nova import objects from nova.objects import base from nova.objects import fields @@ -224,32 +223,23 @@ class ImageMetaProps(base.NovaObject): if target_version < (1, 31): primitive.pop('hw_emulation_architecture', None) if target_version < (1, 30): - video = primitive.get('hw_video_model', None) - if video == fields.VideoModel.BOCHS: - raise exception.ObjectActionError( - action='obj_make_compatible', - reason='hw_video_model=%s not supported in version %s' % - (video, target_version)) + base.raise_on_too_new_values( + target_version, primitive, + 'hw_video_model', (fields.VideoModel.BOCHS,)) if target_version < (1, 29): primitive.pop('hw_input_bus', None) if target_version < (1, 28): - policy = primitive.get('hw_pci_numa_affinity_policy', None) - if policy == fields.PCINUMAAffinityPolicy.SOCKET: - raise exception.ObjectActionError( - action='obj_make_compatible', - reason='hw_numa_affinity_policy=%s not supported ' - 'in version %s' % - (policy, target_version)) + base.raise_on_too_new_values( + target_version, primitive, + 'hw_pci_numa_affinity_policy', + (fields.PCINUMAAffinityPolicy.SOCKET,)) if target_version < (1, 27): primitive.pop('hw_tpm_model', None) primitive.pop('hw_tpm_version', None) if target_version < (1, 26): - policy = primitive.get('hw_cpu_policy', None) - if policy == fields.CPUAllocationPolicy.MIXED: - raise exception.ObjectActionError( - action='obj_make_compatible', - reason='hw_cpu_policy=%s not supported in version %s' % - (policy, target_version)) + base.raise_on_too_new_values( + target_version, primitive, + 'hw_cpu_policy', (fields.CPUAllocationPolicy.MIXED,)) if target_version < (1, 25): primitive.pop('hw_pci_numa_affinity_policy', None) if target_version < (1, 24): @@ -259,12 +249,12 @@ class ImageMetaProps(base.NovaObject): # NOTE(sean-k-mooney): unlike other nova object we version this object # when composed object are updated. if target_version < (1, 22): - video = primitive.get('hw_video_model', None) - if video in ('gop', 'virtio', 'none'): - raise exception.ObjectActionError( - action='obj_make_compatible', - reason='hw_video_model=%s not supported in version %s' % ( - video, target_version)) + base.raise_on_too_new_values( + target_version, primitive, + 'hw_video_model', + (fields.VideoModel.GOP, + fields.VideoModel.VIRTIO, + fields.VideoModel.NONE)) if target_version < (1, 21): primitive.pop('hw_time_hpet', None) if target_version < (1, 20): @@ -303,12 +293,9 @@ class ImageMetaProps(base.NovaObject): primitive.pop('os_require_quiesce', None) if target_version < (1, 6): - bus = primitive.get('hw_disk_bus', None) - if bus in ('lxc', 'uml'): - raise exception.ObjectActionError( - action='obj_make_compatible', - reason='hw_disk_bus=%s not supported in version %s' % ( - bus, target_version)) + base.raise_on_too_new_values( + target_version, primitive, + 'hw_disk_bus', (fields.DiskBus.LXC, fields.DiskBus.UML)) # Maximum number of NUMA nodes permitted for the guest topology NUMA_NODES_MAX = 128 diff --git a/nova/objects/instance_numa.py b/nova/objects/instance_numa.py index 15ad54b63d..f4999c3667 100644 --- a/nova/objects/instance_numa.py +++ b/nova/objects/instance_numa.py @@ -20,7 +20,6 @@ from oslo_utils import versionutils from nova.db.main import api as db from nova import exception -from nova.i18n import _ from nova.objects import base from nova.objects import fields as obj_fields from nova.virt import hardware @@ -48,15 +47,9 @@ class InstanceNUMACell(base.NovaEphemeralObject, # Instance with a 'mixed' CPU policy could not provide a backward # compatibility. if target_version < (1, 6): - if primitive['cpu_policy'] == obj_fields.CPUAllocationPolicy.MIXED: - raise exception.ObjectActionError( - action='obj_make_compatible', - reason=_( - '{policy} policy is not supported in ' - 'version {version}' - ).format(policy=primitive['cpu_policy'], - version=target_version)) - + base.raise_on_too_new_values( + target_version, primitive, + 'cpu_policy', (obj_fields.CPUAllocationPolicy.MIXED,)) # NOTE(huaqiang): Since version 1.5, 'cpuset' is modified to track the # unpinned CPUs only, with pinned CPUs tracked via 'pcpuset' instead. # For a backward compatibility, move the 'dedicated' instance CPU list diff --git a/nova/objects/pci_device.py b/nova/objects/pci_device.py index ab775b4554..fca59b5199 100644 --- a/nova/objects/pci_device.py +++ b/nova/objects/pci_device.py @@ -132,21 +132,14 @@ class PciDevice(base.NovaPersistentObject, base.NovaObject): if target_version < (1, 5) and 'parent_addr' in primitive: added_statuses = (fields.PciDeviceStatus.UNCLAIMABLE, fields.PciDeviceStatus.UNAVAILABLE) - status = primitive['status'] - if status in added_statuses: - raise exception.ObjectActionError( - action='obj_make_compatible', - reason='status=%s not supported in version %s' % ( - status, target_version)) + base.raise_on_too_new_values( + target_version, primitive, 'status', added_statuses) if target_version < (1, 6) and 'uuid' in primitive: del primitive['uuid'] if target_version < (1, 7) and 'dev_type' in primitive: - dev_type = primitive['dev_type'] - if dev_type == fields.PciDeviceType.VDPA: - raise exception.ObjectActionError( - action='obj_make_compatible', - reason='dev_type=%s not supported in version %s' % ( - dev_type, target_version)) + base.raise_on_too_new_values( + target_version, primitive, + 'dev_type', (fields.PciDeviceType.VDPA,)) def __repr__(self): return (