forked from goAWS/cloudformationresources
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnsLambdaEventSourceResource.go
87 lines (77 loc) · 2.88 KB
/
snsLambdaEventSourceResource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cloudformationresources
import (
"fmt"
"github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
gocf "github.com/mweagle/go-cloudformation"
)
// SNSLambdaEventSourceResource is a simple POC showing how to create custom resources
type SNSLambdaEventSourceResource struct {
GoAWSCustomResource
LambdaTargetArn *gocf.StringExpr
SNSTopicArn *gocf.StringExpr
}
func (command SNSLambdaEventSourceResource) updateRegistration(isTargetActive bool,
session *session.Session,
logger *logrus.Logger) (map[string]interface{}, error) {
// Get the current subscriptions...
snsSvc := sns.New(session)
snsInput := &sns.ListSubscriptionsByTopicInput{
TopicArn: aws.String(command.SNSTopicArn.Literal),
}
listSubscriptions, listSubscriptionsErr := snsSvc.ListSubscriptionsByTopic(snsInput)
if nil != listSubscriptionsErr {
return nil, listSubscriptionsErr
}
var lambdaSubscriptionArn string
for _, eachSubscription := range listSubscriptions.Subscriptions {
if *eachSubscription.Protocol == "lambda" && *eachSubscription.Endpoint == command.LambdaTargetArn.Literal {
if "" != lambdaSubscriptionArn {
return nil, fmt.Errorf("Multiple SNS %s registrations found for lambda: %s",
*snsInput.TopicArn,
command.LambdaTargetArn.Literal)
}
lambdaSubscriptionArn = *eachSubscription.SubscriptionArn
}
}
// Just log it...
logger.WithFields(logrus.Fields{
"SNSTopicArn": command.SNSTopicArn,
"LambdaArn": command.LambdaTargetArn,
"ExistingSubscriptionArn": lambdaSubscriptionArn,
}).Info("Current SNS subscription status")
var opErr error
if isTargetActive && "" == lambdaSubscriptionArn {
subscribeInput := &sns.SubscribeInput{
Protocol: aws.String("lambda"),
TopicArn: aws.String(command.SNSTopicArn.Literal),
Endpoint: aws.String(command.LambdaTargetArn.Literal),
}
_, opErr = snsSvc.Subscribe(subscribeInput)
} else if !isTargetActive && "" != lambdaSubscriptionArn {
unsubscribeInput := &sns.UnsubscribeInput{
SubscriptionArn: aws.String(lambdaSubscriptionArn),
}
_, opErr = snsSvc.Unsubscribe(unsubscribeInput)
} else {
// Just log it...
logger.WithFields(logrus.Fields{
"Command": command,
}).Info("No SNS operation required")
}
return nil, opErr
}
func (command SNSLambdaEventSourceResource) create(session *session.Session,
logger *logrus.Logger) (map[string]interface{}, error) {
return command.updateRegistration(true, session, logger)
}
func (command SNSLambdaEventSourceResource) update(session *session.Session,
logger *logrus.Logger) (map[string]interface{}, error) {
return command.updateRegistration(true, session, logger)
}
func (command SNSLambdaEventSourceResource) delete(session *session.Session,
logger *logrus.Logger) (map[string]interface{}, error) {
return command.updateRegistration(false, session, logger)
}