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

Resolve bucket name dynamicly #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ custom:
# (optional) Buckets to remove before a stack is deployed.
bucketsToCleanOnDeploy:
- oldBucketName

# (optional) Get deployment bucket name from AWS. Works only for `sls s3remove` and `sls remove`
autoResolve: false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plugin is not meant to clean just the Serverless deployment buckets, think of stacks that create additional S3 buckets for other purposes, such as web apps. I think autoResolve in this context is a bit too open-ended, so I'd suggest a more targeted name. E.g. cleanDeploymentBucket, includeDeploymentBucket, or something similar?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@f00f sorry for mentioning ) just because you are appears in "maintainers"

@coyoteecd good point. For renaming option.

```

When removing a Serverless Framework stack, this plugin automatically empties the buckets listed under `buckets` option.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-s3-cleaner",
"version": "2.0.2",
"version": "2.1.0",
"description": "Serverless Framework plugin that empties S3 buckets before removing a stack",
"main": "dist/index.js",
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default class ServerlessS3Cleaner implements Plugin {
bucketsToCleanOnDeploy: {
type: 'array', uniqueItems: true, items: { type: 'string' }, nullable: true
},
autoResolve: { type: 'boolean', nullable: true, default: false },
},
additionalProperties: false,
anyOf: [
Expand Down Expand Up @@ -67,6 +68,10 @@ export default class ServerlessS3Cleaner implements Plugin {
const config = this.loadConfig();
let bucketsToEmpty = isDeploying ? config.bucketsToCleanOnDeploy : config.buckets;

if (!isDeploying && config.autoResolve) {
bucketsToEmpty.push(await this.provider.getServerlessDeploymentBucketName());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work properly with an explicit bucket name as well as a generated (default) bucket name?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my mind logic is to delete deployment bucket only on remove operation according with the rest of listed buckets

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jettary in Serverless Framework you can either let the framework auto-generate the deployment bucket itself, or you can specify an externally managed bucket (see here). Does getServerlessDeploymentBucketName resolve properly in that case?

}

if (config.prompt) {
prompt.start();
const bucketPromptResults = await prompt.get(bucketsToEmpty.map(bucket => ({
Expand Down Expand Up @@ -177,6 +182,7 @@ export default class ServerlessS3Cleaner implements Plugin {
buckets: providedConfig.buckets || [],
prompt: providedConfig.prompt || false,
bucketsToCleanOnDeploy: providedConfig.bucketsToCleanOnDeploy || [],
autoResolve: providedConfig.autoResolve || false,
};
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/serverless-s3-cleaner-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ interface ServerlessS3CleanerConfig {
* (and can be removed later once the stack has been upgraded).
*/
bucketsToCleanOnDeploy?: string[];

/**
* Resolve deployment bucket name from AWS. Utilize `serverless.provider.getServerlessDeploymentBucketName`.
* Use this flag in case for CI/CD deploying and removing feature stack and so on.
*/
autoResolve?: boolean;
}
24 changes: 23 additions & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import { DeleteObjectsOutput, DeleteObjectsRequest, ListObjectVersionsOutput, ObjectIdentifier } from 'aws-sdk/clients/s3';
import { DescribeStackResourceOutput } from 'aws-sdk/clients/cloudformation';
import prompt from 'prompt';
import Serverless from 'serverless';
import { Logging } from 'serverless/classes/Plugin';
Expand Down Expand Up @@ -259,6 +260,26 @@ describe('ServerlessS3Cleaner', () => {
Bucket: 'b2'
}));
});

it('should call getServerlessDeploymentBucketName if autoResolve option is true', async () => {
const { requestSpy, serverless } = stubServerlessInstance({
buckets: ['b1', 'b2'],
autoResolve: true,
});
const logging = stubLogging();
const plugin = new ServerlessS3Cleaner(serverless, {}, logging);

requestSpy.withArgs('CloudFormation', 'describeStackResource', jasmine.anything()).and.resolveTo({
StackResourceDetail: {
PhysicalResourceId: 'service-name-stage-serverlessdeploymentbucket-gqzu7wcwjth',
}
} as DescribeStackResourceOutput);

const removeFn = plugin.hooks['before:remove:remove'];
await expectAsync(removeFn()).toBeResolved();

expect(requestSpy).toHaveBeenCalled();
});
});

describe('when executing s3remove command', () => {
Expand Down Expand Up @@ -357,7 +378,8 @@ describe('ServerlessS3Cleaner', () => {
requestSpy,
serverless: jasmine.createSpyObj<Serverless>({
getProvider: ({
request: requestSpy
request: requestSpy,
getServerlessDeploymentBucketName: requestSpy,
}) as unknown as Aws,
}, {
cli: jasmine.createSpyObj(['log']),
Expand Down