~prudhvikrishna/sqs/trunk

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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//
// goamz - Go packages to interact with the Amazon Web Services.
//
//   https://wiki.ubuntu.com/goamz
//
// Copyright (c) 2012 Memeo Inc.
//
// Written by Prudhvi Krishna Surapaneni <me@prudhvi.net>
//
package sqs

import (
	"encoding/xml"
	"fmt"
	"launchpad.net/goamz/aws"
	"log"
	"net/http"
	"net/http/httputil"
	"net/url"
	"strconv"
	"time"
)

// The SQS type encapsulates operations with specific SQS region
type SQS struct {
	aws.Auth
	aws.Region
	private byte // Reserve the right of using private data.
}

const debug = false

// New creates a new SQS handle
func New(auth aws.Auth, region aws.Region) *SQS {
	return &SQS{auth, region, 0}
}

// Queue type encapsulates operations on a SQS Queue
type Queue struct {
	*SQS
	Url string
}

// Response to a CreateQueue request.
//
// See http://goo.gl/sVUjF for more details
type CreateQueueResponse struct {
	QueueUrl string `xml:"CreateQueueResult>QueueUrl"`
	ResponseMetadata
}

// Response to a ListQueues request.
//
// See http://goo.gl/RPRWr for more details
type ListQueuesResponse struct {
	QueueUrl []string `xml:"ListQueuesResult>QueueUrl"`
	ResponseMetadata
}

// Response to a GetQueueUrl request.
//
// See http://goo.gl/hk7Iu for more details
type GetQueueUrlResponse struct {
	QueueUrl string `xml:"GetQueueUrlResult>QueueUrl"`
	ResponseMetadata
}

// Response to a ChangeMessageVisibility request.
//
// See http://goo.gl/EyJKF for more details
type ChangeMessageVisibilityResponse struct {
	ResponseMetadata
}

// See http://goo.gl/XTo0s for more details 
type ResponseMetadata struct {
	RequestId string  `xml:"ResponseMetadata>RequestId"`
}

// Error represents an error in an operation with SQS
type Error struct {
	StatusCode int    // HTTP Status Code (200, 403, ... )
	Code       string // SQS Error Code
	Message    string // The human-oriented error message
	RequestId  string `xml:"RequestID"`
}

func (err *Error) Error() string {
	if err.Code != "" {
		return err.Message
	}

	return fmt.Sprintf("%s (%s)", err.Message, err.Code)
}

// For now a single error inst is being exposed. In the future it may be useful
// to provide access to all of them, but rather than doing it as an array/slice,
// use a *next pointer, so that it's backward compatible and it continues to be
// easy to handle the first error, which is what most people will want.
type xmlErrors struct {
	RequestId string
	Errors    []Error `xml:"Errors>Error"`
}

// Attribute represents an instance of a SQS Queue Attribute.
type Attribute struct {
	Name  string
	Value string
}

// See http://goo.gl/pgffn for more details.
type ChangeMessageVisibilityBatchEntry struct {
	Id                string
	ReceiptHandle     string
	VisibilityTimeout int
}

// ReceiveMessageResponse holds the results of ReceiveMessage
type ReceiveMessageResponse struct {
	Messages []Message `xml:"ReceiveMessageResult>Message"`
	ResponseMetadata
}

type DeleteMessageResponse struct {
	ResponseMetadata
}

type SendMessageResponse struct {
	SendMessageResult
	ResponseMetadata
}

type SendMessageBatchResponse struct {
	SendMessageBatchResult
	ResponseMetadata
}

// SendMessageBatchResult holds the results of SendMessageBatch
type SendMessageBatchResult struct {
	Entries []SendMessageBatchResultEntry `xml:"SendMessageBatchResult>SendMessageBatchResultEntry"`
}

type SendMessageBatchResultEntry struct {
	MD5OfMessageBody string `xml:"MD5OfMessageBody"`
	MessageId        string `xml:"MessageId"`
	Id               string `xml:"Id"`
}

