Navigation Menu

Skip to content

Commit

Permalink
feat(Config Schema): Schame for AWS sqs event (#8227)
Browse files Browse the repository at this point in the history
  • Loading branch information
thewizarodofoz committed Sep 22, 2020
1 parent feece9a commit 4f96ce1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 88 deletions.
52 changes: 13 additions & 39 deletions lib/plugins/aws/package/compile/events/sqs/index.js
Expand Up @@ -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,
},
],
});
}

Expand All @@ -35,51 +46,14 @@ 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: <QueueArn>',
' 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') {
Enabled = event.sqs.enabled;
}
} 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: <QueueArn>',
' 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() {
Expand Down
49 changes: 0 additions & 49 deletions lib/plugins/aws/package/compile/events/sqs/index.test.js
Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 4f96ce1

Please sign in to comment.