From e0c80c53a779dbd31a77e8a044cf202aea209895 Mon Sep 17 00:00:00 2001 From: ozwi Date: Mon, 14 Sep 2020 14:52:25 +0300 Subject: [PATCH 1/6] aws cloudwatch event schema --- .../compile/events/cloudWatchEvent/index.js | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js b/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js index 2f6646423df..653c1ec12ce 100644 --- a/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js +++ b/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js @@ -11,9 +11,31 @@ class AwsCompileCloudWatchEventEvents { 'package:compileEvents': this.compileCloudWatchEventEvents.bind(this), }; - // TODO: Complete schema, see https://github.com/serverless/serverless/issues/8026 this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'cloudwatchEvent', { type: 'object', + properties: { + event: { type: 'object' }, + input: { type: 'string', maxLength: 8192 }, + inputPath: { type: 'string', maxLength: 256 }, + inputTransformer: { + type: 'object', + properties: { + inputPathsMap: { type: 'object' }, + inputTemplates: { type: 'string', maxLength: 8192 }, + }, + required: ['inputTemplates'], + additionalProperties: false, + }, + description: { type: 'string', maxLength: 512 }, + name: { type: 'string', pattern: '[a-zA-Z0-9-_.]+', maxLength: 64 }, + enabled: { type: 'boolean' }, + }, + oneOf: [ + { required: ['input'] }, + { required: ['inputPath'] }, + { required: ['inputTransformer'] }, + ], + additionalProperties: false, }); } From e425e1cc24dbdd0536c55eb0c976c5c1aac961f2 Mon Sep 17 00:00:00 2001 From: ozwi Date: Tue, 15 Sep 2020 10:46:19 +0300 Subject: [PATCH 2/6] extend schema --- .../compile/events/cloudWatchEvent/index.js | 94 +++++++------------ 1 file changed, 33 insertions(+), 61 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js b/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js index 653c1ec12ce..80d42d3b614 100644 --- a/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js +++ b/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js @@ -15,15 +15,23 @@ class AwsCompileCloudWatchEventEvents { type: 'object', properties: { event: { type: 'object' }, - input: { type: 'string', maxLength: 8192 }, - inputPath: { type: 'string', maxLength: 256 }, + input: { + oneOf: [ + { type: 'string', maxLength: 8192 }, + { type: 'object' }, + ] + }, + inputPath: { type: 'string', minLength: 1, maxLength: 256 }, inputTransformer: { type: 'object', properties: { - inputPathsMap: { type: 'object' }, - inputTemplates: { type: 'string', maxLength: 8192 }, + inputPathsMap: { + type: 'object', + additionalProperties: { type: 'string', minLength: 1 }, + }, + inputTemplate: { type: 'string', minLength: 1, maxLength: 8192 }, }, - required: ['inputTemplates'], + required: ['inputTemplate'], additionalProperties: false, }, description: { type: 'string', maxLength: 512 }, @@ -48,60 +56,30 @@ class AwsCompileCloudWatchEventEvents { functionObj.events.forEach(event => { if (event.cloudwatchEvent) { cloudWatchEventNumberInFunction++; - let EventPattern; let State; let Input; - let InputPath; let InputTransformer; - let Description; - let Name; - - if (typeof event.cloudwatchEvent === 'object') { - if (!event.cloudwatchEvent.event) { - const errorMessage = [ - `Missing "event" property for cloudwatch event in function ${functionName}`, - ' Please check the docs for more info.', - ].join(''); - throw new this.serverless.classes.Error(errorMessage); - } - - EventPattern = JSON.stringify(event.cloudwatchEvent.event); - State = 'ENABLED'; - if (event.cloudwatchEvent.enabled === false) { - State = 'DISABLED'; - } - Input = event.cloudwatchEvent.input; - InputPath = event.cloudwatchEvent.inputPath; - InputTransformer = event.cloudwatchEvent.inputTransformer; - Description = event.cloudwatchEvent.description; - Name = event.cloudwatchEvent.name; - - const inputOptions = [Input, InputPath, InputTransformer].filter(i => i); - if (inputOptions.length > 1) { - const errorMessage = [ - 'You can only set one of input, inputPath, or inputTransformer ', - 'properties at the same time for cloudwatch events. ', - 'Please check the AWS docs for more info', - ].join(''); - throw new this.serverless.classes.Error(errorMessage); - } - if (Input && typeof Input === 'object') { - Input = JSON.stringify(Input); - } - if (Input && typeof Input === 'string') { - // escape quotes to favor JSON.parse - Input = Input.replace(/\"/g, '\\"'); // eslint-disable-line - } - if (InputTransformer) { - InputTransformer = this.formatInputTransformer(InputTransformer); - } - } else { - const errorMessage = [ - `CloudWatch event of function "${functionName}" is not an object`, - ' Please check the docs for more info.', - ].join(''); - throw new this.serverless.classes.Error(errorMessage); + const EventPattern = JSON.stringify(event.cloudwatchEvent.event); + State = 'ENABLED'; + if (event.cloudwatchEvent.enabled === false) { + State = 'DISABLED'; + } + const Name = event.cloudwatchEvent.name; + const InputPath = event.cloudwatchEvent.inputPath; + const Description = event.cloudwatchEvent.description; + Input = event.cloudwatchEvent.input; + InputTransformer = event.cloudwatchEvent.inputTransformer; + + if (Input && typeof Input === 'object') { + Input = JSON.stringify(Input); + } + if (Input && typeof Input === 'string') { + // escape quotes to favor JSON.parse + Input = Input.replace(/\"/g, '\\"'); // eslint-disable-line + } + if (InputTransformer) { + InputTransformer = this.formatInputTransformer(InputTransformer); } const lambdaLogicalId = this.provider.naming.getLambdaLogicalId(functionName); @@ -166,12 +144,6 @@ class AwsCompileCloudWatchEventEvents { } formatInputTransformer(inputTransformer) { - if (!inputTransformer.inputTemplate) { - throw new this.serverless.classes.Error( - 'The inputTemplate key is required when specifying an ' + - 'inputTransformer for a cloudwatchEvent event' - ); - } const cfmOutput = { // InputTemplate is required InputTemplate: inputTransformer.inputTemplate, From 3eb322b1a483d7779b782d1ecc5688aa90add835 Mon Sep 17 00:00:00 2001 From: ozwi Date: Tue, 15 Sep 2020 10:46:27 +0300 Subject: [PATCH 3/6] remove schema validation tests --- .../events/cloudWatchEvent/index.test.js | 123 ------------------ 1 file changed, 123 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.test.js b/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.test.js index 685da854056..6c69a814c21 100644 --- a/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.test.js +++ b/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.test.js @@ -23,48 +23,6 @@ describe('awsCompileCloudWatchEventEvents', () => { }); describe('#compileCloudWatchEventEvents()', () => { - it('should throw an error if cloudwatch event type is not an object', () => { - awsCompileCloudWatchEventEvents.serverless.service.functions = { - first: { - events: [ - { - cloudwatchEvent: 42, - }, - ], - }, - }; - - expect(() => awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents()).to.throw(Error); - - awsCompileCloudWatchEventEvents.serverless.service.functions = { - first: { - events: [ - { - cloudwatchEvent: '42', - }, - ], - }, - }; - - expect(() => awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents()).to.throw(Error); - }); - - it('should throw an error if the "event" property is not given', () => { - awsCompileCloudWatchEventEvents.serverless.service.functions = { - first: { - events: [ - { - cloudwatchEvent: { - event: null, - }, - }, - ], - }, - }; - - expect(() => awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents()).to.throw(Error); - }); - it('should create corresponding resources when cloudwatch events are given', () => { awsCompileCloudWatchEventEvents.serverless.service.functions = { first: { @@ -340,87 +298,6 @@ describe('awsCompileCloudWatchEventEvents', () => { ).to.equal('{"key":"value"}'); }); - it('should throw an error when both Input and InputPath are set', () => { - awsCompileCloudWatchEventEvents.serverless.service.functions = { - first: { - events: [ - { - cloudwatchEvent: { - event: { - 'source': ['aws.ec2'], - 'detail-type': ['EC2 Instance State-change Notification'], - 'detail': { state: ['pending'] }, - }, - enabled: false, - input: { - key: 'value', - }, - inputPath: '$.stageVariables', - }, - }, - ], - }, - }; - - expect(() => awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents()).to.throw(Error); - }); - - it('should throw an error when both Input and InputTransformer are set', () => { - awsCompileCloudWatchEventEvents.serverless.service.functions = { - first: { - events: [ - { - cloudwatchEvent: { - event: { - 'source': ['aws.ec2'], - 'detail-type': ['EC2 Instance State-change Notification'], - 'detail': { state: ['pending'] }, - }, - enabled: false, - input: { - key: 'value', - }, - inputTransformer: { - inputPathsMap: { - eventTime: '$.time', - }, - inputTemplate: '{"time": , "key1": "value1"}', - }, - }, - }, - ], - }, - }; - - expect(() => awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents()).to.throw(Error); - }); - - it('should throw an error when inputTransformer does not have inputTemplate', () => { - awsCompileCloudWatchEventEvents.serverless.service.functions = { - first: { - events: [ - { - cloudwatchEvent: { - event: { - 'source': ['aws.ec2'], - 'detail-type': ['EC2 Instance State-change Notification'], - 'detail': { state: ['pending'] }, - }, - enabled: false, - inputTransformer: { - inputPathsMap: { - eventTime: '$.time', - }, - }, - }, - }, - ], - }, - }; - - expect(() => awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents()).to.throw(Error); - }); - it('should respect variables if multi-line variables is given', () => { awsCompileCloudWatchEventEvents.serverless.service.functions = { first: { From 9fcb264ea13e5e71f5be899e15853e811b05917f Mon Sep 17 00:00:00 2001 From: ozwi Date: Tue, 15 Sep 2020 10:47:24 +0300 Subject: [PATCH 4/6] prettify --- .../aws/package/compile/events/cloudWatchEvent/index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js b/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js index 80d42d3b614..d4fceb21e02 100644 --- a/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js +++ b/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js @@ -16,10 +16,7 @@ class AwsCompileCloudWatchEventEvents { properties: { event: { type: 'object' }, input: { - oneOf: [ - { type: 'string', maxLength: 8192 }, - { type: 'object' }, - ] + oneOf: [{ type: 'string', maxLength: 8192 }, { type: 'object' }], }, inputPath: { type: 'string', minLength: 1, maxLength: 256 }, inputTransformer: { From 4c53b771b0fca0d4ffde7269a2e765338d187553 Mon Sep 17 00:00:00 2001 From: ozwi Date: Tue, 15 Sep 2020 11:00:14 +0300 Subject: [PATCH 5/6] add minLength to name --- lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js b/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js index d4fceb21e02..f926db47351 100644 --- a/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js +++ b/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js @@ -32,7 +32,7 @@ class AwsCompileCloudWatchEventEvents { additionalProperties: false, }, description: { type: 'string', maxLength: 512 }, - name: { type: 'string', pattern: '[a-zA-Z0-9-_.]+', maxLength: 64 }, + name: { type: 'string', pattern: '[a-zA-Z0-9-_.]+', minLength: 1, maxLength: 64 }, enabled: { type: 'boolean' }, }, oneOf: [ From 5b1f3ad7cc2a9c5c4de3f3d6118274dbce1d6bf9 Mon Sep 17 00:00:00 2001 From: Oz Weiss Date: Tue, 15 Sep 2020 15:05:52 +0300 Subject: [PATCH 6/6] allow event without any input --- lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js b/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js index f926db47351..bba42f5e032 100644 --- a/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js +++ b/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js @@ -39,6 +39,7 @@ class AwsCompileCloudWatchEventEvents { { required: ['input'] }, { required: ['inputPath'] }, { required: ['inputTransformer'] }, + { required: [] }, ], additionalProperties: false, });