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

Fix Dynamodb stream events #111

Merged
merged 2 commits into from
Mar 16, 2018
Merged
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
47 changes: 47 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
env:
es6: true
node: true
mocha: true

extends:
- eslint:recommended
- plugin:lodash/recommended
- plugin:import/errors
- plugin:import/warnings

plugins:
- promise
- lodash
- import

rules:
indent:
- error
- tab
-
MemberExpression: off
linebreak-style:
- error
- unix
semi:
- error
- always
promise/always-return: error
promise/no-return-wrap: error
promise/param-names: error
promise/catch-or-return: error
promise/no-native: off
promise/no-nesting: off
promise/no-promise-in-callback: warn
promise/no-callback-in-promise: warn
promise/avoid-new: warn
import/no-named-as-default: off
lodash/import-scope: off
lodash/preferred-alias: off
lodash/prop-shorthand: off
lodash/prefer-lodash-method:
- error
-
ignoreObjects:
- BbPromise
48 changes: 0 additions & 48 deletions .eslintrc.json

This file was deleted.

57 changes: 30 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
*/

const BbPromise = require('bluebird')
, _ = require('lodash')
, Path = require('path')
, validate = require('./lib/validate')
, configureAliasStack = require('./lib/configureAliasStack')
, createAliasStack = require('./lib/createAliasStack')
, updateAliasStack = require('./lib/updateAliasStack')
, aliasRestructureStack = require('./lib/aliasRestructureStack')
, stackInformation = require('./lib/stackInformation')
, listAliases = require('./lib/listAliases')
, removeAlias = require('./lib/removeAlias')
, logs = require('./lib/logs')
, collectUserResources = require('./lib/collectUserResources')
, uploadAliasArtifacts = require('./lib/uploadAliasArtifacts')
, updateFunctionAlias = require('./lib/updateFunctionAlias');
, _ = require('lodash')
, Path = require('path')
, validate = require('./lib/validate')
, configureAliasStack = require('./lib/configureAliasStack')
, createAliasStack = require('./lib/createAliasStack')
, updateAliasStack = require('./lib/updateAliasStack')
, aliasRestructureStack = require('./lib/aliasRestructureStack')
, stackInformation = require('./lib/stackInformation')
, listAliases = require('./lib/listAliases')
, removeAlias = require('./lib/removeAlias')
, logs = require('./lib/logs')
, collectUserResources = require('./lib/collectUserResources')
, uploadAliasArtifacts = require('./lib/uploadAliasArtifacts')
, updateFunctionAlias = require('./lib/updateFunctionAlias')
, deferredOutputs = require('./lib/deferredOutputs');

