From 35abd78582f9613e3933f2a69881ce42929f7dbd Mon Sep 17 00:00:00 2001 From: Anna Khmelnitsky Date: Mon, 3 Feb 2020 17:33:11 -0800 Subject: [PATCH] Refactor version dependant attributes in policy Move version check to base class. Child classes where version checks are relevant should define a map of version-dependant attributes, specifying first supporting version as map value. Change-Id: Ifa8bc1e0a26748b1dadbe4269c73b471bc57f1b5 --- vmware_nsxlib/v3/policy/core_defs.py | 71 ++++++++++++++++------------ vmware_nsxlib/v3/policy/lb_defs.py | 19 ++------ 2 files changed, 44 insertions(+), 46 deletions(-) diff --git a/vmware_nsxlib/v3/policy/core_defs.py b/vmware_nsxlib/v3/policy/core_defs.py index 62c5bcab..362e1b81 100644 --- a/vmware_nsxlib/v3/policy/core_defs.py +++ b/vmware_nsxlib/v3/policy/core_defs.py @@ -227,12 +227,23 @@ class ResourceDef(object): for attr in attr_list: self._set_attr_if_supported(body, attr) + @property + def version_dependant_attr_map(self): + """Specify version depenand attributes and supporting NSX version + + Resources that contain version dependant attributes should specify + attribute name and first supporting version in map returned from + this call. + """ + return {} + def _version_dependant_attr_supported(self, attr): """Check if a version dependent attr is supported on current NSX For each resource def, there could be some attributes which only exist - on NSX after certain versions. This abstract method provides a skeleton - to define version requirements of version-dependent attributes. + on NSX after certain versions. These attrs should be defined on def + level via version_dependant_attr_map, where map value indicates NSX + version that first exposes the support. By design, Devs should use _set_attr_if_supported() to add any attrs that are only known to NSX after a certain version. This method works @@ -244,6 +255,21 @@ class ResourceDef(object): any version dependent attr unknown to this lib should be excluded for security and safety reasons. """ + supporting_version = self.version_dependant_attr_map.get(attr) + if not supporting_version: + LOG.warning("Supporting version not defined for attr %s. Assuming " + "no support", attr) + return False + + if (version.LooseVersion(self.nsx_version) >= + version.LooseVersion(supporting_version)): + return True + + LOG.warning( + "Ignoring %s for %s %s: this feature is not supported." + "Current NSX version: %s. Minimum supported version: %s", + attr, self.resource_type, self.attrs.get('name', ''), + self.nsx_version, supporting_version) return False @classmethod @@ -821,22 +847,11 @@ class SegmentDef(BaseSegmentDef): def path_defs(self): return (TenantDef,) - def _version_dependant_attr_supported(self, attr): - if (version.LooseVersion(self.nsx_version) >= - version.LooseVersion(nsx_constants.NSX_VERSION_3_0_0)): - if attr in ('metadata_proxy_id', - 'dhcp_server_config_id', - 'admin_state'): - return True - else: - LOG.warning( - "Ignoring %s for %s %s: this feature is not supported." - "Current NSX version: %s. Minimum supported version: %s", - attr, self.resource_type, self.attrs.get('name', ''), - self.nsx_version, nsx_constants.NSX_VERSION_3_0_0) - return False - - return False + @property + def version_dependant_attr_map(self): + return {'metadata_proxy_id': nsx_constants.NSX_VERSION_3_0_0, + 'dhcp_server_config_id': nsx_constants.NSX_VERSION_3_0_0, + 'admin_state': nsx_constants.NSX_VERSION_3_0_0} def get_obj_dict(self): body = super(SegmentDef, self).get_obj_dict() @@ -1032,18 +1047,10 @@ class SegmentPortDef(ResourceDef): return body - def _version_dependant_attr_supported(self, attr): - if (version.LooseVersion(self.nsx_version) >= - version.LooseVersion(nsx_constants.NSX_VERSION_3_0_0)): - if attr == 'admin_state': - return True - - LOG.warning( - "Ignoring %s for %s %s: this feature is not supported." - "Current NSX version: %s. Minimum supported version: %s", - attr, self.resource_type, self.attrs.get('name', ''), - self.nsx_version, nsx_constants.NSX_VERSION_3_0_0) - return False + @property + def version_dependant_attr_map(self): + return {'hyperbus_mode': nsx_constants.NSX_VERSION_3_0_0, + 'admin_state': nsx_constants.NSX_VERSION_3_0_0} class SegmentBindingMapDefBase(ResourceDef): @@ -1602,6 +1609,10 @@ class SecurityPolicyRuleBaseDef(ResourceDef): rule_def.set_obj_dict(rule_dict) return rule_def + @property + def version_dependant_attr_map(self): + return {'service_entries': nsx_constants.NSX_VERSION_3_0_0} + class CommunicationMapEntryDef(SecurityPolicyRuleBaseDef): diff --git a/vmware_nsxlib/v3/policy/lb_defs.py b/vmware_nsxlib/v3/policy/lb_defs.py index 63fb1838..92a6ace1 100644 --- a/vmware_nsxlib/v3/policy/lb_defs.py +++ b/vmware_nsxlib/v3/policy/lb_defs.py @@ -14,8 +14,6 @@ # under the License. # -from distutils import version - from oslo_log import log as logging from vmware_nsxlib.v3 import nsx_constants from vmware_nsxlib.v3.policy import constants @@ -388,20 +386,9 @@ class LBServiceDef(ResourceDef): self._set_attrs_if_supported(body, ['relax_scale_validation']) return body - def _version_dependant_attr_supported(self, attr): - if (version.LooseVersion(self.nsx_version) >= - version.LooseVersion(nsx_constants.NSX_VERSION_3_0_0)): - if attr == 'relax_scale_validation': - return True - else: - LOG.warning( - "Ignoring %s for %s %s: this feature is not supported." - "Current NSX version: %s. Minimum supported version: %s", - attr, self.resource_type, self.attrs.get('name', ''), - self.nsx_version, nsx_constants.NSX_VERSION_3_0_0) - return False - - return False + @property + def version_dependant_attr_map(self): + return {'relax_scale_validation': nsx_constants.NSX_VERSION_3_0_0} class LBServiceStatisticsDef(ResourceDef):