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 sqs event schema #8227

Merged
merged 4 commits into from Sep 22, 2020
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
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