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
This commit is contained in:
Anna Khmelnitsky 2020-02-03 17:33:11 -08:00 committed by asarfaty
parent 873ebdcbee
commit 35abd78582
2 changed files with 44 additions and 46 deletions

View File

@ -227,12 +227,23 @@ class ResourceDef(object):
for attr in attr_list: for attr in attr_list:
self._set_attr_if_supported(body, attr) 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): def _version_dependant_attr_supported(self, attr):
"""Check if a version dependent attr is supported on current NSX """Check if a version dependent attr is supported on current NSX
For each resource def, there could be some attributes which only exist For each resource def, there could be some attributes which only exist
on NSX after certain versions. This abstract method provides a skeleton on NSX after certain versions. These attrs should be defined on def
to define version requirements of version-dependent attributes. 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 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 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 any version dependent attr unknown to this lib should be excluded
for security and safety reasons. 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 return False
@classmethod @classmethod
@ -821,22 +847,11 @@ class SegmentDef(BaseSegmentDef):
def path_defs(self): def path_defs(self):
return (TenantDef,) return (TenantDef,)
def _version_dependant_attr_supported(self, attr): @property
if (version.LooseVersion(self.nsx_version) >= def version_dependant_attr_map(self):
version.LooseVersion(nsx_constants.NSX_VERSION_3_0_0)): return {'metadata_proxy_id': nsx_constants.NSX_VERSION_3_0_0,
if attr in ('metadata_proxy_id', 'dhcp_server_config_id': nsx_constants.NSX_VERSION_3_0_0,
'dhcp_server_config_id', 'admin_state': nsx_constants.NSX_VERSION_3_0_0}
'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
def get_obj_dict(self): def get_obj_dict(self):
body = super(SegmentDef, self).get_obj_dict() body = super(SegmentDef, self).get_obj_dict()
@ -1032,18 +1047,10 @@ class SegmentPortDef(ResourceDef):
return body return body
def _version_dependant_attr_supported(self, attr): @property
if (version.LooseVersion(self.nsx_version) >= def version_dependant_attr_map(self):
version.LooseVersion(nsx_constants.NSX_VERSION_3_0_0)): return {'hyperbus_mode': nsx_constants.NSX_VERSION_3_0_0,
if attr == 'admin_state': 'admin_state': nsx_constants.NSX_VERSION_3_0_0}
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
class SegmentBindingMapDefBase(ResourceDef): class SegmentBindingMapDefBase(ResourceDef):
@ -1602,6 +1609,10 @@ class SecurityPolicyRuleBaseDef(ResourceDef):
rule_def.set_obj_dict(rule_dict) rule_def.set_obj_dict(rule_dict)
return rule_def return rule_def
@property
def version_dependant_attr_map(self):
return {'service_entries': nsx_constants.NSX_VERSION_3_0_0}
class CommunicationMapEntryDef(SecurityPolicyRuleBaseDef): class CommunicationMapEntryDef(SecurityPolicyRuleBaseDef):

View File

@ -14,8 +14,6 @@
# under the License. # under the License.
# #
from distutils import version
from oslo_log import log as logging from oslo_log import log as logging
from vmware_nsxlib.v3 import nsx_constants from vmware_nsxlib.v3 import nsx_constants
from vmware_nsxlib.v3.policy import constants from vmware_nsxlib.v3.policy import constants
@ -388,20 +386,9 @@ class LBServiceDef(ResourceDef):
self._set_attrs_if_supported(body, ['relax_scale_validation']) self._set_attrs_if_supported(body, ['relax_scale_validation'])
return body return body
def _version_dependant_attr_supported(self, attr): @property
if (version.LooseVersion(self.nsx_version) >= def version_dependant_attr_map(self):
version.LooseVersion(nsx_constants.NSX_VERSION_3_0_0)): return {'relax_scale_validation': 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
class LBServiceStatisticsDef(ResourceDef): class LBServiceStatisticsDef(ResourceDef):