Merge "Add support for modification of instance Security Group"

This commit is contained in:
Jenkins 2012-07-17 15:24:09 +00:00 committed by Gerrit Code Review
commit b9e1d61872
4 changed files with 66 additions and 0 deletions

View File

@ -258,6 +258,18 @@ class Server(base.Resource):
"""
self.manager.reset_state(self, state)
def add_security_group(self, security_group):
"""
Add a security group to an instance.
"""
self.manager.add_security_group(self, security_group)
def remove_security_group(self, security_group):
"""
Remova a security group from an instance.
"""
self.manager.remove_security_group(self, security_group)
class ServerManager(local_base.BootingManagerWithFind):
resource_class = Server
@ -647,6 +659,26 @@ class ServerManager(local_base.BootingManagerWithFind):
"""
self._action('os-resetState', server, dict(state=state))
def add_security_group(self, server, security_group):
"""
Add a Security Group to a instance
:param server: ID of the instance.
:param security_grou: The name of security group to add.
"""
self._action('addSecurityGroup', server, {'name': security_group})
def remove_security_group(self, server, security_group):
"""
Add a Security Group to a instance
:param server: ID of the instance.
:param security_grou: The name of security group to remove.
"""
self._action('removeSecurityGroup', server, {'name': security_group})
def _action(self, action, server, info=None, **kwargs):
"""
Perform a server "action" -- reboot/rebuild/resize/etc.

View File

@ -1103,6 +1103,22 @@ def do_remove_floating_ip(cs, args):
server.remove_floating_ip(args.address)
@utils.arg('server', metavar='<server>', help='Name or ID of server.')
@utils.arg('secgroup', metavar='<secgroup>', help='Name of Security Group.')
def do_add_secgroup(cs, args):
"""Add a Security Group to a server."""
server = _find_server(cs, args.server)
server.add_security_group(args.secgroup)
@utils.arg('server', metavar='<server>', help='Name or ID of server.')
@utils.arg('secgroup', metavar='<secgroup>', help='Name of Security Group.')
def do_remove_secgroup(cs, args):
"""Remove a Security Group from a server."""
server = _find_server(cs, args.server)
server.remove_security_group(args.secgroup)
@utils.arg('pool',
metavar='<floating_ip_pool>',
help='Name of Floating IP Pool. (Optional)',

View File

@ -341,6 +341,10 @@ class FakeHTTPClient(base_client.HTTPClient):
'disk_over_commit'])
elif action == 'os-resetState':
assert body[action].keys() == ['state']
elif action == 'addSecurityGroup':
assert body[action].keys() == ['name']
elif action == 'removeSecurityGroup':
assert body[action].keys() == ['name']
else:
raise AssertionError("Unexpected server action: %s" % action)
return (resp, _body)

View File

@ -310,3 +310,17 @@ class ServersTest(utils.TestCase):
cs.assert_called('POST', '/servers/1234/action')
cs.servers.reset_state(s, 'newstate')
cs.assert_called('POST', '/servers/1234/action')
def test_add_security_group(self):
s = cs.servers.get(1234)
s.add_security_group('newsg')
cs.assert_called('POST', '/servers/1234/action')
cs.servers.add_security_group(s, 'newsg')
cs.assert_called('POST', '/servers/1234/action')
def test_remove_security_group(self):
s = cs.servers.get(1234)
s.remove_security_group('oldsg')
cs.assert_called('POST', '/servers/1234/action')
cs.servers.remove_security_group(s, 'oldsg')
cs.assert_called('POST', '/servers/1234/action')