-
Notifications
You must be signed in to change notification settings - Fork 179
/
sns.go
90 lines (70 loc) · 2.16 KB
/
sns.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
88
89
90
package handler
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sns"
)
func HandleSNSSubcription(req Request) (string, map[string]string, error) {
defer recoverFailure(req)
switch req.RequestType {
case "Create":
fmt.Println("CREATING SNSSUBSCRIPTION")
fmt.Printf("req %+v\n", req)
return SNSSubscriptionCreate(req)
case "Update":
fmt.Println("UPDATING SNSSUBSCRIPTION")
fmt.Printf("req %+v\n", req)
return SNSSubscriptionUpdate(req)
case "Delete":
fmt.Println("DELETING SNSSUBSCRIPTION")
fmt.Printf("req %+v\n", req)
return SNSSubscriptionDelete(req)
}
return "", nil, fmt.Errorf("unknown RequestType: %s", req.RequestType)
}
func SNSSubscriptionCreate(req Request) (string, map[string]string, error) {
endpoint := req.ResourceProperties["Endpoint"].(string)
topicArn := req.ResourceProperties["TopicArn"].(string)
input := sns.SubscribeInput{
Endpoint: aws.String(endpoint),
Protocol: aws.String(req.ResourceProperties["Protocol"].(string)),
TopicArn: aws.String(topicArn),
}
_, err := SNS(req).Subscribe(&input)
if err != nil {
return "failed", nil, err
}
outputs := make(map[string]string)
outputs["Endpoint"] = endpoint
return endpoint, outputs, nil
}
func SNSSubscriptionUpdate(req Request) (string, map[string]string, error) {
return SNSSubscriptionCreate(req)
}
func SNSSubscriptionDelete(req Request) (string, map[string]string, error) {
if req.PhysicalResourceId == "failed" {
return req.PhysicalResourceId, nil, nil
}
topicArn := req.ResourceProperties["TopicArn"].(string)
params := &sns.ListSubscriptionsByTopicInput{
TopicArn: aws.String(topicArn),
}
resp, err := SNS(req).ListSubscriptionsByTopic(params)
if err != nil {
fmt.Printf("error: %s\n", err)
return req.PhysicalResourceId, nil, nil
}
for _, s := range resp.Subscriptions {
if *s.Endpoint == req.PhysicalResourceId {
_, err := SNS(req).Unsubscribe(&sns.UnsubscribeInput{
SubscriptionArn: aws.String(*s.SubscriptionArn),
})
if err != nil {
fmt.Printf("error: %s\n", err)
return req.PhysicalResourceId, nil, nil
}
return *s.Endpoint, nil, nil
}
}
return req.PhysicalResourceId, nil, nil
}