From 7ed3fd00f5aab8b23c74801806aab888576480a2 Mon Sep 17 00:00:00 2001 From: Ran Gu Date: Thu, 30 Apr 2020 17:03:37 +0800 Subject: [PATCH] Add init_state for SegmentPortApi Set initial state when a new logical port is created. 'UNBLOCKED_VLAN' means new port will be unblocked on traffic in creation, also VLAN will be set with corresponding logical switch setting. This port setting can only be configured at port creation, and cannot be modified. VIF_RESTORE indicates proton to fetch and restore VIF attachment from ESX host Change-Id: I84c62ec15934fa2cb0f1f8dc8788ea12f43a403d --- .../tests/unit/v3/policy/test_resources.py | 43 +++++++++++++++++-- vmware_nsxlib/v3/nsx_constants.py | 4 ++ vmware_nsxlib/v3/policy/core_defs.py | 16 ++++++- vmware_nsxlib/v3/policy/core_resources.py | 2 + 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py index 69f3a62b..e5c60a9e 100644 --- a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py @@ -4629,6 +4629,7 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase): tags = [{'scope': 'a', 'tag': 'b'}] hyperbus_mode = 'DISABLE' admin_state = True + init_state = 'VIF_RESTORE' with mock.patch.object( self.policy_api, "create_or_update") as api_call, \ @@ -4642,7 +4643,8 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase): allocate_addresses=allocate_addresses, hyperbus_mode=hyperbus_mode, admin_state=admin_state, tags=tags, - tenant=TEST_TENANT) + tenant=TEST_TENANT, + init_state=init_state) expected_def = core_defs.SegmentPortDef( nsx_version=nsxlib_testcase.LATEST_VERSION, @@ -4660,7 +4662,8 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase): admin_state=admin_state, tags=tags, tenant=TEST_TENANT, - hyperbus_mode=hyperbus_mode) + hyperbus_mode=hyperbus_mode, + init_state=init_state) self.assert_called_with_def(api_call, expected_def) self.assertIsNotNone(result) @@ -4678,6 +4681,7 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase): allocate_addresses = "BOTH" tags = [{'scope': 'a', 'tag': 'b'}] hyperbus_mode = 'DISABLE' + init_state = 'VIF_RESTORE' with mock.patch.object( self.policy_api, "create_or_update") as api_call, \ @@ -4688,7 +4692,8 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase): 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) + tenant=TEST_TENANT, hyperbus_mode=hyperbus_mode, + init_state=init_state) expected_def = core_defs.SegmentPortDef( nsx_version=self.policy_lib.get_version(), segment_id=segment_id, @@ -4708,6 +4713,38 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase): self.assert_called_with_def(api_call, expected_def) self.assertIsNotNone(result) + def test_create_with_invalid_init_state(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' + init_state = 'OK' + + with mock.patch.object( + self.policy_api, "create_or_update") as api_call, \ + mock.patch.object(self.resourceApi, 'version', + nsxlib_testcase.LATEST_VERSION): + with self.assertRaises(nsxlib_exc.InvalidInput): + 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, hyperbus_mode=hyperbus_mode, + init_state=init_state) + actual_def = api_call.call_args_list[0][0][0] + actual_def.get_obj_dict() + def test_attach(self): segment_id = "segment" port_id = "port" diff --git a/vmware_nsxlib/v3/nsx_constants.py b/vmware_nsxlib/v3/nsx_constants.py index 83e0ca4a..cf45113d 100644 --- a/vmware_nsxlib/v3/nsx_constants.py +++ b/vmware_nsxlib/v3/nsx_constants.py @@ -33,6 +33,10 @@ VIF_TYPE_CHILD = "CHILD" ALLOCATE_ADDRESS_NONE = "None" +# SegmentPort init_state types +INIT_STATE_UNBLOCKED_VLAN = 'UNBLOCKED_VLAN' +INIT_STATE_VIF_RESTORE = 'VIF_RESTORE' + # NSXv3 L2 Gateway constants BRIDGE_ENDPOINT = "BRIDGEENDPOINT" FAILOVER_MODE_PREEMPTIVE = "PREEMPTIVE" diff --git a/vmware_nsxlib/v3/policy/core_defs.py b/vmware_nsxlib/v3/policy/core_defs.py index 96ba0a33..75866813 100644 --- a/vmware_nsxlib/v3/policy/core_defs.py +++ b/vmware_nsxlib/v3/policy/core_defs.py @@ -20,6 +20,7 @@ from distutils import version from oslo_log import log as logging import six +from vmware_nsxlib.v3 import exceptions from vmware_nsxlib.v3 import nsx_constants from vmware_nsxlib.v3.policy import constants from vmware_nsxlib.v3 import utils @@ -1151,12 +1152,25 @@ class SegmentPortDef(ResourceDef): self._set_attr_if_specified(body, 'admin_state', value=admin_state) + if (self.has_attr('init_state') and + self._version_dependant_attr_supported('init_state')): + valid_list = [nsx_constants.INIT_STATE_UNBLOCKED_VLAN, + nsx_constants.INIT_STATE_VIF_RESTORE] + init_state = self.get_attr('init_state') + if init_state not in valid_list: + raise exceptions.InvalidInput( + operation='create_segment_port', + arg_val=init_state, + arg_name='init_state') + self._set_attr_if_specified(body, 'init_state') + return body @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} + 'admin_state': nsx_constants.NSX_VERSION_3_0_0, + 'init_state': nsx_constants.NSX_VERSION_3_1_0} class SegmentBindingMapDefBase(ResourceDef): diff --git a/vmware_nsxlib/v3/policy/core_resources.py b/vmware_nsxlib/v3/policy/core_resources.py index 38cba13d..af2b0bd8 100644 --- a/vmware_nsxlib/v3/policy/core_resources.py +++ b/vmware_nsxlib/v3/policy/core_resources.py @@ -2143,6 +2143,7 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): allocate_addresses=IGNORE, hyperbus_mode=IGNORE, admin_state=IGNORE, + init_state=IGNORE, tags=IGNORE, tenant=constants.POLICY_INFRA_TENANT): @@ -2160,6 +2161,7 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): allocate_addresses=allocate_addresses, hyperbus_mode=hyperbus_mode, admin_state=admin_state, + init_state=init_state, tags=tags, tenant=tenant) self._create_or_store(port_def)