diff --git a/lib/handler.js b/lib/handler.js index 1df0ccb..9d9716d 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -29,15 +29,19 @@ let Handler = function () { /** * Initializes the handler, sets up the connection to wcs + * @params credentials which includes apikey, workspace_id, workspace_name, iam_apikey description, iam_apikey, workspace url, version, version_date */ -Handler.prototype.initialize = function () { - if(WaEnvUtils.isValidWaEnvVars()) { +Handler.prototype.initialize = function (credentials) { + //check if credentials are coming from the handler initialize call and also valid from the index.js of the skill + if((credentials && WaEnvUtils.isValidWaCredentials(credentials))){ + this.wcsCredentials = WaEnvUtils.setupWCSCredentialsfromVCAP(credentials); + } else if(WaEnvUtils.isValidWaEnvVars()) { this.wcsCredentials = WaEnvUtils.setupWCSCredentialsFromEnv(); } else { let rawJson = fs.readFileSync(path.join(process.env.skillSDKResDir, nluFolder, wcsFileName)); this.wcsCredentials = JSON.parse(rawJson); } - if (this.wcsCredentials) { + if(this.wcsCredentials){ let credentials = this.wcsCredentials.credentials; setupWcs(this, credentials.url, credentials.username, credentials.password, credentials.version_date, credentials.version, credentials.iam_apikey) } @@ -339,7 +343,8 @@ let setupWcs = function (handler, wcsUrl, wcsUsername, wcsPassword, versionDate, options = { version: version, iam_apikey: apiKey, - url: wcsUrl + url: wcsUrl, + export: false } } else { diff --git a/lib/nlu/factory.js b/lib/nlu/factory.js index 6f7aba7..6f5065b 100644 --- a/lib/nlu/factory.js +++ b/lib/nlu/factory.js @@ -96,13 +96,13 @@ class Factory { }); } - - static getNLUs(manifest) { +//pass the credentials sent from the skill to getNLU's + static getNLUs(manifest, credentials) { return new Promise(function (resolve, reject) { let promises = []; let types = manifest.nlu; types.forEach(function (type) { - promises.push(getNLU(type, manifest.name)); + promises.push(getNLU(type, manifest.name, credentials)); }); Promise.all(promises.map(reflect)).then(function (results) { @@ -124,11 +124,14 @@ class Factory { module.exports = Factory; - -function getNLU(type, name) { +//pass the credentials from the skill +function getNLU(type, name, credentials) { return new Promise(function (resolve, reject) { let nlu; - if(WaEnvUtils.isValidWaEnvVars() && type === 'wcs') { + if(credentials && WaEnvUtils.isValidWaCredentials(credentials) && type === 'wcs'){ + nlu = WaEnvUtils.setupWCSCredentialsfromVCAP(credentials); + } + else if(WaEnvUtils.isValidWaEnvVars() && type === 'wcs') { nlu = WaEnvUtils.setupWCSCredentialsFromEnv(); } else { nlu = require(`${resPath}/nlu/${type}`); diff --git a/lib/nlu/utils.js b/lib/nlu/utils.js index 10ef8d4..c19be8c 100644 --- a/lib/nlu/utils.js +++ b/lib/nlu/utils.js @@ -19,6 +19,17 @@ let isValidWaEnvVars = function() { process.env.WCS_WORKSPACE_LANGUAGE)); }; +/** + * check that all the WA credentials sent from the skill are valid + * @returns {string} + */ + +let isValidWaCredentials = function(credentials) { + return ((process.env.WCS_USERNAME && process.env.WCS_URL && process.env.WCS_PASSWORD && process.env.WCS_VERSION_DATE + && process.env.WCS_VERSION) || + (credentials.apikey && process.env.WCS_WORKSPACE_LANGUAGE && credentials.workspace_name && credentials.workspace_name && credentials.url)); +}; + /** * Sets up the handler's wcs credentials from environment variables @@ -55,8 +66,47 @@ let setupWCSCredentialsFromEnv = function () { return options; }; +/** + * Sets up the handler's wcs credentials from VCAP credentials read from the skill + * @param handler + */ + +let setupWCSCredentialsfromVCAP = function (credentials) { + + let options = { + "workspace": { + [process.env.WCS_WORKSPACE_LANGUAGE]: { + "name": credentials.workspace_name, + "workspace_id": credentials.workspace_id + } + } + }; + // new way of wa authentication - with iam api-key + if(credentials.apikey) { + options.credentials = { + "url": credentials.url, + "version": credentials.version_date, + "iam_apikey": credentials.apikey + } + } + // deprecated way of wa authentication + else if(process.env.WCS_PASSWORD && process.env.WCS_USERNAME) { + options.credentials = { + "url": process.env.WCS_URL, + "version": process.env.WCS_VERSION, + "version_date": process.env.WCS_VERSION_DATE, + "password": process.env.WCS_PASSWORD, + "username": process.env.WCS_USERNAME + } + } + return options; + +}; + module.exports = { isValidWaEnvVars: isValidWaEnvVars, - setupWCSCredentialsFromEnv: setupWCSCredentialsFromEnv + isValidWaCredentials: isValidWaCredentials, + setupWCSCredentialsFromEnv: setupWCSCredentialsFromEnv, + setupWCSCredentialsfromVCAP: setupWCSCredentialsfromVCAP }; \ No newline at end of file diff --git a/package.json b/package.json index 2d2a576..3bc4eca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "skill-sdk-nodejs", - "version": "0.0.18", + "version": "0.0.19", "description": "This is the SDK to be used when developing a skill for Watson Assistant Solutions using Node.js", "main": "index.js", "scripts": {