@@ -115,46 +115,74 @@ bluemix <- setRefClass("bluemix",
115
115
)
116
116
117
117
# ' CloudObjectStorage is a class that is designed for IBM cloud object storage (COS)
118
- # ' It sets up the hadoop config for COS and provide the final file url.
118
+ # ' It sets up the hadoop config for COS and provide the final file url. It also supports
119
+ # connecting to an IBM COS instance that is being hosted on bluemix.
119
120
# '
120
121
# ' sparkContext: a SparkContext object.
121
122
# ''
122
- # ' credentials: a dictionary with the following required keys:
123
- # ' endpoint
124
- # ' accessKey
125
- # ' secretKey
123
+ # ' credentials: a dictionary with the required keys to connect to an IBM cloud object storage.
124
+ # ' The required keys differ according to the type of COS.
125
+ # ' - for COS type "softlayer_cos" the following keys are required:
126
+ # ' endpoint [required]
127
+ # ' accessKey [required]
128
+ # ' secretKey [required]
129
+ # ' - for COS type "bluemix_cos", here are the required/optional key:
130
+ # ' endPoint [required]
131
+ # ' serviceId [required]
132
+ # ' apiKey OR iamToken depends on the selected authorization method (authMethod) [required]
133
+ # ' iamServiceEndpoint [optional] (default: https://iam.ng.bluemix.net/oidc/token)
134
+ # ' v2SignerType [optional]
135
+ # '
136
+ # ' configurationName: string identifies the configurations to be set.
137
+ # '
126
138
# '
127
- # ' configurationName: string identifies the configurations that has been
128
- # ' set.
139
+ # ' cosType [optional]: string that identifies the type of COS to connect to. The supported types of COS
140
+ # ' are "softlayer_cos" and "bluemix_cos". "softlayer_cos" will be chosen as default if no cosType is passed.
141
+ # '
142
+ # ' authMethod [optional]: string that identifies the type of authorization method to use when connecting to COS. This parameter
143
+ # ' is not reqired for softlayer_cos but only needed for bluemix_cos. Two options can be chosen for this params
144
+ # ' "api_key" or "iam_token". "api_key" will be chosen as default if the value is not set.
129
145
# ' @export CloudObjectStorage
130
146
# ' @exportClass CloudObjectStorage
131
147
CloudObjectStorage <- setRefClass(" CloudObjectStorage" ,
132
- fields = list (configName = " character" ),
148
+ fields = list (configName = " character" , cosType = " character " , authMethod = " character " ),
133
149
methods = list (
134
- initialize = function (... , sparkContext , credentials , configurationName = " " ) {
135
-
150
+ initialize = function (... , sparkContext , credentials , configurationName = " " ,
151
+ cosType = " softlayer_cos " , authMethod = " api_key " ) {
136
152
137
- if (is.null(credentials [" endpoint" ][[1 ]])) {
138
- stop(" Attribute endpoint in credentials is missing!" )
139
- }
140
-
141
- if (is.null(credentials [" accessKey" ][[1 ]])) {
142
- stop(" Attribute accessKey in credentials is missing!" )
143
- }
144
-
145
- if (is.null(credentials [" secretKey" ][[1 ]])) {
146
- stop(" Attribute secretKey in credentials is missing!" )
147
- }
153
+ # validate input
154
+ validateInput(credentials , cosType , authMethod )
148
155
149
156
# bind config name
150
157
.self $ configName = configurationName
151
158
152
- # set prefix for hadoop config
159
+ # set up hadoop config
153
160
prefix = paste(" fs.cos" , getConfigName(), sep = ' .' )
154
161
hConf = SparkR ::: callJMethod(sparkContext , " hadoopConfiguration" )
162
+
155
163
SparkR ::: callJMethod(hConf , " set" , paste(prefix , " endpoint" , sep = ' .' ), credentials [' endpoint' ][[1 ]])
156
- SparkR ::: callJMethod(hConf , " set" , paste(prefix , " access.key" , sep = ' .' ), credentials [' accessKey' ][[1 ]])
157
- SparkR ::: callJMethod(hConf , " set" , paste(prefix , " secret.key" , sep = ' .' ), credentials [' secretKey' ][[1 ]])
164
+
165
+ if (cosType == " softlayer_cos" ) {
166
+ # softlayer COS case
167
+ SparkR ::: callJMethod(hConf , " set" , paste(prefix , " access.key" , sep = ' .' ), credentials [' accessKey' ][[1 ]])
168
+ SparkR ::: callJMethod(hConf , " set" , paste(prefix , " secret.key" , sep = ' .' ), credentials [' secretKey' ][[1 ]])
169
+ } else if (cosType == " bluemix_cos" ) {
170
+ # bluemix COS case
171
+ SparkR ::: callJMethod(hConf , " set" , paste(prefix , " iam.service.id" , sep = ' .' ), credentials [' serviceId' ][[1 ]])
172
+ if (authMethod == " api_key" ) {
173
+ SparkR ::: callJMethod(hConf , " set" , paste(prefix , " iam.api.key" , sep = ' .' ), credentials [' apiKey' ][[1 ]])
174
+ } else if (authMethod == " iam_token" ) {
175
+ SparkR ::: callJMethod(hConf , " set" , paste(prefix , " iam.token" , sep = ' .' ), credentials [' iamToken' ][[1 ]])
176
+ }
177
+
178
+ if (" iamServiceEndpoint" %in% names(credentials )) {
179
+ SparkR ::: callJMethod(hConf , " set" , paste(prefix , " iam.endpoint" , sep = ' .' ), credentials [' iamServiceEndpoint' ][[1 ]])
180
+ }
181
+
182
+ if (" v2SignerType" %in% names(credentials )) {
183
+ SparkR ::: callJMethod(hConf , " set" , paste(prefix , " v2.signer.type" , sep = ' .' ), credentials [' v2SignerType' ][[1 ]])
184
+ }
185
+ }
158
186
},
159
187
160
188
getConfigName = function () {
@@ -164,6 +192,37 @@ CloudObjectStorage <- setRefClass("CloudObjectStorage",
164
192
return (" service" )
165
193
},
166
194
195
+ validateInput = function (credentials , cosType , authMethod ) {
196
+ requiredKeys = get_required_key_array(cosType , authMethod )
197
+
198
+ # check the existence of all required values in credentials
199
+ for (key in requiredKeys ) {
200
+ if (! key %in% names(credentials )) {
201
+ stop(paste(" Invalid input: missing required input [" , key , " ]!" , sep = ' ' ))
202
+ }
203
+ }
204
+ },
205
+
206
+ get_required_key_array = function (cosType , authMethod ) {
207
+ requiredKeySoftlayerCos = list (" endpoint" , " accessKey" , " secretKey" )
208
+ requiredKeyListIamApiKey = list (" endpoint" , " apiKey" , " serviceId" )
209
+ requiredKeyListIamToken = list (" endpoint" , " iamToken" , " serviceId" )
210
+
211
+ if (cosType == " bluemix_cos" ) {
212
+ if (authMethod == " api_key" ) {
213
+ return (requiredKeyListIamApiKey )
214
+ } else if (authMethod == " iam_token" ) {
215
+ return (requiredKeyListIamToken )
216
+ } else {
217
+ stop(" Invalid input: authMethod. authMethod is optional but if set, it should have one of the following values: api_key, iam_token" )
218
+ }
219
+ } else if (cosType == " softlayer_cos" ) {
220
+ return (requiredKeySoftlayerCos )
221
+ } else {
222
+ stop(" Invalid input: cosType. cosType is optional but if set, it should have one of the following values: softlayer_cos, bluemix_cos" )
223
+ }
224
+ },
225
+
167
226
url = function (bucketName , objectName ) {
168
227
serviceName = getConfigName()
169
228
return (paste(" cos://" , bucketName , " ." , serviceName , " /" , objectName , sep = " " ))
0 commit comments