Merge "Escape illegal chars in LS name"

This commit is contained in:
Zuul 2018-06-24 08:56:36 +00:00 committed by Gerrit Code Review
commit 85be41b3b5
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