From 66a98966bf8924b01c983ac9ac31d4e45476339c Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Wed, 22 May 2013 16:28:34 +0800 Subject: [PATCH] Add update method of security group name and description make it possible to edit the name and description of common security groups, we can not rename the default. nova patch : https://review.openstack.org/#/c/29490/ Fixes: bug #918393 Change-Id: I559f2fa09c1f205d3bbe7352fc169152e6b38586 --- README.rst | 1 + novaclient/tests/v1_1/fakes.py | 6 ++++++ novaclient/tests/v1_1/test_security_groups.py | 6 ++++++ novaclient/tests/v1_1/test_shell.py | 7 +++++++ novaclient/v1_1/security_groups.py | 16 ++++++++++++++++ novaclient/v1_1/shell.py | 13 +++++++++++++ 6 files changed, 49 insertions(+) diff --git a/README.rst b/README.rst index ed4dc96b5..6207d89f0 100644 --- a/README.rst +++ b/README.rst @@ -160,6 +160,7 @@ You'll find complete documentation on the shell by running Add a source group rule to a security group. secgroup-add-rule Add a rule to a security group. secgroup-create Create a security group. + secgroup-update Update a security group. secgroup-delete Delete a security group. secgroup-delete-group-rule Delete a source group rule from a security group. diff --git a/novaclient/tests/v1_1/fakes.py b/novaclient/tests/v1_1/fakes.py index 924d39051..3d6eb7221 100644 --- a/novaclient/tests/v1_1/fakes.py +++ b/novaclient/tests/v1_1/fakes.py @@ -1130,6 +1130,12 @@ class FakeHTTPClient(base_client.HTTPClient): self.get_os_security_groups()[2]['security_groups'][0]} return (202, {}, r) + def put_os_security_groups_1(self, body, **kw): + assert body.keys() == ['security_group'] + fakes.assert_has_keys(body['security_group'], + required=['name', 'description']) + return (205, {}, body) + # # Security Group Rules # diff --git a/novaclient/tests/v1_1/test_security_groups.py b/novaclient/tests/v1_1/test_security_groups.py index ce8e66337..9f3ae46bb 100644 --- a/novaclient/tests/v1_1/test_security_groups.py +++ b/novaclient/tests/v1_1/test_security_groups.py @@ -45,6 +45,12 @@ class SecurityGroupsTest(utils.TestCase): cs.assert_called('POST', '/os-security-groups') self.assertTrue(isinstance(sg, security_groups.SecurityGroup)) + def test_update_security_group(self): + sg = cs.security_groups.list()[0] + secgroup = cs.security_groups.update(sg, "update", "update") + cs.assert_called('PUT', '/os-security-groups/1') + self.assertTrue(isinstance(secgroup, security_groups.SecurityGroup)) + def test_refresh_security_group(self): sg = cs.security_groups.get(1) sg2 = cs.security_groups.get(1) diff --git a/novaclient/tests/v1_1/test_shell.py b/novaclient/tests/v1_1/test_shell.py index 5b17b2b9e..5d4336490 100644 --- a/novaclient/tests/v1_1/test_shell.py +++ b/novaclient/tests/v1_1/test_shell.py @@ -1206,6 +1206,13 @@ class ShellTest(utils.TestCase): {'name': 'test', 'description': 'FAKE_SECURITY_GROUP'}}) + def test_security_group_update(self): + self.run_command('secgroup-update test te FAKE_SECURITY_GROUP') + self.assert_called('PUT', '/os-security-groups/1', + {'security_group': + {'name': 'te', + 'description': 'FAKE_SECURITY_GROUP'}}) + def test_security_group_list(self): self.run_command('secgroup-list') self.assert_called('GET', '/os-security-groups') diff --git a/novaclient/v1_1/security_groups.py b/novaclient/v1_1/security_groups.py index 551811526..d0778634a 100644 --- a/novaclient/v1_1/security_groups.py +++ b/novaclient/v1_1/security_groups.py @@ -29,6 +29,9 @@ class SecurityGroup(base.Resource): def delete(self): self.manager.delete(self) + def update(self): + self.manager.update(self) + class SecurityGroupManager(base.ManagerWithFind): resource_class = SecurityGroup @@ -44,6 +47,19 @@ class SecurityGroupManager(base.ManagerWithFind): body = {"security_group": {"name": name, 'description': description}} return self._create('/os-security-groups', body, 'security_group') + def update(self, group, name, description): + """ + Update a security group + + :param group: The security group to delete (group or ID) + :param name: name for the security group to update + :param description: description for the security group to update + :rtype: the security group object + """ + body = {"security_group": {"name": name, 'description': description}} + return self._update('/os-security-groups/%s' % base.getid(group), + body, 'security_group') + def delete(self, group): """ Delete a security group diff --git a/novaclient/v1_1/shell.py b/novaclient/v1_1/shell.py index 33457c59a..d1ee17536 100644 --- a/novaclient/v1_1/shell.py +++ b/novaclient/v1_1/shell.py @@ -1978,6 +1978,19 @@ def do_secgroup_create(cs, args): _print_secgroups([secgroup]) +@utils.arg('secgroup', + metavar='', + help='ID or name of security group.') +@utils.arg('name', metavar='', help='Name of security group.') +@utils.arg('description', metavar='', + help='Description of security group.') +def do_secgroup_update(cs, args): + """Update a security group.""" + sg = _get_secgroup(cs, args.secgroup) + secgroup = cs.security_groups.update(sg, args.name, args.description) + _print_secgroups([secgroup]) + + @utils.arg('secgroup', metavar='', help='ID or name of security group.')