Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aws cloudwatchlog event schema #8228

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -233,6 +233,10 @@ class AwsProvider {
UpdateReplacePolicy: { type: 'string' },
Condition: { type: 'string' },
},
awsLogGroupName: {
type: 'string',
pattern: '^[/#A-Za-z0-9-_.]+$',
},
},
provider: {
properties: {
Expand Down