/
pubsub.go
361 lines (310 loc) · 9.27 KB
/
pubsub.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package radosgw
import (
"encoding/xml"
"errors"
"fmt"
"io"
"net/http"
"strings"
"github.com/beevik/etree"
)
type (
MetaDataFilter map[string]string
TagFilter map[string]string
)
type ListTopicsResponse struct {
XMLName xml.Name `xml:"ListTopicsResponse"`
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
ListTopicsResult struct {
Text string `xml:",chardata"`
Topics struct {
Text string `xml:",chardata"`
Member []struct {
Text string `xml:",chardata"`
User string `xml:"User"`
Name string `xml:"Name"`
EndPoint struct {
Text string `xml:",chardata"`
EndpointAddress string `xml:"EndpointAddress"`
EndpointArgs string `xml:"EndpointArgs"`
EndpointTopic string `xml:"EndpointTopic"`
} `xml:"EndPoint"`
TopicArn string `xml:"TopicArn"`
} `xml:"member"`
} `xml:"Topics"`
} `xml:"ListTopicsResult"`
ResponseMetadata struct {
Text string `xml:",chardata"`
RequestId string `xml:"RequestId"`
} `xml:"ResponseMetadata"`
}
type GetTopicResponse struct {
XMLName xml.Name `xml:"GetTopicResponse"`
Text string `xml:",chardata"`
GetTopicResult struct {
Text string `xml:",chardata"`
Topic struct {
Text string `xml:",chardata"`
User string `xml:"User"`
Name string `xml:"Name"`
EndPoint struct {
Text string `xml:",chardata"`
EndpointAddress string `xml:"EndpointAddress"`
EndpointArgs string `xml:"EndpointArgs"`
EndpointTopic string `xml:"EndpointTopic"`
HasStoredSecret string `xml:"HasStoredSecret"`
Persistent string `xml:"Persistent"`
} `xml:"EndPoint"`
TopicArn string `xml:"TopicArn"`
OpaqueData string `xml:"OpaqueData"`
} `xml:"Topic"`
} `xml:"GetTopicResult"`
ResponseMetadata struct {
Text string `xml:",chardata"`
RequestId string `xml:"RequestId"`
} `xml:"ResponseMetadata"`
}
type CreateTopicResponse struct {
XMLName xml.Name `xml:"CreateTopicResponse"`
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
CreateTopicResult struct {
Text string `xml:",chardata"`
TopicArn string `xml:"TopicArn"`
} `xml:"CreateTopicResult"`
ResponseMetadata struct {
Text string `xml:",chardata"`
RequestId string `xml:"RequestId"`
} `xml:"ResponseMetadata"`
}
type NotificationConfiguration struct {
XMLName xml.Name `xml:"NotificationConfiguration"`
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
TopicConfiguration []struct {
Text string `xml:",chardata"`
ID string `xml:"Id"`
Topic string `xml:"Topic"`
Event []string `xml:"Event"`
Filter struct {
Text string `xml:",chardata"`
S3Key struct {
Text string `xml:",chardata"`
FilterRule []struct {
Text string `xml:",chardata"`
Name string `xml:"Name"`
Value string `xml:"Value"`
} `xml:"FilterRule"`
} `xml:"S3Key"`
S3Metadata struct {
Text string `xml:",chardata"`
FilterRule []struct {
Text string `xml:",chardata"`
Name string `xml:"Name"`
Value string `xml:"Value"`
} `xml:"FilterRule"`
} `xml:"S3Metadata"`
S3Tags struct {
Text string `xml:",chardata"`
FilterRule []struct {
Text string `xml:",chardata"`
Name string `xml:"Name"`
Value string `xml:"Value"`
} `xml:"FilterRule"`
} `xml:"S3Tags"`
} `xml:"Filter"`
} `xml:"TopicConfiguration"`
}
func (rgw *RGWClient) ListTopics() (*ListTopicsResponse, error) {
body := strings.NewReader("Action=ListTopics&Version=2010-03-31")
url := fmt.Sprintf("%s/", *rgw.config.Endpoint)
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
resp, err := rgw.buildSignerV2AndSendReq(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
buff, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var topics ListTopicsResponse
err = xml.Unmarshal(buff, &topics)
if err != nil {
return nil, err
}
return &topics, nil
}
func (rgw *RGWClient) CreateTopic(topicName, pushEndpoint string) (string, error) {
body := fmt.Sprintf("Action=CreateTopic&Version=2010-03-31&Name=%s&push-endpoint=%s", topicName, pushEndpoint)
url := fmt.Sprintf("%s/", *rgw.config.Endpoint)
req, err := http.NewRequest("POST", url, strings.NewReader(body))
if err != nil {
return "", err
}
resp, err := rgw.buildSignerV2AndSendReq(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return "", err
}
buff, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
var topic CreateTopicResponse
err = xml.Unmarshal(buff, &topic)
if err != nil {
return "", err
}
return topic.CreateTopicResult.TopicArn, nil
}
func (rgw *RGWClient) GetTopic(topicArn string) (*GetTopicResponse, error) {
body := fmt.Sprintf("Action=GetTopic&Version=2010-03-31&TopicArn=%s", topicArn)
url := fmt.Sprintf("%s/", *rgw.config.Endpoint)
req, err := http.NewRequest("POST", url, strings.NewReader(body))
if err != nil {
return nil, err
}
resp, err := rgw.buildSignerV2AndSendReq(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
if resp.StatusCode == 404 {
return nil, nil
}
buff, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var topic GetTopicResponse
err = xml.Unmarshal(buff, &topic)
if err != nil {
return nil, err
}
return &topic, nil
}
func (rgw *RGWClient) DeleteTopic(topicArn string) (*http.Response, error) {
body := fmt.Sprintf("Action=DeleteTopic&Version=2010-03-31&TopicArn=%s", topicArn)
url := fmt.Sprintf("%s/", *rgw.config.Endpoint)
req, err := http.NewRequest("POST", url, strings.NewReader(body))
if err != nil {
return nil, err
}
resp, err := rgw.buildSignerV2AndSendReq(req)
return resp, err
}
// CreateNotification TODO Support tag and regular expression filtering
func (rgw *RGWClient) CreateNotification(topicArn, bucket, notificationId, prefix, suffix string, metaData MetaDataFilter, events []string) (*http.Response, error) {
body, err := rgw.buildNotificationBody(topicArn, notificationId, prefix, suffix, metaData, events)
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s/%s?notification", *rgw.config.Endpoint, bucket)
req, err := http.NewRequest("PUT", url, strings.NewReader(body))
if err != nil {
return nil, err
}
resp, err := rgw.buildSignerV2AndSendReq(req)
return resp, err
}
// GetNotification TODO define a struct to hold response for easier use by the client
func (rgw *RGWClient) GetNotification(bucket, notificationId string) (*NotificationConfiguration, error) {
if bucket == "" {
err := errors.New("bucket can not be empty")
return nil, err
}
url := fmt.Sprintf("%s/%s?notification", *rgw.config.Endpoint, bucket)
if notificationId != "" {
url += "=" + notificationId
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
resp, err := rgw.buildSignerV2AndSendReq(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
if resp.StatusCode == 404 {
return nil, nil
}
buff, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var notif NotificationConfiguration
err = xml.Unmarshal(buff, ¬if)
if err != nil {
return nil, err
}
return ¬if, nil
}
func (rgw *RGWClient) DeleteNotification(bucket, notificationId string) (*http.Response, error) {
url := fmt.Sprintf("%s/%s?notification=%s", *rgw.config.Endpoint, bucket, notificationId)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return nil, err
}
resp, err := rgw.buildSignerV2AndSendReq(req)
return resp, err
}
// TODO Support tag and regular expression filtering
func (rgw *RGWClient) buildNotificationBody(topicArn, NotificationId, prefix, suffix string, metaData MetaDataFilter, events []string) (string, error) {
doc := etree.NewDocument()
doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`)
notificationConfiguration := doc.CreateElement("NotificationConfiguration")
topicConfiguration := notificationConfiguration.CreateElement("TopicConfiguration")
id := topicConfiguration.CreateElement("Id")
id.CreateText(NotificationId)
topic := topicConfiguration.CreateElement("Topic")
topic.CreateText(topicArn)
for _, e := range events {
event := topicConfiguration.CreateElement("Event")
event.CreateText(e)
}
filter := topicConfiguration.CreateElement("Filter")
s3Key := filter.CreateElement("S3Key")
if prefix != "" {
filterRule := s3Key.CreateElement("FilterRule")
name := filterRule.CreateElement("Name")
name.CreateText("prefix")
value := filterRule.CreateElement("Value")
value.CreateText(prefix)
}
if suffix != "" {
filterRule := s3Key.CreateElement("FilterRule")
name := filterRule.CreateElement("Name")
name.CreateText("suffix")
value := filterRule.CreateElement("Value")
value.CreateText(suffix)
}
if len(metaData) > 0 {
s3Metadata := filter.CreateElement("S3Metadata")
for k, v := range metaData {
filterRule := s3Metadata.CreateElement("FilterRule")
name := filterRule.CreateElement("Name")
name.CreateText(k)
value := filterRule.CreateElement("Value")
value.CreateText(v)
}
}
buff, err := doc.WriteToBytes()
if err != nil {
return "", err
}
return string(buff), nil
}