From 14321aa62484211d784d9e7c45b08e377130a14a Mon Sep 17 00:00:00 2001 From: Martin Booth Date: Thu, 7 Apr 2016 11:39:50 +1000 Subject: [PATCH] Allow topicArn config for sns event instead of topicName --- lib/actions/EventLambdaSNS.js | 8 +++++--- lib/actions/EventRemove.js | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/actions/EventLambdaSNS.js b/lib/actions/EventLambdaSNS.js index 15382fd9a..03b6fff27 100644 --- a/lib/actions/EventLambdaSNS.js +++ b/lib/actions/EventLambdaSNS.js @@ -49,8 +49,10 @@ module.exports = function(S) { functionName = event.getFunction().getDeployedName(_this.evt.options), statementId = 'sEvents-' + functionName + '-' + event.name + '-' + _this.evt.options.stage, awsAccountId = _this.aws.getAccountId(_this.evt.options.stage, _this.evt.options.region), - topicArn = 'arn:aws:sns:' + _this.evt.options.region + ':' + awsAccountId + ':' + populatedEvent.config.topicName, - lambdaArn = 'arn:aws:lambda:' + _this.evt.options.region + ':' + awsAccountId + ':function:' + functionName + ':' + _this.evt.options.stage; + lambdaArn = 'arn:aws:lambda:' + _this.evt.options.region + ':' + awsAccountId + ':function:' + functionName + ':' + _this.evt.options.stage, + topic = populatedEvent.config.topic || populatedEvent.config.topicName, + topicArn = topic && topic.indexOf('arn:') === 0 ? topic : ('arn:aws:sns:' + _this.evt.options.region + ':' + awsAccountId + ':' + topic), + topicRegion = /arn:aws:sns:(.*):(.*):(.*)/.exec(topicArn)[1]; let params = { FunctionName: lambdaArn, @@ -81,7 +83,7 @@ module.exports = function(S) { TopicArn: topicArn, Endpoint: lambdaArn }; - return _this.aws.request('SNS', 'subscribe', params, _this.evt.options.stage, _this.evt.options.region) + return _this.aws.request('SNS', 'subscribe', params, _this.evt.options.stage, topicRegion) }) .then(function(data){ SUtils.sDebug(`Subscription to SNS topic ${topicArn} added for lambda ${lambdaArn}`); diff --git a/lib/actions/EventRemove.js b/lib/actions/EventRemove.js index e768a6eea..1981da299 100644 --- a/lib/actions/EventRemove.js +++ b/lib/actions/EventRemove.js @@ -276,13 +276,15 @@ class EventRemover extends S.classes.Plugin { awsAccountId = this.aws.getAccountId(stage, region), Endpoint = 'arn:aws:lambda:' + region + ':' + awsAccountId + ':function:' + functionName + ':' + stage, populatedEvent = event.toObjectPopulated({stage, region}), - TopicArn = 'arn:aws:sns:' + region + ':' + awsAccountId + ':' + populatedEvent.config.topicName; + topic = populatedEvent.config.topic || populatedEvent.config.topicName, + TopicArn = topic && topic.indexOf('arn:') === 0 ? topic : ('arn:aws:sns:' + region + ':' + awsAccountId + ':' + topic), + TopicRegion = /arn:aws:sns:(.*):(.*):(.*)/.exec(TopicArn)[1]; - return this._SNSlistSubscriptionsByTopic(TopicArn, stage, region) + return this._SNSlistSubscriptionsByTopic(TopicArn, stage, TopicRegion) .then((subscriptions) => _.filter(subscriptions, {Endpoint})) .then((subscriptions) => subscriptions.length && subscriptions || BbPromise.reject(new SError(`Subscription for "${event.name}" is not found`))) .map((subscription) => subscription.SubscriptionArn) - .map((SubscriptionArn) => this.aws.request('SNS', 'unsubscribe', {SubscriptionArn}, stage, region)); + .map((SubscriptionArn) => this.aws.request('SNS', 'unsubscribe', {SubscriptionArn}, stage, TopicRegion)); } _SNSlistSubscriptionsByTopic(TopicArn, stage, region, NextToken, subscriptions) {