type SendMessageBatchRequestEntry struct {
	Id           string
	MessageBody  string
	DelaySeconds int
}

type SendMessageResult struct {
	MD5OfMessageBody string `xml:"SendMessageResult>MD5OfMessageBody"`
	MessageId        string `xml:"SendMessageResult>MessageId"`
}

// Represents an instance of a SQS Message
type Message struct {
	MessageId     string      `xml:"MessageId"`
	Body          string      `xml:"Body"`
	MD5OfBody     string      `xml:"MD5OfBody"`
	ReceiptHandle string      `xml:"ReceiptHandle"`
	Attribute     []Attribute `xml:"Attribute"`
}

type ChangeMessageVisibilityBatchResponse struct {
	Id []string `xml:"ChangeMessageVisibilityBatchResult>ChangeMessageVisibilityBatchResultEntry>Id"`
	ResponseMetadata
}

type GetQueueAttributesResponse struct {
	Attributes []Attribute `xml:"GetQueueAttributesResult>Attribute"`
	ResponseMetadata
}

type DeleteMessageBatchResult struct {
	Ids []string `xml:"DeleteMessageBatchResult>DeleteMessageBatchResultEntry>Id"`
}

type DeleteMessageBatchResponse struct {
	DeleteMessageBatchResult `xml:"DeleteMessageBatchResponse>DeleteMessageBatchResult"`
	ResponseMetadata
}

type DeleteMessageBatch struct {
	Id            string
	ReceiptHandle string
}

type AccountPermission struct {
	AWSAccountId string
	ActionName   string
}

type AddPermissionResponse struct {
	ResponseMetadata
}

type RemovePermissionResponse struct {
	ResponseMetadata
}

type SetQueueAttributesResponse struct {
	ResponseMetadata
}

type DeleteQueueResponse struct {
	ResponseMetadata
}

// CreateQueue action creates a new queue.
//
// See http://goo.gl/sVUjF for more details
func (s *SQS) CreateQueue(name string, attributes []Attribute) (queue *Queue, err error) {
	resp := &CreateQueueResponse{}
	params := makeParams("CreateQueue")
	queue = nil

	for i, attribute := range attributes {
		params["Attribute."+strconv.Itoa(i+1)+".Name"] = attribute.Name
		params["Attribute."+strconv.Itoa(i+1)+".Value"] = attribute.Value
	}

	params["QueueName"] = name
	err = s.query("", params, resp)
	if err != nil {
		return nil, err
	}
	queue = &Queue{s, resp.QueueUrl}
	return queue, err
}

// AddPermission action adds a permission to a queue for a specific principal.
//
// See http://goo.gl/8WBp8 for more details
func (q *Queue) AddPermission(label string, accountPermissions []AccountPermission) (resp *AddPermissionResponse, err error) {
	resp = &AddPermissionResponse{}
	params := makeParams("AddPermission")

	params["Label"] = label
	for i, accountPermission := range accountPermissions {
		params["AWSAccountId."+strconv.Itoa(i+1)] = accountPermission.AWSAccountId
		params["ActionName."+strconv.Itoa(i+1)] = accountPermission.ActionName
	}

	err = q.SQS.query(q.Url, params, resp)
	return
}

// RemovePermission action revokes any permissions in the queue policy that matches the Label parameter.
//
// See http://goo.gl/YLOe8 for more details
func (q *Queue) RemovePermission(label string) (resp *RemovePermissionResponse, err error) {
	resp = &RemovePermissionResponse{}
	params := makeParams("RemovePermission")

	params["Label"] = label
	err = q.SQS.query(q.Url, params, resp)
	return
}

