This repository has been archived by the owner on Oct 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsqs_queue.go
220 lines (176 loc) · 5.42 KB
/
sqs_queue.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package grim
// Copyright 2015 MediaMath <http://www.mediamath.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import (
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
)
var policyFormat = `{
"Version": "2008-10-17",
"Id": "grim-policy",
"Statement": [
{
"Sid": "1",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "SQS:*",
"Resource": "%v",
"Condition": {
"ArnEquals": {
"aws:SourceArn": %v
}
}
}
]
}`
type sqsQueue struct {
URL string
ARN string
}
func prepareSQSQueue(key, secret, region, queue string) (*sqsQueue, error) {
session := getSession(key, secret, region)
queueURL, err := getQueueURLByName(session, queue)
if err != nil {
queueURL, err = createQueue(session, queue)
}
if err != nil {
return nil, err
}
queueARN, err := getARNForQueueURL(session, queueURL)
if err != nil {
return nil, err
}
return &sqsQueue{queueURL, queueARN}, nil
}
func getNextMessage(key, secret, region, queueURL string) (string, error) {
session := getSession(key, secret, region)
message, err := getMessage(session, queueURL)
if err != nil {
return "", err
} else if message == nil || message.ReceiptHandle == nil {
return "", nil
}
err = deleteMessage(session, queueURL, *message.ReceiptHandle)
if err != nil {
return "", err
}
if message.Body == nil {
return "", nil
}
return *message.Body, nil
}
func setPolicy(key, secret, region, queueARN, queueURL string, topicARNs []string) error {
svc := sqs.New(getSession(key, secret, region))
bs, err := json.Marshal(topicARNs)
if err != nil {
return fmt.Errorf("error while creating policy for SQS queue: %v", err)
}
policy := fmt.Sprintf(policyFormat, queueARN, string(bs))
params := &sqs.SetQueueAttributesInput{
Attributes: map[string]*string{
"Policy": aws.String(policy),
},
QueueUrl: aws.String(queueURL),
}
_, err = svc.SetQueueAttributes(params)
if awserr, ok := err.(awserr.Error); ok {
return fmt.Errorf("aws error while setting policy for SQS queue: %v %v", awserr.Code(), awserr.Message())
} else if err != nil {
return fmt.Errorf("error while setting policy for SQS queue: %v", err)
}
return nil
}
func getQueueURLByName(session *session.Session, queue string) (string, error) {
svc := sqs.New(session)
params := &sqs.GetQueueUrlInput{
QueueName: aws.String(queue),
}
resp, err := svc.GetQueueUrl(params)
if awserr, ok := err.(awserr.Error); ok {
return "", fmt.Errorf("aws error while getting URL for SQS queue: %v %v", awserr.Code(), awserr.Message())
} else if err != nil {
return "", fmt.Errorf("error while getting URL for SQS queue: %v", err)
} else if resp == nil || resp.QueueUrl == nil {
return "", nil
}
return *resp.QueueUrl, nil
}
func getARNForQueueURL(session *session.Session, queueURL string) (string, error) {
svc := sqs.New(session)
arnKey := "QueueArn"
params := &sqs.GetQueueAttributesInput{
QueueUrl: aws.String(string(queueURL)),
AttributeNames: []*string{
aws.String(arnKey),
},
}
resp, err := svc.GetQueueAttributes(params)
if awserr, ok := err.(awserr.Error); ok {
return "", fmt.Errorf("aws error while getting ARN for SQS queue: %v %v", awserr.Code(), awserr.Message())
} else if err != nil {
return "", fmt.Errorf("error while getting ARN for SQS queue: %v", err)
} else if resp == nil || resp.Attributes == nil {
return "", nil
}
atts := resp.Attributes
arnPtr, ok := atts[arnKey]
if !ok || arnPtr == nil {
return "", nil
}
return *arnPtr, nil
}
func createQueue(session *session.Session, queue string) (string, error) {
svc := sqs.New(session)
params := &sqs.CreateQueueInput{
QueueName: aws.String(queue),
Attributes: map[string]*string{
"ReceiveMessageWaitTimeSeconds": aws.String("5"),
},
}
resp, err := svc.CreateQueue(params)
if awserr, ok := err.(awserr.Error); ok {
return "", fmt.Errorf("aws error while creating SQS queue: %v %v", awserr.Code(), awserr.Message())
} else if err != nil {
return "", fmt.Errorf("error while creating SQS queue: %v", err)
} else if resp == nil || resp.QueueUrl == nil {
return "", nil
}
return *resp.QueueUrl, nil
}
func getMessage(session *session.Session, queueURL string) (*sqs.Message, error) {
svc := sqs.New(session)
params := &sqs.ReceiveMessageInput{
QueueUrl: aws.String(queueURL),
MaxNumberOfMessages: aws.Int64(1),
}
resp, err := svc.ReceiveMessage(params)
if awserr, ok := err.(awserr.Error); ok {
return nil, fmt.Errorf("aws error while receiving message from SQS: %v %v", awserr.Code(), awserr.Message())
} else if err != nil {
return nil, fmt.Errorf("error while receiving message from SQS: %v", err)
} else if resp == nil || len(resp.Messages) == 0 {
return nil, nil
}
return resp.Messages[0], nil
}
func deleteMessage(session *session.Session, queueURL string, receiptHandle string) error {
svc := sqs.New(session)
params := &sqs.DeleteMessageInput{
QueueUrl: aws.String(queueURL),
ReceiptHandle: aws.String(receiptHandle),
}
_, err := svc.DeleteMessage(params)
if awserr, ok := err.(awserr.Error); ok {
return fmt.Errorf("aws error while deleting message from SQS: %v %v", awserr.Code(), awserr.Message())
} else if err != nil {
return fmt.Errorf("error while deleting message from SQS: %v", err)
}
return nil
}