Repository: ACloudGuru/serverless-plugin-aws-alerts (js)

Issue:
Notifications for SNS topics
Add notification configuration for SNS topics that are created by the plugin.

Pull request description:
Support notifications within the plugin
Closes #14 

Patch:
diff --git a/example/serverless.yml b/example/serverless.yml
index 593624d..c22131e 100644
--- a/example/serverless.yml
+++ b/example/serverless.yml
@@ -9,18 +9,25 @@ package:
     - yarn.lock
 
 custom:
+  notifications:
+    - protocol: email
+      endpoint: john@acloud.guru
+
   alerts:
     stages: # Optionally - select which stages to deploy alarms to
       - producton
       - staging
     topics:
-      ok: ${self:service}-${opt:stage}-alerts-ok
-      alarm: ${self:service}-${opt:stage}-alerts-alarm
-      insufficientData: ${self:service}-${opt:stage}-alerts-insufficientData
-    # notifications:
-    #   - protocol: email
-    #     endpoint: contact@johncmckim.me
-    #   - protocol: 
+      ok: 
+        topic: ${self:service}-${opt:stage}-alerts-ok
+        notifications: ${self:custom.notifications}
+      alarm:
+        topic: ${self:service}-${opt:stage}-alerts-alarm
+        notifications: ${self:custom.notifications}
+      insufficientData:
+        topic: ${self:service}-${opt:stage}-alerts-insufficientData
+        notifications: ${self:custom.notifications}
+
     definitions:  # these defaults are merged with your definitions
       functionErrors:
         period: 600
diff --git a/package.json b/package.json
index 7a84079..d8408a8 100644
--- a/package.json
+++ b/package.json
@@ -14,7 +14,7 @@
     "url": "https://github.com/ACloudGuru/serverless-plugin-aws-alerts.git",
     "type": "git"
   },
-  "author": "John McKim <johncmckim@gmail.com>",
+  "author": "John McKim <john@acloud.guru>",
   "license": "MIT",
   "dependencies": {
     "lodash": "^4.16.6"
diff --git a/src/index.js b/src/index.js
index afb3968..3111b6e 100644
--- a/src/index.js
+++ b/src/index.js
@@ -119,11 +119,17 @@ class Plugin {
 		};
 	}
 
-	getSnsTopicCloudFormation(topicName) {
+	getSnsTopicCloudFormation(topicName, notifications) {
+		const subscription = (notifications || []).map((n) => ({
+			Protocol: n.protocol,
+			Endpoint: n.endpoint
+		}));
+
 		return {
 			Type: 'AWS::SNS::Topic',
 			Properties: {
 				TopicName: topicName,
+				Subscription: subscription,
 			}
 		};
 	}
@@ -133,17 +139,23 @@ class Plugin {
 
 		if(config.topics) {
 			Object.keys(config.topics).forEach((key) => {
-				const topic = config.topics[key];
-
-				if (topic.indexOf('arn:') === 0) {
-					alertTopics[key] = topic;
-				} else {
-					const cfRef = `AwsAlerts${_.upperFirst(key)}`;
-					alertTopics[key] = { Ref: cfRef };
-
-					this.addCfResources({
-						[cfRef]: this.getSnsTopicCloudFormation(topic),
-					});
+				const topicConfig = config.topics[key];
+				const isTopicConfigAnObject = _.isObject(topicConfig);
+
+				const topic = isTopicConfigAnObject ? topicConfig.topic : topicConfig;
+				const notifications = isTopicConfigAnObject ? topicConfig.notifications : [];
+
+				if(topic) {
+					if (topic.indexOf('arn:') === 0) {
+						alertTopics[key] = topic;
+					} else {
+						const cfRef = `AwsAlerts${_.upperFirst(key)}`;
+						alertTopics[key] = { Ref: cfRef };
+
+						this.addCfResources({
+							[cfRef]: this.getSnsTopicCloudFormation(topic, notifications),
+						});
+					}
 				}
 			});
 		}


Test patch:
diff --git a/src/index.test.js b/src/index.test.js
index 534c6a2..9bcb3bd 100644
--- a/src/index.test.js
+++ b/src/index.test.js
@@ -397,7 +397,43 @@ describe('#index', function () {
 				'AwsAlertsOk': {
 					Type: 'AWS::SNS::Topic',
 					Properties: {
-						TopicName: topicName
+						TopicName: topicName,
+						Subscription: [],
+					}
+				}
+			});
+		});
+
+		it('should create SNS topic with notificaitons', () => {
+			const topicName = 'ok-topic';
+			const plugin = pluginFactory({
+				topics: {
+					ok: {
+						topic: topicName,
+						notifications: [{
+							protocol: 'email',
+							endpoint: 'test@email.com',
+						}]
+					}
+				}
+			});
+
+			const config = plugin.getConfig();
+			const topics = plugin.compileAlertTopics(config);
+
+			expect(topics).toEqual({
+				ok: { Ref: `AwsAlertsOk` }
+			});
+
+			expect(plugin.serverless.service.provider.compiledCloudFormationTemplate.Resources).toEqual({
+				'AwsAlertsOk': {
+					Type: 'AWS::SNS::Topic',
+					Properties: {
+						TopicName: topicName,
+						Subscription: [{
+							Protocol: 'email',
+							Endpoint: 'test@email.com',
+						}],
 					}
 				}
 			});