// GetQueueAttributes action returns one or all attributes of a queue.
//
// See http://goo.gl/WejDu for more details
func (q *Queue) GetQueueAttributes(attributes []string) (resp *GetQueueAttributesResponse, err error) {
	resp = &GetQueueAttributesResponse{}
	params := makeParams("GetQueueAttributes")

	for i, attribute := range attributes {
		params["Attribute."+strconv.Itoa(i+1)] = attribute
	}

	err = q.SQS.query(q.Url, params, resp)
	return
}

// ChangeMessageVisibility action changes the visibility timeout of a specified message in a queue to a new value.
//
// See http://goo.gl/EyJKF for more details
func (q *Queue) ChangeMessageVisibility(receiptHandle string, visibilityTimeout int) (resp *ChangeMessageVisibilityResponse, err error) {
	resp = &ChangeMessageVisibilityResponse{}
	params := makeParams("ChangeMessageVisibility")

	params["VisibilityTimeout"] = strconv.Itoa(visibilityTimeout)
	params["ReceiptHandle"] = receiptHandle

	err = q.SQS.query(q.Url, params, resp)
	return
}

// ChangeMessageVisibilityBatch action is a batch version of the ChangeMessageVisibility action.
//
// See http://goo.gl/pgffn for more details
func (q *Queue) ChangeMessageVisibilityBatch(messageVisibilityBatch []ChangeMessageVisibilityBatchEntry) (resp *ChangeMessageVisibilityBatchResponse, err error) {
	resp = &ChangeMessageVisibilityBatchResponse{}
	params := makeParams("ChangeMessageVisibilityBatch")

	for i, messageVisibility := range messageVisibilityBatch {
		params["ChangeMessageVisibilityBatchRequestEntry."+strconv.Itoa(i+1)+".Id"] = messageVisibility.Id
		params["ChangeMessageVisibilityBatchRequestEntry."+strconv.Itoa(i+1)+".ReceiptHandle"] = messageVisibility.ReceiptHandle
		params["ChangeMessageVisibilityBatchRequestEntry."+strconv.Itoa(i+1)+".VisibilityTimeout"] = strconv.Itoa(messageVisibility.VisibilityTimeout)
	}

	err = q.SQS.query(q.Url, params, resp)
	return
}

// ReceiveMessage action retrieves one or more messages from the specified queue.
//
// See http://goo.gl/ThPrF for more details
func (q *Queue) ReceiveMessage(attributes []string, maxNumberOfMessages int, visibilityTimeout int) (resp *ReceiveMessageResponse, err error) {
	resp = &ReceiveMessageResponse{}
	params := makeParams("ReceiveMessage")

	for i, attribute := range attributes {
		params["Attribute."+strconv.Itoa(i+1)] = attribute
	}

	params["MaxNumberOfMessages"] = strconv.Itoa(maxNumberOfMessages)
	params["VisibilityTimeout"] = strconv.Itoa(visibilityTimeout)

	err = q.SQS.query(q.Url, params, resp)
	return
}

// DeleteMessage action deletes the specified message from the specified queue.
//
// See http://goo.gl/6XBv7 for more details
func (q *Queue) DeleteMessage(receiptHandle string) (resp *DeleteMessageResponse, err error) {
	resp = &DeleteMessageResponse{}
	params := makeParams("DeleteMessage")

	params["ReceiptHandle"] = receiptHandle

	err = q.SQS.query(q.Url, params, resp)
	return
}

// DeleteMessageBatch action is a batch version of the DeleteMessage action.
//
// See http://goo.gl/y1ehG for more details
func (q *Queue) DeleteMessageBatch(deleteMessageBatch []DeleteMessageBatch) (resp *DeleteMessageBatchResponse, err error) {
	resp = &DeleteMessageBatchResponse{}
	params := makeParams("DeleteMessageBatch")

	for i, deleteMessage := range deleteMessageBatch {
		params["DeleteMessageBatchRequestEntry."+strconv.Itoa(i+1)+".Id"] = deleteMessage.Id
		params["DeleteMessageBatchRequestEntry."+strconv.Itoa(i+1)+".ReceiptHandle"] = deleteMessage.ReceiptHandle
	}

	err = q.SQS.query(q.Url, params, resp)
	return
}

