diff --git a/vmware_nsxlib/tests/unit/v3/test_resources.py b/vmware_nsxlib/tests/unit/v3/test_resources.py index 7b6b8d5d..bfe1ca0a 100644 --- a/vmware_nsxlib/tests/unit/v3/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/test_resources.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( diff --git a/vmware_nsxlib/v3/core_resources.py b/vmware_nsxlib/v3/core_resources.py index af96f1c4..7b42d626 100644 --- a/vmware_nsxlib/v3/core_resources.py +++ b/vmware_nsxlib/v3/core_resources.py @@ -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: diff --git a/vmware_nsxlib/v3/utils.py b/vmware_nsxlib/v3/utils.py index 9674d63a..59e0450e 100644 --- a/vmware_nsxlib/v3/utils.py +++ b/vmware_nsxlib/v3/utils.py @@ -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