Merge "Add cli for remove security group"

This commit is contained in:
Zuul 2018-01-19 06:03:06 +00:00 committed by Gerrit Code Review
commit cba0c9a81b
5 changed files with 78 additions and 0 deletions

View File

@ -63,6 +63,7 @@ openstack.container.v1 =
appcontainer_network_detach = zunclient.osc.v1.containers:NetworkDetach appcontainer_network_detach = zunclient.osc.v1.containers:NetworkDetach
appcontainer_network_attach = zunclient.osc.v1.containers:NetworkAttach appcontainer_network_attach = zunclient.osc.v1.containers:NetworkAttach
appcontainer_image_search = zunclient.osc.v1.images:SearchImage appcontainer_image_search = zunclient.osc.v1.images:SearchImage
appcontainer_remove_security_group = zunclient.osc.v1.containers:RemoveSecurityGroup
[build_sphinx] [build_sphinx]
source-dir = doc/source source-dir = doc/source

View File

@ -1017,6 +1017,37 @@ class AddSecurityGroup(command.Command):
"%(e)s" % {'container': parsed_args.container, 'e': e}) "%(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='<container>',
help='ID or name of the container to remove security group.')
parser.add_argument(
'security_group',
metavar='<security_group>',
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): class NetworkDetach(command.Command):
"""Detach neutron network from specified container.""" """Detach neutron network from specified container."""
log = logging.getLogger(__name__ + ".NetworkDetach") log = logging.getLogger(__name__ + ".NetworkDetach")

View File

@ -327,6 +327,15 @@ fake_responses = {
None, 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.assertEqual(expect, self.api.calls)
self.assertTrue(containers) 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)

View File

@ -215,3 +215,7 @@ class ContainerManager(base.Manager):
def network_attach(self, container, network): def network_attach(self, container, network):
return self._action(container, '/network_attach', return self._action(container, '/network_attach',
qparams={'network': network}) qparams={'network': network})
def remove_security_group(self, id, security_group):
return self._action(id, '/remove_security_group',
qparams={'name': security_group})

View File

@ -769,3 +769,24 @@ def do_network_attach(cs, args):
except Exception as e: except Exception as e:
print("Attach network to container %(container)s " print("Attach network to container %(container)s "
"failed: %(e)s" % {'container': args.container, 'e': e}) "failed: %(e)s" % {'container': args.container, 'e': e})
@utils.arg('container',
metavar='<container>',
help='ID or name of the container to remove security group.')
@utils.arg('security_group',
metavar='<security_group>',
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})