From e1bceef7f92abc75e5cddd0b5afeb6b1bf224410 Mon Sep 17 00:00:00 2001 From: "Boyd, Zach" Date: Wed, 24 Jan 2018 08:49:47 -0600 Subject: [PATCH 1/4] Issue #94 - Added support to have an alias subscribe to an existing SNS topic --- lib/stackops/snsEvents.js | 18 ++++++++++++++++++ test/data/sns-stack.json | 18 ++++++++++++++++++ test/stackops/snsEvents.test.js | 2 ++ 3 files changed, 38 insertions(+) diff --git a/lib/stackops/snsEvents.js b/lib/stackops/snsEvents.js index 9cf626b..68f7d67 100644 --- a/lib/stackops/snsEvents.js +++ b/lib/stackops/snsEvents.js @@ -46,6 +46,23 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac delete stageStack.Resources[name]; }); + const snsSubscriptions = + _.assign({}, + _.pickBy(stageStack.Resources, [ 'Type', 'AWS::SNS::Subscription' ])); + + _.forOwn(snsSubscriptions, (subscription, name) => { + + const functionNameRef = utils.findAllReferences(_.get(subscription.Properties, 'Endpoint')); + const functionName = _.replace(_.get(functionNameRef, '[0].ref', ''), /LambdaFunction$/, ''); + const versionName = _.find(_.keys(versions), version => _.startsWith(version, functionName)); + const aliasName = _.find(_.keys(aliases), alias => _.startsWith(alias, functionName)); + + subscription.Properties.Endpoint = { Ref: aliasName }; + subscription.DependsOn = [ versionName, aliasName ]; + + delete stageStack.Resources[name]; + }); + // Fetch lambda permissions. These have to be updated later to allow the aliased functions. const snsLambdaPermissions = _.assign({}, @@ -71,6 +88,7 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac // Add all alias stack owned resources aliasResources.push(snsTopics); + aliasResources.push(snsSubscriptions); aliasResources.push(snsLambdaPermissions); _.forEach(aliasResources, resource => _.assign(aliasStack.Resources, resource)); diff --git a/test/data/sns-stack.json b/test/data/sns-stack.json index 55ebbd4..eb7c18a 100644 --- a/test/data/sns-stack.json +++ b/test/data/sns-stack.json @@ -250,6 +250,24 @@ ] } }, + "SNSTopicSubscriptionSlstestprojecttopic": { + "Type" : "AWS::SNS::Subscription", + "Properties": { + "Endpoint": { + "Fn::GetAtt": [ + "Testfct1LambdaFunction", + "Arn" + ] + }, + "Protocol": "lambda", + "TopicArn": { + "Fn::GetAtt": [ + "SNSTopicSlstestprojecttopic", + "Arn" + ] + } + } + }, "Testfct1LambdaPermissionSlstestprojecttopicSNS": { "Type": "AWS::Lambda::Permission", "Properties": { diff --git a/test/stackops/snsEvents.test.js b/test/stackops/snsEvents.test.js index 499be45..f935703 100644 --- a/test/stackops/snsEvents.test.js +++ b/test/stackops/snsEvents.test.js @@ -75,8 +75,10 @@ describe('SNS Events', () => { return expect(awsAlias.aliasHandleSNSEvents({}, [], {})).to.be.fulfilled .then(() => BbPromise.all([ expect(snsStack).to.not.have.a.nested.property('Resources.SNSTopicSlstestprojecttopic'), + expect(snsStack).to.not.have.a.nested.property('Resources.SNSTopicSubscriptionSlstestprojecttopic'), expect(snsStack).to.not.have.a.nested.property('Resources.Testfct1LambdaPermissionSlstestprojecttopicSNS'), expect(aliasStack).to.have.a.nested.property('Resources.SNSTopicSlstestprojecttopic'), + expect(aliasStack).to.have.a.nested.property('Resources.SNSTopicSubscriptionSlstestprojecttopic'), expect(aliasStack).to.have.a.nested.property('Resources.Testfct1LambdaPermissionSlstestprojecttopicSNS'), ])); }); From 08ecada1fc75dc6436c0bfcee5cffbff645f7eda Mon Sep 17 00:00:00 2001 From: "Boyd, Zach" Date: Wed, 24 Jan 2018 08:54:01 -0600 Subject: [PATCH 2/4] Fixed spacing issue --- lib/stackops/snsEvents.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/stackops/snsEvents.js b/lib/stackops/snsEvents.js index 68f7d67..fcca7f3 100644 --- a/lib/stackops/snsEvents.js +++ b/lib/stackops/snsEvents.js @@ -87,9 +87,9 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac }); // Add all alias stack owned resources - aliasResources.push(snsTopics); + aliasResources.push(snsTopics); aliasResources.push(snsSubscriptions); - aliasResources.push(snsLambdaPermissions); + aliasResources.push(snsLambdaPermissions); _.forEach(aliasResources, resource => _.assign(aliasStack.Resources, resource)); From b4027841d2590a7ab664bf0808f68a3cb25aaf7d Mon Sep 17 00:00:00 2001 From: "Boyd, Zach" Date: Wed, 24 Jan 2018 09:33:41 -0600 Subject: [PATCH 3/4] attempting to fix spacing issues again --- lib/stackops/snsEvents.js | 26 +++++++++++++------------- test/stackops/snsEvents.test.js | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/stackops/snsEvents.js b/lib/stackops/snsEvents.js index fcca7f3..36c20a4 100644 --- a/lib/stackops/snsEvents.js +++ b/lib/stackops/snsEvents.js @@ -47,20 +47,20 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac }); const snsSubscriptions = - _.assign({}, - _.pickBy(stageStack.Resources, [ 'Type', 'AWS::SNS::Subscription' ])); + _.assign({}, + _.pickBy(stageStack.Resources, [ 'Type', 'AWS::SNS::Subscription' ])); - _.forOwn(snsSubscriptions, (subscription, name) => { + _.forOwn(snsSubscriptions, (subscription, name) => { - const functionNameRef = utils.findAllReferences(_.get(subscription.Properties, 'Endpoint')); - const functionName = _.replace(_.get(functionNameRef, '[0].ref', ''), /LambdaFunction$/, ''); - const versionName = _.find(_.keys(versions), version => _.startsWith(version, functionName)); - const aliasName = _.find(_.keys(aliases), alias => _.startsWith(alias, functionName)); + const functionNameRef = utils.findAllReferences(_.get(subscription.Properties, 'Endpoint')); + const functionName = _.replace(_.get(functionNameRef, '[0].ref', ''), /LambdaFunction$/, ''); + const versionName = _.find(_.keys(versions), version => _.startsWith(version, functionName)); + const aliasName = _.find(_.keys(aliases), alias => _.startsWith(alias, functionName)); - subscription.Properties.Endpoint = { Ref: aliasName }; - subscription.DependsOn = [ versionName, aliasName ]; + subscription.Properties.Endpoint = { Ref: aliasName }; + subscription.DependsOn = [ versionName, aliasName ]; - delete stageStack.Resources[name]; + delete stageStack.Resources[name]; }); // Fetch lambda permissions. These have to be updated later to allow the aliased functions. @@ -87,9 +87,9 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac }); // Add all alias stack owned resources - aliasResources.push(snsTopics); - aliasResources.push(snsSubscriptions); - aliasResources.push(snsLambdaPermissions); + aliasResources.push(snsTopics); + aliasResources.push(snsSubscriptions); + aliasResources.push(snsLambdaPermissions); _.forEach(aliasResources, resource => _.assign(aliasStack.Resources, resource)); diff --git a/test/stackops/snsEvents.test.js b/test/stackops/snsEvents.test.js index f935703..262c2e2 100644 --- a/test/stackops/snsEvents.test.js +++ b/test/stackops/snsEvents.test.js @@ -75,10 +75,10 @@ describe('SNS Events', () => { return expect(awsAlias.aliasHandleSNSEvents({}, [], {})).to.be.fulfilled .then(() => BbPromise.all([ expect(snsStack).to.not.have.a.nested.property('Resources.SNSTopicSlstestprojecttopic'), - expect(snsStack).to.not.have.a.nested.property('Resources.SNSTopicSubscriptionSlstestprojecttopic'), + expect(snsStack).to.not.have.a.nested.property('Resources.SNSTopicSubscriptionSlstestprojecttopic'), expect(snsStack).to.not.have.a.nested.property('Resources.Testfct1LambdaPermissionSlstestprojecttopicSNS'), expect(aliasStack).to.have.a.nested.property('Resources.SNSTopicSlstestprojecttopic'), - expect(aliasStack).to.have.a.nested.property('Resources.SNSTopicSubscriptionSlstestprojecttopic'), + expect(aliasStack).to.have.a.nested.property('Resources.SNSTopicSubscriptionSlstestprojecttopic'), expect(aliasStack).to.have.a.nested.property('Resources.Testfct1LambdaPermissionSlstestprojecttopicSNS'), ])); }); From 24992eb6f84cc515f532e0d490c5ee1c5337e6af Mon Sep 17 00:00:00 2001 From: "Boyd, Zach" Date: Wed, 24 Jan 2018 09:45:54 -0600 Subject: [PATCH 4/4] Fixed linting issues --- lib/stackops/snsEvents.js | 22 +++++++++++----------- test/stackops/snsEvents.test.js | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/stackops/snsEvents.js b/lib/stackops/snsEvents.js index 36c20a4..9f92316 100644 --- a/lib/stackops/snsEvents.js +++ b/lib/stackops/snsEvents.js @@ -46,22 +46,22 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac delete stageStack.Resources[name]; }); - const snsSubscriptions = - _.assign({}, + const snsSubscriptions = + _.assign({}, _.pickBy(stageStack.Resources, [ 'Type', 'AWS::SNS::Subscription' ])); - _.forOwn(snsSubscriptions, (subscription, name) => { + _.forOwn(snsSubscriptions, (subscription, name) => { - const functionNameRef = utils.findAllReferences(_.get(subscription.Properties, 'Endpoint')); - const functionName = _.replace(_.get(functionNameRef, '[0].ref', ''), /LambdaFunction$/, ''); - const versionName = _.find(_.keys(versions), version => _.startsWith(version, functionName)); - const aliasName = _.find(_.keys(aliases), alias => _.startsWith(alias, functionName)); + const functionNameRef = utils.findAllReferences(_.get(subscription.Properties, 'Endpoint')); + const functionName = _.replace(_.get(functionNameRef, '[0].ref', ''), /LambdaFunction$/, ''); + const versionName = _.find(_.keys(versions), version => _.startsWith(version, functionName)); + const aliasName = _.find(_.keys(aliases), alias => _.startsWith(alias, functionName)); - subscription.Properties.Endpoint = { Ref: aliasName }; - subscription.DependsOn = [ versionName, aliasName ]; + subscription.Properties.Endpoint = { Ref: aliasName }; + subscription.DependsOn = [ versionName, aliasName ]; - delete stageStack.Resources[name]; - }); + delete stageStack.Resources[name]; + }); // Fetch lambda permissions. These have to be updated later to allow the aliased functions. const snsLambdaPermissions = diff --git a/test/stackops/snsEvents.test.js b/test/stackops/snsEvents.test.js index 262c2e2..7321d1c 100644 --- a/test/stackops/snsEvents.test.js +++ b/test/stackops/snsEvents.test.js @@ -75,10 +75,10 @@ describe('SNS Events', () => { return expect(awsAlias.aliasHandleSNSEvents({}, [], {})).to.be.fulfilled .then(() => BbPromise.all([ expect(snsStack).to.not.have.a.nested.property('Resources.SNSTopicSlstestprojecttopic'), - expect(snsStack).to.not.have.a.nested.property('Resources.SNSTopicSubscriptionSlstestprojecttopic'), + expect(snsStack).to.not.have.a.nested.property('Resources.SNSTopicSubscriptionSlstestprojecttopic'), expect(snsStack).to.not.have.a.nested.property('Resources.Testfct1LambdaPermissionSlstestprojecttopicSNS'), expect(aliasStack).to.have.a.nested.property('Resources.SNSTopicSlstestprojecttopic'), - expect(aliasStack).to.have.a.nested.property('Resources.SNSTopicSubscriptionSlstestprojecttopic'), + expect(aliasStack).to.have.a.nested.property('Resources.SNSTopicSubscriptionSlstestprojecttopic'), expect(aliasStack).to.have.a.nested.property('Resources.Testfct1LambdaPermissionSlstestprojecttopicSNS'), ])); });