From 5cbd2831fbcddccce0f66d92cd9af6ffb885419b Mon Sep 17 00:00:00 2001 From: Sidharth Surana Date: Mon, 15 Aug 2016 12:38:38 -0700 Subject: [PATCH] Make it possible for DvsManager to manage its own DVS For the nsxv plugin when using the dvs features, we should not repeat the dvs name information unless required, as the dvs moref may be set as part of the nsxv configuration. And if the dvs moref is set in the nsxv configuration then, we should use that to initialize DvsManager. Change-Id: Ibd81190aa91f255237c76f93e0fc15ab2659d6c7 --- vmware_nsx/common/config.py | 3 ++- vmware_nsx/dvs/dvs.py | 14 +++++++++----- vmware_nsx/dvs/dvs_utils.py | 4 ++-- vmware_nsx/plugins/nsx_v/plugin.py | 2 +- vmware_nsx/tests/unit/dvs/test_plugin.py | 6 +++--- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/vmware_nsx/common/config.py b/vmware_nsx/common/config.py index 03098f8eb0..ba2af8ab1d 100644 --- a/vmware_nsx/common/config.py +++ b/vmware_nsx/common/config.py @@ -635,7 +635,8 @@ def validate_nsxv_config_options(): LOG.warning(_LW("dvs_id must be configured to support VLANs!")) if cfg.CONF.nsxv.vdn_scope_id is None: LOG.warning(_LW("vdn_scope_id must be configured to support VXLANs!")) - if cfg.CONF.nsxv.use_dvs_features and not dvs_utils.dvs_is_enabled(): + if cfg.CONF.nsxv.use_dvs_features and not dvs_utils.dvs_is_enabled( + dvs_id=cfg.CONF.nsxv.dvs_id): error = _("dvs host/vcenter credentials must be defined to use " "dvs features") raise nsx_exc.NsxPluginException(err_msg=error) diff --git a/vmware_nsx/dvs/dvs.py b/vmware_nsx/dvs/dvs.py index d1bdafd9bc..35f29cc047 100644 --- a/vmware_nsx/dvs/dvs.py +++ b/vmware_nsx/dvs/dvs.py @@ -31,7 +31,7 @@ API_FIND_ALL_BY_UUID = 'FindAllByUuid' class DvsManager(object): """Management class for dvs related tasks.""" - def __init__(self): + def __init__(self, dvs_id=None): """Initializer. A global session with the VC will be established. In addition to this @@ -42,8 +42,12 @@ class DvsManager(object): """ self._session = dvs_utils.dvs_create_session() # In the future we may decide to support more than one DVS - self._dvs_moref = self._get_dvs_moref(self._session, - dvs_utils.dvs_name_get()) + if dvs_id is None: + self._dvs_moref = self._get_dvs_moref(self._session, + dvs_utils.dvs_name_get()) + else: + self._dvs_moref = vim_util.get_moref(dvs_id, + 'VmwareDistributedVirtualSwitch') def _get_dvs_moref(self, session, dvs_name): """Get the moref of the configured DVS.""" @@ -97,7 +101,7 @@ class DvsManager(object): LOG.info(_LI("%(net_id)s with tag %(vlan_tag)s created on %(dvs)s."), {'net_id': net_id, 'vlan_tag': vlan_tag, - 'dvs': dvs_utils.dvs_name_get()}) + 'dvs': self._dvs_moref.value}) def _net_id_to_moref(self, net_id): """Gets the moref for the specific neutron network.""" @@ -302,7 +306,7 @@ class DvsManager(object): net_id) LOG.info(_LI("%(net_id)s delete from %(dvs)s."), {'net_id': net_id, - 'dvs': dvs_utils.dvs_name_get()}) + 'dvs': self._dvs_moref.value}) def get_vm_moref(self, instance_uuid): """Get reference to the VM. diff --git a/vmware_nsx/dvs/dvs_utils.py b/vmware_nsx/dvs/dvs_utils.py index 3546a2c169..67a5af146c 100644 --- a/vmware_nsx/dvs/dvs_utils.py +++ b/vmware_nsx/dvs/dvs_utils.py @@ -57,10 +57,10 @@ CONF = cfg.CONF CONF.register_opts(dvs_opts, 'dvs') -def dvs_is_enabled(): +def dvs_is_enabled(dvs_id=None): """Returns the configured DVS status.""" return bool(CONF.dvs.host_ip and CONF.dvs.host_username and - CONF.dvs.host_password and CONF.dvs.dvs_name) + CONF.dvs.host_password and (dvs_id or CONF.dvs.dvs_name)) def dvs_create_session(): diff --git a/vmware_nsx/plugins/nsx_v/plugin.py b/vmware_nsx/plugins/nsx_v/plugin.py index fbb170e6dc..cab2b5b4c9 100644 --- a/vmware_nsx/plugins/nsx_v/plugin.py +++ b/vmware_nsx/plugins/nsx_v/plugin.py @@ -211,7 +211,7 @@ class NsxVPluginV2(addr_pair_db.AllowedAddressPairsMixin, self._router_managers = managers.RouterTypeManager(self) if cfg.CONF.nsxv.use_dvs_features: - self._dvs = dvs.DvsManager() + self._dvs = dvs.DvsManager(dvs_id=self.dvs_id) else: self._dvs = None diff --git a/vmware_nsx/tests/unit/dvs/test_plugin.py b/vmware_nsx/tests/unit/dvs/test_plugin.py index 9853992a98..9705df8a9c 100644 --- a/vmware_nsx/tests/unit/dvs/test_plugin.py +++ b/vmware_nsx/tests/unit/dvs/test_plugin.py @@ -50,12 +50,12 @@ class DvsTestCase(base.BaseTestCase): @mock.patch.object(dvs_utils, 'dvs_create_session', return_value=fake_session()) @mock.patch.object(dvs.DvsManager, '_get_dvs_moref', - return_value='dvs-moref') + return_value=mock.MagicMock()) def setUp(self, mock_moref, mock_session): super(DvsTestCase, self).setUp() cfg.CONF.set_override('dvs_name', 'fake_dvs', group='dvs') self._dvs = dvs.DvsManager() - self.assertEqual('dvs-moref', self._dvs._dvs_moref) + self.assertEqual(mock_moref.return_value, self._dvs._dvs_moref) mock_moref.assert_called_once_with(mock_session.return_value, 'fake_dvs') @@ -141,7 +141,7 @@ class NeutronSimpleDvsTest(test_plugin.NeutronDbPluginV2TestCase): @mock.patch.object(dvs_utils, 'dvs_create_session', return_value=fake_session()) @mock.patch.object(dvs.DvsManager, '_get_dvs_moref', - return_value='dvs-moref') + return_value=mock.MagicMock()) def setUp(self, mock_moref, mock_session, plugin=PLUGIN_NAME, ext_mgr=None,