Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
feat(triggers): adding support for SNS -> SQS triggers (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
razlo authored and ranrib committed Dec 13, 2018
1 parent e5d5eac commit ddcc91d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/events/aws_sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const serverlessEvent = require('../proto/event_pb.js');
const eventInterface = require('../event.js');
const errorCode = require('../proto/error_code_pb.js');
const { STEP_ID_NAME } = require('../consts.js');
const resourceUtils = require('../resource_utils/sqs_utils.js');

const s3EventCreator = {
/**
Expand Down Expand Up @@ -192,6 +193,10 @@ const SQSEventCreator = {
'Message ID': `${response.data.Messages[0].MessageId}`,
'MD5 Of Message Body': `${response.data.Messages[0].MD5OfBody}`,
});
const snsData = resourceUtils.getSNSTrigger(response.data.Messages);
if (snsData != null) {
eventInterface.addToMetadata(event, { 'SNS Trigger': snsData });
}
}
eventInterface.addToMetadata(event, { 'Number Of Messages': messagesNumber });
break;
Expand Down
44 changes: 44 additions & 0 deletions src/resource_utils/sqs_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @fileoverview SQS utility functions
*/

/**
* If exists, gets the SNS message that triggered the SQS,
* and generates event data out of it.
* @param {object} messages The SQS messages object
* @returns {object} SNS event data json
*/
function getSNSTrigger(messages) {
let foundSnsEvent = null;
messages.some((message) => {
try {
let body = null;
if ('Body' in message) {
body = JSON.parse(message.Body);
} else if ('body' in message) {
body = JSON.parse(message.body);
} else {
return true;
}

if ('Type' in body &&
'MessageId' in body &&
'TopicArn' in body &&
'Message' in body &&
'Timestamp' in body &&
'SignatureVersion' in body &&
'Signature' in body) {
foundSnsEvent = body;
return true;
}
} catch (ex) {
// Continue to the next message
}

return true;
});

return foundSnsEvent;
}

module.exports.getSNSTrigger = getSNSTrigger;
5 changes: 5 additions & 0 deletions src/triggers/aws_lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const serverlessEvent = require('../proto/event_pb.js');
const errorCode = require('../proto/error_code_pb.js');
const eventInterface = require('../event.js');
const utils = require('../utils');
const resourceUtils = require('../resource_utils/sqs_utils.js');

/**
* Fills the common fields for a trigger event
Expand Down Expand Up @@ -111,6 +112,10 @@ function createSQSTrigger(event, trigger) {
}, {
'Message Body': event.Records[0].body,
});
const snsData = resourceUtils.getSNSTrigger(event.Records);
if (snsData != null) {
eventInterface.addToMetadata(trigger, { 'SNS Trigger': snsData });
}
}

/**
Expand Down

0 comments on commit ddcc91d

Please sign in to comment.