From 42676d34d4cb33cb59fd54c6a78ed07c965146e5 Mon Sep 17 00:00:00 2001 From: Oz Weiss Date: Tue, 22 Sep 2020 10:38:29 +0300 Subject: [PATCH] feat(Config Schema): Schema for AWS `cloudwatchLog` event (#8228) --- .../compile/events/cloudWatchLog/index.js | 36 ++++------ .../events/cloudWatchLog/index.test.js | 72 ------------------- lib/plugins/aws/provider/awsProvider.js | 4 ++ 3 files changed, 16 insertions(+), 96 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js b/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js index 9c9eaee3a1a..63cb9a7ce4e 100644 --- a/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js +++ b/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js @@ -11,9 +11,19 @@ class AwsCompileCloudWatchLogEvents { 'package:compileEvents': this.compileCloudWatchLogEvents.bind(this), }; - // TODO: Complete schema, see https://github.com/serverless/serverless/issues/8027 this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'cloudwatchLog', { - anyOf: [{ type: 'string' }, { type: 'object' }], + anyOf: [ + { $ref: '#/definitions/awsLogGroupName' }, + { + type: 'object', + properties: { + logGroup: { $ref: '#/definitions/awsLogGroupName' }, + filter: { type: 'string' }, + }, + required: ['logGroup'], + additionalProperties: false, + }, + ], }); } @@ -34,22 +44,6 @@ class AwsCompileCloudWatchLogEvents { let FilterPattern; if (typeof event.cloudwatchLog === 'object') { - if (!event.cloudwatchLog.logGroup) { - const errorMessage = [ - 'Missing "logGroup" property for cloudwatchLog event ', - `in function ${functionName} Please check the docs for more info.`, - ].join(''); - throw new this.serverless.classes.Error(errorMessage); - } - - if (event.cloudwatchLog.filter && typeof event.cloudwatchLog.filter !== 'string') { - const errorMessage = [ - `"filter" property for cloudwatchLog event in function ${functionName} `, - 'should be string. Please check the docs for more info.', - ].join(''); - throw new this.serverless.classes.Error(errorMessage); - } - LogGroupName = event.cloudwatchLog.logGroup.replace(/\r?\n/g, ''); FilterPattern = event.cloudwatchLog.filter ? event.cloudwatchLog.filter.replace(/\r?\n/g, '') @@ -57,12 +51,6 @@ class AwsCompileCloudWatchLogEvents { } else if (typeof event.cloudwatchLog === 'string') { LogGroupName = event.cloudwatchLog.replace(/\r?\n/g, ''); FilterPattern = ''; - } else { - const errorMessage = [ - `cloudwatchLog event of function "${functionName}" is not an object or a string`, - ' Please check the docs for more info.', - ].join(''); - throw new this.serverless.classes.Error(errorMessage); } if (logGroupNames.includes(LogGroupName)) { diff --git a/lib/plugins/aws/package/compile/events/cloudWatchLog/index.test.js b/lib/plugins/aws/package/compile/events/cloudWatchLog/index.test.js index 7803c51814b..83e3e3a9ecd 100644 --- a/lib/plugins/aws/package/compile/events/cloudWatchLog/index.test.js +++ b/lib/plugins/aws/package/compile/events/cloudWatchLog/index.test.js @@ -23,46 +23,6 @@ describe('AwsCompileCloudWatchLogEvents', () => { }); describe('#compileCloudWatchLogEvents()', () => { - it('should throw an error if cloudwatchLog event type is not an object or a string', () => { - awsCompileCloudWatchLogEvents.serverless.service.functions = { - first: { - events: [ - { - cloudwatchLog: 42, - }, - ], - }, - }; - - expect(() => awsCompileCloudWatchLogEvents.compileCloudWatchLogEvents()).to.throw(Error); - - awsCompileCloudWatchLogEvents.serverless.service.functions = { - first: { - events: [ - { - cloudwatchLog: [42], - }, - ], - }, - }; - - expect(() => awsCompileCloudWatchLogEvents.compileCloudWatchLogEvents()).to.throw(Error); - }); - - it('should throw an error if the "logGroup" property is not given', () => { - awsCompileCloudWatchLogEvents.serverless.service.functions = { - first: { - events: [ - { - cloudwatchLog: {}, - }, - ], - }, - }; - - expect(() => awsCompileCloudWatchLogEvents.compileCloudWatchLogEvents()).to.throw(Error); - }); - it('should create corresponding resources when cloudwatchLog events are given', () => { awsCompileCloudWatchLogEvents.serverless.service.functions = { first: { @@ -199,38 +159,6 @@ describe('AwsCompileCloudWatchLogEvents', () => { ).to.equal(''); }); - it('should throw an error if "filter" variable is not a string', () => { - awsCompileCloudWatchLogEvents.serverless.service.functions = { - first: { - events: [ - { - cloudwatchLog: { - logGroup: '/aws/lambda/hello1', - filter: {}, - }, - }, - ], - }, - }; - - expect(() => awsCompileCloudWatchLogEvents.compileCloudWatchLogEvents()).to.throw(Error); - - awsCompileCloudWatchLogEvents.serverless.service.functions = { - first: { - events: [ - { - cloudwatchLog: { - logGroup: '/aws/lambda/hello1', - filter: [], - }, - }, - ], - }, - }; - - expect(() => awsCompileCloudWatchLogEvents.compileCloudWatchLogEvents()).to.throw(Error); - }); - it('should create corresponding resources when cloudwatchLog events are given as a string', () => { awsCompileCloudWatchLogEvents.serverless.service.functions = { first: { diff --git a/lib/plugins/aws/provider/awsProvider.js b/lib/plugins/aws/provider/awsProvider.js index 45bc0f1cb9a..7371766d409 100644 --- a/lib/plugins/aws/provider/awsProvider.js +++ b/lib/plugins/aws/provider/awsProvider.js @@ -314,6 +314,10 @@ class AwsProvider { }, additionalProperties: false, }, + awsLogGroupName: { + type: 'string', + pattern: '^[/#A-Za-z0-9-_.]+$', + }, }, provider: { properties: {