From 8b98d869f9cbd6cb9e1fc83d9421a8fe54c411fe Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Thu, 4 Jan 2018 09:54:29 +0200 Subject: [PATCH] TVD: create subnet bulk support Change-Id: I7870f414b9ece4ccd51ea627ddc53a11a7504bcf --- vmware_nsx/plugins/nsx/plugin.py | 7 +++ vmware_nsx/tests/unit/nsx_tvd/test_plugin.py | 50 ++++++++++++++++---- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/vmware_nsx/plugins/nsx/plugin.py b/vmware_nsx/plugins/nsx/plugin.py index 10c248e87f..c9c9fa2829 100644 --- a/vmware_nsx/plugins/nsx/plugin.py +++ b/vmware_nsx/plugins/nsx/plugin.py @@ -421,6 +421,13 @@ class NsxTVDPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, p = self._get_plugin_from_net_id(context, id) return p.create_subnet(context, subnet) + def create_subnet_bulk(self, context, subnets): + # look at the first subnet to find out the project & plugin + items = subnets['subnets'] + id = items[0]['subnet']['network_id'] + p = self._get_plugin_from_net_id(context, id) + return p.create_subnet_bulk(context, subnets) + def update_subnet(self, context, id, subnet): db_subnet = self._get_subnet(context, id) p = self._get_plugin_from_net_id(context, db_subnet['network_id']) diff --git a/vmware_nsx/tests/unit/nsx_tvd/test_plugin.py b/vmware_nsx/tests/unit/nsx_tvd/test_plugin.py index 5995a35b84..5d4ce4df8a 100644 --- a/vmware_nsx/tests/unit/nsx_tvd/test_plugin.py +++ b/vmware_nsx/tests/unit/nsx_tvd/test_plugin.py @@ -72,28 +72,46 @@ 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, project_id=None): - method_name = 'create_%s' % obj_name + def _test_call_create(self, obj_name, calls_count=1, project_id=None, + is_bulk=False): + method_name = single_name = 'create_%s' % obj_name + if is_bulk: + method_name = method_name + '_bulk' 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': project_id}}) - self.assertEqual(calls_count, sub_func.call_count) + with mock.patch.object(self.sub_plugin, method_name) as sub_func,\ + mock.patch.object(self.sub_plugin, single_name) as single_func: + if is_bulk: + func_to_call(self.context, + {obj_name + 's': [{obj_name: + {'tenant_id': project_id}}]}) + else: + func_to_call(self.context, + {obj_name: {'tenant_id': project_id}}) + self.assertEqual(calls_count, + sub_func.call_count or single_func.call_count) def _test_call_create_with_net_id(self, obj_name, field_name='network_id', - calls_count=1): + calls_count=1, is_bulk=False): method_name = 'create_%s' % obj_name + if is_bulk: + method_name = method_name + '_bulk' func_to_call = getattr(self.core_plugin, method_name) net_id = _uuid() with mock.patch.object(self.sub_plugin, method_name) as sub_func,\ mock.patch.object(self.core_plugin, '_get_network', return_value={'tenant_id': self.project_id}): - func_to_call(self.context, - {obj_name: {'tenant_id': self.project_id, - field_name: net_id}}) + if is_bulk: + func_to_call(self.context, + {obj_name + 's': [{obj_name: + {'tenant_id': self.project_id, + field_name: net_id}}]}) + else: + func_to_call(self.context, + {obj_name: {'tenant_id': self.project_id, + field_name: net_id}}) self.assertEqual(calls_count, sub_func.call_count) def _test_call_delete(self, obj_name): @@ -207,6 +225,18 @@ class TestPluginWithDefaultPlugin(NsxTVDPluginTestCase): # plugin will be called twice because of the default sg self._test_call_create('security_group', calls_count=2) + def test_create_security_group_rule(self): + self._test_call_create('security_group_rule') + + def test_create_network_bulk(self): + self._test_call_create('network', is_bulk=True) + + def test_create_subnet_bulk(self): + self._test_call_create_with_net_id('subnet', is_bulk=True) + + def test_create_security_group_rule_bulk(self): + self._test_call_create('security_group_rule', is_bulk=True) + def test_delete_network(self): self._test_call_delete('network')