class AwsAlias {

Expand All @@ -40,18 +41,18 @@ class AwsAlias {
* Load stack helpers from Serverless installation.
*/
const monitorStack = require(
Path.join(this._serverless.config.serverlessPath,
'plugins',
'aws',
'lib',
'monitorStack')
Path.join(this._serverless.config.serverlessPath,
'plugins',
'aws',
'lib',
'monitorStack')
);
const setBucketName = require(
Path.join(this._serverless.config.serverlessPath,
'plugins',
'aws',
'lib',
'setBucketName')
Path.join(this._serverless.config.serverlessPath,
'plugins',
'aws',
'lib',
'setBucketName')
);

_.assign(
Expand All @@ -69,7 +70,8 @@ class AwsAlias {
uploadAliasArtifacts,
updateFunctionAlias,
setBucketName,
monitorStack
monitorStack,
deferredOutputs
);

this._commands = {
Expand Down Expand Up @@ -116,10 +118,11 @@ class AwsAlias {
.then(this.createAliasStack),

'after:aws:deploy:deploy:uploadArtifacts': () => BbPromise.bind(this)
.then(this.setBucketName)
.then(this.uploadAliasArtifacts),
.then(() => BbPromise.resolve()),

'after:aws:deploy:deploy:updateStack': () => BbPromise.bind(this)
.then(this.setBucketName)
.then(this.uploadAliasArtifacts)
.then(this.updateAliasStack),

'before:deploy:function:initialize': () => BbPromise.bind(this)
Expand Down
59 changes: 59 additions & 0 deletions lib/deferredOutputs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

/**
* Handle deferred output resolution.
* Some references to outputs of the base stack cannot be done
* by Fn::ImportValue because they will change from deployment to deployment.
* So we resolve them after the base stack has been deployed and set their
* values accordingly.
*/

const _ = require('lodash');
const BbPromise = require('bluebird');

const deferredOutputs = {};

module.exports = {

/**
* Register a deferred output
* @param {string} sourceOutput
* @param {Object} targetObject
* @param {string} targetPropertyName
*/
addDeferredOutput(sourceOutput, targetObject, targetPropertyName) {
this.options.verbose && this.serverless.cli.log(`Register deferred output ${sourceOutput} -> ${targetPropertyName}`);

deferredOutputs[sourceOutput] = deferredOutputs[sourceOutput] || [];
deferredOutputs[sourceOutput].push({
target: targetObject,
property: targetPropertyName
});
},

resolveDeferredOutputs() {
this.options.verbose && this.serverless.cli.log('Resolving deferred outputs');

if (_.isEmpty(deferredOutputs)) {
return BbPromise.resolve();
}

return this.aliasGetExports()
.then(cfExports => {
_.forOwn(deferredOutputs, (references, output) => {
if (_.has(cfExports, output)) {
const value = cfExports[output];
this.options.verbose && this.serverless.cli.log(` ${output} -> ${value}`);
_.forEach(references, reference => {
_.set(reference.target, reference.property, value);
});
}
else {
this.serverless.cli.log(`ERROR: Output ${output} not found.`);
}
});
return null;
});
}

};
24 changes: 24 additions & 0 deletions lib/stackInformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,30 @@ module.exports = {
});
},

aliasGetExports() {
const fetchExports = (result, token) => {
const params = {};
if (token) {
params.NextToken = token;
}
return this._provider.request('CloudFormation', 'listExports', params)
.then(cfData => {
const newResult = _.reduce(cfData.Exports, (__, cfExport) => {
__[cfExport.Name] = cfExport.Value;
return __;
}, result);

if (cfData.NextToken) {
return fetchExports(newResult, cfData.NextToken);
}

return newResult;
});
};

return fetchExports({});
},

aliasStackLoadCurrentCFStackAndDependencies() {
return BbPromise.join(
BbPromise.bind(this).then(this.aliasStackLoadCurrentTemplate),
Expand Down
13 changes: 9 additions & 4 deletions lib/stackops/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac

const subscriptions = _.assign({}, _.pickBy(_.get(stageStack, 'Resources', {}), [ 'Type', 'AWS::Lambda::EventSourceMapping' ]));

this.options.verbose && this._serverless.cli.log('Processing event source subscriptions');

_.forOwn(subscriptions, (subscription, name) => {
// Reference alias as FunctionName
const functionNameRef = utils.findAllReferences(_.get(subscription, 'Properties.FunctionName'));
Expand Down Expand Up @@ -44,14 +46,17 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
Name: `${stackName}-${resourceRefName}`
}
};
// Add the outpur to the referenced alias outputs
// Add the output to the referenced alias outputs
const aliasOutputs = JSON.parse(aliasStack.Outputs.AliasOutputs.Value);
aliasOutputs.push(resourceRefName);
aliasStack.Outputs.AliasOutputs.Value = JSON.stringify(aliasOutputs);
// Replace the reference with the cross stack reference
subscription.Properties.EventSourceArn = {
'Fn::ImportValue': `${stackName}-${resourceRefName}`
};
subscription.Properties.EventSourceArn = {};

// Event source ARNs can be volatile - e.g. DynamoDB and must not be referenced
// with Fn::ImportValue which does not allow for changes. So we have to register
// them for delayed lookup until the base stack has been updated.
this.addDeferredOutput(`${stackName}-${resourceRefName}`, subscription.Properties, 'EventSourceArn');

// Remove mapping from stage stack
delete stageStack.Resources[name];
Expand Down
1 change: 1 addition & 0 deletions lib/uploadAliasArtifacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = {
}

return BbPromise.bind(this)
.then(this.resolveDeferredOutputs)
.then(this.uploadAliasCloudFormationFile);
},

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"eslint-plugin-lodash": "^2.5.0",
"eslint-plugin-promise": "^3.6.0",
"get-installed-path": "^4.0.8",
"mocha": "^4.0.1",
"mocha": "^5.0.4",
"nyc": "^11.2.1",
"serverless": "^1.23.0",
"sinon": "^4.0.2",
Expand Down
17 changes: 9 additions & 8 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,20 @@ describe('AwsAlias', () => {
});

it('after:aws:deploy:deploy:uploadArtifacts should resolve', () => {
setBucketNameStub.returns(BbPromise.resolve());
uploadAliasArtifactsStub.returns(BbPromise.resolve());
return expect(awsAlias.hooks['after:aws:deploy:deploy:uploadArtifacts']()).to.eventually.be.fulfilled
.then(() => BbPromise.join(
expect(setBucketNameStub).to.be.calledOnce,
expect(uploadAliasArtifactsStub).to.be.calledOnce
));
return expect(awsAlias.hooks['after:aws:deploy:deploy:uploadArtifacts']()).to.eventually.be.fulfilled;
});

it('after:aws:deploy:deploy:updateStack should resolve', () => {
setBucketNameStub.returns(BbPromise.resolve());
uploadAliasArtifactsStub.returns(BbPromise.resolve());
updateAliasStackStub.returns(BbPromise.resolve());
return expect(awsAlias.hooks['after:aws:deploy:deploy:updateStack']()).to.eventually.be.fulfilled
.then(() => expect(updateAliasStackStub).to.be.calledOnce);
.then(() => {
expect(setBucketNameStub).to.be.calledOnce;
expect(uploadAliasArtifactsStub).to.be.calledOnce;
expect(updateAliasStackStub).to.be.calledOnce;
return null;
});
});

it('after:info:info should resolve', () => {
Expand Down
Loading