From 8fee5bf5848741b191b58cd94625192c03157d51 Mon Sep 17 00:00:00 2001 From: ehdsouza Date: Mon, 16 Sep 2019 12:17:37 -0700 Subject: [PATCH 01/14] feat(base): Add method read_external_sources --- ibm_cloud_sdk_core/base_service.py | 4 +-- ibm_cloud_sdk_core/utils.py | 47 ++++++++++++++++++------------ resources/ibm-credentials-iam.env | 4 ++- test/test_base_service.py | 4 --- test/test_utils.py | 2 +- 5 files changed, 35 insertions(+), 26 deletions(-) diff --git a/ibm_cloud_sdk_core/base_service.py b/ibm_cloud_sdk_core/base_service.py index 5dd3697..3693515 100644 --- a/ibm_cloud_sdk_core/base_service.py +++ b/ibm_cloud_sdk_core/base_service.py @@ -21,7 +21,7 @@ import requests from requests.structures import CaseInsensitiveDict from .version import __version__ -from .utils import has_bad_first_or_last_char, remove_null_values, cleanup_values, read_from_env_variables +from .utils import has_bad_first_or_last_char, remove_null_values, cleanup_values, read_external_sources from .detailed_response import DetailedResponse from .api_exception import ApiException from .authenticators import Authenticator @@ -65,7 +65,7 @@ def __init__(self, if display_name: service_name = display_name.replace(' ', '_').lower() - config = read_from_env_variables(service_name) + config = read_external_sources(service_name) if config.get('url'): self.service_url = config.get('url') if config.get('disable_ssl'): diff --git a/ibm_cloud_sdk_core/utils.py b/ibm_cloud_sdk_core/utils.py index 8b25624..83aa7ba 100644 --- a/ibm_cloud_sdk_core/utils.py +++ b/ibm_cloud_sdk_core/utils.py @@ -54,29 +54,40 @@ def string_to_datetime(string): """ return date_parser.parse(string) -def get_authenticator_from_environment(service_name): +def read_external_sources(service_name): """ - Checks the credentials file and VCAP_SERVICES environment variable + Try to get config from external sources, with the following priority: + 1. Credentials file(ibm-credentials.env) + 2. Environment variables + 3. VCAP Services(Cloud Foundry) :param service_name: The service name - :return: the authenticator + :return: dict """ - authenticator = None - # 1. Credentials from credential file + config = {} + config = read_from_credential_file(service_name) - if config: - authenticator = contruct_authenticator(config) - # 2. From env variables - if not authenticator: + if not config: config = read_from_env_variables(service_name) - if config: - authenticator = contruct_authenticator(config) - # 3. Credentials from VCAP - if not authenticator: + if not config: config = read_from_vcap_services(service_name) - if config: - authenticator = contruct_authenticator(config) + + return config + +def get_authenticator_from_environment(service_name): + """ + Try to get authenticator from external sources, with the following priority: + 1. Credentials file(ibm-credentials.env) + 2. Environment variables + 3. VCAP Services(Cloud Foundry) + :param service_name: The service name + :return: the authenticator + """ + authenticator = None + config = read_external_sources(service_name) + if config: + authenticator = contruct_authenticator(config) return authenticator def read_from_env_variables(service_name): @@ -125,7 +136,7 @@ def read_from_credential_file(service_name, separator='='): return config def _parse_key_and_update_config(config, service_name, key, value): - if service_name in key: + if key.startswith(service_name): index = key.find('_') if index != -1: config[key[index + 1:]] = value @@ -133,7 +144,7 @@ def _parse_key_and_update_config(config, service_name, key, value): def read_from_vcap_services(service_name): service_name = service_name.replace(' ', '_').lower() vcap_services = getenv('VCAP_SERVICES') - vcap_service_credentials = None + vcap_service_credentials = {} if vcap_services: services = json_import.loads(vcap_services) @@ -147,7 +158,7 @@ def read_from_vcap_services(service_name): elif vcap_service_credentials.get('apikey'): # rc vcap_service_credentials['auth_type'] = 'iam' else: # no other auth mechanism is supported - vcap_service_credentials = None + vcap_service_credentials = {} return vcap_service_credentials def contruct_authenticator(config): diff --git a/resources/ibm-credentials-iam.env b/resources/ibm-credentials-iam.env index 05c00bb..04b02b9 100644 --- a/resources/ibm-credentials-iam.env +++ b/resources/ibm-credentials-iam.env @@ -1,2 +1,4 @@ WATSON_APIKEY=5678efgh -WATSON_AUTH_TYPE=iam \ No newline at end of file +WATSON_AUTH_TYPE=iam +WATSON_URL=https://gateway-s.watsonplatform.net/watson/api +WATSON_DISABLE_SSL=False \ No newline at end of file diff --git a/test/test_base_service.py b/test/test_base_service.py index eb454d8..8c2b97c 100644 --- a/test/test_base_service.py +++ b/test/test_base_service.py @@ -139,13 +139,9 @@ def test_iam(): file_path = os.path.join( os.path.dirname(__file__), '../resources/ibm-credentials-iam.env') os.environ['IBM_CREDENTIALS_FILE'] = file_path - os.environ['WATSON_URL'] = 'https://gateway-s.watsonplatform.net/watson/api' - os.environ['WATSON_DISABLE_SSL'] = 'False' service = AnyServiceV1('2017-07-07', authenticator=iam_authenticator) assert service.service_url == 'https://gateway-s.watsonplatform.net/watson/api' del os.environ['IBM_CREDENTIALS_FILE'] - del os.environ['WATSON_URL'] - del os.environ['WATSON_DISABLE_SSL'] assert service.authenticator is not None response = { diff --git a/test/test_utils.py b/test/test_utils.py index 5ab36cc..90051e5 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -48,7 +48,7 @@ def test_get_authenticator_from_credential_file(): assert authenticator.bearer_token is not None del os.environ['IBM_CREDENTIALS_FILE'] -def test_get_authenticator_from_env_variabled(): +def test_get_authenticator_from_env_variables(): os.environ['TEST_APIKEY'] = '5678efgh' authenticator = get_authenticator_from_environment('test') assert authenticator is not None From 5c3d545c9b645c72edaebc3e714a542b83188252 Mon Sep 17 00:00:00 2001 From: ehdsouza Date: Mon, 16 Sep 2019 12:22:06 -0700 Subject: [PATCH 02/14] fix(credential): Check for credential file in working dir before home dir --- ibm_cloud_sdk_core/utils.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ibm_cloud_sdk_core/utils.py b/ibm_cloud_sdk_core/utils.py index 83aa7ba..dbe0b4d 100644 --- a/ibm_cloud_sdk_core/utils.py +++ b/ibm_cloud_sdk_core/utils.py @@ -111,16 +111,16 @@ def read_from_credential_file(service_name, separator='='): # File path specified by an env variable credential_file_path = getenv('IBM_CREDENTIALS_FILE') - # Home directory + # Top-level of the project directory if credential_file_path is None: - file_path = join(expanduser('~'), DEFAULT_CREDENTIALS_FILE_NAME) + file_path = join( + dirname(dirname(abspath(__file__))), DEFAULT_CREDENTIALS_FILE_NAME) if isfile(file_path): credential_file_path = file_path - # Top-level of the project directory + # Home directory if credential_file_path is None: - file_path = join( - dirname(dirname(abspath(__file__))), DEFAULT_CREDENTIALS_FILE_NAME) + file_path = join(expanduser('~'), DEFAULT_CREDENTIALS_FILE_NAME) if isfile(file_path): credential_file_path = file_path From 01912cd4ebaa87ff585172937947437b5701a2b5 Mon Sep 17 00:00:00 2001 From: ehdsouza Date: Tue, 17 Sep 2019 12:43:01 -0700 Subject: [PATCH 03/14] chore(replace): service name replace - to _ --- ibm_cloud_sdk_core/base_service.py | 2 +- ibm_cloud_sdk_core/utils.py | 10 +++++----- ibm_cloud_sdk_core/version.py | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ibm_cloud_sdk_core/base_service.py b/ibm_cloud_sdk_core/base_service.py index 3693515..09c9e76 100644 --- a/ibm_cloud_sdk_core/base_service.py +++ b/ibm_cloud_sdk_core/base_service.py @@ -64,7 +64,7 @@ def __init__(self, 'authenticator should be of type Authenticator') if display_name: - service_name = display_name.replace(' ', '_').lower() + service_name = display_name.replace(' ', '_').replace('-', '_').lower() config = read_external_sources(service_name) if config.get('url'): self.service_url = config.get('url') diff --git a/ibm_cloud_sdk_core/utils.py b/ibm_cloud_sdk_core/utils.py index dbe0b4d..a5b062c 100644 --- a/ibm_cloud_sdk_core/utils.py +++ b/ibm_cloud_sdk_core/utils.py @@ -87,14 +87,14 @@ def get_authenticator_from_environment(service_name): authenticator = None config = read_external_sources(service_name) if config: - authenticator = contruct_authenticator(config) + authenticator = _construct_authenticator(config) return authenticator def read_from_env_variables(service_name): """ :return dict config: parsed env variables """ - service_name = service_name.replace(' ', '_').lower() + service_name = service_name.replace(' ', '_').replace('-', '_').lower() config = {} for key, value in environ.items(): _parse_key_and_update_config(config, service_name.lower(), key.lower(), value) @@ -105,7 +105,7 @@ def read_from_credential_file(service_name, separator='='): :param str service_name: The service name :return dict config: parsed key values pairs """ - service_name = service_name.replace(' ', '_').lower() + service_name = service_name.replace(' ', '_').replace('-', '_').lower() DEFAULT_CREDENTIALS_FILE_NAME = 'ibm-credentials.env' # File path specified by an env variable @@ -142,7 +142,7 @@ def _parse_key_and_update_config(config, service_name, key, value): config[key[index + 1:]] = value def read_from_vcap_services(service_name): - service_name = service_name.replace(' ', '_').lower() + service_name = service_name.replace(' ', '_').replace('-', '_').lower() vcap_services = getenv('VCAP_SERVICES') vcap_service_credentials = {} if vcap_services: @@ -161,7 +161,7 @@ def read_from_vcap_services(service_name): vcap_service_credentials = {} return vcap_service_credentials -def contruct_authenticator(config): +def _construct_authenticator(config): auth_type = config.get('auth_type').lower() if config.get('auth_type') else 'iam' authenticator = None from .authenticators import BasicAuthenticator, BearerTokenAuthenticator, CloudPakForDataAuthenticator, IAMAuthenticator, NoAuthAuthenticator diff --git a/ibm_cloud_sdk_core/version.py b/ibm_cloud_sdk_core/version.py index f15ce4a..fc385f5 100644 --- a/ibm_cloud_sdk_core/version.py +++ b/ibm_cloud_sdk_core/version.py @@ -1 +1 @@ -__version__ = '1.0.0-rc5' +__version__ = '1.0.0-rc6' From fe36514509fbef79d8326b9dd511a6c5915703c5 Mon Sep 17 00:00:00 2001 From: ehdsouza Date: Tue, 17 Sep 2019 12:45:22 -0700 Subject: [PATCH 04/14] chore(doc): Update comment --- ibm_cloud_sdk_core/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ibm_cloud_sdk_core/utils.py b/ibm_cloud_sdk_core/utils.py index a5b062c..6ddcc83 100644 --- a/ibm_cloud_sdk_core/utils.py +++ b/ibm_cloud_sdk_core/utils.py @@ -111,7 +111,7 @@ def read_from_credential_file(service_name, separator='='): # File path specified by an env variable credential_file_path = getenv('IBM_CREDENTIALS_FILE') - # Top-level of the project directory + # Current working directory if credential_file_path is None: file_path = join( dirname(dirname(abspath(__file__))), DEFAULT_CREDENTIALS_FILE_NAME) From 50fad59b9a9d834b683ee096497b37019ed8f50f Mon Sep 17 00:00:00 2001 From: ehdsouza Date: Wed, 18 Sep 2019 09:48:12 -0700 Subject: [PATCH 05/14] fix(config): Handle multi word service name --- ibm_cloud_sdk_core/utils.py | 10 +++++----- test/test_base_service.py | 5 +++++ test/test_utils.py | 7 +++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/ibm_cloud_sdk_core/utils.py b/ibm_cloud_sdk_core/utils.py index 6ddcc83..eac2897 100644 --- a/ibm_cloud_sdk_core/utils.py +++ b/ibm_cloud_sdk_core/utils.py @@ -94,10 +94,9 @@ def read_from_env_variables(service_name): """ :return dict config: parsed env variables """ - service_name = service_name.replace(' ', '_').replace('-', '_').lower() config = {} for key, value in environ.items(): - _parse_key_and_update_config(config, service_name.lower(), key.lower(), value) + _parse_key_and_update_config(config, service_name, key, value) return config def read_from_credential_file(service_name, separator='='): @@ -105,7 +104,6 @@ def read_from_credential_file(service_name, separator='='): :param str service_name: The service name :return dict config: parsed key values pairs """ - service_name = service_name.replace(' ', '_').replace('-', '_').lower() DEFAULT_CREDENTIALS_FILE_NAME = 'ibm-credentials.env' # File path specified by an env variable @@ -136,10 +134,12 @@ def read_from_credential_file(service_name, separator='='): return config def _parse_key_and_update_config(config, service_name, key, value): + service_name = service_name.replace(' ', '_').replace('-', '_').lower() + key = key.replace(' ', '_').replace('-', '_').lower() if key.startswith(service_name): - index = key.find('_') + index = key.find(service_name) if index != -1: - config[key[index + 1:]] = value + config[key[index + len(service_name) + 1:]] = value def read_from_vcap_services(service_name): service_name = service_name.replace(' ', '_').replace('-', '_').lower() diff --git a/test/test_base_service.py b/test/test_base_service.py index 8c2b97c..c427649 100644 --- a/test/test_base_service.py +++ b/test/test_base_service.py @@ -422,3 +422,8 @@ def test_service_url_not_set(): with pytest.raises(ValueError) as err: service.prepare_request('POST', url='') assert str(err.value) == 'The service_url is required' + +def test_multi_word_service_name(): + os.environ['personality-insights_url'] = 'xyz' + service = BaseService(service_url='', authenticator=NoAuthAuthenticator(), display_name='personality-insights') + assert service.service_url == 'xyz' diff --git a/test/test_utils.py b/test/test_utils.py index 90051e5..a08f525 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -86,3 +86,10 @@ def test_vcap_credentials(): authenticator = get_authenticator_from_environment('test') assert authenticator is None del os.environ['VCAP_SERVICES'] + +def test_multi_word_service_name(): + os.environ['PERSONALITY-INSIGHTS_APIKEY'] = '5678efgh' + authenticator = get_authenticator_from_environment('personality-insights') + assert authenticator is not None + assert authenticator.token_manager.apikey == '5678efgh' + del os.environ['PERSONALITY-INSIGHTS_APIKEY'] From 414803434c6dbe370cd09eee5f4fb3d4220761e6 Mon Sep 17 00:00:00 2001 From: ehdsouza Date: Wed, 18 Sep 2019 13:50:02 -0700 Subject: [PATCH 06/14] fix(client props): Setting of the external client config happens in individual services --- ibm_cloud_sdk_core/base_service.py | 9 --------- test/test_base_service.py | 9 ++------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/ibm_cloud_sdk_core/base_service.py b/ibm_cloud_sdk_core/base_service.py index 09c9e76..1004827 100644 --- a/ibm_cloud_sdk_core/base_service.py +++ b/ibm_cloud_sdk_core/base_service.py @@ -63,15 +63,6 @@ def __init__(self, raise ValueError( 'authenticator should be of type Authenticator') - if display_name: - service_name = display_name.replace(' ', '_').replace('-', '_').lower() - config = read_external_sources(service_name) - if config.get('url'): - self.service_url = config.get('url') - if config.get('disable_ssl'): - self.disable_ssl_verification = config.get('disable_ssl') - - def _get_system_info(self): return '{0} {1} {2}'.format( platform.system(), # OS diff --git a/test/test_base_service.py b/test/test_base_service.py index c427649..97c6567 100644 --- a/test/test_base_service.py +++ b/test/test_base_service.py @@ -140,7 +140,7 @@ def test_iam(): os.path.dirname(__file__), '../resources/ibm-credentials-iam.env') os.environ['IBM_CREDENTIALS_FILE'] = file_path service = AnyServiceV1('2017-07-07', authenticator=iam_authenticator) - assert service.service_url == 'https://gateway-s.watsonplatform.net/watson/api' + assert service.service_url == 'https://gateway.watsonplatform.net/test/api' del os.environ['IBM_CREDENTIALS_FILE'] assert service.authenticator is not None @@ -158,7 +158,7 @@ def test_iam(): status=200) responses.add( responses.GET, - url='https://gateway-s.watsonplatform.net/watson/api', + url='https://gateway.watsonplatform.net/test/api', body=json.dumps({ "foobar": "baz" }), @@ -422,8 +422,3 @@ def test_service_url_not_set(): with pytest.raises(ValueError) as err: service.prepare_request('POST', url='') assert str(err.value) == 'The service_url is required' - -def test_multi_word_service_name(): - os.environ['personality-insights_url'] = 'xyz' - service = BaseService(service_url='', authenticator=NoAuthAuthenticator(), display_name='personality-insights') - assert service.service_url == 'xyz' From 7981d92cfc73d224e1de225bab92dd9f71032fa4 Mon Sep 17 00:00:00 2001 From: ehdsouza Date: Wed, 18 Sep 2019 14:07:08 -0700 Subject: [PATCH 07/14] chore(service_url): Make service url as None --- ibm_cloud_sdk_core/base_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ibm_cloud_sdk_core/base_service.py b/ibm_cloud_sdk_core/base_service.py index 1004827..28a6c89 100644 --- a/ibm_cloud_sdk_core/base_service.py +++ b/ibm_cloud_sdk_core/base_service.py @@ -37,7 +37,7 @@ class BaseService(object): SDK_NAME = 'ibm-python-sdk-core' def __init__(self, - service_url, + service_url=None, authenticator=None, disable_ssl_verification=False, display_name=None): From 41a3a51687878f51103fc45960665e899484a179 Mon Sep 17 00:00:00 2001 From: ehdsouza Date: Wed, 18 Sep 2019 14:13:28 -0700 Subject: [PATCH 08/14] test(service name): Mullti word service name --- resources/ibm-credentials-iam.env | 8 ++++---- test/test_base_service.py | 7 ++++--- test/test_utils.py | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/resources/ibm-credentials-iam.env b/resources/ibm-credentials-iam.env index 04b02b9..a46c2cf 100644 --- a/resources/ibm-credentials-iam.env +++ b/resources/ibm-credentials-iam.env @@ -1,4 +1,4 @@ -WATSON_APIKEY=5678efgh -WATSON_AUTH_TYPE=iam -WATSON_URL=https://gateway-s.watsonplatform.net/watson/api -WATSON_DISABLE_SSL=False \ No newline at end of file +IBM-WATSON_APIKEY=5678efgh +IBM-WATSON_AUTH_TYPE=iam +IBM-WATSON_URL=https://gateway-s.watsonplatform.net/watson/api +IBM-WATSON_DISABLE_SSL=False \ No newline at end of file diff --git a/test/test_base_service.py b/test/test_base_service.py index 97c6567..cf9a42b 100644 --- a/test/test_base_service.py +++ b/test/test_base_service.py @@ -9,6 +9,7 @@ from ibm_cloud_sdk_core import ApiException from ibm_cloud_sdk_core import CP4DTokenManager from ibm_cloud_sdk_core.authenticators import IAMAuthenticator, NoAuthAuthenticator, Authenticator, BasicAuthenticator, CloudPakForDataAuthenticator +from ibm_cloud_sdk_core import get_authenticator_from_environment class AnyServiceV1(BaseService): @@ -24,7 +25,7 @@ def __init__(self, service_url=service_url, authenticator=authenticator, disable_ssl_verification=disable_ssl_verification, - display_name='Watson') + display_name='ibm Watson') self.version = version def op_with_path_params(self, path0, path1): @@ -135,10 +136,10 @@ def test_fail_http_config(): @responses.activate def test_iam(): - iam_authenticator = IAMAuthenticator('my_apikey', 'https://iam-test.cloud.ibm.com/identity/token') file_path = os.path.join( os.path.dirname(__file__), '../resources/ibm-credentials-iam.env') os.environ['IBM_CREDENTIALS_FILE'] = file_path + iam_authenticator = get_authenticator_from_environment('ibm-watson') service = AnyServiceV1('2017-07-07', authenticator=iam_authenticator) assert service.service_url == 'https://gateway.watsonplatform.net/test/api' del os.environ['IBM_CREDENTIALS_FILE'] @@ -153,7 +154,7 @@ def test_iam(): } responses.add( responses.POST, - url='https://iam-test.cloud.ibm.com/identity/token', + url='https://iam.cloud.ibm.com/identity/token', body=json.dumps(response), status=200) responses.add( diff --git a/test/test_utils.py b/test/test_utils.py index a08f525..e2bbfed 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -11,7 +11,7 @@ def test_get_authenticator_from_credential_file(): file_path = os.path.join( os.path.dirname(__file__), '../resources/ibm-credentials-iam.env') os.environ['IBM_CREDENTIALS_FILE'] = file_path - authenticator = get_authenticator_from_environment('watson') + authenticator = get_authenticator_from_environment('ibm watson') assert authenticator is not None assert authenticator.token_manager.apikey == '5678efgh' del os.environ['IBM_CREDENTIALS_FILE'] From d21243c64e757f616bd52dbec2b5458d03b0e713 Mon Sep 17 00:00:00 2001 From: Erika Dsouza Date: Wed, 18 Sep 2019 14:16:19 -0700 Subject: [PATCH 09/14] Update version.py --- ibm_cloud_sdk_core/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ibm_cloud_sdk_core/version.py b/ibm_cloud_sdk_core/version.py index fc385f5..f15ce4a 100644 --- a/ibm_cloud_sdk_core/version.py +++ b/ibm_cloud_sdk_core/version.py @@ -1 +1 @@ -__version__ = '1.0.0-rc6' +__version__ = '1.0.0-rc5' From a3b5305d423f67b22375585fa1312c13474c76d8 Mon Sep 17 00:00:00 2001 From: ehdsouza Date: Wed, 18 Sep 2019 14:38:15 -0700 Subject: [PATCH 10/14] chore(external_sources): Export externall sources --- ibm_cloud_sdk_core/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ibm_cloud_sdk_core/__init__.py b/ibm_cloud_sdk_core/__init__.py index 4030265..71ea82b 100644 --- a/ibm_cloud_sdk_core/__init__.py +++ b/ibm_cloud_sdk_core/__init__.py @@ -19,4 +19,4 @@ from .jwt_token_manager import JWTTokenManager from .cp4d_token_manager import CP4DTokenManager from .api_exception import ApiException -from .utils import datetime_to_string, string_to_datetime, get_authenticator_from_environment +from .utils import datetime_to_string, string_to_datetime, get_authenticator_from_environment, read_external_sources From b7b372370cd710890c34f2d1067cf6c5a10fe0ef Mon Sep 17 00:00:00 2001 From: ehdsouza Date: Wed, 18 Sep 2019 15:00:34 -0700 Subject: [PATCH 11/14] chore(index): remove additional index checking --- ibm_cloud_sdk_core/utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ibm_cloud_sdk_core/utils.py b/ibm_cloud_sdk_core/utils.py index eac2897..4a1070d 100644 --- a/ibm_cloud_sdk_core/utils.py +++ b/ibm_cloud_sdk_core/utils.py @@ -137,9 +137,7 @@ def _parse_key_and_update_config(config, service_name, key, value): service_name = service_name.replace(' ', '_').replace('-', '_').lower() key = key.replace(' ', '_').replace('-', '_').lower() if key.startswith(service_name): - index = key.find(service_name) - if index != -1: - config[key[index + len(service_name) + 1:]] = value + config[key[len(service_name) + 1:]] = value def read_from_vcap_services(service_name): service_name = service_name.replace(' ', '_').replace('-', '_').lower() From c4ba31d44c01260d9f0f7392ac5c8fe7a6348567 Mon Sep 17 00:00:00 2001 From: ehdsouza Date: Wed, 18 Sep 2019 15:38:29 -0700 Subject: [PATCH 12/14] chore(key): can't replace key but only convert to lower --- ibm_cloud_sdk_core/utils.py | 2 +- resources/ibm-credentials-iam.env | 8 ++++---- test/test_utils.py | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ibm_cloud_sdk_core/utils.py b/ibm_cloud_sdk_core/utils.py index 4a1070d..0d17316 100644 --- a/ibm_cloud_sdk_core/utils.py +++ b/ibm_cloud_sdk_core/utils.py @@ -135,7 +135,7 @@ def read_from_credential_file(service_name, separator='='): def _parse_key_and_update_config(config, service_name, key, value): service_name = service_name.replace(' ', '_').replace('-', '_').lower() - key = key.replace(' ', '_').replace('-', '_').lower() + key = key.lower() if key.startswith(service_name): config[key[len(service_name) + 1:]] = value diff --git a/resources/ibm-credentials-iam.env b/resources/ibm-credentials-iam.env index a46c2cf..337c06e 100644 --- a/resources/ibm-credentials-iam.env +++ b/resources/ibm-credentials-iam.env @@ -1,4 +1,4 @@ -IBM-WATSON_APIKEY=5678efgh -IBM-WATSON_AUTH_TYPE=iam -IBM-WATSON_URL=https://gateway-s.watsonplatform.net/watson/api -IBM-WATSON_DISABLE_SSL=False \ No newline at end of file +IBM_WATSON_APIKEY=5678efgh +IBM_WATSON_AUTH_TYPE=iam +IBM_WATSON_URL=https://gateway-s.watsonplatform.net/watson/api +IBM_WATSON_DISABLE_SSL=False \ No newline at end of file diff --git a/test/test_utils.py b/test/test_utils.py index e2bbfed..2feafd8 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -88,8 +88,8 @@ def test_vcap_credentials(): del os.environ['VCAP_SERVICES'] def test_multi_word_service_name(): - os.environ['PERSONALITY-INSIGHTS_APIKEY'] = '5678efgh' + os.environ['PERSONALITY_INSIGHTS_APIKEY'] = '5678efgh' authenticator = get_authenticator_from_environment('personality-insights') assert authenticator is not None assert authenticator.token_manager.apikey == '5678efgh' - del os.environ['PERSONALITY-INSIGHTS_APIKEY'] + del os.environ['PERSONALITY_INSIGHTS_APIKEY'] From 4742b8e47e226143253e7ffdb9e760a003719d8f Mon Sep 17 00:00:00 2001 From: ehdsouza Date: Wed, 18 Sep 2019 16:16:38 -0700 Subject: [PATCH 13/14] fix(displlay_name): display_name unused in constructor --- ibm_cloud_sdk_core/base_service.py | 6 ++---- test/test_base_service.py | 5 ++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/ibm_cloud_sdk_core/base_service.py b/ibm_cloud_sdk_core/base_service.py index 28a6c89..dcac532 100644 --- a/ibm_cloud_sdk_core/base_service.py +++ b/ibm_cloud_sdk_core/base_service.py @@ -39,13 +39,11 @@ class BaseService(object): def __init__(self, service_url=None, authenticator=None, - disable_ssl_verification=False, - display_name=None): + disable_ssl_verification=False): """ - :attr str url: The url for service api calls + :attr str service_url: The url for service api calls :attr Authenticator authenticator: The authenticator for authentication :attr bool disable_ssl_verification: enables/ disables ssl verification - :attr str display_name the name used for mapping services in environment file """ self.service_url = service_url self.http_config = {} diff --git a/test/test_base_service.py b/test/test_base_service.py index cf9a42b..757058d 100644 --- a/test/test_base_service.py +++ b/test/test_base_service.py @@ -24,8 +24,7 @@ def __init__(self, self, service_url=service_url, authenticator=authenticator, - disable_ssl_verification=disable_ssl_verification, - display_name='ibm Watson') + disable_ssl_verification=disable_ssl_verification) self.version = version def op_with_path_params(self, path0, path1): @@ -419,7 +418,7 @@ def test_json(): assert req.get('data') == "{\"hello\": \"world\"}" def test_service_url_not_set(): - service = BaseService(service_url='', authenticator=NoAuthAuthenticator(), display_name='Watson') + service = BaseService(service_url='', authenticator=NoAuthAuthenticator()) with pytest.raises(ValueError) as err: service.prepare_request('POST', url='') assert str(err.value) == 'The service_url is required' From 164d02e76f4745ea8503d6501422e5ea48804367 Mon Sep 17 00:00:00 2001 From: ehdsouza Date: Wed, 18 Sep 2019 16:51:09 -0700 Subject: [PATCH 14/14] chore(upper): Use upper() instead of lower() --- ibm_cloud_sdk_core/utils.py | 44 ++++++++++++++++++------------------- test/test_utils.py | 2 +- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/ibm_cloud_sdk_core/utils.py b/ibm_cloud_sdk_core/utils.py index 0d17316..af247d8 100644 --- a/ibm_cloud_sdk_core/utils.py +++ b/ibm_cloud_sdk_core/utils.py @@ -128,62 +128,62 @@ def read_from_credential_file(service_name, separator='='): for line in fp: key_val = line.strip().split(separator) if len(key_val) == 2: - key = key_val[0].lower() + key = key_val[0] value = key_val[1] _parse_key_and_update_config(config, service_name, key, value) return config def _parse_key_and_update_config(config, service_name, key, value): - service_name = service_name.replace(' ', '_').replace('-', '_').lower() - key = key.lower() + service_name = service_name.replace(' ', '_').replace('-', '_').upper() if key.startswith(service_name): config[key[len(service_name) + 1:]] = value def read_from_vcap_services(service_name): - service_name = service_name.replace(' ', '_').replace('-', '_').lower() vcap_services = getenv('VCAP_SERVICES') vcap_service_credentials = {} if vcap_services: services = json_import.loads(vcap_services) for key in services.keys(): - name = key.replace('-', '_') - if name == service_name: + if key == service_name: vcap_service_credentials = services[key][0]['credentials'] if vcap_service_credentials is not None and isinstance(vcap_service_credentials, dict): if vcap_service_credentials.get('username') and vcap_service_credentials.get('password'): # cf - vcap_service_credentials['auth_type'] = 'basic' + vcap_service_credentials['AUTH_TYPE'] = 'basic' + vcap_service_credentials['USERNAME'] = vcap_service_credentials.get('username') + vcap_service_credentials['PASSWORD'] = vcap_service_credentials.get('password') elif vcap_service_credentials.get('apikey'): # rc - vcap_service_credentials['auth_type'] = 'iam' + vcap_service_credentials['AUTH_TYPE'] = 'iam' + vcap_service_credentials['APIKEY'] = vcap_service_credentials.get('apikey') else: # no other auth mechanism is supported vcap_service_credentials = {} return vcap_service_credentials def _construct_authenticator(config): - auth_type = config.get('auth_type').lower() if config.get('auth_type') else 'iam' + auth_type = config.get('AUTH_TYPE').lower() if config.get('AUTH_TYPE') else 'iam' authenticator = None from .authenticators import BasicAuthenticator, BearerTokenAuthenticator, CloudPakForDataAuthenticator, IAMAuthenticator, NoAuthAuthenticator if auth_type == 'basic': authenticator = BasicAuthenticator( - username=config.get('username'), - password=config.get('password')) + username=config.get('USERNAME'), + password=config.get('PASSWORD')) elif auth_type == 'bearertoken': authenticator = BearerTokenAuthenticator( - bearer_token=config.get('bearer_token')) + bearer_token=config.get('BEARER_TOKEN')) elif auth_type == 'cp4d': authenticator = CloudPakForDataAuthenticator( - username=config.get('username'), - password=config.get('password'), - url=config.get('auth_url'), - disable_ssl_verification=config.get('auth_disable_ssl')) - elif auth_type == 'iam' and config.get('apikey'): + username=config.get('USERNAME'), + password=config.get('PASSWORD'), + url=config.get('AUTH_URL'), + disable_ssl_verification=config.get('AUTH_DISABLE_SSL')) + elif auth_type == 'iam' and config.get('APIKEY'): authenticator = IAMAuthenticator( - apikey=config.get('apikey'), - url=config.get('auth_url'), - client_id=config.get('client_id'), - client_secret=config.get('client_secret'), - disable_ssl_verification=config.get('auth_disable_ssl')) + apikey=config.get('APIKEY'), + url=config.get('AUTH_URL'), + client_id=config.get('CLIENT_ID'), + client_secret=config.get('CLIENT_SECRET'), + disable_ssl_verification=config.get('AUTH_DISABLE_SSL')) elif auth_type == 'noauth': authenticator = NoAuthAuthenticator() diff --git a/test/test_utils.py b/test/test_utils.py index 2feafd8..89aa7ad 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -9,7 +9,7 @@ def test_datetime_conversion(): def test_get_authenticator_from_credential_file(): file_path = os.path.join( - os.path.dirname(__file__), '../resources/ibm-credentials-iam.env') + os.path.dirname(__file__), '../resources/ibm-credentials-iam.env') os.environ['IBM_CREDENTIALS_FILE'] = file_path authenticator = get_authenticator_from_environment('ibm watson') assert authenticator is not None