@@ -129,54 +129,114 @@ class bluemix(sc: SparkContext, name: String, creds: HashMap[String, String],
129
129
}
130
130
131
131
/**
132
- * CloudObjectStorage class sets up a s3d connection between an IBM Spark service
133
- * instance and an IBM Cloud Object Storage instance.
132
+ * This class allows you to connect to an IBM cloud object storage (COS) instance. It also support
133
+ connecting to a COS instance that is being hosted on bluemix .
134
134
135
135
* Constructor arguments:
136
136
137
137
* sparkcontext: a SparkContext object.
138
138
139
- * credentials: a dictionary with the following required keys:
140
- *
141
- * endpoint
142
-
143
- * accessKey
144
-
145
- * secretKey
139
+ * credentials: a dictionary with the following required keys to connect to COS.
140
+ The required keys differ according to the type of COS.
141
+ * - for COS type "softlayer_cos" the following key are required:
142
+ * endPoint [required]
143
+ * accessKey [required]
144
+ * secretKey [required]
145
+ * - for COS type "bluemix_cos", here are the required/optional key:
146
+ * endPoint [required]
147
+ * serviceId [required]
148
+ * apiKey OR iamToken depends on the selected authorization method (authMethod) [required]
149
+ * iamServiceEndpoint [optional] (default: https://iam.ng.bluemix.net/oidc/token)
150
+ * v2SignerType [optional]
146
151
147
152
* configurationName [optional]: string that identifies this configuration. You can
148
153
use any string you like. This allows you to create
149
154
multiple configurations to different Object Storage accounts.
150
155
if a configuration name is not passed the default one will be used "service".
156
+
157
+ * cosType [optional]: string that identifies the type of COS to connect to. The supported types of COS
158
+ are "softlayer_cos" and "bluemix_cos". "softlayer_cos" will be chosen as default if no cosType is passed.
159
+
160
+ * authMethod [optional]: string that identifies the type of authorization method to use when connecting to COS. This parameter
161
+ is not reqired for softlayer_cos but only needed for bluemix_cos. Two options can be chosen for this params
162
+ "api_key" or "iam_token". "api_key" will be chosen as default if the value is not set.
151
163
*/
152
- class CloudObjectStorage (sc : SparkContext , credentials : HashMap [String , String ], configurationName : String = " " ) {
164
+ class CloudObjectStorage (sc : SparkContext , credentials : HashMap [String , String ],
165
+ configurationName : String = " " , cosType : String = " softlayer_cos" ,
166
+ authMethod : String = " api_key" ) {
153
167
154
- // check if all credentials are available
155
- val requiredValues = Array (" endPoint" , " accessKey" , " secretKey" )
156
- for ( key <- requiredValues ) {
157
- if (! credentials.contains(key)) {
158
- throw new IllegalArgumentException (" Invalid input: missing required input [" + key + " ]" )
159
- }
160
- }
168
+ // check for valid credentials
169
+ _validate_credentials(credentials, cosType, authMethod)
161
170
162
171
// set config
163
172
val hadoopConf = sc.hadoopConfiguration
164
- val prefix = " fs.cos." + getConfigurationName ()
173
+ val prefix = " fs.cos." + _getConfigurationName ()
165
174
166
175
hadoopConf.set(prefix + " .endpoint" , credentials(" endPoint" ))
167
- hadoopConf.set(prefix + " .access.key" , credentials(" accessKey" ))
168
- hadoopConf.set(prefix + " .secret.key" , credentials(" secretKey" ))
169
176
170
- private def getConfigurationName () : String = {
177
+ if (cosType == " softlayer_cos" ) {
178
+ // softlayer cos case
179
+ hadoopConf.set(prefix + " .access.key" , credentials(" accessKey" ))
180
+ hadoopConf.set(prefix + " .secret.key" , credentials(" secretKey" ))
181
+ } else if (cosType == " bluemix_cos" ) {
182
+ // bluemix COS case
183
+ hadoopConf.set(prefix + " .iam.service.id" , credentials(" serviceId" ))
184
+ if (authMethod == " api_key" ) {
185
+ hadoopConf.set(prefix + " .iam.api.key" , credentials(" apiKey" ))
186
+ } else if (authMethod == " iam_token" ) {
187
+ hadoopConf.set(prefix + " .iam.token" , credentials(" iamToken" ))
188
+ }
189
+
190
+ if (credentials.contains(" iamServiceEndpoint" )) {
191
+ hadoopConf.set(prefix + " .iam.endpoint" , credentials(" iamServiceEndpoint" ))
192
+ }
193
+
194
+ if (credentials.contains(" v2SignerType" )) {
195
+ hadoopConf.set(prefix + " .v2.signer.type" , credentials(" v2SignerType" ))
196
+ }
197
+ }
198
+
199
+ private def _getConfigurationName () : String = {
171
200
if (configurationName != " " ) {
172
201
return configurationName
173
202
} else {
174
203
return globalVariables.DEFAULT_SERVICE_NAME
175
204
}
176
205
}
177
206
207
+ private def _validate_credentials (credentials : HashMap [String , String ], cosType : String , authMethod : String ) = {
208
+ val requiredKeys = _get_required_key_array(cosType, authMethod)
209
+
210
+ // check the existence of all required values in credentials
211
+ for ( key <- requiredKeys ) {
212
+ if (! credentials.contains(key)) {
213
+ throw new IllegalArgumentException (" Invalid input: missing required input [" + key + " ]" )
214
+ }
215
+ }
216
+ }
217
+
218
+ private def _get_required_key_array (cosType : String , authMethod : String ) : Array [String ] = {
219
+ val requiredKeySoftlayerCos = Array (" endPoint" , " accessKey" , " secretKey" )
220
+ val requiredKeyListIamApiKey = Array (" endPoint" , " apiKey" , " serviceId" )
221
+ val requiredKeyListIamToken = Array (" endPoint" , " iamToken" , " serviceId" )
222
+
223
+ if (cosType == " bluemix_cos" ) {
224
+ if (authMethod == " api_key" ) {
225
+ return requiredKeyListIamApiKey
226
+ } else if (authMethod == " iam_token" ) {
227
+ return requiredKeyListIamToken
228
+ } else {
229
+ throw new IllegalArgumentException (" Invalid input: authMethod. authMethod is optional but if set, it should have one of the following values: api_key, iam_token" )
230
+ }
231
+ } else if (cosType == " softlayer_cos" ) {
232
+ return requiredKeySoftlayerCos
233
+ } else {
234
+ throw new IllegalArgumentException (" Invalid input: cosType. cosType is optional but if set, it should have one of the following values: softlayer_cos, bluemix_cos" )
235
+ }
236
+ }
237
+
178
238
def url (bucketName : String , objectName : String ) : String = {
179
- var serviceName = getConfigurationName ()
239
+ var serviceName = _getConfigurationName ()
180
240
return " cos://" + bucketName + " ." + serviceName + " /" + objectName
181
241
}
182
242
}
0 commit comments