Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/Retry updating WSO2 api definitions while it is outdated #119

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"test:e2e": "jest e2e --verbose",
"test:e2e:destroy": "yarn stop:wso2-2.6.0 && yarn stop:wso2-3.2.0 && yarn stop:localstack",
"test": "yarn test:unit",
"test:watch": "yarn test --watch",
"start:wso2-2.6.0": "docker run --name api-manager-260 -p 127.0.0.1:8260:8243 -p 127.0.0.1:9260:9443 --rm wso2/wso2am:2.6.0",
"start:wso2-3.2.0": "docker run --name api-manager-320 -p 127.0.0.1:8320:8243 -p 127.0.0.1:9320:9443 --rm wso2/wso2am:3.2.0",
"start:localstack": "docker run --name localstack -p 127.0.0.1:4566:4566 -p 127.0.0.1:4571:4571 --rm localstack/localstack:1.4",
Expand Down Expand Up @@ -42,23 +43,18 @@
"serverless"
],
"devDependencies": {
"axios": "^1.6.3",
"babel-eslint": "^10.1.0",
"chalk": "^4.1.0",
"codecov": "^3.8.1",
"concurrently": "^6.0.0",
"eslint": "^7.20.0",
"eslint-plugin-jest": "^24.1.5",
"form-data": "^4.0.0",
"fs": "^0.0.2",
"https": "^1.0.0",
"jest": "^26.6.3",
"qs": "^6.11.2",
"release": "^6.3.0",
"serverless": "^1.75.1",
"serverless-deployment-bucket": "^1.4.1",
"serverless-localstack": "^0.4.30",
"split-ca": "^1.0.1"
"serverless-localstack": "^0.4.30"
},
"dependencies": {
"axios": "^1.6.3",
Expand All @@ -72,6 +68,9 @@
},
"jest": {
"coverageDirectory": "./coverage/",
"collectCoverage": true
"collectCoverage": true,
"watchPathIgnorePatterns": [
"<rootDir>/jest.json"
]
}
}
52 changes: 51 additions & 1 deletion src/2.6.0/wso2apim.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,14 +609,62 @@ async function upsertSwaggerSpec(wso2APIM, accessToken, apiId, swaggerSpec) {
data.append('apiDefinition', JSON.stringify(swaggerSpec));

return axios.put(url, data, config)
.then((_) => undefined); // eat the http response, not needed outside of this api layer
.then(() => undefined); // eat the http response, not needed outside of this api layer
}
catch (err) {
utils.renderError(err);
throw err;
}
}

/**
* Retrieves the API Definition saved at the WSO2 platform
*
* @param {*} wso2APIM
* @param {string} accessToken
* @param {string} apiId
* @returns {*}
*/
async function getApiDef(wso2APIM, accessToken, apiId) {
const url = `https://${wso2APIM.host}:${wso2APIM.port}/api/am/publisher/${wso2APIM.versionSlug}/apis/${apiId}`;

try {
const result = await axios.get(url, {
headers: {
'Authorization': 'Bearer ' + accessToken
},
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
});

return result.data;
} catch (err) {
utils.renderError(err);
throw err;
}
}

/**
* Check if the API def is up to date
*
* @param {*} wso2APIM
* @param {string} accessToken
* @param {string} apiId
* @param {Object} apiDef
* @returns {Promise<boolean>}
*/
async function checkApiDefIsUpdated(wso2APIM, accessToken, apiId, apiDef) {
const newApiDef = constructAPIDef(wso2APIM.user, wso2APIM.gatewayEnv, apiDef, apiId);

// ? When no cors configuration is set, it applies the default one from wso2
if (!newApiDef.corsConfiguration) return true;

const currentApiDef = await getApiDef(wso2APIM, accessToken, apiId);

// TODO: We should test for any intersection data between api definition and swagger specs
return utils.isEqual(newApiDef.corsConfiguration, currentApiDef.corsConfiguration);
}

module.exports = {
registerClient,
Expand All @@ -634,4 +682,6 @@ module.exports = {
removeAPIDef,
listInvokableAPIUrl,
upsertSwaggerSpec,
getApiDef,
checkApiDefIsUpdated,
};
Loading
Loading