// SendMessage action delivers a message to the specified queue.
// The maximum allowed size is 64KB
//
// See http://goo.gl/7OnPb for more details
func (q *Queue) SendMessage(messageBody string) (resp *SendMessageResponse, err error) {
	resp = &SendMessageResponse{}
	params := makeParams("SendMessage")

	params["MessageBody"] = messageBody
	err = q.SQS.query(q.Url, params, resp)
	return
}

// SendMessageWithDelay is a helper function for SendMessage action which delivers a message to the specified queue
// with a delay.
//
// See http://goo.gl/7OnPb for more details
func (q *Queue) SendMessageWithDelay(messageBody string, delaySeconds int) (resp *SendMessageResponse, err error) {
	resp = &SendMessageResponse{}
	params := makeParams("SendMessage")

	params["MessageBody"] = messageBody
	params["DelaySeconds"] = strconv.Itoa(delaySeconds)
	err = q.SQS.query(q.Url, params, resp)
	return
}

// SendMessageBatch action delivers up to ten messages to the specified queue.
//
// See http://goo.gl/mNytv for more details
func (q *Queue) SendMessageBatch(sendMessageBatchRequests []SendMessageBatchRequestEntry) (resp *SendMessageBatchResponse, err error) {
	resp = &SendMessageBatchResponse{}
	params := makeParams("SendMessageBatch")

	for i, sendMessageBatchRequest := range sendMessageBatchRequests {
		params["SendMessageBatchRequestEntry."+strconv.Itoa(i+1)+".Id"] = sendMessageBatchRequest.Id
		params["SendMessageBatchRequestEntry."+strconv.Itoa(i+1)+".MessageBody"] = sendMessageBatchRequest.MessageBody
		params["SendMessageBatchRequestEntry."+strconv.Itoa(i+1)+".DelaySeconds"] = strconv.Itoa(sendMessageBatchRequest.DelaySeconds)
	}

	err = q.SQS.query(q.Url, params, resp)
	return
}

// Delete action deletes the queue specified by the queue URL, regardless of whether the queue is empty.
//
// See http://goo.gl/c3YCr for more details
func (q *Queue) Delete() (resp *DeleteQueueResponse, err error) {
	resp = &DeleteQueueResponse{}
	params := makeParams("Delete")

	err = q.SQS.query(q.Url, params, resp)
	return
}

// SetQueueAttributes action sets one attribute of a queue per request.
//
// See http://goo.gl/LyZnj for more details
func (q *Queue) SetQueueAttributes(attribute Attribute) (resp *SetQueueAttributesResponse, err error) {
	resp = &SetQueueAttributesResponse{}
	params := makeParams("SetQueueAttributes")

	params["Attribute.Name"] = attribute.Name
	params["Attribute.Value"] = attribute.Value

	err = q.SQS.query(q.Url, params, resp)
	return
}

// ListQueues  action returns a list of your queues.
//
// See http://goo.gl/RPRWr for more details

func (s *SQS) ListQueues() (resp *ListQueuesResponse, err error) {
	resp = &ListQueuesResponse{}
	params := makeParams("ListQueues")

	err = s.query("", params, resp)
	return
}

// ListQueuesWithPrefix action returns only a list of queues with a name beginning with the specified value are returned
//
// See http://goo.gl/RPRWr for more details
func (s *SQS) ListQueuesWithPrefix(queueNamePrefix string) (resp *ListQueuesResponse, err error) {
	resp = &ListQueuesResponse{}
	params := makeParams("ListQueues")

	if queueNamePrefix != "" {
		params["QueueNamePrefix"] = queueNamePrefix
	}

	err = s.query("", params, resp)
	return
}

