From 764bc0672ea2073877cf743f7e47899ce22af66c Mon Sep 17 00:00:00 2001 From: ranp Date: Tue, 10 Sep 2019 10:57:24 -0700 Subject: [PATCH] Support setting hyperbus_mode in SegmentPort Api Change-Id: I1160db3fe03e01c697443daff998e46d68dbffa5 --- .../tests/unit/v3/policy/test_resources.py | 80 +++++++++++++++++-- vmware_nsxlib/v3/nsx_constants.py | 1 + vmware_nsxlib/v3/policy/__init__.py | 2 + vmware_nsxlib/v3/policy/core_defs.py | 22 +++++ vmware_nsxlib/v3/policy/core_resources.py | 8 +- vmware_nsxlib/v3/policy/lb_defs.py | 14 ++-- 6 files changed, 112 insertions(+), 15 deletions(-) diff --git a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py index 4c5cea97..0e992375 100644 --- a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py @@ -4010,6 +4010,18 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase): super(TestPolicySegmentPort, self).setUp() self.resourceApi = self.policy_lib.segment_port + def test_feature_supported(self): + with mock.patch.object(self.policy_lib, "get_version", + return_value='2.5.0'): + self.assertFalse( + self.policy_lib.feature_supported( + nsx_constants.FEATURE_SWITCH_HYPERBUS_MODE)) + with mock.patch.object(self.policy_lib, "get_version", + return_value='3.0.0'): + self.assertTrue( + self.policy_lib.feature_supported( + nsx_constants.FEATURE_SWITCH_HYPERBUS_MODE)) + def test_create(self): name = 'test' description = 'desc' @@ -4022,18 +4034,67 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase): traffic_tag = 10 allocate_addresses = "BOTH" tags = [{'scope': 'a', 'tag': 'b'}] + hyperbus_mode = 'DISABLE' - with mock.patch.object(self.policy_api, - "create_or_update") as api_call: + with mock.patch.object( + self.policy_api, "create_or_update") as api_call, \ + mock.patch.object(self.resourceApi, 'version', '3.0.0'): + result = self.resourceApi.create_or_overwrite( + name, segment_id, description=description, + address_bindings=address_bindings, + attachment_type=attachment_type, vif_id=vif_id, app_id=app_id, + context_id=context_id, traffic_tag=traffic_tag, + allocate_addresses=allocate_addresses, + hyperbus_mode=hyperbus_mode, + tags=tags, + tenant=TEST_TENANT) + + expected_def = core_defs.SegmentPortDef( + nsx_version='3.0.0', + segment_id=segment_id, + port_id=mock.ANY, + name=name, + description=description, + address_bindings=address_bindings, + attachment_type=attachment_type, + vif_id=vif_id, + app_id=app_id, + context_id=context_id, + traffic_tag=traffic_tag, + allocate_addresses=allocate_addresses, + tags=tags, + tenant=TEST_TENANT, + hyperbus_mode=hyperbus_mode) + + self.assert_called_with_def(api_call, expected_def) + self.assertIsNotNone(result) + + def test_create_with_unsupported_attribute(self): + name = 'test' + description = 'desc' + segment_id = "segment" + address_bindings = [] + attachment_type = "CHILD" + vif_id = "vif" + app_id = "app" + context_id = "context" + traffic_tag = 10 + allocate_addresses = "BOTH" + tags = [{'scope': 'a', 'tag': 'b'}] + hyperbus_mode = 'DISABLE' + + with mock.patch.object( + self.policy_api, "create_or_update") as api_call, \ + mock.patch.object(self.resourceApi, 'version', '0.0.0'): result = self.resourceApi.create_or_overwrite( name, segment_id, description=description, address_bindings=address_bindings, attachment_type=attachment_type, vif_id=vif_id, app_id=app_id, context_id=context_id, traffic_tag=traffic_tag, allocate_addresses=allocate_addresses, tags=tags, - tenant=TEST_TENANT) - + tenant=TEST_TENANT, hyperbus_mode=hyperbus_mode) expected_def = core_defs.SegmentPortDef( + nsx_version=self.policy_lib.get_version(), segment_id=segment_id, port_id=mock.ANY, name=name, @@ -4061,16 +4122,20 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase): traffic_tag = 10 allocate_addresses = "BOTH" tags = [{'scope': 'a', 'tag': 'b'}] - with mock.patch.object(self.policy_api, - "create_or_update") as api_call: + hyperbus_mode = 'DISABLE' + with mock.patch.object( + self.policy_api, "create_or_update") as api_call, \ + mock.patch.object(self.resourceApi, 'version', '3.0.0'): self.resourceApi.attach( segment_id, port_id, attachment_type=attachment_type, vif_id=vif_id, app_id=app_id, context_id=context_id, traffic_tag=traffic_tag, - allocate_addresses=allocate_addresses, tags=tags, + allocate_addresses=allocate_addresses, + hyperbus_mode=hyperbus_mode, tags=tags, tenant=TEST_TENANT) expected_def = core_defs.SegmentPortDef( + nsx_version='3.0.0', segment_id=segment_id, port_id=port_id, attachment_type=attachment_type, @@ -4079,6 +4144,7 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase): context_id=context_id, traffic_tag=traffic_tag, allocate_addresses=allocate_addresses, + hyperbus_mode=hyperbus_mode, tags=tags, tenant=TEST_TENANT) diff --git a/vmware_nsxlib/v3/nsx_constants.py b/vmware_nsxlib/v3/nsx_constants.py index bbfdecaf..51ebda91 100644 --- a/vmware_nsxlib/v3/nsx_constants.py +++ b/vmware_nsxlib/v3/nsx_constants.py @@ -171,6 +171,7 @@ FEATURE_ROUTER_ALLOCATION_PROFILE = 'Router Allocation Profile' FEATURE_ENABLE_STANDBY_RELOCATION = 'Router Enable standby relocation' FEATURE_PARTIAL_UPDATES = 'Partial Update with PATCH' FEATURE_RELAX_SCALE_VALIDATION = 'Relax Scale Validation for LbService' +FEATURE_SWITCH_HYPERBUS_MODE = 'Switch hyperbus mode with policy API' # Features available depending on the Policy Manager backend version FEATURE_NSX_POLICY = 'NSX Policy' diff --git a/vmware_nsxlib/v3/policy/__init__.py b/vmware_nsxlib/v3/policy/__init__.py index 1a0ab3cc..df81668b 100644 --- a/vmware_nsxlib/v3/policy/__init__.py +++ b/vmware_nsxlib/v3/policy/__init__.py @@ -172,6 +172,8 @@ class NsxPolicyLib(lib.NsxLibBase): # features available since 3.0.0 if feature == nsx_constants.FEATURE_PARTIAL_UPDATES: return True + if feature == nsx_constants.FEATURE_SWITCH_HYPERBUS_MODE: + return True return (feature == nsx_constants.FEATURE_NSX_POLICY) diff --git a/vmware_nsxlib/v3/policy/core_defs.py b/vmware_nsxlib/v3/policy/core_defs.py index 419765ad..ac652662 100644 --- a/vmware_nsxlib/v3/policy/core_defs.py +++ b/vmware_nsxlib/v3/policy/core_defs.py @@ -16,12 +16,18 @@ import abc +from distutils import version + +from oslo_log import log as logging import six +from vmware_nsxlib.v3 import nsx_constants from vmware_nsxlib.v3 import utils from vmware_nsxlib.v3.policy import constants +LOG = logging.getLogger(__name__) + TENANTS_PATH_PATTERN = "%s/" DOMAINS_PATH_PATTERN = TENANTS_PATH_PATTERN + "domains/" IP_BLOCKS_PATH_PATTERN = TENANTS_PATH_PATTERN + "ip-blocks/" @@ -838,10 +844,26 @@ class SegmentPortDef(ResourceDef): 'app_id', 'traffic_tag', 'allocate_addresses']) + self._set_attr_if_supported(body, 'hyperbus_mode') body['attachment'] = attachment 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 == 'hyperbus_mode': + 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 SegmentBindingMapDefBase(ResourceDef): diff --git a/vmware_nsxlib/v3/policy/core_resources.py b/vmware_nsxlib/v3/policy/core_resources.py index c1b35661..1496b20c 100644 --- a/vmware_nsxlib/v3/policy/core_resources.py +++ b/vmware_nsxlib/v3/policy/core_resources.py @@ -1991,6 +1991,7 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): context_id=IGNORE, traffic_tag=IGNORE, allocate_addresses=IGNORE, + hyperbus_mode=IGNORE, tags=IGNORE, tenant=constants.POLICY_INFRA_TENANT): @@ -2006,6 +2007,7 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): context_id=context_id, traffic_tag=traffic_tag, allocate_addresses=allocate_addresses, + hyperbus_mode=hyperbus_mode, tags=tags, tenant=tenant) self._create_or_store(port_def) @@ -2034,6 +2036,7 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): name=IGNORE, description=IGNORE, address_bindings=IGNORE, + hyperbus_mode=IGNORE, tags=IGNORE, tenant=constants.POLICY_INFRA_TENANT): @@ -2042,6 +2045,7 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): name=name, description=description, address_bindings=address_bindings, + hyperbus_mode=hyperbus_mode, tags=tags, tenant=tenant) @@ -2063,10 +2067,11 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): app_id=None, context_id=None, traffic_tag=None, + hyperbus_mode=IGNORE, tags=IGNORE, tenant=constants.POLICY_INFRA_TENANT): - port_def = self.entry_def(segment_id=segment_id, + port_def = self._init_def(segment_id=segment_id, port_id=port_id, attachment_type=attachment_type, allocate_addresses=allocate_addresses, @@ -2074,6 +2079,7 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): app_id=app_id, context_id=context_id, traffic_tag=traffic_tag, + hyperbus_mode=hyperbus_mode, tags=tags, tenant=tenant) diff --git a/vmware_nsxlib/v3/policy/lb_defs.py b/vmware_nsxlib/v3/policy/lb_defs.py index 693297b7..32700c93 100644 --- a/vmware_nsxlib/v3/policy/lb_defs.py +++ b/vmware_nsxlib/v3/policy/lb_defs.py @@ -393,13 +393,13 @@ class LBServiceDef(ResourceDef): version.LooseVersion(nsx_constants.NSX_VERSION_2_5_1)): 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_2_5_1) - return False + 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_2_5_1) + return False return False