Remove 'ProfileAction' and related arguments

The 'ProfileAction' class and the related user_preferences are
pre-history relics. Keeping them in senlinclient is only causing
confusion considering that 1) senlin shell will be deprecated someday;
2) SDK has some tweaks to the design that we cannot follow up; 3) these
arguments are rarely used.

Change-Id: Ia79a069bde4fd94bc1bd00f719b4e6b4a56770f8
This commit is contained in:
tengqm 2016-08-10 03:12:59 -04:00
parent 1c6496caeb
commit 92f52735cb
6 changed files with 3 additions and 130 deletions

View File

@ -13,7 +13,6 @@
import argparse import argparse
from senlinclient.common.i18n import _ from senlinclient.common.i18n import _
from senlinclient.common import sdk
from senlinclient.common import utils from senlinclient.common import utils
@ -140,35 +139,6 @@ def add_global_identity_args(parser):
default=utils.env('OS_ACCESS_INFO'), default=utils.env('OS_ACCESS_INFO'),
help=_('Access info, defaults to env[OS_ACCESS_INFO]')) help=_('Access info, defaults to env[OS_ACCESS_INFO]'))
parser.add_argument(
'--os-api-name', dest='user_preferences',
metavar='<service>=<name>',
action=sdk.ProfileAction,
default=sdk.ProfileAction.env('OS_API_NAME'),
help=_('Desired API names, defaults to env[OS_API_NAME]'))
parser.add_argument(
'--os-api-region', dest='user_preferences',
metavar='<service>=<region>',
action=sdk.ProfileAction,
default=sdk.ProfileAction.env('OS_API_REGION', 'OS_REGION_NAME'),
help=_('Desired API region, defaults to env[OS_API_REGION]'))
parser.add_argument(
'--os-api-version', dest='user_preferences',
metavar='<service>=<version>',
action=sdk.ProfileAction,
default=sdk.ProfileAction.env('OS_API_VERSION'),
help=_('Desired API versions, defaults to env[OS_API_VERSION]'))
parser.add_argument(
'--os-api-interface', dest='user_preferences',
metavar='<service>=<interface>',
action=sdk.ProfileAction,
default=sdk.ProfileAction.env('OS_INTERFACE'),
help=_('Desired API interface, defaults to env[OS_INTERFACE]'))
# parser.add_argument( # parser.add_argument(
# '--os-cert', # '--os-cert',
# help=_('Path of certificate file to use in SSL connection. This ' # help=_('Path of certificate file to use in SSL connection. This '

View File

@ -10,67 +10,12 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import argparse
import os
from openstack import connection from openstack import connection
from openstack import exceptions from openstack import exceptions
from openstack import profile from openstack import profile
from openstack import resource as base
from senlinclient.common import exc from senlinclient.common import exc
# Alias here for consistency
prop = base.prop
class ProfileAction(argparse.Action):
"""A custom action to parse user preferences as key=value pairs
Stores results in users preferences object.
"""
prof = profile.Profile()
@classmethod
def env(cls, *vars):
for v in vars:
values = os.environ.get(v, None)
if values is None:
continue
cls.set_option(v, values)
return cls.prof
return cls.prof
@classmethod
def set_option(cls, var, values):
if var == '--os-extensions':
cls.prof.load_extension(values)
return
if var == 'OS_REGION_NAME':
var = 'region'
var = var.replace('--os-api-', '')
var = var.replace('OS_API_', '')
var = var.lower()
for kvp in values.split(','):
if '=' in kvp:
service, value = kvp.split('=')
else:
service = cls.prof.ALL
value = kvp
if var == 'name':
cls.prof.set_name(service, value)
elif var == 'region':
cls.prof.set_region(service, value)
elif var == 'version':
cls.prof.set_version(service, value)
elif var == 'interface':
cls.prof.set_interface(service, value)
def __call__(self, parser, namespace, values, option_string=None):
if getattr(namespace, self.dest, None) is None:
setattr(namespace, self.dest, ProfileAction.prof)
self.set_option(option_string, values)
def create_connection(prof=None, user_agent=None, **kwargs): def create_connection(prof=None, user_agent=None, **kwargs):
if not prof: if not prof:

View File

