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

Add functionality to remove bucket #10

Merged
merged 3 commits into from
Oct 21, 2017
Merged
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
132 changes: 84 additions & 48 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class Client {
lifecycleEvents:[
'deploy'
]
},
remove: {
usage: 'Removes deployed files and bucket',
lifecycleEvents: [
'remove'
]
}
}
}
Expand All @@ -42,8 +48,82 @@ class Client {
'client:deploy:deploy': () => {
this._validateAndPrepare()
.then(this._processDeployment.bind(this));
},
'client:remove:remove': () => {
this._removeDeployedResources();
}
};
}

// Shared functions

listBuckets() {
return this.aws.request('S3', 'listBuckets', {}, this.stage, this.region).bind(this);
}

findBucket(data) {
data.Buckets.forEach(function(bucket) {
if (bucket.Name === this.bucketName) {
this.bucketExists = true;
this.serverless.cli.log(`Bucket ${this.bucketName} exists`);
}
}.bind(this));
}

listObjectsInBucket() {
if (!this.bucketExists) return BbPromise.resolve();

this.serverless.cli.log(`Listing objects in bucket ${this.bucketName}...`);

let params = {
Bucket: this.bucketName
};

return this.aws.request('S3', 'listObjectsV2', params, this.stage, this.region);
}

deleteObjectsFromBucket(data) {
if (!this.bucketExists) return BbPromise.resolve();

this.serverless.cli.log(`Deleting all objects from bucket ${this.bucketName}...`);

if (!data.Contents[0]) {
return BbPromise.resolve();
} else {
let Objects = _.map(data.Contents, function (content) {
return _.pick(content, 'Key');
});

let params = {
Bucket: this.bucketName,
Delete: { Objects: Objects }
};

return this.aws.request('S3', 'deleteObjects', params, this.stage, this.region);
}
}

// Hook handlers

_removeDeployedResources() {
this.bucketName = this.serverless.service.custom.client.bucketName;
var safetyDelay = 3000;
this.serverless.cli.log(`Preparing to empty and remove bucket ${this.bucketName}, waiting for ${safetyDelay/1000} seconds...`);

function deleteBucket() {
this.serverless.cli.log(`Removing bucket ${this.bucketName}...`);
let params = {
Bucket: this.bucketName
};
return this.aws.request('S3', 'deleteBucket', params, this.stage, this.region);
}

return BbPromise.delay(safetyDelay).bind(this)
.then(this.listBuckets)
.then(this.findBucket)
.then(this.listObjectsInBucket)
.then(this.deleteObjectsFromBucket)
.then(deleteBucket)
}

_validateAndPrepare() {
Expand Down Expand Up @@ -72,49 +152,6 @@ class Client {
_processDeployment() {
this.serverless.cli.log('Deploying client to stage "' + this.stage + '" in region "' + this.region + '"...');


function listBuckets(data) {
data.Buckets.forEach(function(bucket) {
if (bucket.Name === this.bucketName) {
this.bucketExists = true;
this.serverless.cli.log(`Bucket ${this.bucketName} already exists`);
}
}.bind(this));
}

function listObjectsInBucket() {
if (!this.bucketExists) return BbPromise.resolve();

this.serverless.cli.log(`Listing objects in bucket ${this.bucketName}...`);

let params = {
Bucket: this.bucketName
};

return this.aws.request('S3', 'listObjectsV2', params, this.stage, this.region);
}

function deleteObjectsFromBucket(data) {
if (!this.bucketExists) return BbPromise.resolve();

this.serverless.cli.log(`Deleting all objects from bucket ${this.bucketName}...`);

if (!data.Contents[0]) {
return BbPromise.resolve();
} else {
let Objects = _.map(data.Contents, function (content) {
return _.pick(content, 'Key');
});

let params = {
Bucket: this.bucketName,
Delete: { Objects: Objects }
};

return this.aws.request('S3', 'deleteObjects', params, this.stage, this.region);
}
}

function createBucket() {
if (this.bucketExists) return BbPromise.resolve();
this.serverless.cli.log(`Creating bucket ${this.bucketName}...`);
Expand Down Expand Up @@ -211,11 +248,10 @@ class Client {
return this.aws.request('S3', 'putBucketCors', params, this.stage, this.region);
}

return this.aws.request('S3', 'listBuckets', {}, this.stage, this.region)
.bind(this)
.then(listBuckets)
.then(listObjectsInBucket)
.then(deleteObjectsFromBucket)
return this.listBuckets()
.then(this.findBucket)
.then(this.listObjectsInBucket)
.then(this.deleteObjectsFromBucket)
.then(createBucket)
.then(configureBucket)
.then(configurePolicyForBucket)
Expand Down