From ead62e03746a52e35bfe45f20ab52318f77df0ac Mon Sep 17 00:00:00 2001 From: cooldharma06 Date: Thu, 11 Jan 2018 14:44:11 +0530 Subject: [PATCH] Add cli for remove security group Change-Id: I35919f3e6edd0239a4edc0b68c6622f910d85be6 Closes-Bug: #1737152 --- setup.cfg | 1 + zunclient/osc/v1/containers.py | 31 ++++++++++++++++++++++ zunclient/tests/unit/v1/test_containers.py | 21 +++++++++++++++ zunclient/v1/containers.py | 4 +++ zunclient/v1/containers_shell.py | 21 +++++++++++++++ 5 files changed, 78 insertions(+) diff --git a/setup.cfg b/setup.cfg index 89357731..acb12fe7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -63,6 +63,7 @@ openstack.container.v1 = appcontainer_network_detach = zunclient.osc.v1.containers:NetworkDetach appcontainer_network_attach = zunclient.osc.v1.containers:NetworkAttach appcontainer_image_search = zunclient.osc.v1.images:SearchImage + appcontainer_remove_security_group = zunclient.osc.v1.containers:RemoveSecurityGroup [build_sphinx] source-dir = doc/source diff --git a/zunclient/osc/v1/containers.py b/zunclient/osc/v1/containers.py index 35f0739c..92627a8c 100644 --- a/zunclient/osc/v1/containers.py +++ b/zunclient/osc/v1/containers.py @@ -1017,6 +1017,37 @@ class AddSecurityGroup(command.Command): "%(e)s" % {'container': parsed_args.container, 'e': e}) +class RemoveSecurityGroup(command.Command): + """Remove security group for specified container.""" + log = logging.getLogger(__name__ + ".RemoveSecurityGroup") + + def get_parser(self, prog_name): + parser = super(RemoveSecurityGroup, self).get_parser(prog_name) + parser.add_argument( + 'container', + metavar='', + help='ID or name of the container to remove security group.') + parser.add_argument( + 'security_group', + metavar='', + help='The security group to remove from specified container. ') + return parser + + def take_action(self, parsed_args): + client = _get_client(self, parsed_args) + opts = {} + opts['id'] = parsed_args.container + opts['security_group'] = parsed_args.security_group + opts = zun_utils.remove_null_parms(**opts) + try: + client.containers.remove_security_group(**opts) + print("Request to remove security group from container %s " + "has been accepted." % parsed_args.container) + except Exception as e: + print("Remove security group from container %(container)s failed: " + "%(e)s" % {'container': parsed_args.container, 'e': e}) + + class NetworkDetach(command.Command): """Detach neutron network from specified container.""" log = logging.getLogger(__name__ + ".NetworkDetach") diff --git a/zunclient/tests/unit/v1/test_containers.py b/zunclient/tests/unit/v1/test_containers.py index a1cdab06..9d4b46fb 100644 --- a/zunclient/tests/unit/v1/test_containers.py +++ b/zunclient/tests/unit/v1/test_containers.py @@ -327,6 +327,15 @@ fake_responses = { None, ), }, + '/v1/containers/%s/remove_security_group?%s' + % (CONTAINER1['id'], parse.urlencode({'name': security_group})): + { + 'POST': ( + {}, + None, + ), + }, + } @@ -679,3 +688,15 @@ class ContainerManagerTest(testtools.TestCase): ] self.assertEqual(expect, self.api.calls) self.assertTrue(containers) + + def test_containers_remove_security_group(self): + containers = self.mgr.remove_security_group( + CONTAINER1['id'], security_group) + expect = [ + ('POST', '/v1/containers/%s/remove_security_group?%s' + % (CONTAINER1['id'], + parse.urlencode({'name': security_group})), + {'Content-Length': '0'}, None) + ] + self.assertEqual(expect, self.api.calls) + self.assertTrue(containers) diff --git a/zunclient/v1/containers.py b/zunclient/v1/containers.py index ae9b491b..357600a3 100644 --- a/zunclient/v1/containers.py +++ b/zunclient/v1/containers.py @@ -215,3 +215,7 @@ class ContainerManager(base.Manager): def network_attach(self, container, network): return self._action(container, '/network_attach', qparams={'network': network}) + + def remove_security_group(self, id, security_group): + return self._action(id, '/remove_security_group', + qparams={'name': security_group}) diff --git a/zunclient/v1/containers_shell.py b/zunclient/v1/containers_shell.py index 5b9f6a38..8f0da8c8 100644 --- a/zunclient/v1/containers_shell.py +++ b/zunclient/v1/containers_shell.py @@ -769,3 +769,24 @@ def do_network_attach(cs, args): except Exception as e: print("Attach network to container %(container)s " "failed: %(e)s" % {'container': args.container, 'e': e}) + + +@utils.arg('container', + metavar='', + help='ID or name of the container to remove security group.') +@utils.arg('security_group', + metavar='', + help='The security group to remove from specified container.') +def do_remove_security_group(cs, args): + """Remove security group for specified container.""" + opts = {} + opts['id'] = args.container + opts['security_group'] = args.security_group + opts = zun_utils.remove_null_parms(**opts) + try: + cs.containers.remove_security_group(**opts) + print("Request to remove security group for container %s " + "has been accepted." % args.container) + except Exception as e: + print("Remove security group for container %(container)s " + "failed: %(e)s" % {'container': args.container, 'e': e})