diff --git a/.gitignore b/.gitignore index e3d9213..cc7d94f 100644 --- a/.gitignore +++ b/.gitignore @@ -65,3 +65,4 @@ typings/ #vs code folder .vscode +.DS_Store diff --git a/README.md b/README.md index a9e0cb1..011233c 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,19 @@ custom: Run severless remove command to remove the created api key and usage plan. If the Usage plan is associated with more than one api then the plan and key will be deleted only when the last service is removed. +If you dont want to delete a key as part of `sls remove` command then you can set `deleteAtRemoval` as `false` (default is `true` if not set) + +```yaml +custom: + apiKeys: + - name: name1 + deleteAtRemoval: false + - name: name2 +``` + +based on above configuration, key `name1` will not be deleted when running `sls remove` but key `name2` will be removed. + + For more info on how to get started with Serverless Framework click [here](https://serverless.com/framework/docs/getting-started/). @@ -133,9 +146,9 @@ For more info on how to get started with Serverless Framework click [here](https * 3.3.0 - Added UsagePlan settings * 3.3.1 - Added unit tests, examples and travis-ci - * 4.0.0 - Added Remove hook - Added option to read the usage plan name from the provider section. * 4.0.1 - Updated unit tests and added pre-commit and pre-push hooks using husky * 4.0.2 - Fixed usagePlan config selection criterion +* 4.1.0 - Added an option to not delete apiKey with sls remove diff --git a/package-lock.json b/package-lock.json index 64891ad..069f2bf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "serverless-add-api-key", - "version": "4.0.2", + "version": "4.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 9697d69..0b6a969 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-add-api-key", - "version": "4.0.2", + "version": "4.1.0", "description": "serverless plugin to create a api key and usage pattern (if they don't already exist) and associates them to the Rest Api", "main": "src/index.js", "scripts": { diff --git a/src/helper.js b/src/helper.js index 4c2d84a..81cd381 100644 --- a/src/helper.js +++ b/src/helper.js @@ -394,7 +394,7 @@ const removeApiKey = async (serverless) => { for (let apiKey of apiKeys) { const apiKeyName = apiKey.name; - + const canBeDeleted = apiKey.deleteAtRemoval if (apiKey.usagePlan && apiKey.usagePlan.name) { planName = apiKey.usagePlan.name; } else if (defaultUsagePlan.name) { @@ -403,6 +403,11 @@ const removeApiKey = async (serverless) => { planName = `${apiKeyName}-usage-plan` } + if (canBeDeleted == 'false') { + serverless.cli.consoleLog(`RemoveApiKey: ${chalk.yellow(`Api Key ${apiKeyName} is protected from deletion`)}`); + return; + } + const plan = await module.exports.getUsagePlan(planName, ag, serverless.cli); if (!plan) { serverless.cli.consoleLog(`RemoveApiKey: ${chalk.red(`${planName} not found. Checking and deleting Api key.`)}`); diff --git a/test/unittest/index.js b/test/unittest/index.js index 304b779..b5b850a 100644 --- a/test/unittest/index.js +++ b/test/unittest/index.js @@ -931,6 +931,29 @@ describe('test removeApiKey function', () => { sandbox.restore(); }); + it ('should not delete apikey if deleteAtRemoval is set to false', done => { + serverless.service.custom.apiKeys = [ + { + name: 'test-api-key', + deleteAtRemoval: 'false' + } + ]; + + plugin.removeApiKey(serverless) + .then(() => { + sandbox.assert.notCalled(plugin.getUsagePlan); + sandbox.assert.notCalled(plugin.deleteUsagePlan); + sandbox.assert.notCalled(plugin.getApiKey); + sandbox.assert.notCalled(plugin.deleteApiKey); + done(); + }) + .catch(err => { + console.log(err); + done(err); + }); + + }); + it ('should delete api if usage plan not found', done => { serverless.service.custom.apiKeys = [ {