Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

feat: add support for notifications on existing sns topics #182

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
"pinst": "^2.1.4",
"prettier": "^2.2.1"
},
"peerDependencies": {
"serverless": "^2.4.0"
},
"engines": {
"node": ">=8.10.0"
},
Expand Down
22 changes: 22 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,28 @@ class AlertsPlugin {
} else {
alertTopics[key] = topic;
}

const subscriptions = (notifications || []).reduce((memo, n) => {
const nameSuffix = n.name || '';
const cfRef = `AwsAlerts${upperFirst(key)}${upperFirst(n.protocol)}${nameSuffix}Subscription`;

if (memo[cfRef]) {
throw new Error('Subscription with the same protocol already exists! Use the name property to uniquely identify it');
}

return Object.assign(memo, {
[cfRef]: {
Type: 'AWS::SNS::Subscription',
Properties: {
TopicArn: topic,
Protocol: n.protocol,
Endpoint: n.endpoint,
},
},
});
}, {});

this.addCfResources(subscriptions);
} else {
const cfRef = `AwsAlerts${
customAlarmName ? upperFirst(customAlarmName) : ''
Expand Down
89 changes: 88 additions & 1 deletion src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ describe('#index', () => {
});
});

it('should create SNS topic with notificaitons', () => {
it('should create SNS topic with notifications', () => {
const topicName = 'ok-topic';
const plugin = pluginFactory({
topics: {
Expand Down Expand Up @@ -528,6 +528,93 @@ describe('#index', () => {
});
});

it('should create SNS subscription for existing topic', () => {
const plugin = pluginFactory({
topics: {
ok: {
topic: {
Ref: 'AwsAlertsAlarm',
},
notifications: [
{
protocol: 'email',
endpoint: '[email protected]',
},
],
},
},
});

const config = plugin.getConfig();
const topics = plugin.compileAlertTopics(config);

expect(topics).toEqual({
ok: {
Ref: `AwsAlertsAlarm`,
},
});

expect(
plugin.serverless.service.provider.compiledCloudFormationTemplate
.Resources
).toEqual({
AwsAlertsOkEmailSubscription: {
Type: 'AWS::SNS::Subscription',
Properties: {
TopicArn: {
Ref: 'AwsAlertsAlarm',
},
Protocol: 'email',
Endpoint: '[email protected]',
},
},
});
});

it('should create SNS subscription for existing topic with a custom name suffix', () => {
const plugin = pluginFactory({
topics: {
ok: {
topic: {
Ref: 'AwsAlertsAlarm',
},
notifications: [
{
name: 'Test',
protocol: 'email',
endpoint: '[email protected]',
},
],
},
},
});

const config = plugin.getConfig();
const topics = plugin.compileAlertTopics(config);

expect(topics).toEqual({
ok: {
Ref: `AwsAlertsAlarm`,
},
});

expect(
plugin.serverless.service.provider.compiledCloudFormationTemplate
.Resources
).toEqual({
AwsAlertsOkEmailTestSubscription: {
Type: 'AWS::SNS::Subscription',
Properties: {
TopicArn: {
Ref: 'AwsAlertsAlarm',
},
Protocol: 'email',
Endpoint: '[email protected]',
},
},
});
});

it('should create SNS topic with nested definitions', () => {
const plugin = pluginFactory({
topics: {
Expand Down