Return new secgroup object
The create method for a security group had a bug in that it was not returning the newly created security group. This also moves the logic for normalizing the nova data into the _utils module since it is reused. Change-Id: I19a3063769f1fc8c7bfd1d57aae0e5b839b8189c
This commit is contained in:
@@ -908,22 +908,7 @@ class OpenStackCloud(object):
|
||||
raise OpenStackCloudException(
|
||||
"Error fetching security group list"
|
||||
)
|
||||
# Make Nova data look like Neutron data. This doesn't make them
|
||||
# look exactly the same, but pretty close.
|
||||
return [{'id': g['id'],
|
||||
'name': g['name'],
|
||||
'description': g['description'],
|
||||
'security_group_rules': [{
|
||||
'id': r['id'],
|
||||
'direction': 'ingress',
|
||||
'ethertype': 'IPv4',
|
||||
'port_range_min': r['from_port'],
|
||||
'port_range_max': r['to_port'],
|
||||
'protocol': r['ip_protocol'],
|
||||
'remote_ip_prefix': r['ip_range'].get('cidr', None),
|
||||
'security_group_id': r['parent_group_id'],
|
||||
} for r in g['rules']]
|
||||
} for g in groups]
|
||||
return _utils.normalize_nova_secgroups(groups)
|
||||
|
||||
# Security groups not supported
|
||||
else:
|
||||
@@ -2363,13 +2348,15 @@ class OpenStackCloud(object):
|
||||
:param string name: A name for the security group.
|
||||
:param string description: Describes the security group.
|
||||
|
||||
:returns: A dict representing the new security group.
|
||||
|
||||
:raises: OpenStackCloudException on operation error.
|
||||
:raises: OpenStackCloudUnavailableFeature if security groups are
|
||||
not supported on this cloud.
|
||||
"""
|
||||
if self.secgroup_source == 'neutron':
|
||||
try:
|
||||
self.manager.submitTask(
|
||||
group = self.manager.submitTask(
|
||||
_tasks.NeutronSecurityGroupCreate(
|
||||
body=dict(security_group=dict(name=name,
|
||||
description=description))
|
||||
@@ -2382,12 +2369,15 @@ class OpenStackCloud(object):
|
||||
raise OpenStackCloudException(
|
||||
"failed to create security group '{name}': {msg}".format(
|
||||
name=name, msg=str(e)))
|
||||
return group['security_group']
|
||||
|
||||
elif self.secgroup_source == 'nova':
|
||||
try:
|
||||
self.manager.submitTask(
|
||||
_tasks.NovaSecurityGroupCreate(
|
||||
name=name, description=description
|
||||
group = meta.obj_to_dict(
|
||||
self.manager.submitTask(
|
||||
_tasks.NovaSecurityGroupCreate(
|
||||
name=name, description=description
|
||||
)
|
||||
)
|
||||
)
|
||||
except Exception as e:
|
||||
@@ -2397,6 +2387,7 @@ class OpenStackCloud(object):
|
||||
raise OpenStackCloudException(
|
||||
"failed to create security group '{name}': {msg}".format(
|
||||
name=name, msg=str(e)))
|
||||
return _utils.normalize_nova_secgroups([group])[0]
|
||||
|
||||
# Security groups not supported
|
||||
else:
|
||||
|
||||
@@ -112,3 +112,30 @@ def _get_entity(func, name_or_id, filters):
|
||||
raise exc.OpenStackCloudException(
|
||||
"Multiple matches found for %s" % name_or_id)
|
||||
return entities[0]
|
||||
|
||||
|
||||
def normalize_nova_secgroups(groups):
|
||||
"""Normalize the structure of nova security groups
|
||||
|
||||
This makes security group dicts, as returned from nova, look like the
|
||||
security group dicts as returned from neutron. This does not make them
|
||||
look exactly the same, but it's pretty close.
|
||||
|
||||
:param list groups: A list of security group dicts.
|
||||
|
||||
:returns: A list of normalized dicts.
|
||||
"""
|
||||
return [{'id': g['id'],
|
||||
'name': g['name'],
|
||||
'description': g['description'],
|
||||
'security_group_rules': [{
|
||||
'id': r['id'],
|
||||
'direction': 'ingress',
|
||||
'ethertype': 'IPv4',
|
||||
'port_range_min': r['from_port'],
|
||||
'port_range_max': r['to_port'],
|
||||
'protocol': r['ip_protocol'],
|
||||
'remote_ip_prefix': r['ip_range'].get('cidr', None),
|
||||
'security_group_id': r['parent_group_id'],
|
||||
} for r in g['rules']]
|
||||
} for g in groups]
|
||||
|
||||
@@ -139,13 +139,21 @@ class TestSecurityGroups(base.TestCase):
|
||||
|
||||
@mock.patch.object(shade.OpenStackCloud, 'nova_client')
|
||||
def test_create_security_group_nova(self, mock_nova):
|
||||
self.cloud.secgroup_source = 'nova'
|
||||
group_name = self.getUniqueString()
|
||||
group_desc = 'security group from test_create_security_group_neutron'
|
||||
self.cloud.create_security_group(group_name, group_desc)
|
||||
new_group = fakes.FakeSecgroup(id='2',
|
||||
name=group_name,
|
||||
description=group_desc,
|
||||
rules=[])
|
||||
|
||||
mock_nova.security_groups.create.return_value = new_group
|
||||
self.cloud.secgroup_source = 'nova'
|
||||
r = self.cloud.create_security_group(group_name, group_desc)
|
||||
mock_nova.security_groups.create.assert_called_once_with(
|
||||
name=group_name, description=group_desc
|
||||
)
|
||||
self.assertEqual(group_name, r['name'])
|
||||
self.assertEqual(group_desc, r['description'])
|
||||
|
||||
@mock.patch.object(shade.OpenStackCloud, 'neutron_client')
|
||||
@mock.patch.object(shade.OpenStackCloud, 'nova_client')
|
||||
|
||||
Reference in New Issue
Block a user