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
This commit is contained in:
Ran Gu 2020-04-30 17:03:37 +08:00
parent f4e0b18e2b
commit 7ed3fd00f5
4 changed files with 61 additions and 4 deletions

View File

@ -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"

View File

@ -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"

View File

@ -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):

View File

@ -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)