Merge "Bulk creation of SecurityGroups"

This commit is contained in:
Jenkins 2016-12-25 05:48:16 +00:00 committed by Gerrit Code Review
commit 7b3d7ba98e
4 changed files with 48 additions and 6 deletions

View File

@ -46,9 +46,9 @@ class SecurityGroupDbMixin(ext_sg.SecurityGroupPluginBase):
__native_bulk_support = True
def create_security_group_bulk(self, context, security_group_rule):
def create_security_group_bulk(self, context, security_groups):
return self._create_bulk('security_group', context,
security_group_rule)
security_groups)
def _registry_notify(self, res, event, id=None, exc_cls=None, **kwargs):
# NOTE(armax): a callback exception here will prevent the request

View File

@ -53,3 +53,17 @@ class SecGroupTest(base.BaseSecGroupTest):
self.assertEqual(show_body['security_group']['name'], new_name)
self.assertEqual(show_body['security_group']['description'],
new_description)
@test.idempotent_id('7c0ecb10-b2db-11e6-9b14-000c29248b0d')
def test_create_bulk_sec_groups(self):
# Creates 2 sec-groups in one request
sec_nm = [data_utils.rand_name('secgroup'),
data_utils.rand_name('secgroup')]
body = self.client.create_bulk_security_groups(sec_nm)
created_sec_grps = body['security_groups']
self.assertEqual(2, len(created_sec_grps))
for secgrp in created_sec_grps:
self.addCleanup(self.client.delete_security_group,
secgrp['id'])
self.assertIn(secgrp['name'], sec_nm)
self.assertIsNotNone(secgrp['id'])

View File

@ -251,6 +251,18 @@ class NetworkClientJSON(service_client.RestClient):
self.expected_success(201, resp.status)
return service_client.ResponseBody(resp, body)
def create_bulk_security_groups(self, security_group_list):
group_list = [{'security_group': {'name': name}}
for name in security_group_list]
post_data = {'security_groups': group_list}
body = self.serialize_list(post_data, 'security_groups',
'security_group')
uri = self.get_uri("security-groups")
resp, body = self.post(uri, body)
body = {'security_groups': self.deserialize_list(body)}
self.expected_success(201, resp.status)
return service_client.ResponseBody(resp, body)
def wait_for_resource_deletion(self, resource_type, id):
"""Waits for a resource to be deleted."""
start_time = int(time.time())

View File

@ -69,14 +69,16 @@ class SecurityGroupTestExtensionManager(object):
class SecurityGroupsTestCase(test_db_base_plugin_v2.NeutronDbPluginV2TestCase):
def _create_security_group(self, fmt, name, description, **kwargs):
def _build_security_group(self, name, description, **kwargs):
data = {
'security_group': {
'name': name,
'tenant_id': kwargs.get('tenant_id',
test_db_base_plugin_v2.TEST_TENANT_ID),
'tenant_id': kwargs.get(
'tenant_id', test_db_base_plugin_v2.TEST_TENANT_ID),
'description': description}}
return data
def _create_security_group_response(self, fmt, data, **kwargs):
security_group_req = self.new_create_request('security-groups', data,
fmt)
if (kwargs.get('set_context') and 'tenant_id' in kwargs):
@ -85,6 +87,10 @@ class SecurityGroupsTestCase(test_db_base_plugin_v2.NeutronDbPluginV2TestCase):
context.Context('', kwargs['tenant_id']))
return security_group_req.get_response(self.ext_api)
def _create_security_group(self, fmt, name, description, **kwargs):
data = self._build_security_group(name, description, **kwargs)
return self._create_security_group_response(fmt, data, **kwargs)
def _build_security_group_rule(
self, security_group_id, direction, proto,
port_range_min=None, port_range_max=None,
@ -284,6 +290,16 @@ class TestSecurityGroups(SecurityGroupDBTestCase):
'port_range_min': None}
self._assert_sg_rule_has_kvs(v6_rule, expected)
def test_create_security_group_bulk(self):
rule1 = self._build_security_group("sg_1", "sec_grp_1")
rule2 = self._build_security_group("sg_2", "sec_grp_2")
rules = {'security_groups': [rule1['security_group'],
rule2['security_group']]}
res = self._create_security_group_response(self.fmt, rules)
ret = self.deserialize(self.fmt, res)
self.assertEqual(webob.exc.HTTPCreated.code, res.status_int)
self.assertEqual(2, len(ret['security_groups']))
def test_skip_duplicate_default_sg_error(self):
num_called = [0]
original_func = self.plugin.create_security_group