Skip to content

Commit

Permalink
move global lodash to eventdef.js, start using it
Browse files Browse the repository at this point in the history
  • Loading branch information
homeyjd committed Mar 26, 2019
1 parent 94e2647 commit 3c96fe5
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 78 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ env:
es6: true
node: true
mocha: true
globals:
_: readonly
extends:
- eslint:recommended
- plugin:lodash/canonical
- plugin:lodash/recommended
- plugin:promise/recommended
- plugin:import/errors
Expand Down
5 changes: 5 additions & 0 deletions src/eventdef.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const _ = require("lodash");

// Init Lodash as a global
// EventDef is a requirement for running a parser, so it's more appropriate
// to put this here instead of within index.js.
global._ = _;

/**
* Event abstraction to help with Lambda and SNS un-wrapping.
*/
Expand Down
78 changes: 37 additions & 41 deletions src/parsers/autoscaling.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,46 @@
//
// AWS Auto-Scaling event parser
//
const _ = require("lodash");
module.exports.matches = event =>
_.has(event.message, "AutoScalingGroupARN");

module.exports = {
module.exports.parse = event => {
const message = event.message;
const accountId = _.get(message, "AccountId");
//const requestId = _.get(message, "RequestId");
const arn = _.get(message, "AutoScalingGroupARN");
const groupName = _.get(message, "AutoScalingGroupName");
const service = _.get(message, "Service");
const eventName = _.get(message, "Event");

matches: event => _.has(event.message, "AutoScalingGroupARN"),

parse: event => {
const message = event.message;
const accountId = _.get(message, "AccountId");
//const requestId = _.get(message, "RequestId");
const arn = _.get(message, "AutoScalingGroupARN");
const groupName = _.get(message, "AutoScalingGroupName");
const service = _.get(message, "Service");
const eventName = _.get(message, "Event");

// Example ARN: arn:aws:autoscaling:{region}:{accountId}:autoScalingGroup:{group-id}:autoScalingGroupName/{group-name}
const arnParts = _.split(arn, ":");
let region;
if (arnParts.length >= 0) {
region = arnParts[3];
}
// Example ARN: arn:aws:autoscaling:{region}:{accountId}:autoScalingGroup:{group-id}:autoScalingGroupName/{group-name}
const arnParts = _.split(arn, ":");
let region;
if (arnParts.length >= 0) {
region = arnParts[3];
}

const signInLink = `https://${accountId}.signin.aws.amazon.com/console/ec2?region=${region}`;
const consoleLink = `https://console.aws.amazon.com/ec2/autoscaling/home?region=${region}#AutoScalingGroups:id=${groupName}`;
const signInLink = `https://${accountId}.signin.aws.amazon.com/console/ec2?region=${region}`;
const consoleLink = `https://console.aws.amazon.com/ec2/autoscaling/home?region=${region}#AutoScalingGroups:id=${groupName}`;

const text = `Auto Scaling triggered ${eventName} for service ${service}.`;
const text = `Auto Scaling triggered ${eventName} for service ${service}.`;

return event.attachmentWithDefaults({
author_name: `AWS AutoScaling (${region} - ${accountId})`,
author_link: signInLink,
title: `${groupName} - ${eventName}`,
title_link: consoleLink,
text,
fallback: text,
color: event.COLORS.neutral,
fields: [{
title: "Service",
value: service,
short: true,
}, {
title: "Event",
value: eventName,
short: true,
}]
});
}
return event.attachmentWithDefaults({
author_name: `AWS AutoScaling (${region} - ${accountId})`,
author_link: signInLink,
title: `${groupName} - ${eventName}`,
title_link: consoleLink,
text,
fallback: text,
color: event.COLORS.neutral,
fields: [{
title: "Service",
value: service,
short: true,
}, {
title: "Event",
value: eventName,
short: true,
}]
});
};
5 changes: 2 additions & 3 deletions src/parsers/aws-health.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//
// AWS Health Dashboard Message
//
const _ = require("lodash");

module.exports.matches = event => event.getSource() === "health";
module.exports.matches = event =>
event.getSource() === "health";

module.exports.parse = event => {
const message = event.message;
Expand Down
5 changes: 2 additions & 3 deletions src/parsers/batch-events.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//
// AWS Batch event parser
//
const _ = require("lodash");

module.exports.matches = event => event.getSource() === "batch";
module.exports.matches = event =>
event.getSource() === "batch";

module.exports.parse = event => {
const message = event.message;
Expand Down
4 changes: 1 addition & 3 deletions src/parsers/beanstalk.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//
// AWS Elastic Beanstalk event parser
//
const _ = require("lodash");

module.exports.matches = event =>
// Will only match SNS messages
_.startsWith(event.getSubject(), "AWS Elastic Beanstalk Notification");
Expand All @@ -14,7 +12,7 @@ module.exports.parse = event => {
if (!_.isEmpty(line) && _.includes(line, ":")) {
const key = _.trim(line.substr(0, line.indexOf(":")));
const value = _.trim(line.substr(key.length + 1));
return _.extend(returnObj, {[key]: value});
return _.extend(returnObj, { [key]: value });
}
return returnObj;
}, {});
Expand Down
2 changes: 0 additions & 2 deletions src/parsers/cloudformation.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//
// AWS CloudFormation event parser
//
const _ = require("lodash");

module.exports.matches = event =>
// Will only match SNS messages
_.startsWith(event.getSubject(), "AWS CloudFormation Notification");
Expand Down
8 changes: 5 additions & 3 deletions src/parsers/codebuild.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const _ = require("lodash");

module.exports.matches = event => event.getSource() === "codebuild";
//
// AWS CodeBuild events
//
module.exports.matches = event =>
event.getSource() === "codebuild";

module.exports.parse = event => {
const buildStatus = _.get(event, "detail.build-status");
Expand Down
5 changes: 2 additions & 3 deletions src/parsers/codedeployCloudWatch.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//
// AWS CodeDeploy via CloudWatch event rule
//
const _ = require("lodash");

module.exports.matches = event => event.getSource() === "codedeploy";
module.exports.matches = event =>
event.getSource() === "codedeploy";

module.exports.parse = event => {
const deployState = _.get(event, "detail.state");
Expand Down
2 changes: 0 additions & 2 deletions src/parsers/codedeploySns.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//
// AWS CodeDeploy SNS notifications
//
const _ = require("lodash");

module.exports.matches = event =>
_.has(event.message, "deploymentId") && _.has(event.message, "deploymentGroupName");

Expand Down
4 changes: 1 addition & 3 deletions src/parsers/codepipeline-approval.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//
// AWS CodePipeline Approval
// AWS CodePipeline Approval Stage parser
//
const _ = require("lodash");

module.exports.matches = event =>
_.has(event.message, "consoleLink")
&& _.has(event.message, "approval.pipelineName");
Expand Down
4 changes: 1 addition & 3 deletions src/parsers/codepipeline.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//
// AWS CodePipeline via CloudWatch event parser
// AWS CodePipeline event parser
//
const _ = require("lodash");

module.exports.matches = event =>
event.getSource() === "codepipeline"
&& !_.has(event.message, "approval.pipelineName");
Expand Down
12 changes: 7 additions & 5 deletions src/parsers/generic.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint lodash/prefer-lodash-method:0 */
const _ = require("lodash");

// Match every event
module.exports.matches = () => true;
//
// Generic event parser
// Should safely be able to parse ANY kind of message and generate a Slack Message containing
// the contents of that structure.
//
module.exports.matches = () => true; // Match every event

module.exports.parse = event => {
// Clone object so we can delete known keys
Expand Down Expand Up @@ -43,6 +44,7 @@ module.exports.parse = event => {
// Serialize the whole event data
fields = objectToFields(message);
text = fields ? ""
// eslint-disable-next-line lodash/prefer-lodash-method
: JSON.stringify(message, null, 2)
.replace(/^{\n/, "")
.replace(/\n}\n?$/, "");
Expand Down
4 changes: 1 addition & 3 deletions src/parsers/guardduty.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
//
// AWS GuardDuty event parser
//
const _ = require("lodash");

module.exports.matches = event =>
_.get(event.getSource(), "source") === "guardduty"
event.getSource() === "guardduty"
|| _.get(event.message, "detail.service.serviceName") === "guardduty";

module.exports.parse = event => {
Expand Down
2 changes: 0 additions & 2 deletions src/parsers/rds.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//
// RDS Message
//
const _ = require("lodash");

module.exports.matches = event =>
_.get(event.message, "Event Source") === "db-instance";

Expand Down
2 changes: 0 additions & 2 deletions src/parsers/ses-received.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//
// SES "Received" notifications incoming via SNS
//
const _ = require("lodash");

module.exports.matches = event =>
_.get(event.message, "notificationType") === "Received";

Expand Down

0 comments on commit 3c96fe5

Please sign in to comment.