Skip to content

Commit

Permalink
Merge cd7febc into 6b9d0ac
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsemar committed Dec 23, 2018
2 parents 6b9d0ac + cd7febc commit 0ef307a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -165,6 +165,7 @@ class AwsAlias {

'alias:remove:remove': () => BbPromise.bind(this)
.then(this.validate)
.then(this.setBucketName)
.then(this.aliasStackLoadCurrentCFStackAndDependencies)
.spread(this.removeAlias)
};
Expand Down
43 changes: 39 additions & 4 deletions lib/removeAlias.js
Expand Up @@ -5,6 +5,7 @@ const _ = require('lodash');
const utils = require('./utils');

const NO_UPDATE_MESSAGE = 'No updates are to be performed.';
const TEMPLATE_MIN_SIZE_FOR_UPLOAD = 51200;

module.exports = {

Expand Down Expand Up @@ -135,7 +136,7 @@ module.exports = {
});
},

aliasApplyStackChanges(currentTemplate, aliasStackTemplates, currentAliasStackTemplate) {
aliasApplyStackChanges(currentTemplate, aliasStackTemplates, currentAliasStackTemplate, templateUrl) {

const stackName = this._provider.naming.getStackName();

Expand All @@ -155,10 +156,15 @@ module.exports = {
'CAPABILITY_NAMED_IAM',
],
Parameters: [],
TemplateBody: JSON.stringify(currentTemplate),
Tags: _.map(_.keys(stackTags), key => ({ Key: key, Value: stackTags[key] })),
};

if (templateUrl) {
params.TemplateURL = templateUrl;
} else {
params.TemplateBody = JSON.stringify(currentTemplate);
}

this.options.verbose && this._serverless.cli.log(`Checking stack policy`);

// Policy must have at least one statement, otherwise no updates would be possible at all
Expand All @@ -185,8 +191,38 @@ module.exports = {

},

uploadCloudFormationTemplate(currentTemplate, aliasStackTemplates, currentAliasStackTemplate) {
const templateSize = JSON.stringify(currentTemplate).length;
if (templateSize < TEMPLATE_MIN_SIZE_FOR_UPLOAD) {
this.serverless.cli.log(`Skipping Upload of CloudFormation alias file to S3, size is only ${templateSize}`);
return BbPromise.resolve([ currentTemplate, aliasStackTemplates, currentAliasStackTemplate]);
}
this.serverless.cli.log('Uploading CloudFormation alias file to S3...');
const body = JSON.stringify(currentTemplate);

const fileName = 'compiled-cloudformation-template-alias.json';

let params = {
Bucket: this.bucketName,
Key: `${this.serverless.service.package.artifactDirectoryName}/${fileName}`,
Body: body,
ContentType: 'application/json',
};

return this.provider.request('S3',
'putObject',
params,
this._options.stage,
this._options.region)
.then(() => {
const templateUrl = `https://s3.amazonaws.com/${this.bucketName}/${this._serverless.service.package.artifactDirectoryName}/compiled-cloudformation-template-alias.json`;
return BbPromise.resolve([ currentTemplate, aliasStackTemplates, currentAliasStackTemplate, templateUrl ]);
});
},

aliasRemoveAliasStack(currentTemplate, aliasStackTemplates, currentAliasStackTemplate) {


const stackName = `${this._provider.naming.getStackName()}-${this._alias}`;

this.options.verbose && this._serverless.cli.log(`Removing CF stack ${stackName}`);
Expand Down Expand Up @@ -245,9 +281,8 @@ module.exports = {
return BbPromise.resolve([ currentTemplate, aliasStackTemplates, currentAliasStackTemplate ]).bind(this)
.spread(this.aliasCreateStackChanges)
.spread(this.aliasRemoveAliasStack)
.spread(this.uploadCloudFormationTemplate)
.spread(this.aliasApplyStackChanges)
.then(() => BbPromise.resolve());

}

};
4 changes: 4 additions & 0 deletions test/removeAlias.test.js
Expand Up @@ -67,12 +67,14 @@ describe('removeAlias', () => {
let aliasCreateStackChangesStub;
let aliasRemoveAliasStackStub;
let aliasApplyStackChangesStub;
let uploadCloudFormationTemplateStub;
let pluginManagerSpawnStub;

beforeEach(() => {
aliasApplyStackChangesStub = sandbox.stub(awsAlias, 'aliasApplyStackChanges');
aliasCreateStackChangesStub = sandbox.stub(awsAlias, 'aliasCreateStackChanges');
aliasRemoveAliasStackStub = sandbox.stub(awsAlias, 'aliasRemoveAliasStack');
uploadCloudFormationTemplateStub = sandbox.stub(awsAlias, 'uploadCloudFormationTemplate');
pluginManagerSpawnStub = sandbox.stub(awsAlias._serverless.pluginManager, 'spawn');
});

Expand Down Expand Up @@ -131,12 +133,14 @@ describe('removeAlias', () => {
aliasApplyStackChangesStub.returns([ slsStack1, [ aliasStack2 ], aliasStack1 ]);
aliasCreateStackChangesStub.returns([ slsStack1, [ aliasStack2 ], aliasStack1 ]);
aliasRemoveAliasStackStub.returns([ slsStack1, [ aliasStack2 ], aliasStack1 ]);
uploadCloudFormationTemplateStub.returns([ slsStack1, [ aliasStack2 ], aliasStack1, 'templateUrl' ]);

return expect(awsAlias.removeAlias(slsStack1, [ aliasStack2 ], aliasStack1)).to.be.fulfilled
.then(() => BbPromise.all([
expect(aliasCreateStackChangesStub).to.have.been.calledOnce,
expect(aliasRemoveAliasStackStub).to.have.been.calledOnce,
expect(aliasApplyStackChangesStub).to.have.been.calledOnce,
expect(uploadCloudFormationTemplateStub).to.have.been.calledOnce,
expect(pluginManagerSpawnStub).to.not.have.been.called,
]));
});
Expand Down

0 comments on commit 0ef307a

Please sign in to comment.