Skip to content

Commit

Permalink
Merge 8cc36a4 into b98b321
Browse files Browse the repository at this point in the history
  • Loading branch information
rrahul963 committed Apr 4, 2020
2 parents b98b321 + 8cc36a4 commit 9f39b02
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -65,3 +65,4 @@ typings/

#vs code folder
.vscode
.DS_Store
15 changes: 14 additions & 1 deletion README.md
Expand Up @@ -126,16 +126,29 @@ 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/).


### Revisions:

* 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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 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": {
Expand Down
7 changes: 6 additions & 1 deletion src/helper.js
Expand Up @@ -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) {
Expand All @@ -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.`)}`);
Expand Down
23 changes: 23 additions & 0 deletions test/unittest/index.js
Expand Up @@ -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 = [
{
Expand Down

0 comments on commit 9f39b02

Please sign in to comment.