From a9ca1551383534549a80f1bcc4361fc159b41979 Mon Sep 17 00:00:00 2001 From: Ram Grandhi Date: Fri, 31 Jul 2020 13:53:04 +0200 Subject: [PATCH 1/2] + retrieves invokable API URLs now --- index.js | 23 +++++++++++++++++++++-- src/2.6.0/wso2apim.js | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 6db3f03..92262fb 100644 --- a/index.js +++ b/index.js @@ -224,12 +224,30 @@ class ServerlessPlugin { // Compare apples-to-apples (configured-to-deployed) and record deployment status if (this.cache.deployedAPIs.some(deployedAPI => deployedAPI.apiClob === apiDefClob)) { + const apiStatus = this.cache.deployedAPIs.find(deployedAPI => deployedAPI.apiClob === apiDefClob).apiStatus; + const apiId = this.cache.deployedAPIs.find(deployedAPI => deployedAPI.apiClob === apiDefClob).apiId; + var invokableAPIURL = null; + + // Check for PUBLISHED state, if PUBLISHED then retrieve Invokable API URL + try { + if (apiStatus == 'PUBLISHED') { + const data = await wso2apim.listInvokableAPIUrl( + "https://" + this.wso2APIM.host + ":" + this.wso2APIM.port + "/api/am/store/" + this.wso2APIM.versionSlug + "/apis", + this.cache.accessToken, + apiId); + invokableAPIURL = data.endpointURLs.filter((Url) => { return Url.environmentName == this.wso2APIM.gatewayEnv })[0].environmentURLs.https; + } + } + catch (err) { + this.serverless.cli.log("An error occurred while retrieving Invokable API URL for " + `${this.apiDefs[i].name}` + ", proceeding further."); + } + this.cache.deploymentStatus.push({ apiName: apiDef.name, apiVersion: apiDef.version, apiContext: apiDef.rootContext, - apiStatus: this.cache.deployedAPIs.find(deployedAPI => deployedAPI.apiClob === apiDefClob).apiStatus, - apiId: this.cache.deployedAPIs.find(deployedAPI => deployedAPI.apiClob === apiDefClob).apiId, + apiStatus: apiStatus, + invokableAPIURL: invokableAPIURL + " 🚀", }); } else { @@ -238,6 +256,7 @@ class ServerlessPlugin { apiVersion: apiDef.version, apiContext: apiDef.rootContext, apiStatus: "TO BE CREATED", + invokableAPIURL: "TO BE CREATED", }) } } diff --git a/src/2.6.0/wso2apim.js b/src/2.6.0/wso2apim.js index b9c781d..39cefc5 100644 --- a/src/2.6.0/wso2apim.js +++ b/src/2.6.0/wso2apim.js @@ -279,6 +279,37 @@ async function publishAPIDef(url, accessToken, apiId) { } }; + +// Retrieves invokable API endpoint +async function listInvokableAPIUrl(url, accessToken, apiId) { + try { + url = url + "/" + apiId; + var config = { + headers: { + 'Authorization': 'Bearer ' + accessToken + }, + httpsAgent: new https.Agent({ + rejectUnauthorized: false + }) + }; + + return new Promise((resolve, reject) => { + axios.get(url, config) + .then((res) => { + resolve(res.data); + }) + .catch((err) => { + utils.renderError(err); + reject(err); + }); + }); + } + catch (err) { + utils.renderError(err); + } +}; + + // Uploads backend certificate async function uploadCert(url, accessToken, certAlias, certFile, backendUrl) { try { @@ -318,7 +349,7 @@ async function uploadCert(url, accessToken, certAlias, certFile, backendUrl) { // Updates API definition async function updateAPIDef(url, user, accessToken, gatewayEnv, apiDef, apiId) { try { - url = url + "/" + apiId; + url = "url + "/"" + apiId; var data = constructAPIDef(user, gatewayEnv, apiDef, apiId); var config = { headers: { @@ -419,5 +450,6 @@ module.exports = { uploadCert, updateAPIDef, removeAPIDef, - removeCert + removeCert, + listInvokableAPIUrl, }; \ No newline at end of file From 7d12c67e54cab2fb2f93055b77f4b6f47fa44095 Mon Sep 17 00:00:00 2001 From: Ram Grandhi Date: Fri, 31 Jul 2020 14:05:42 +0200 Subject: [PATCH 2/2] fixed regression issue --- index.js | 8 +++++--- src/2.6.0/wso2apim.js | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 92262fb..48aad51 100644 --- a/index.js +++ b/index.js @@ -223,9 +223,9 @@ class ServerlessPlugin { }); // Compare apples-to-apples (configured-to-deployed) and record deployment status - if (this.cache.deployedAPIs.some(deployedAPI => deployedAPI.apiClob === apiDefClob)) { - const apiStatus = this.cache.deployedAPIs.find(deployedAPI => deployedAPI.apiClob === apiDefClob).apiStatus; - const apiId = this.cache.deployedAPIs.find(deployedAPI => deployedAPI.apiClob === apiDefClob).apiId; + if (this.cache.deployedAPIs.some(deployedAPI => deployedAPI.apiClob == apiDefClob)) { + const apiStatus = this.cache.deployedAPIs.find(deployedAPI => deployedAPI.apiClob == apiDefClob).apiStatus; + const apiId = this.cache.deployedAPIs.find(deployedAPI => deployedAPI.apiClob == apiDefClob).apiId; var invokableAPIURL = null; // Check for PUBLISHED state, if PUBLISHED then retrieve Invokable API URL @@ -247,6 +247,7 @@ class ServerlessPlugin { apiVersion: apiDef.version, apiContext: apiDef.rootContext, apiStatus: apiStatus, + apiId, apiId, invokableAPIURL: invokableAPIURL + " 🚀", }); } @@ -256,6 +257,7 @@ class ServerlessPlugin { apiVersion: apiDef.version, apiContext: apiDef.rootContext, apiStatus: "TO BE CREATED", + apiId: null, invokableAPIURL: "TO BE CREATED", }) } diff --git a/src/2.6.0/wso2apim.js b/src/2.6.0/wso2apim.js index 39cefc5..7fa02fa 100644 --- a/src/2.6.0/wso2apim.js +++ b/src/2.6.0/wso2apim.js @@ -349,7 +349,7 @@ async function uploadCert(url, accessToken, certAlias, certFile, backendUrl) { // Updates API definition async function updateAPIDef(url, user, accessToken, gatewayEnv, apiDef, apiId) { try { - url = "url + "/"" + apiId; + url = url + "/" + apiId; var data = constructAPIDef(user, gatewayEnv, apiDef, apiId); var config = { headers: {