From eedabf0cfb8aee5233b66281f2259bc3471e7c01 Mon Sep 17 00:00:00 2001 From: Me OutPerformIt Date: Tue, 11 Apr 2017 20:30:33 +0300 Subject: [PATCH 1/4] Fix SNS event --- docs/providers/aws/events/sns.md | 21 +++++++ .../aws/deploy/compile/events/sns/index.js | 63 ++++++++++++++----- .../deploy/compile/events/sns/index.test.js | 44 +++++++++++++ 3 files changed, 113 insertions(+), 15 deletions(-) diff --git a/docs/providers/aws/events/sns.md b/docs/providers/aws/events/sns.md index a2b8689b3..44ca463a6 100644 --- a/docs/providers/aws/events/sns.md +++ b/docs/providers/aws/events/sns.md @@ -50,6 +50,27 @@ functions: - sns: arn:xxx ``` +Or with intrinsic cloud formation function like `Fn::Join` or `Fn::GetAtt`, + +```yml +functions: + dispatcher: + handler: dispatcher.dispatch + events: + - sns: + arn: + Fn::Join: + - "" + - - "arn:aws:sns:" + - Ref: "AWS::Region" + - ":" + - Ref: "AWS::AccountId" + - ":MyCustomTopic" + topicName: MyCustomTopic +``` + +**Note:** It is important to know that `topicArn` must contain the value given in the `topicName` property. + ## Setting a display name This event definition ensures that the `aggregator` function gets called every time a message is sent to the diff --git a/lib/plugins/aws/deploy/compile/events/sns/index.js b/lib/plugins/aws/deploy/compile/events/sns/index.js index 4f46adad5..3ffdc805a 100644 --- a/lib/plugins/aws/deploy/compile/events/sns/index.js +++ b/lib/plugins/aws/deploy/compile/events/sns/index.js @@ -26,22 +26,55 @@ class AwsCompileSNSEvents { let displayName = ''; if (typeof event.sns === 'object') { - ['topicName', 'displayName'].forEach((property) => { - if (typeof event.sns[property] === 'string') { - return; + if (event.sns.arn) { + if (typeof event.sns.arn === 'object' || + typeof event.sns.arn === 'string') { + if (event.sns.topicName && typeof event.sns.topicName === 'string') { + topicArn = event.sns.arn; + topicName = event.sns.topicName; + } else { + const errorMessage = [ + 'Missing or invalid topicName property for sns event', + ` in function ${functionName}`, + ' The correct syntax is: sns: topic-name-or-arn', + ' OR an object with:', + ' - arn as object or string AND topicName as string OR', + ' - topicName AND displayName strings.', + ' Please check the docs for more info.', + ].join(''); + throw new this.serverless.classes + .Error(errorMessage); + } + } else { + const errorMessage = [ + 'Invalid value type provided .arn property for sns event', + ` in function ${functionName}`, + ' The correct types are: object, string.', + ' Please check the docs for more info.', + ].join(''); + throw new this.serverless.classes + .Error(errorMessage); } - const errorMessage = [ - `Missing or invalid "${property}" property for sns event`, - ` in function ${functionName}`, - ' The correct syntax is: sns: topic-name-or-arn', - ' OR an object with "topicName" AND "displayName" strings.', - ' Please check the docs for more info.', - ].join(''); - throw new this.serverless.classes - .Error(errorMessage); - }); - topicName = event.sns.topicName; - displayName = event.sns.displayName; + } else { + ['topicName', 'displayName'].forEach((property) => { + if (typeof event.sns[property] === 'string') { + return; + } + const errorMessage = [ + 'Missing or invalid topicName property for sns event', + ` in function ${functionName}`, + ' The correct syntax is: sns: topic-name-or-arn', + ' OR an object with:', + ' - arn as object or string AND topicName as string OR', + ' - topicName AND displayName strings.', + ' Please check the docs for more info.', + ].join(''); + throw new this.serverless.classes + .Error(errorMessage); + }); + displayName = event.sns.displayName; + topicName = event.sns.topicName; + } } else if (typeof event.sns === 'string') { if (event.sns.indexOf('arn:') === 0) { topicArn = event.sns; diff --git a/lib/plugins/aws/deploy/compile/events/sns/index.test.js b/lib/plugins/aws/deploy/compile/events/sns/index.test.js index ec0c50e2e..ee71a4eac 100644 --- a/lib/plugins/aws/deploy/compile/events/sns/index.test.js +++ b/lib/plugins/aws/deploy/compile/events/sns/index.test.js @@ -157,5 +157,49 @@ describe('AwsCompileSNSEvents', () => { .provider.compiledCloudFormationTemplate.Resources.FirstLambdaPermissionFooSNS.Type ).to.equal('AWS::Lambda::Permission'); }); + + + it('should raise error when only arn is present', () => { + awsCompileSNSEvents.serverless.service.functions = { + first: { + events: [ + { + sns: { + arn: 'arn:aws:sns:region:accountid:bar', + }, + }, + ], + }, + }; + + expect(() => { awsCompileSNSEvents.compileSNSEvents(); }).to.throw(Error); + }); + + it('should not create SNS topic when arn is provided', () => { + awsCompileSNSEvents.serverless.service.functions = { + first: { + events: [ + { + sns: { + topicName: 'bar', + arn: 'arn:aws:sns:region:accountid:bar', + }, + }, + ], + }, + }; + + awsCompileSNSEvents.compileSNSEvents(); + + expect(Object.keys(awsCompileSNSEvents.serverless.service + .provider.compiledCloudFormationTemplate.Resources) + ).to.have.length(2); + expect(awsCompileSNSEvents.serverless.service + .provider.compiledCloudFormationTemplate.Resources.FirstSnsSubscriptionBar.Type + ).to.equal('AWS::SNS::Subscription'); + expect(awsCompileSNSEvents.serverless.service + .provider.compiledCloudFormationTemplate.Resources.FirstLambdaPermissionBarSNS.Type + ).to.equal('AWS::Lambda::Permission'); + }); }); }); From 8a12bd96e317507f9752ee8ac59bc36679a1f684 Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Fri, 14 Apr 2017 09:32:59 +0200 Subject: [PATCH 2/4] Fix typo --- docs/providers/aws/events/sns.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/providers/aws/events/sns.md b/docs/providers/aws/events/sns.md index 44ca463a6..3481150fa 100644 --- a/docs/providers/aws/events/sns.md +++ b/docs/providers/aws/events/sns.md @@ -50,7 +50,7 @@ functions: - sns: arn:xxx ``` -Or with intrinsic cloud formation function like `Fn::Join` or `Fn::GetAtt`, +Or with intrinsic CloudFormation function like `Fn::Join` or `Fn::GetAtt`. ```yml functions: From 41d03aff8d686229a732e6bc05908abc9edc369c Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Fri, 14 Apr 2017 09:45:32 +0200 Subject: [PATCH 3/4] Update error messages --- .../aws/package/compile/events/sns/index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/sns/index.js b/lib/plugins/aws/package/compile/events/sns/index.js index 1094a1b4b..1fcba97d2 100644 --- a/lib/plugins/aws/package/compile/events/sns/index.js +++ b/lib/plugins/aws/package/compile/events/sns/index.js @@ -35,11 +35,11 @@ class AwsCompileSNSEvents { } else { const errorMessage = [ 'Missing or invalid topicName property for sns event', - ` in function ${functionName}`, + ` in function "${functionName}"`, ' The correct syntax is: sns: topic-name-or-arn', - ' OR an object with:', - ' - arn as object or string AND topicName as string OR', - ' - topicName AND displayName strings.', + ' OR an object with ', + ' arn and topicName OR', + ' topicName and displayName.', ' Please check the docs for more info.', ].join(''); throw new this.serverless.classes @@ -62,11 +62,11 @@ class AwsCompileSNSEvents { } const errorMessage = [ 'Missing or invalid topicName property for sns event', - ` in function ${functionName}`, + ` in function "${functionName}"`, ' The correct syntax is: sns: topic-name-or-arn', - ' OR an object with:', - ' - arn as object or string AND topicName as string OR', - ' - topicName AND displayName strings.', + ' OR an object with ', + ' arn and topicName OR', + ' topicName and displayName.', ' Please check the docs for more info.', ].join(''); throw new this.serverless.classes From 6f1547d42345f610d6bd0aeffe762cdf7b24fa3e Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Fri, 14 Apr 2017 09:46:24 +0200 Subject: [PATCH 4/4] Fix typo in test --- lib/plugins/aws/package/compile/events/sns/index.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/sns/index.test.js b/lib/plugins/aws/package/compile/events/sns/index.test.js index ee71a4eac..d59af889e 100644 --- a/lib/plugins/aws/package/compile/events/sns/index.test.js +++ b/lib/plugins/aws/package/compile/events/sns/index.test.js @@ -158,8 +158,7 @@ describe('AwsCompileSNSEvents', () => { ).to.equal('AWS::Lambda::Permission'); }); - - it('should raise error when only arn is present', () => { + it('should raise an error when only arn is present', () => { awsCompileSNSEvents.serverless.service.functions = { first: { events: [