Escape illegal chars in LS name

The NSX does not except few characters in the LS display name.
Replace those with '.' before creating or updating the LS name

Change-Id: I1fc4bb1530e37f09f61f92713c09ad0307e12d47
This commit is contained in:
Adit Sarfaty 2018-06-14 15:48:35 +03:00
parent 756a33b6cf
commit 2bab55a5f3
3 changed files with 23 additions and 2 deletions

View File

@ -1490,12 +1490,13 @@ class NsxLibSwitchTestCase(BaseTestResource):
core_resources.NsxLibLogicalSwitch)
self._tz_id = uuidutils.generate_uuid()
def _create_body(self, admin_state=nsx_constants.ADMIN_STATE_UP,
def _create_body(self, display_name="fake_name",
admin_state=nsx_constants.ADMIN_STATE_UP,
vlan_id=None, description=None, trunk_vlan=None):
body = {
"transport_zone_id": self._tz_id,
"replication_mode": "MTEP",
"display_name": "fake_name",
"display_name": display_name,
"tags": [],
"admin_state": admin_state
}
@ -1598,6 +1599,17 @@ class NsxLibSwitchTestCase(BaseTestResource):
mocks.FAKE_NAME, self._tz_id, [],
trunk_vlan_range=trunk_vlan)
def test_create_logical_switch_illegal_name(self):
"""Test creating switch with illegal name that will be escaped"""
ls = self.get_mocked_resource()
ls.create(mocks.FAKE_NAME + ';|=,~@', self._tz_id, [])
data = self._create_body(display_name=mocks.FAKE_NAME + '......')
test_client.assert_json_call(
'post', ls,
'https://1.2.3.4/api/v1/logical-switches',
data=jsonutils.dumps(data, sort_keys=True),
headers=self.default_headers())
def test_delete_resource(self):
"""Test deleting switch"""
super(NsxLibSwitchTestCase, self).test_delete_resource(

View File

@ -124,6 +124,8 @@ class NsxLibLogicalSwitch(utils.NsxLibApiBase):
mac_pool_id=None, description=None,
trunk_vlan_range=None):
operation = "Create logical switch"
if display_name:
display_name = utils.escape_display_name(display_name)
# TODO(salv-orlando): Validate Replication mode and admin_state
# NOTE: These checks might be moved to the API client library if one
# that performs such checks in the client is available
@ -189,6 +191,7 @@ class NsxLibLogicalSwitch(utils.NsxLibApiBase):
description=None):
body = {}
if name:
name = utils.escape_display_name(name)
body['display_name'] = name
if admin_state is not None:
if admin_state:

View File

@ -239,6 +239,12 @@ def escape_tag_data(data):
return data.replace('/', '\\/').replace('-', '\\-')
def escape_display_name(display_name):
# Illegal characters for the display names are ;|=,~@
rx = re.compile('([;|=,~@])')
return rx.sub('.', display_name)
class NsxLibCache(object):
def __init__(self, timeout):
self.timeout = timeout