-
Notifications
You must be signed in to change notification settings - Fork 2
/
sqs.go
234 lines (205 loc) · 5.88 KB
/
sqs.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package sqs
//TODO:work in progress
import (
"fmt"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
conf "github.com/hiromaily/golibs/config"
awslib "github.com/hiromaily/golibs/workinprogress/aws"
)
var svc *sqs.SQS
// SendMessageToQueue is to send message to queue
func SendMessageToQueue(params *sqs.SendMessageInput) {
req, resp := svc.SendMessageRequest(params)
err := req.Send()
if err == nil {
// resp is now filled
fmt.Println(resp)
}
}
// SendMultipleMessagesToQueue is to send multiple messages to queue
func SendMultipleMessagesToQueue(params *sqs.SendMessageBatchInput) {
req, resp := svc.SendMessageBatchRequest(params)
err := req.Send()
if err == nil {
// resp is now filled
fmt.Println(resp)
}
}
// PurgeQueue is to purge queue
// 削除するタイミングに制限あり(一定間隔内における連続削除はできない)
// http://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_PurgeQueue.html
func PurgeQueue(url *string) {
fmt.Println(*url)
params := &sqs.PurgeQueueInput{
QueueUrl: aws.String(*url), // Required
}
req, resp := svc.PurgeQueueRequest(params)
err := req.Send()
if err != nil { // resp is now filled
fmt.Println(err)
} else {
fmt.Println(resp)
}
}
// CreateSendMessageInput is to create input parameter
// TODO:このAttributesのパラメータは動的に変更せねばならない。
// TODO:contentTypeも動的に変更できるように
func CreateSendMessageInput(url *string, body *string, acid *string, ot string, ct string) (params *sqs.SendMessageInput) {
//fmt.Println(*url)
//fmt.Println(*body)
var acidData = "736afbdc388752eb"
if *acid != "" {
acidData = *acid
}
if ot != "2" {
params = &sqs.SendMessageInput{
MessageBody: aws.String(*body),
QueueUrl: aws.String(*url),
DelaySeconds: aws.Int64(0),
MessageAttributes: map[string]*sqs.MessageAttributeValue{
"operationType": {
DataType: aws.String("Number"),
StringValue: aws.String(ot),
},
"clientId": {
DataType: aws.String("String"),
StringValue: aws.String(acidData),
},
"contentType": {
DataType: aws.String("Number"),
StringValue: aws.String(ct),
},
},
}
} else {
params = &sqs.SendMessageInput{
MessageBody: aws.String(*body),
QueueUrl: aws.String(*url),
DelaySeconds: aws.Int64(0),
MessageAttributes: map[string]*sqs.MessageAttributeValue{
"operationType": {
DataType: aws.String("Number"),
StringValue: aws.String(ot),
},
"clientId": {
DataType: aws.String("String"),
StringValue: aws.String(acidData),
},
"messageScheduledId": {
DataType: aws.String("Number"),
StringValue: aws.String("1122"),
},
"trackingFlag": {
DataType: aws.String("Number"),
StringValue: aws.String("0"),
},
"contentType": {
DataType: aws.String("Number"),
StringValue: aws.String("99"),
},
},
}
}
return
}
// CreateSendMessageBatchInput is to create input parameter for batch
// up to ten messages!!
func CreateSendMessageBatchInput(url *string, body *string, acid *string, ot string, ct string, num int) (params *sqs.SendMessageBatchInput) {
var acidData = "736afbdc388752eb"
if *acid != "" {
acidData = *acid
}
conf := conf.GetConf()
if ot == "0" {
ot = conf.Aws.Sqs.MsgAttr.OpType
}
if ct == "0" {
ct = conf.Aws.Sqs.MsgAttr.OpType
}
var entries []*sqs.SendMessageBatchRequestEntry
for i := 0; i < num; i++ {
entry := &sqs.SendMessageBatchRequestEntry{
Id: aws.String(strconv.Itoa(i + 1)),
MessageBody: aws.String(*body),
//QueueUrl: aws.String(*url),
DelaySeconds: aws.Int64(0),
MessageAttributes: map[string]*sqs.MessageAttributeValue{
"operationType": {
DataType: aws.String("Number"),
StringValue: aws.String(ot),
},
"clientId": {
DataType: aws.String("String"),
StringValue: aws.String(acidData),
},
"contentType": {
DataType: aws.String("Number"),
StringValue: aws.String(ct),
},
},
}
entries = append(entries, entry)
}
params = &sqs.SendMessageBatchInput{
Entries: entries,
QueueUrl: aws.String(*url), // Required
}
return
}
// CreateNewQueue is to create new queue
func CreateNewQueue(params *sqs.CreateQueueInput) (*sqs.CreateQueueOutput, error) {
//create
resp, err := svc.CreateQueue(params)
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
return nil, err
}
// Pretty-print the response data.
return resp, nil
}
// CreateInputParam is to create input parameter
func CreateInputParam(queueName string) *sqs.CreateQueueInput {
//parameter
params := &sqs.CreateQueueInput{
QueueName: aws.String(queueName),
Attributes: map[string]*string{
//"VisibilityTimeout": aws.String("30"),
//"MessageRetentionPeriod": aws.String("345600"),
//"MaximumMessageSize": aws.String("262144"),
"DelaySeconds": aws.String("0"),
"ReceiveMessageWaitTimeSeconds": aws.String("20"),
},
}
return params
}
// CreateAttributesParams is to create attribute parameter
func CreateAttributesParams(url *string) *sqs.GetQueueAttributesInput {
params := &sqs.GetQueueAttributesInput{
QueueUrl: aws.String(*url), // Required
AttributeNames: []*string{
aws.String("All"), // Required
// More values...
},
}
return params
}
// GetQueueAttributes to get queue attributes
func GetQueueAttributes(params *sqs.GetQueueAttributesInput) (*sqs.GetQueueAttributesOutput, error) {
resp, err := svc.GetQueueAttributes(params)
return resp, err
}
// New is to create sqs client
func New() {
//set environment variable
awslib.InitAwsEnv("", "")
//get config
conf := conf.GetConf()
//create client for sqs
//nolint:staticcheck
svc = sqs.New(session.New(), aws.NewConfig().WithRegion(conf.Aws.Region))
fmt.Println(svc)
}