diff --git a/setup.cfg b/setup.cfg index 7ec02f2d..4127a86c 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 8fa3ea41..4b7a061b 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 f4e74306..7853f0ec 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, + ), + }, + } @@ -680,3 +689,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 35ec269c..ee0bddfc 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 17675a38..f632e92d 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})