Skip to content

Commit ca8e0cc

Browse files
committed
Add new params for cos bluemix + write logic to validate new param and
required credentials
1 parent 3f7b519 commit ca8e0cc

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

python/ibmos2spark/osconfig.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def url(self, container_name, object_name):
170170

171171
class CloudObjectStorage(object):
172172

173-
def __init__(self, sparkcontext, credentials, configuration_name='', bucket_name=''):
173+
def __init__(self, sparkcontext, credentials, configuration_name='', cos_type='classic_cos', auth_method='api_key', bucket_name=''):
174174

175175
'''
176176
sparkcontext: a SparkContext object.
@@ -191,10 +191,12 @@ def __init__(self, sparkcontext, credentials, configuration_name='', bucket_name
191191
you use the url function.
192192
193193
'''
194+
# check if all required values are availble
195+
_validate_input(credentials, cos_type, auth_method)
196+
194197
self.bucket_name = bucket_name
195198
self.conf_name = configuration_name
196199

197-
# check if all required values are availble
198200
credential_key_list = ["endpoint", "access_key", "secret_key"]
199201

200202
for i in range(len(credential_key_list)):
@@ -215,6 +217,34 @@ def __init__(self, sparkcontext, credentials, configuration_name='', bucket_name
215217
hconf.set(prefix + ".access.key", credentials['access_key'])
216218
hconf.set(prefix + ".secret.key", credentials['secret_key'])
217219

220+
def _validate_input(self, credentials, cos_type, auth_method):
221+
required_key_classic_cos = ["endpoint", "access_key", "secret_key"]
222+
required_key_list_iam_api_key = ["endpoint", "api_key", "service_id"]
223+
required_key_list_iam_token = ["endpoint", "token", "service_id"]
224+
225+
def _get_required_keys(cos_type, auth_method):
226+
if (cos_type == "bluemix_cos"):
227+
if (auth_method == "api_key"):
228+
return required_key_list_iam_api_key
229+
elif (auth_method == "iam_token")
230+
return required_key_list_iam_token
231+
else:
232+
raise ValueError("Invalid input: auth_method. auth_method is optional but if set, it should have one of the following values: api_key, iam_token")
233+
elif (cos_type == "classic_cos"):
234+
return required_key_classic_cos
235+
else:
236+
raise ValueError("Invalid input: cos_type. cos_type is optional but if set, it should have one of the following values: classic_cos, bluemix_cos")
237+
238+
# check keys
239+
required_key_list = _get_required_keys()
240+
241+
for i in range(len(required_key_list)):
242+
key = required_key_list[i]
243+
if (key not in credentials):
244+
raise ValueError("Invalid input: credentials. {} is required!".format(key))
245+
246+
return True
247+
218248
def url(self, object_name, bucket_name=''):
219249
bucket_name_var = ''
220250
service_name = DEFAULT_SERVICE_NAME

0 commit comments

Comments
 (0)