// GetQueue is a helper function for GetQueueUrl action that returns an instance of a queue with specified name.
//
// See http://goo.gl/hk7Iu for more details
func (s *SQS) GetQueue(queueName string) (queue *Queue, err error) {
	resp, err := s.GetQueueUrl(queueName)
	if err != nil {
		return nil, err
	}

	queue = &Queue{s, resp.QueueUrl}
	return
}

// GetQueueOfOwner is a helper function for GetQueueUrl action that returns an instance of a queue with specified name
// and belongs to the specified AWS Account Id.
//
// See http://goo.gl/hk7Iu for more details
func (s *SQS) GetQueueOfOwner(queueName, queueOwnerAWSAccountId string) (queue *Queue, err error) {
	resp, err := s.GetQueueUrlOfOwner(queueName, queueOwnerAWSAccountId)
	if err != nil {
		return nil, err
	}

	queue = &Queue{s, resp.QueueUrl}
	return

}

// GetQueueUrl action returns the Uniform Resource Locater (URL) of a queue.
//
// See http://goo.gl/hk7Iu for more details
func (s *SQS) GetQueueUrl(queueName string) (resp *GetQueueUrlResponse, err error) {
	resp = &GetQueueUrlResponse{}
	params := makeParams("GetQueueUrl")

	params["QueueName"] = queueName

	err = s.query("", params, resp)
	return
}

// GetQueueUrlOfOwner is a helper function for GetQueueUrl action that returns the URL of a queue with specified name
// and belongs to the specified AWS Account Id.
//
// See http://goo.gl/hk7Iu for more details for more details
func (s *SQS) GetQueueUrlOfOwner(queueName, queueOwnerAWSAccountId string) (resp *GetQueueUrlResponse, err error) {
	resp = &GetQueueUrlResponse{}
	params := makeParams("GetQueueUrl")

	params["QueueName"] = queueName

	if queueOwnerAWSAccountId != "" {
		params["QueueOwnerAWSAccountId"] = queueOwnerAWSAccountId
	}

	err = s.query("", params, resp)
	return
}

func (s *SQS) query(queueUrl string, params map[string]string, resp interface{}) error {
	params["Version"] = "2011-10-01"
	params["Timestamp"] = time.Now().In(time.UTC).Format(time.RFC3339)
	endpoint, err := url.Parse(s.Region.SQSEndpoint)
	if err != nil {
		return err
	}
	var path string
	if queueUrl != "" {
		path = queueUrl[len(s.Region.SQSEndpoint):]
	} else {
		path = "/"
	}

	sign(s.Auth, "GET", path, params, endpoint.Host)
	endpoint.RawQuery = multimap(params).Encode()
	if debug {
		log.Printf("get { %v } -> {\n", endpoint.String())
	}

	r, err := http.Get(endpoint.String())
	if err != nil {
		return err
	}
	defer r.Body.Close()

	if debug {
		dump, _ := httputil.DumpResponse(r, true)
		log.Printf("response:\n")
		log.Printf("%v\n}\n", string(dump))
	}
	if r.StatusCode != 200 {
		return buildError(r)
	}
	err = xml.NewDecoder(r.Body).Decode(resp)
	return err
}

func multimap(p map[string]string) url.Values {
	q := make(url.Values, len(p))
	for k, v := range p {
		q[k] = []string{v}
	}
	return q
}

func buildError(r *http.Response) error {
	errors := xmlErrors{}
	xml.NewDecoder(r.Body).Decode(&errors)
	var err Error
	if len(errors.Errors) > 0 {
		err = errors.Errors[0]
	}
	err.RequestId = errors.RequestId
	err.StatusCode = r.StatusCode
	if err.Message == "" {
		err.Message = r.Status
	}
	return &err
}

func makeParams(action string) map[string]string {
	params := make(map[string]string)
	params["Action"] = action
	return params
}