From 2bfbd59789a2f7f4eba0728658661c1f8f743ae4 Mon Sep 17 00:00:00 2001 From: Gary Kotton Date: Sun, 28 May 2017 23:48:03 -0700 Subject: [PATCH] NSX|V3: treat DHCP server max entries Ensure two things: 1. DHCP server exception will rollback the neutron/nsx port 2. A more appropriate exception is raised Change-Id: I480d53665c8aaaab5458cdac0fdd9d07938f6a96 (cherry picked from commit ea8afbabae9ad6acae96f6e2e74c3fe656686bbf) --- vmware_nsx/plugins/nsx_v3/plugin.py | 9 ++++++++- vmware_nsx/tests/unit/nsx_v3/test_plugin.py | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/vmware_nsx/plugins/nsx_v3/plugin.py b/vmware_nsx/plugins/nsx_v3/plugin.py index b786130251..075afdb8cc 100644 --- a/vmware_nsx/plugins/nsx_v3/plugin.py +++ b/vmware_nsx/plugins/nsx_v3/plugin.py @@ -2070,7 +2070,14 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, # Add Mac/IP binding to native DHCP server and neutron DB. if cfg.CONF.nsx_v3.native_dhcp_metadata: - self._add_dhcp_binding(context, port_data) + try: + self._add_dhcp_binding(context, port_data) + except nsx_lib_exc.ManagerError: + # Rollback create port + self.delete_port(context, port_data['id']) + msg = _('Unable to create port. Please contact admin') + LOG.exception(msg) + raise nsx_exc.NsxPluginException(err_msg=msg) if not cfg.CONF.nsx_v3.native_dhcp_metadata: nsx_rpc.handle_port_metadata_access(self, context, neutron_db) diff --git a/vmware_nsx/tests/unit/nsx_v3/test_plugin.py b/vmware_nsx/tests/unit/nsx_v3/test_plugin.py index 567e2169ab..10715db63f 100644 --- a/vmware_nsx/tests/unit/nsx_v3/test_plugin.py +++ b/vmware_nsx/tests/unit/nsx_v3/test_plugin.py @@ -49,6 +49,7 @@ from vmware_nsx.tests import unit as vmware from vmware_nsx.tests.unit.extensions import test_metadata from vmware_nsxlib.tests.unit.v3 import mocks as nsx_v3_mocks from vmware_nsxlib.tests.unit.v3 import nsxlib_testcase +from vmware_nsxlib.v3 import exceptions as nsxlib_exc PLUGIN_NAME = 'vmware_nsx.plugin.NsxV3Plugin' @@ -460,6 +461,16 @@ class TestPortsV2(test_plugin.TestPortsV2, NsxV3PluginTestCaseMixin, self._get_ports_with_fields(tenid, 'mac_address', 4) self._get_ports_with_fields(tenid, 'network_id', 4) + def test_port_failure_rollback_dhcp_exception(self): + cfg.CONF.set_override('native_dhcp_metadata', True, 'nsx_v3') + self.plugin = directory.get_plugin() + with mock.patch.object(self.plugin, '_add_dhcp_binding', + side_effect=nsxlib_exc.ManagerError): + self.port() + ctx = context.get_admin_context() + networks = self.plugin.get_ports(ctx) + self.assertListEqual([], networks) + class DHCPOptsTestCase(test_dhcpopts.TestExtraDhcpOpt, NsxV3PluginTestCaseMixin):