Skip to content

Commit 8fee5bf

Browse files
committed
feat(base): Add method read_external_sources
1 parent 7ecc329 commit 8fee5bf

File tree

5 files changed

+35
-26
lines changed

5 files changed

+35
-26
lines changed

ibm_cloud_sdk_core/base_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import requests
2222
from requests.structures import CaseInsensitiveDict
2323
from .version import __version__
24-
from .utils import has_bad_first_or_last_char, remove_null_values, cleanup_values, read_from_env_variables
24+
from .utils import has_bad_first_or_last_char, remove_null_values, cleanup_values, read_external_sources
2525
from .detailed_response import DetailedResponse
2626
from .api_exception import ApiException
2727
from .authenticators import Authenticator
@@ -65,7 +65,7 @@ def __init__(self,
6565

6666
if display_name:
6767
service_name = display_name.replace(' ', '_').lower()
68-
config = read_from_env_variables(service_name)
68+
config = read_external_sources(service_name)
6969
if config.get('url'):
7070
self.service_url = config.get('url')
7171
if config.get('disable_ssl'):

ibm_cloud_sdk_core/utils.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,40 @@ def string_to_datetime(string):
5454
"""
5555
return date_parser.parse(string)
5656

57-
def get_authenticator_from_environment(service_name):
57+
def read_external_sources(service_name):
5858
"""
59-
Checks the credentials file and VCAP_SERVICES environment variable
59+
Try to get config from external sources, with the following priority:
60+
1. Credentials file(ibm-credentials.env)
61+
2. Environment variables
62+
3. VCAP Services(Cloud Foundry)
6063
:param service_name: The service name
61-
:return: the authenticator
64+
:return: dict
6265
"""
63-
authenticator = None
64-
# 1. Credentials from credential file
66+
config = {}
67+
6568
config = read_from_credential_file(service_name)
66-
if config:
67-
authenticator = contruct_authenticator(config)
6869

69-
# 2. From env variables
70-
if not authenticator:
70+
if not config:
7171
config = read_from_env_variables(service_name)
72-
if config:
73-
authenticator = contruct_authenticator(config)
7472

75-
# 3. Credentials from VCAP
76-
if not authenticator:
73+
if not config:
7774
config = read_from_vcap_services(service_name)
78-
if config:
79-
authenticator = contruct_authenticator(config)
75+
76+
return config
77+
78+
def get_authenticator_from_environment(service_name):
79+
"""
80+
Try to get authenticator from external sources, with the following priority:
81+
1. Credentials file(ibm-credentials.env)
82+
2. Environment variables
83+
3. VCAP Services(Cloud Foundry)
84+
:param service_name: The service name
85+
:return: the authenticator
86+
"""
87+
authenticator = None
88+
config = read_external_sources(service_name)
89+
if config:
90+
authenticator = contruct_authenticator(config)
8091
return authenticator
8192

8293
def read_from_env_variables(service_name):
@@ -125,15 +136,15 @@ def read_from_credential_file(service_name, separator='='):
125136
return config
126137

127138
def _parse_key_and_update_config(config, service_name, key, value):
128-
if service_name in key:
139+
if key.startswith(service_name):
129140
index = key.find('_')
130141
if index != -1:
131142
config[key[index + 1:]] = value
132143

133144
def read_from_vcap_services(service_name):
134145
service_name = service_name.replace(' ', '_').lower()
135146
vcap_services = getenv('VCAP_SERVICES')
136-
vcap_service_credentials = None
147+
vcap_service_credentials = {}
137148
if vcap_services:
138149
services = json_import.loads(vcap_services)
139150

@@ -147,7 +158,7 @@ def read_from_vcap_services(service_name):
147158
elif vcap_service_credentials.get('apikey'): # rc
148159
vcap_service_credentials['auth_type'] = 'iam'
149160
else: # no other auth mechanism is supported
150-
vcap_service_credentials = None
161+
vcap_service_credentials = {}
151162
return vcap_service_credentials
152163

153164
def contruct_authenticator(config):

resources/ibm-credentials-iam.env

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
WATSON_APIKEY=5678efgh
2-
WATSON_AUTH_TYPE=iam
2+
WATSON_AUTH_TYPE=iam
3+
WATSON_URL=https://gateway-s.watsonplatform.net/watson/api
4+
WATSON_DISABLE_SSL=False

test/test_base_service.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,9 @@ def test_iam():
139139
file_path = os.path.join(
140140
os.path.dirname(__file__), '../resources/ibm-credentials-iam.env')
141141
os.environ['IBM_CREDENTIALS_FILE'] = file_path
142-
os.environ['WATSON_URL'] = 'https://gateway-s.watsonplatform.net/watson/api'
143-
os.environ['WATSON_DISABLE_SSL'] = 'False'
144142
service = AnyServiceV1('2017-07-07', authenticator=iam_authenticator)
145143
assert service.service_url == 'https://gateway-s.watsonplatform.net/watson/api'
146144
del os.environ['IBM_CREDENTIALS_FILE']
147-
del os.environ['WATSON_URL']
148-
del os.environ['WATSON_DISABLE_SSL']
149145
assert service.authenticator is not None
150146

151147
response = {

test/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_get_authenticator_from_credential_file():
4848
assert authenticator.bearer_token is not None
4949
del os.environ['IBM_CREDENTIALS_FILE']
5050

51-
def test_get_authenticator_from_env_variabled():
51+
def test_get_authenticator_from_env_variables():
5252
os.environ['TEST_APIKEY'] = '5678efgh'
5353
authenticator = get_authenticator_from_environment('test')
5454
assert authenticator is not None

0 commit comments

Comments
 (0)