Using common methods from oslo cliutils

There are some common methods in cliutils we can use in novaclient:
 arg, env, unauthenticated, isunauthenticated.

 + Replaces utils.env to add alias env from cliutils.
 + Replaces utils.arg to add alias arg from cliutils.
 + Removes unused methods: add_arg, unauthenticated, isunauthenticated
   To use methods from clituils.

Related to blueprint common-client-library-2
Change-Id: Ic7c132c37d6a91cf3eae55530300efd153c31903
This commit is contained in:
Sahid Orentino Ferdjaoui 2014-01-08 12:07:30 +01:00 committed by Gerrit Code Review
parent daa33c56c9
commit da11e62216
3 changed files with 8 additions and 57 deletions

@ -45,6 +45,7 @@ import novaclient.auth_plugin
from novaclient import client
from novaclient import exceptions as exc
import novaclient.extension
from novaclient.openstack.common import cliutils
from novaclient.openstack.common import strutils
from novaclient import utils
from novaclient.v1_1 import shell as shell_v1_1
@ -593,7 +594,7 @@ class OpenStackComputeShell(object):
# If we have an auth token but no management_url, we must auth anyway.
# Expired tokens are handled by client.py:_cs_request
must_auth = not (utils.isunauthenticated(args.func)
must_auth = not (cliutils.isunauthenticated(args.func)
or (auth_token and management_url))
#FIXME(usrleon): Here should be restrict for project id same as
@ -678,7 +679,7 @@ class OpenStackComputeShell(object):
try:
# This does a couple of bits which are useful even if we've
# got the token + service URL already. It exits fast in that case.
if not utils.isunauthenticated(args.func):
if not cliutils.isunauthenticated(args.func):
self.cs.authenticate()
except exc.Unauthorized:
raise exc.CommandError("Invalid OpenStack Nova credentials.")

@ -49,7 +49,8 @@ class ShellTest(utils.TestCase):
self.useFixture(fixtures.MonkeyPatch(
'novaclient.client.get_client_class',
mock.MagicMock))
self.nc_util = mock.patch('novaclient.utils.isunauthenticated').start()
self.nc_util = mock.patch(
'novaclient.openstack.common.cliutils.isunauthenticated').start()
self.nc_util.return_value = False
def shell(self, argstr, exitcodes=(0,)):

@ -12,7 +12,6 @@
# under the License.
import json
import os
import pkg_resources
import sys
import textwrap
@ -22,42 +21,13 @@ import prettytable
import six
from novaclient import exceptions
from novaclient.openstack.common import cliutils
from novaclient.openstack.common import jsonutils
from novaclient.openstack.common import strutils
def arg(*args, **kwargs):
"""Decorator for CLI args."""
def _decorator(func):
add_arg(func, *args, **kwargs)
return func
return _decorator
def env(*args, **kwargs):
"""
returns the first environment variable set
if none are non-empty, defaults to '' or keyword arg default
"""
for arg in args:
value = os.environ.get(arg, None)
if value:
return value
return kwargs.get('default', '')
def add_arg(f, *args, **kwargs):
"""Bind CLI arguments to a shell.py `do_foo` function."""
if not hasattr(f, 'arguments'):
f.arguments = []
# NOTE(sirp): avoid dups that can occur when the module is shared across
# tests.
if (args, kwargs) not in f.arguments:
# Because of the semantics of decorator composition if we just append
# to the options list positional options will appear to be backwards.
f.arguments.insert(0, (args, kwargs))
arg = cliutils.arg
env = cliutils.env
def add_resource_manager_extra_kwargs_hook(f, hook):
@ -96,27 +66,6 @@ def get_resource_manager_extra_kwargs(f, args, allow_conflicts=False):
return extra_kwargs
def unauthenticated(f):
"""
Adds 'unauthenticated' attribute to decorated function.
Usage:
@unauthenticated
def mymethod(f):
...
"""
f.unauthenticated = True
return f
def isunauthenticated(f):
"""
Checks to see if the function is marked as not requiring authentication
with the @unauthenticated decorator. Returns True if decorator is
set to True, False otherwise.
"""
return getattr(f, 'unauthenticated', False)
def service_type(stype):
"""
Adds 'service_type' attribute to decorated function.