Skip to content

Commit

Permalink
feat(Config Schema): Schema for AWS cloudwatchLog event (#8228)
Browse files Browse the repository at this point in the history
  • Loading branch information
thewizarodofoz committed Sep 22, 2020
1 parent 1fceb89 commit 42676d3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 96 deletions.
36 changes: 12 additions & 24 deletions lib/plugins/aws/package/compile/events/cloudWatchLog/index.js
Expand Up @@ -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,
},
],
});
}

Expand All @@ -34,35 +44,13 @@ 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, '')
: '';
} 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)) {
Expand Down
72 changes: 0 additions & 72 deletions lib/plugins/aws/package/compile/events/cloudWatchLog/index.test.js
Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down
4 changes: 4 additions & 0 deletions lib/plugins/aws/provider/awsProvider.js
Expand Up @@ -314,6 +314,10 @@ class AwsProvider {
},
additionalProperties: false,
},
awsLogGroupName: {
type: 'string',
pattern: '^[/#A-Za-z0-9-_.]+$',
},
},
provider: {
properties: {
Expand Down

0 comments on commit 42676d3

Please sign in to comment.