Fix SNS event

This commit is contained in:
Me OutPerformIt 2017-04-11 20:30:33 +03:00
parent 2e3d30c05d
commit eedabf0cfb
3 changed files with 113 additions and 15 deletions

View File

@ -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

View File

@ -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;

View File

@ -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');
});
});
});