From 8424d04cf91f1bce14fbe6ecb4b5fcdb8b4e3057 Mon Sep 17 00:00:00 2001 From: deepak_mourya Date: Thu, 29 Mar 2018 10:56:31 +0530 Subject: [PATCH] Enhance zun update command Right now, for renaming the container we need to use zun rename but we can move this into PATCH /containers/ https://github.com/openstack/zun/blob/master/zun/api/controllers/v1/containers.py#L570) because this is the convention of API design in OpenStack (e.g. nova implements server rename at PUT /servers/). This PS adds support for rename container using update command as well: zun update [--cpu ] [-m ] [--name ] Change-Id: I237a849dec549f53efe560a04fc0ca64929c14e7 Related-Bug: #1759459 --- setup.cfg | 1 - tox.ini | 2 +- zunclient/api_versions.py | 2 +- zunclient/common/cliutils.py | 9 +++++ zunclient/osc/v1/containers.py | 35 ++++--------------- .../tests/functional/osc/v1/test_container.py | 15 -------- zunclient/tests/unit/test_shell.py | 8 ++--- zunclient/v1/containers_shell.py | 33 +++++++++++------ 8 files changed, 43 insertions(+), 62 deletions(-) diff --git a/setup.cfg b/setup.cfg index ba546745..5ef306da 100644 --- a/setup.cfg +++ b/setup.cfg @@ -48,7 +48,6 @@ openstack.container.v1 = appcontainer_kill = zunclient.osc.v1.containers:KillContainer appcontainer_stop = zunclient.osc.v1.containers:StopContainer appcontainer_run = zunclient.osc.v1.containers:RunContainer - appcontainer_rename = zunclient.osc.v1.containers:RenameContainer appcontainer_top = zunclient.osc.v1.containers:TopContainer appcontainer_update = zunclient.osc.v1.containers:UpdateContainer appcontainer_attach = zunclient.osc.v1.containers:AttachContainer diff --git a/tox.ini b/tox.ini index 7f5901d5..d6e25e2e 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] minversion = 1.6 -envlist = py35,py27,pypy,pep8 +envlist = py35,py27,pep8 skipsdist = True [testenv] diff --git a/zunclient/api_versions.py b/zunclient/api_versions.py index 4b3b5a3e..da4c6257 100644 --- a/zunclient/api_versions.py +++ b/zunclient/api_versions.py @@ -31,7 +31,7 @@ if not LOG.handlers: HEADER_NAME = "OpenStack-API-Version" SERVICE_TYPE = "container" MIN_API_VERSION = '1.1' -MAX_API_VERSION = '1.13' +MAX_API_VERSION = '1.14' DEFAULT_API_VERSION = MAX_API_VERSION _SUBSTITUTIONS = {} diff --git a/zunclient/common/cliutils.py b/zunclient/common/cliutils.py index 51c0de60..4169bed3 100644 --- a/zunclient/common/cliutils.py +++ b/zunclient/common/cliutils.py @@ -24,6 +24,7 @@ import os import sys import textwrap +import decorator from oslo_utils import encodeutils from oslo_utils import strutils import prettytable @@ -289,3 +290,11 @@ def exit(msg=''): if msg: print(msg, file=sys.stderr) sys.exit(1) + + +def deprecated(message): + @decorator.decorator + def wrapper(func, *args, **kwargs): + print(message) + return func(*args, **kwargs) + return wrapper diff --git a/zunclient/osc/v1/containers.py b/zunclient/osc/v1/containers.py index f9ae1671..41680f2c 100644 --- a/zunclient/osc/v1/containers.py +++ b/zunclient/osc/v1/containers.py @@ -805,35 +805,6 @@ class RunContainer(command.ShowOne): return columns, utils.get_item_properties(container, columns) -class RenameContainer(command.Command): - """Rename specified container""" - log = logging.getLogger(__name__ + ".RenameContainer") - - def get_parser(self, prog_name): - parser = super(RenameContainer, self).get_parser(prog_name) - parser.add_argument( - 'container', - metavar='', - help='ID or name of the container to rename.') - parser.add_argument( - 'name', - metavar='', - help='The new name for the container') - return parser - - def take_action(self, parsed_args): - client = _get_client(self, parsed_args) - container = parsed_args.container - name = parsed_args.name - try: - client.containers.rename(container, name) - print(_('Request to rename container %s has been accepted') - % container) - except Exception as e: - print("rename for container %(container)s failed: %(e)s" % - {'container': container, 'e': e}) - - class TopContainer(command.Command): """Display the running processes inside the container""" log = logging.getLogger(__name__ + ".TopContainer") @@ -882,6 +853,11 @@ class UpdateContainer(command.ShowOne): '--memory', metavar='', help='The container memory size in MiB') + parser.add_argument( + '--name', + metavar='', + help='The new name of container to update') + return parser def take_action(self, parsed_args): @@ -890,6 +866,7 @@ class UpdateContainer(command.ShowOne): opts = {} opts['memory'] = parsed_args.memory opts['cpu'] = parsed_args.cpu + opts['name'] = parsed_args.name opts = zun_utils.remove_null_parms(**opts) if not opts: raise exc.CommandError("You must update at least one property") diff --git a/zunclient/tests/functional/osc/v1/test_container.py b/zunclient/tests/functional/osc/v1/test_container.py index 62d2d739..9f3ec4ae 100644 --- a/zunclient/tests/functional/osc/v1/test_container.py +++ b/zunclient/tests/functional/osc/v1/test_container.py @@ -102,21 +102,6 @@ class ContainerTests(base.TestCase): self.assertEqual(container['image'], container['image']) self.container_delete(container['name']) - def test_rename(self): - """Check container rename command with name and UUID arguments. - - Test steps: - 1) Create container in setUp. - 2) rename container calling it with name and UUID arguments. - 3) Check new name. - """ - container = self.container_create(name='test_rename') - new_name = 'test_new' - self.container_rename(container['name'], new_name) - container_list = self.container_list() - self.assertIn(new_name, [x['name'] for x in container_list]) - self.container_delete(new_name) - def test_execute(self): """Check container execute command with name and UUID arguments. diff --git a/zunclient/tests/unit/test_shell.py b/zunclient/tests/unit/test_shell.py index 8b6446cf..9501ed84 100644 --- a/zunclient/tests/unit/test_shell.py +++ b/zunclient/tests/unit/test_shell.py @@ -246,7 +246,7 @@ class ShellTest(utils.TestCase): project_domain_id='', project_domain_name='', user_domain_id='', user_domain_name='', profile=None, endpoint_override=None, insecure=False, - version=api_versions.APIVersion('1.13')) + version=api_versions.APIVersion('1.14')) def test_main_option_region(self): self.make_env() @@ -274,7 +274,7 @@ class ShellTest(utils.TestCase): project_domain_id='', project_domain_name='', user_domain_id='', user_domain_name='', profile=None, endpoint_override=None, insecure=False, - version=api_versions.APIVersion('1.13')) + version=api_versions.APIVersion('1.14')) @mock.patch('zunclient.client.Client') def test_main_endpoint_internal(self, mock_client): @@ -288,7 +288,7 @@ class ShellTest(utils.TestCase): project_domain_id='', project_domain_name='', user_domain_id='', user_domain_name='', profile=None, endpoint_override=None, insecure=False, - version=api_versions.APIVersion('1.13')) + version=api_versions.APIVersion('1.14')) class ShellTestKeystoneV3(ShellTest): @@ -319,4 +319,4 @@ class ShellTestKeystoneV3(ShellTest): project_domain_id='', project_domain_name='Default', user_domain_id='', user_domain_name='Default', endpoint_override=None, insecure=False, profile=None, - version=api_versions.APIVersion('1.13')) + version=api_versions.APIVersion('1.14')) diff --git a/zunclient/v1/containers_shell.py b/zunclient/v1/containers_shell.py index 42caaba0..1244b87d 100644 --- a/zunclient/v1/containers_shell.py +++ b/zunclient/v1/containers_shell.py @@ -28,6 +28,12 @@ from zunclient.common.websocketclient import websocketclient from zunclient import exceptions as exc +DEPRECATION_MESSAGE = ( + 'WARNING: Rename container command deprecated and will be removed ' + 'in a future release.\nUse Update container command to avoid ' + 'seeing this message.') + + def _show_container(container): zun_utils.format_container_addresses(container) utils.print_dict(container._info) @@ -636,17 +642,6 @@ def do_run(cs, args): raise exceptions.InvalidWebSocketLink(container_uuid) -@utils.arg('container', - metavar='', - help='ID or name of the container to rename.') -@utils.arg('name', - metavar='', - help='The new name for the container') -def do_rename(cs, args): - """Rename a container.""" - cs.containers.rename(args.container, args.name) - - @utils.arg('container', metavar='', help="ID or name of the container to update.") @@ -656,11 +651,15 @@ def do_rename(cs, args): @utils.arg('-m', '--memory', metavar='', help='The container memory size in MiB') +@utils.arg('--name', + metavar='', + help='The new name for the container') def do_update(cs, args): """Update one or more attributes of the container.""" opts = {} opts['memory'] = args.memory opts['cpu'] = args.cpu + opts['name'] = args.name opts = zun_utils.remove_null_parms(**opts) if not opts: raise exc.CommandError("You must update at least one property") @@ -668,6 +667,18 @@ def do_update(cs, args): _show_container(container) +@utils.deprecated(DEPRECATION_MESSAGE) +@utils.arg('container', + metavar='', + help='ID or name of the container to rename.') +@utils.arg('name', + metavar='', + help='The new name for the container') +def do_rename(cs, args): + """Rename a container.""" + cs.containers.rename(args.container, args.name) + + @utils.arg('container', metavar='', help='ID or name of the container to be attached to.')