Merge "Add secgroup update API"

This commit is contained in:
Jenkins 2015-06-09 17:21:38 +00:00 committed by Gerrit Code Review
commit 56cf58d201
3 changed files with 103 additions and 0 deletions

View File

@ -2464,6 +2464,63 @@ class OpenStackCloud(object):
"Unavailable feature: security groups"
)
@valid_kwargs('name', 'description')
def update_security_group(self, name_or_id, **kwargs):
"""Update a security group
:param string name_or_id: Name or ID of the security group to update.
:param string name: New name for the security group.
:param string description: New description for the security group.
:returns: A dictionary describing the updated security group.
:raises: OpenStackCloudException on operation error.
"""
secgroup = self.get_security_group(name_or_id)
if secgroup is None:
raise OpenStackCloudException(
"Security group %s not found." % name_or_id)
if self.secgroup_source == 'neutron':
try:
group = self.manager.submitTask(
_tasks.NeutronSecurityGroupUpdate(
security_group=secgroup['id'],
body={'security_group': kwargs})
)
except Exception as e:
self.log.debug(
"neutron failed to update security group '{group}'".format(
group=name_or_id), exc_info=True)
raise OpenStackCloudException(
"failed to update security group '{group}': {msg}".format(
group=name_or_id, msg=str(e)))
return group['security_group']
elif self.secgroup_source == 'nova':
try:
group = meta.obj_to_dict(
self.manager.submitTask(
_tasks.NovaSecurityGroupUpdate(
group=secgroup['id'], **kwargs)
)
)
except Exception as e:
self.log.debug(
"nova failed to update security group '{group}'".format(
group=name_or_id), exc_info=True)
raise OpenStackCloudException(
"failed to update security group '{group}': {msg}".format(
group=name_or_id, msg=str(e)))
return _utils.normalize_nova_secgroups([group])[0]
# Security groups not supported
else:
raise OpenStackCloudUnavailableFeature(
"Unavailable feature: security groups"
)
class OperatorCloud(OpenStackCloud):
"""Represent a privileged/operator connection to an OpenStack Cloud.

View File

@ -207,6 +207,11 @@ class NeutronSecurityGroupDelete(task_manager.Task):
return client.neutron_client.delete_security_group(**self.args)
class NeutronSecurityGroupUpdate(task_manager.Task):
def main(self, client):
return client.neutron_client.update_security_group(**self.args)
class NovaSecurityGroupList(task_manager.Task):
def main(self, client):
return client.nova_client.security_groups.list()
@ -222,6 +227,11 @@ class NovaSecurityGroupDelete(task_manager.Task):
return client.nova_client.security_groups.delete(**self.args)
class NovaSecurityGroupUpdate(task_manager.Task):
def main(self, client):
return client.nova_client.security_groups.update(**self.args)
# TODO: Do this with neutron instead of nova if possible
class FloatingIPList(task_manager.Task):
def main(self, client):

View File

@ -13,6 +13,7 @@
# under the License.
import copy
import mock
import shade
@ -164,3 +165,38 @@ class TestSecurityGroups(base.TestCase):
'', '')
self.assertFalse(mock_neutron.create_security_group.called)
self.assertFalse(mock_nova.security_groups.create.called)
@mock.patch.object(shade.OpenStackCloud, 'neutron_client')
def test_update_security_group_neutron(self, mock_neutron):
self.cloud.secgroup_source = 'neutron'
neutron_return = dict(security_groups=[neutron_grp_dict])
mock_neutron.list_security_groups.return_value = neutron_return
self.cloud.update_security_group(neutron_grp_obj.id, name='new_name')
mock_neutron.update_security_group.assert_called_once_with(
security_group=neutron_grp_dict['id'],
body={'security_group': {'name': 'new_name'}}
)
@mock.patch.object(shade.OpenStackCloud, 'nova_client')
def test_update_security_group_nova(self, mock_nova):
new_name = self.getUniqueString()
self.cloud.secgroup_source = 'nova'
nova_return = [nova_grp_obj]
update_return = copy.deepcopy(nova_grp_obj)
update_return.name = new_name
mock_nova.security_groups.list.return_value = nova_return
mock_nova.security_groups.update.return_value = update_return
r = self.cloud.update_security_group(nova_grp_obj.id, name=new_name)
mock_nova.security_groups.update.assert_called_once_with(
group=nova_grp_obj.id, name=new_name
)
self.assertEqual(r['name'], new_name)
@mock.patch.object(shade.OpenStackCloud, 'neutron_client')
@mock.patch.object(shade.OpenStackCloud, 'nova_client')
def test_update_security_group_bad_kwarg(self, mock_nova, mock_neutron):
self.assertRaises(TypeError,
self.cloud.update_security_group,
'doesNotExist', bad_arg='')
self.assertFalse(mock_neutron.create_security_group.called)
self.assertFalse(mock_nova.security_groups.create.called)