From 7f4d0ba86ca17a478c89ca4d9d10f467301e583c Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Thu, 21 Dec 2017 12:17:11 +0200 Subject: [PATCH] TVD: Add default plugin configuration New configuration parameter under [nsx_tvd] named default_plugin. It can be one of 'nsx-t', 'nsx-v', 'dvs, with the default of default 'nsx-t'. When an unknown/new project uses one of the core plugin methods, without a project-plugin mapping in the DB, it will be automatically mapped to the default plugin. Change-Id: I850a4cc00ee11a9c5e0d0aac385a8bae79018958 --- vmware_nsx/common/config.py | 6 ++++++ vmware_nsx/plugins/nsx/plugin.py | 18 ++++++++++-------- vmware_nsx/tests/unit/nsx_tvd/test_plugin.py | 18 +++++++++++++++--- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/vmware_nsx/common/config.py b/vmware_nsx/common/config.py index 108ceece29..c14b2df57a 100644 --- a/vmware_nsx/common/config.py +++ b/vmware_nsx/common/config.py @@ -21,6 +21,7 @@ from neutron.conf.db import l3_hamode_db from vmware_nsx._i18n import _ from vmware_nsx.common import exceptions as nsx_exc from vmware_nsx.dvs import dvs_utils +from vmware_nsx.extensions import projectpluginmap from vmware_nsx.extensions import routersize LOG = logging.getLogger(__name__) @@ -829,6 +830,11 @@ nsx_tvd_opts = [ help=_("An ordered list of DVS extension driver " "entrypoints to be loaded from the " "vmware_nsx.extension_drivers namespace.")), + cfg.StrOpt('default_plugin', + default=projectpluginmap.NsxPlugins.NSX_T, + choices=projectpluginmap.VALID_TYPES, + help=_("The default plugin that will be used for new projects " + "that were not added to the projects plugin mapping.")), ] # Register the configuration options diff --git a/vmware_nsx/plugins/nsx/plugin.py b/vmware_nsx/plugins/nsx/plugin.py index 521f653cbc..c2024ab6b1 100644 --- a/vmware_nsx/plugins/nsx/plugin.py +++ b/vmware_nsx/plugins/nsx/plugin.py @@ -18,6 +18,7 @@ from neutron_lib.api.definitions import port as port_def from neutron_lib.api.definitions import subnet as subnet_def from neutron_lib import context as n_context from neutron_lib.plugins import directory +from oslo_config import cfg from oslo_log import log as logging from neutron.db import _resource_extend as resource_extend @@ -41,7 +42,7 @@ from neutron_lib.api import validators from neutron_lib import exceptions as n_exc from vmware_nsx.common import availability_zones as nsx_com_az -from vmware_nsx.common import config # noqa +from vmware_nsx.common import config from vmware_nsx.common import exceptions as nsx_exc from vmware_nsx.common import managers as nsx_managers from vmware_nsx.db import ( @@ -116,6 +117,7 @@ class NsxTVDPlugin(addr_pair_db.AllowedAddressPairsMixin, def init_plugins(self): # initialize all supported plugins self.plugins = {} + try: self.plugins[projectpluginmap.NsxPlugins.NSX_T] = t.NsxV3Plugin() except Exception as e: @@ -141,13 +143,13 @@ class NsxTVDPlugin(addr_pair_db.AllowedAddressPairsMixin, msg = _("No active plugins were found") raise nsx_exc.NsxPluginException(err_msg=msg) - # update the default plugin for new projects as the NSX-T - # TODO(asarfaty): make the default configurable? - if projectpluginmap.NsxPlugins.NSX_T in self.plugins: - self.default_plugin = projectpluginmap.NsxPlugins.NSX_T - else: - # If the NSX-T is not supported, use another one - self.default_plugin = self.plugins.keys()[0] + # update the default plugin for new projects + self.default_plugin = cfg.CONF.nsx_tvd.default_plugin + if self.default_plugin not in self.plugins: + msg = (_("The default plugin %s failed to start") % + self.default_plugin) + raise nsx_exc.NsxPluginException(err_msg=msg) + LOG.info("NSX-TVD plugin will use %s as the default plugin", self.default_plugin) diff --git a/vmware_nsx/tests/unit/nsx_tvd/test_plugin.py b/vmware_nsx/tests/unit/nsx_tvd/test_plugin.py index 2d1d22c9c7..5995a35b84 100644 --- a/vmware_nsx/tests/unit/nsx_tvd/test_plugin.py +++ b/vmware_nsx/tests/unit/nsx_tvd/test_plugin.py @@ -15,6 +15,7 @@ import mock +from oslo_config import cfg from oslo_utils import uuidutils from neutron_lib import context @@ -37,6 +38,12 @@ class NsxTVDPluginTestCase(v_tests.NsxVPluginV2TestCase, plugin=PLUGIN_NAME, ext_mgr=None, service_plugins=None): + + # set the default plugin + if self.plugin_type: + cfg.CONF.set_override('default_plugin', self.plugin_type, + group="nsx_tvd") + super(NsxTVDPluginTestCase, self).setUp( plugin=plugin, ext_mgr=ext_mgr) @@ -65,13 +72,14 @@ class NsxTVDPluginTestCase(v_tests.NsxVPluginV2TestCase, self.assertTrue(self.core_plugin.is_tvd_plugin()) self.assertIsNotNone(self.sub_plugin) - def _test_call_create(self, obj_name, calls_count=1): + def _test_call_create(self, obj_name, calls_count=1, project_id=None): method_name = 'create_%s' % obj_name func_to_call = getattr(self.core_plugin, method_name) - + if not project_id: + project_id = self.project_id with mock.patch.object(self.sub_plugin, method_name) as sub_func: func_to_call(self.context, - {obj_name: {'tenant_id': self.project_id}}) + {obj_name: {'tenant_id': project_id}}) self.assertEqual(calls_count, sub_func.call_count) def _test_call_create_with_net_id(self, obj_name, field_name='network_id', @@ -326,6 +334,10 @@ class TestPluginWithDefaultPlugin(NsxTVDPluginTestCase): self.core_plugin.disassociate_floatingips(self.context, port_id) sub_func.assert_called_once() + def test_new_user(self): + project_id = _uuid() + self._test_call_create('network', project_id=project_id) + class TestPluginWithNsxv(TestPluginWithDefaultPlugin): """Test TVD plugin with the NSX-V sub plugin"""