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
This commit is contained in:
Andrey Kurilin 2015-08-07 21:03:39 +03:00 committed by Andrey Kurilin
parent 46289cafef
commit a570b60d67
2 changed files with 33 additions and 3 deletions

View File

@ -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

View File

@ -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):