Merge "Escape illegal chars in LS name"

changes/49/579849/1
Zuul 4 years ago committed by Gerrit Code Review
commit 85be41b3b5
  1. 16
      vmware_nsxlib/tests/unit/v3/test_resources.py
  2. 3
      vmware_nsxlib/v3/core_resources.py
  3. 6
      vmware_nsxlib/v3/utils.py

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

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

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

Loading…
Cancel
Save