From 4f96ce1042079c08578ef70ddbb4c2def32d6663 Mon Sep 17 00:00:00 2001 From: Oz Weiss Date: Tue, 22 Sep 2020 09:42:07 +0300 Subject: [PATCH] feat(Config Schema): Schame for AWS `sqs` event (#8227) --- .../aws/package/compile/events/sqs/index.js | 52 +++++-------------- .../package/compile/events/sqs/index.test.js | 49 ----------------- 2 files changed, 13 insertions(+), 88 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/sqs/index.js b/lib/plugins/aws/package/compile/events/sqs/index.js index edaaf6b7947..2f5a61dee26 100644 --- a/lib/plugins/aws/package/compile/events/sqs/index.js +++ b/lib/plugins/aws/package/compile/events/sqs/index.js @@ -12,9 +12,20 @@ class AwsCompileSQSEvents { 'package:compileEvents': this.compileSQSEvents.bind(this), }; - // TODO: Complete schema, see https://github.com/serverless/serverless/issues/8033 this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'sqs', { - anyOf: [{ type: 'string' }, { type: 'object' }], + oneOf: [ + { $ref: '#/definitions/awsArn' }, + { + type: 'object', + properties: { + arn: { $ref: '#/definitions/awsArn' }, + batchSize: { type: 'integer', minimum: 1, maximum: 10 }, + enabled: { type: 'boolean' }, + }, + required: ['arn'], + additionalProperties: false, + }, + ], }); } @@ -35,36 +46,7 @@ class AwsCompileSQSEvents { let BatchSize = 10; let Enabled = true; - // TODO validate arn syntax if (typeof event.sqs === 'object') { - if (!event.sqs.arn) { - const errorMessage = [ - `Missing "arn" property for sqs event in function "${functionName}"`, - ' The correct syntax is: sqs: ', - ' OR an object with an "arn" property.', - ' Please check the docs for more info.', - ].join(''); - throw new this.serverless.classes.Error(errorMessage); - } - if (typeof event.sqs.arn !== 'string') { - // for dynamic arns (GetAtt/ImportValue) - if ( - Object.keys(event.sqs.arn).length !== 1 || - !( - event.sqs.arn['Fn::ImportValue'] || - event.sqs.arn['Fn::GetAtt'] || - event.sqs.arn['Fn::Join'] - ) - ) { - const errorMessage = [ - `Bad dynamic ARN property on sqs event in function "${functionName}"`, - ' If you use a dynamic "arn" (such as with Fn::GetAtt or Fn::ImportValue)', - ' there must only be one key (either Fn::GetAtt or Fn::ImportValue) in the arn', - ' object. Please check the docs for more info.', - ].join(''); - throw new this.serverless.classes.Error(errorMessage); - } - } EventSourceArn = event.sqs.arn; BatchSize = event.sqs.batchSize || BatchSize; if (typeof event.sqs.enabled !== 'undefined') { @@ -72,14 +54,6 @@ class AwsCompileSQSEvents { } } else if (typeof event.sqs === 'string') { EventSourceArn = event.sqs; - } else { - const errorMessage = [ - `SQS event of function "${functionName}" is not an object nor a string`, - ' The correct syntax is: sqs: ', - ' OR an object with an "arn" property.', - ' Please check the docs for more info.', - ].join(''); - throw new this.serverless.classes.Error(errorMessage); } const queueName = (function() { diff --git a/lib/plugins/aws/package/compile/events/sqs/index.test.js b/lib/plugins/aws/package/compile/events/sqs/index.test.js index ed558c5dcec..c1e3e2c47ec 100644 --- a/lib/plugins/aws/package/compile/events/sqs/index.test.js +++ b/lib/plugins/aws/package/compile/events/sqs/index.test.js @@ -38,36 +38,6 @@ describe('AwsCompileSQSEvents', () => { }); describe('#compileSQSEvents()', () => { - it('should throw an error if sqs event type is not a string or an object', () => { - awsCompileSQSEvents.serverless.service.functions = { - first: { - events: [ - { - sqs: 42, - }, - ], - }, - }; - - expect(() => awsCompileSQSEvents.compileSQSEvents()).to.throw(Error); - }); - - it('should throw an error if the "arn" property is not given', () => { - awsCompileSQSEvents.serverless.service.functions = { - first: { - events: [ - { - sqs: { - arn: null, - }, - }, - ], - }, - }; - - expect(() => awsCompileSQSEvents.compileSQSEvents()).to.throw(Error); - }); - it('should not throw error or merge role statements if default policy is not present', () => { awsCompileSQSEvents.serverless.service.functions = { first: { @@ -501,25 +471,6 @@ describe('AwsCompileSQSEvents', () => { }); }); - it('fails if keys other than Fn::GetAtt/ImportValue/Join are used for dynamic ARNs', () => { - awsCompileSQSEvents.serverless.service.functions = { - first: { - events: [ - { - sqs: { - arn: { - 'Fn::GetAtt': ['SomeQueue', 'Arn'], - 'batchSize': 1, - }, - }, - }, - ], - }, - }; - - expect(() => awsCompileSQSEvents.compileSQSEvents()).to.throw(Error); - }); - it('should add the necessary IAM role statements', () => { awsCompileSQSEvents.serverless.service.functions = { first: {