From a570b60d674d6bbdf90f26440e68771f22b704db Mon Sep 17 00:00:00 2001 From: Andrey Kurilin Date: Fri, 7 Aug 2015 21:03:39 +0300 Subject: [PATCH] Copy cli arguments in api_versions.wraps decorator novaclient.api_versions.wraps decorator can be used in shell. Most of shell function has cli arguments which are added via another decorator. Currently, to keep this cli arguments, novaclient.api_versions.wraps decorator should be used in the top of decorators. Something like: @api_versions.wraps("123.456") @cliutils.arg("name", help="Name of the something") @cliutils.arg("action", help="Some action") def do_something(cs, args): pass This patch adds ability to put api_versions.wraps decorator everywhere: @api_versions.wraps("123.456") @cliutils.arg("name", help="Name of the something") @cliutils.arg("action", help="Some action") def do_something_1(cs, args): pass @cliutils.arg("name", help="Name of the something") @cliutils.arg("action", help="Some action") @api_versions.wraps("123.456") def do_something_2(cs, args): pass Related to bp api-microversion-support Change-Id: I8d599b712b17dcfc0be940a61c537d2dfe1b715b --- novaclient/api_versions.py | 13 +++++++++--- novaclient/tests/unit/test_api_versions.py | 23 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/novaclient/api_versions.py b/novaclient/api_versions.py index ba4a56db8..b31801411 100644 --- a/novaclient/api_versions.py +++ b/novaclient/api_versions.py @@ -22,6 +22,7 @@ from oslo_utils import strutils import novaclient from novaclient import exceptions from novaclient.i18n import _, _LW +from novaclient.openstack.common import cliutils from novaclient import utils LOG = logging.getLogger(__name__) @@ -340,8 +341,14 @@ def wraps(start_version, end_version=None): if not methods: raise exceptions.VersionNotFoundForAPIMethod( obj.api_version.get_string(), name) - else: - return max(methods, key=lambda f: f.start_version).func( - obj, *args, **kwargs) + + method = max(methods, key=lambda f: f.start_version) + + return method.func(obj, *args, **kwargs) + + if hasattr(func, 'arguments'): + for cli_args, cli_kwargs in func.arguments: + cliutils.add_arg(substitution, *cli_args, **cli_kwargs) return substitution + return decor diff --git a/novaclient/tests/unit/test_api_versions.py b/novaclient/tests/unit/test_api_versions.py index e3c204de4..02df69a41 100644 --- a/novaclient/tests/unit/test_api_versions.py +++ b/novaclient/tests/unit/test_api_versions.py @@ -18,6 +18,7 @@ import mock import novaclient from novaclient import api_versions from novaclient import exceptions +from novaclient.openstack.common import cliutils from novaclient.tests.unit import utils from novaclient.v2 import versions @@ -250,6 +251,28 @@ class WrapsTestCase(utils.TestCase): checker.assert_called_once_with(*((obj,) + some_args), **some_kwargs) + def test_cli_args_are_copied(self): + + @api_versions.wraps("2.2", "2.6") + @cliutils.arg("name_1", help="Name of the something") + @cliutils.arg("action_1", help="Some action") + def some_func_1(cs, args): + pass + + @cliutils.arg("name_2", help="Name of the something") + @cliutils.arg("action_2", help="Some action") + @api_versions.wraps("2.2", "2.6") + def some_func_2(cs, args): + pass + + args_1 = [(('name_1',), {'help': 'Name of the something'}), + (('action_1',), {'help': 'Some action'})] + self.assertEqual(args_1, some_func_1.arguments) + + args_2 = [(('name_2',), {'help': 'Name of the something'}), + (('action_2',), {'help': 'Some action'})] + self.assertEqual(args_2, some_func_2.arguments) + class DiscoverVersionTestCase(utils.TestCase): def setUp(self):