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