@ -235,8 +235,7 @@ class SenlinShell(object):
'trust_id': args.trust_id, 'trust_id': args.trust_id,
} }
return senlin_client.Client('1', prof=args.user_preferences, return senlin_client.Client('1', user_agent=USER_AGENT, **kwargs)
user_agent=USER_AGENT, **kwargs)
def main(self, argv): def main(self, argv):
# Parse args once to find version # Parse args once to find version

View File

@ -41,10 +41,6 @@ class TestCliArgs(testtools.TestCase):
'--os-trust-id', '--os-trust-id',
'--os-token', '--os-token',
'--os-access-info', '--os-access-info',
'--os-api-name',
'--os-api-region',
'--os-api-version',
'--os-api-interface'
] ]
options = [arg[0][0] for arg in parser.add_argument.call_args_list] options = [arg[0][0] for arg in parser.add_argument.call_args_list]

View File

@ -11,7 +11,6 @@
# under the License. # under the License.
import mock import mock
import os
import testtools import testtools
from openstack import connection as sdk_connection from openstack import connection as sdk_connection
@ -22,42 +21,6 @@ from senlinclient.common import sdk
class TestSdk(testtools.TestCase): class TestSdk(testtools.TestCase):
@mock.patch('senlinclient.common.sdk.ProfileAction.set_option')
def test_env(self, mock_set_option):
os.environ['test_senlin_sdk_env'] = '1'
sdk.ProfileAction.env('test_senlin_sdk_env')
mock_set_option.assert_called_once_with('test_senlin_sdk_env', '1')
@mock.patch('senlinclient.common.sdk.ProfileAction.prof')
def test_set_option_set_name(self, mock_prof):
mock_prof.ALL = 'mock_prof.ALL'
sdk.ProfileAction.set_option('name', 'test=val1')
mock_prof.set_name.assert_called_once_with('test', 'val1')
mock_prof.reset_mock()
sdk.ProfileAction.set_option('name', 'val2')
mock_prof.set_name.assert_called_once_with(mock_prof.ALL, 'val2')
@mock.patch('senlinclient.common.sdk.ProfileAction.prof')
def test_set_option_set_region(self, mock_prof):
mock_prof.ALL = 'mock_prof.ALL'
sdk.ProfileAction.set_option('OS_REGION_NAME', 'test=val1')
mock_prof.set_region.assert_called_once_with('test', 'val1')
mock_prof.reset_mock()
sdk.ProfileAction.set_option('OS_REGION_NAME', 'val2')
mock_prof.set_region.assert_called_once_with(mock_prof.ALL, 'val2')
@mock.patch('senlinclient.common.sdk.ProfileAction.prof')
def test_set_option_set_version(self, mock_prof):
mock_prof.ALL = 'mock_prof.ALL'
sdk.ProfileAction.set_option('version', 'test=val1')
mock_prof.set_version.assert_called_once_with('test', 'val1')
@mock.patch('senlinclient.common.sdk.ProfileAction.prof')
def test_set_option_set_interface(self, mock_prof):
mock_prof.ALL = 'mock_prof.ALL'
sdk.ProfileAction.set_option('interface', 'test=val1')
mock_prof.set_interface.assert_called_once_with('test', 'val1')
@mock.patch.object(sdk_connection, 'Connection') @mock.patch.object(sdk_connection, 'Connection')
def test_create_connection_with_profile(self, mock_connection): def test_create_connection_with_profile(self, mock_connection):
mock_prof = mock.Mock() mock_prof = mock.Mock()

View File

@ -366,8 +366,8 @@ class ShellTest(testtools.TestCase):
mock_conn.return_value = conn mock_conn.return_value = conn
conn.session = mock.Mock() conn.session = mock.Mock()
sh._setup_senlin_client('1', args) sh._setup_senlin_client('1', args)
mock_conn.assert_called_once_with(prof=args.user_preferences, mock_conn.assert_called_once_with(prof=None, user_agent=USER_AGENT,
user_agent=USER_AGENT, **kwargs) **kwargs)
client = mock.Mock() client = mock.Mock()
senlin_client.Client = mock.MagicMock(return_value=client) senlin_client.Client = mock.MagicMock(return_value=client)
self.assertEqual(client, sh._setup_senlin_client('1', args)) self.assertEqual(client, sh._setup_senlin_client('1', args))