1# Copyright (c) 2006-2009 Mitch Garnaat http://garnaat.org/
2#
3# Permission is hereby granted, free of charge, to any person obtaining a
4# copy of this software and associated documentation files (the
5# "Software"), to deal in the Software without restriction, including
6# without limitation the rights to use, copy, modify, merge, publish, dis-
7# tribute, sublicense, and/or sell copies of the Software, and to permit
8# persons to whom the Software is furnished to do so, subject to the fol-
9# lowing conditions:
10#
11# The above copyright notice and this permission notice shall be included
12# in all copies or substantial portions of the Software.
13#
14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20# IN THE SOFTWARE.
21
22import boto
23from boto.connection import AWSQueryConnection
24from boto.sqs.regioninfo import SQSRegionInfo
25from boto.sqs.queue import Queue
26from boto.sqs.message import Message
27from boto.sqs.attributes import Attributes
28from boto.sqs.batchresults import BatchResults
29from boto.exception import SQSError, BotoServerError
30
31
32class SQSConnection(AWSQueryConnection):
33    """
34    A Connection to the SQS Service.
35    """
36    DefaultRegionName = boto.config.get('Boto', 'sqs_region_name', 'us-east-1')
37    DefaultRegionEndpoint = boto.config.get('Boto', 'sqs_region_endpoint',
38                                            'queue.amazonaws.com')
39    APIVersion = boto.config.get('Boto', 'sqs_version', '2012-11-05')
40    DefaultContentType = 'text/plain'
41    ResponseError = SQSError
42    AuthServiceName = 'sqs'
43
44    def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
45                 is_secure=True, port=None, proxy=None, proxy_port=None,
46                 proxy_user=None, proxy_pass=None, debug=0,
47                 https_connection_factory=None, region=None, path='/',
48                 security_token=None, validate_certs=True, profile_name=None):
49        if not region:
50            region = SQSRegionInfo(self, self.DefaultRegionName,
51                                   self.DefaultRegionEndpoint)
52        self.region = region
53        super(SQSConnection, self).__init__(aws_access_key_id,
54                                    aws_secret_access_key,
55                                    is_secure, port,
56                                    proxy, proxy_port,
57                                    proxy_user, proxy_pass,
58                                    self.region.endpoint, debug,
59                                    https_connection_factory, path,
60                                    security_token=security_token,
61                                    validate_certs=validate_certs,
62                                    profile_name=profile_name)
63        self.auth_region_name = self.region.name
64
65    def _required_auth_capability(self):
66        return ['hmac-v4']
67
68    def create_queue(self, queue_name, visibility_timeout=None):
69        """
70        Create an SQS Queue.
71
72        :type queue_name: str or unicode
73        :param queue_name: The name of the new queue.  Names are
74            scoped to an account and need to be unique within that
75            account.  Calling this method on an existing queue name
76            will not return an error from SQS unless the value for
77            visibility_timeout is different than the value of the
78            existing queue of that name.  This is still an expensive
79            operation, though, and not the preferred way to check for
80            the existence of a queue.  See the
81            :func:`boto.sqs.connection.SQSConnection.lookup` method.
82
83        :type visibility_timeout: int
84        :param visibility_timeout: The default visibility timeout for
85            all messages written in the queue.  This can be overridden
86            on a per-message.
87
88        :rtype: :class:`boto.sqs.queue.Queue`
89        :return: The newly created queue.
90
91        """
92        params = {'QueueName': queue_name}
93        if visibility_timeout:
94            params['Attribute.1.Name'] = 'VisibilityTimeout'
95            params['Attribute.1.Value'] = int(visibility_timeout)
96        return self.get_object('CreateQueue', params, Queue)
97
98    def delete_queue(self, queue, force_deletion=False):
99        """
100        Delete an SQS Queue.
101
102        :type queue: A Queue object
103        :param queue: The SQS queue to be deleted
104
105        :type force_deletion: Boolean
106        :param force_deletion: A deprecated parameter that is no longer used by
107            SQS's API.
108
109        :rtype: bool
110        :return: True if the command succeeded, False otherwise
111        """
112        return self.get_status('DeleteQueue', None, queue.id)
113
114    def purge_queue(self, queue):
115        """
116        Purge all messages in an SQS Queue.
117
118        :type queue: A Queue object
119        :param queue: The SQS queue to be purged
120
121        :rtype: bool
122        :return: True if the command succeeded, False otherwise
123        """
124        return self.get_status('PurgeQueue', None, queue.id)
125
126    def get_queue_attributes(self, queue, attribute='All'):
127        """
128        Gets one or all attributes of a Queue
129
130        :type queue: A Queue object
131        :param queue: The SQS queue to get attributes for
132
133        :type attribute: str
134        :type attribute: The specific attribute requested.  If not
135            supplied, the default is to return all attributes.  Valid
136            attributes are:
137
138            * All
139            * ApproximateNumberOfMessages
140            * ApproximateNumberOfMessagesNotVisible
141            * VisibilityTimeout
142            * CreatedTimestamp
143            * LastModifiedTimestamp
144            * Policy
145            * MaximumMessageSize
146            * MessageRetentionPeriod
147            * QueueArn
148            * ApproximateNumberOfMessagesDelayed
149            * DelaySeconds
150            * ReceiveMessageWaitTimeSeconds
151            * RedrivePolicy
152
153        :rtype: :class:`boto.sqs.attributes.Attributes`
154        :return: An Attributes object containing request value(s).
155        """
156        params = {'AttributeName' : attribute}
157        return self.get_object('GetQueueAttributes', params,
158                               Attributes, queue.id)
159
160    def set_queue_attribute(self, queue, attribute, value):
161        params = {'Attribute.Name' : attribute, 'Attribute.Value' : value}
162        return self.get_status('SetQueueAttributes', params, queue.id)
163
164    def receive_message(self, queue, number_messages=1,
165                        visibility_timeout=None, attributes=None,
166                        wait_time_seconds=None, message_attributes=None):
167        """
168        Read messages from an SQS Queue.
169
170        :type queue: A Queue object
171        :param queue: The Queue from which messages are read.
172
173        :type number_messages: int
174        :param number_messages: The maximum number of messages to read
175                                (default=1)
176
177        :type visibility_timeout: int
178        :param visibility_timeout: The number of seconds the message should
179            remain invisible to other queue readers
180            (default=None which uses the Queues default)
181
182        :type attributes: str
183        :param attributes: The name of additional attribute to return
184            with response or All if you want all attributes.  The
185            default is to return no additional attributes.  Valid
186            values:
187            * All
188            * SenderId
189            * SentTimestamp
190            * ApproximateReceiveCount
191            * ApproximateFirstReceiveTimestamp
192
193        :type wait_time_seconds: int
194        :param wait_time_seconds: The duration (in seconds) for which the call
195            will wait for a message to arrive in the queue before returning.
196            If a message is available, the call will return sooner than
197            wait_time_seconds.
198
199        :type message_attributes: list
200        :param message_attributes: The name(s) of additional message
201            attributes to return. The default is to return no additional
202            message attributes. Use ``['All']`` or ``['.*']`` to return all.
203
204        :rtype: list
205        :return: A list of :class:`boto.sqs.message.Message` objects.
206
207        """
208        params = {'MaxNumberOfMessages' : number_messages}
209        if visibility_timeout is not None:
210            params['VisibilityTimeout'] = visibility_timeout
211        if attributes is not None:
212            self.build_list_params(params, attributes, 'AttributeName')
213        if wait_time_seconds is not None:
214            params['WaitTimeSeconds'] = wait_time_seconds
215        if message_attributes is not None:
216            self.build_list_params(params, message_attributes,
217                                   'MessageAttributeName')
218        return self.get_list('ReceiveMessage', params,
219                             [('Message', queue.message_class)],
220                             queue.id, queue)
221
222    def delete_message(self, queue, message):
223        """
224        Delete a message from a queue.
225
226        :type queue: A :class:`boto.sqs.queue.Queue` object
227        :param queue: The Queue from which messages are read.
228
229        :type message: A :class:`boto.sqs.message.Message` object
230        :param message: The Message to be deleted
231
232        :rtype: bool
233        :return: True if successful, False otherwise.
234        """
235        params = {'ReceiptHandle' : message.receipt_handle}
236        return self.get_status('DeleteMessage', params, queue.id)
237
238    def delete_message_batch(self, queue, messages):
239        """
240        Deletes a list of messages from a queue in a single request.
241
242        :type queue: A :class:`boto.sqs.queue.Queue` object.
243        :param queue: The Queue to which the messages will be written.
244
245        :type messages: List of :class:`boto.sqs.message.Message` objects.
246        :param messages: A list of message objects.
247        """
248        params = {}
249        for i, msg in enumerate(messages):
250            prefix = 'DeleteMessageBatchRequestEntry'
251            p_name = '%s.%i.Id' % (prefix, (i+1))
252            params[p_name] = msg.id
253            p_name = '%s.%i.ReceiptHandle' % (prefix, (i+1))
254            params[p_name] = msg.receipt_handle
255        return self.get_object('DeleteMessageBatch', params, BatchResults,
256                               queue.id, verb='POST')
257
258    def delete_message_from_handle(self, queue, receipt_handle):
259        """
260        Delete a message from a queue, given a receipt handle.
261
262        :type queue: A :class:`boto.sqs.queue.Queue` object
263        :param queue: The Queue from which messages are read.
264
265        :type receipt_handle: str
266        :param receipt_handle: The receipt handle for the message
267
268        :rtype: bool
269        :return: True if successful, False otherwise.
270        """
271        params = {'ReceiptHandle' : receipt_handle}
272        return self.get_status('DeleteMessage', params, queue.id)
273
274    def send_message(self, queue, message_content, delay_seconds=None,
275                     message_attributes=None):
276        """
277        Send a new message to the queue.
278
279        :type queue: A :class:`boto.sqs.queue.Queue` object.
280        :param queue: The Queue to which the messages will be written.
281
282        :type message_content: string
283        :param message_content: The body of the message
284
285        :type delay_seconds: int
286        :param delay_seconds: Number of seconds (0 - 900) to delay this
287            message from being processed.
288
289        :type message_attributes: dict
290        :param message_attributes: Message attributes to set. Should be
291            of the form:
292
293            {
294                "name1": {
295                    "data_type": "Number",
296                    "string_value": "1"
297                },
298                "name2": {
299                    "data_type": "String",
300                    "string_value": "Bob"
301                }
302            }
303
304        """
305        params = {'MessageBody' : message_content}
306        if delay_seconds:
307            params['DelaySeconds'] = int(delay_seconds)
308
309        if message_attributes is not None:
310            keys = sorted(message_attributes.keys())
311            for i, name in enumerate(keys, start=1):
312                attribute = message_attributes[name]
313                params['MessageAttribute.%s.Name' % i] = name
314                if 'data_type' in attribute:
315                    params['MessageAttribute.%s.Value.DataType' % i] = \
316                        attribute['data_type']
317                if 'string_value' in attribute:
318                    params['MessageAttribute.%s.Value.StringValue' % i] = \
319                        attribute['string_value']
320                if 'binary_value' in attribute:
321                    params['MessageAttribute.%s.Value.BinaryValue' % i] = \
322                        attribute['binary_value']
323                if 'string_list_value' in attribute:
324                    params['MessageAttribute.%s.Value.StringListValue' % i] = \
325                        attribute['string_list_value']
326                if 'binary_list_value' in attribute:
327                    params['MessageAttribute.%s.Value.BinaryListValue' % i] = \
328                        attribute['binary_list_value']
329
330        return self.get_object('SendMessage', params, Message,
331                               queue.id, verb='POST')
332
333    def send_message_batch(self, queue, messages):
334        """
335        Delivers up to 10 messages to a queue in a single request.
336
337        :type queue: A :class:`boto.sqs.queue.Queue` object.
338        :param queue: The Queue to which the messages will be written.
339
340        :type messages: List of lists.
341        :param messages: A list of lists or tuples.  Each inner
342            tuple represents a single message to be written
343            and consists of and ID (string) that must be unique
344            within the list of messages, the message body itself
345            which can be a maximum of 64K in length, an
346            integer which represents the delay time (in seconds)
347            for the message (0-900) before the message will
348            be delivered to the queue, and an optional dict of
349            message attributes like those passed to ``send_message``
350            above.
351
352        """
353        params = {}
354        for i, msg in enumerate(messages):
355            base = 'SendMessageBatchRequestEntry.%i' % (i + 1)
356            params['%s.Id' % base] = msg[0]
357            params['%s.MessageBody' % base] = msg[1]
358            params['%s.DelaySeconds' % base] = msg[2]
359            if len(msg) > 3:
360                base += '.MessageAttribute'
361                keys = sorted(msg[3].keys())
362                for j, name in enumerate(keys):
363                    attribute = msg[3][name]
364
365                    p_name = '%s.%i.Name' % (base, j + 1)
366                    params[p_name] = name
367
368                    if 'data_type' in attribute:
369                        p_name = '%s.%i.DataType' % (base, j + 1)
370                        params[p_name] = attribute['data_type']
371                    if 'string_value' in attribute:
372                        p_name = '%s.%i.StringValue' % (base, j + 1)
373                        params[p_name] = attribute['string_value']
374                    if 'binary_value' in attribute:
375                        p_name = '%s.%i.BinaryValue' % (base, j + 1)
376                        params[p_name] = attribute['binary_value']
377                    if 'string_list_value' in attribute:
378                        p_name = '%s.%i.StringListValue' % (base, j + 1)
379                        params[p_name] = attribute['string_list_value']
380                    if 'binary_list_value' in attribute:
381                        p_name = '%s.%i.BinaryListValue' % (base, j + 1)
382                        params[p_name] = attribute['binary_list_value']
383
384        return self.get_object('SendMessageBatch', params, BatchResults,
385                               queue.id, verb='POST')
386
387    def change_message_visibility(self, queue, receipt_handle,
388                                  visibility_timeout):
389        """
390        Extends the read lock timeout for the specified message from
391        the specified queue to the specified value.
392
393        :type queue: A :class:`boto.sqs.queue.Queue` object
394        :param queue: The Queue from which messages are read.
395
396        :type receipt_handle: str
397        :param receipt_handle: The receipt handle associated with the message
398                               whose visibility timeout will be changed.
399
400        :type visibility_timeout: int
401        :param visibility_timeout: The new value of the message's visibility
402                                   timeout in seconds.
403        """
404        params = {'ReceiptHandle' : receipt_handle,
405                  'VisibilityTimeout' : visibility_timeout}
406        return self.get_status('ChangeMessageVisibility', params, queue.id)
407
408    def change_message_visibility_batch(self, queue, messages):
409        """
410        A batch version of change_message_visibility that can act
411        on up to 10 messages at a time.
412
413        :type queue: A :class:`boto.sqs.queue.Queue` object.
414        :param queue: The Queue to which the messages will be written.
415
416        :type messages: List of tuples.
417        :param messages: A list of tuples where each tuple consists
418            of a :class:`boto.sqs.message.Message` object and an integer
419            that represents the new visibility timeout for that message.
420        """
421        params = {}
422        for i, t in enumerate(messages):
423            prefix = 'ChangeMessageVisibilityBatchRequestEntry'
424            p_name = '%s.%i.Id' % (prefix, (i+1))
425            params[p_name] = t[0].id
426            p_name = '%s.%i.ReceiptHandle' % (prefix, (i+1))
427            params[p_name] = t[0].receipt_handle
428            p_name = '%s.%i.VisibilityTimeout' % (prefix, (i+1))
429            params[p_name] = t[1]
430        return self.get_object('ChangeMessageVisibilityBatch',
431                               params, BatchResults,
432                               queue.id, verb='POST')
433
434    def get_all_queues(self, prefix=''):
435        """
436        Retrieves all queues.
437
438        :keyword str prefix: Optionally, only return queues that start with
439            this value.
440        :rtype: list
441        :returns: A list of :py:class:`boto.sqs.queue.Queue` instances.
442        """
443        params = {}
444        if prefix:
445            params['QueueNamePrefix'] = prefix
446        return self.get_list('ListQueues', params, [('QueueUrl', Queue)])
447
448    def get_queue(self, queue_name, owner_acct_id=None):
449        """
450        Retrieves the queue with the given name, or ``None`` if no match
451        was found.
452
453        :param str queue_name: The name of the queue to retrieve.
454        :param str owner_acct_id: Optionally, the AWS account ID of the account that created the queue.
455        :rtype: :py:class:`boto.sqs.queue.Queue` or ``None``
456        :returns: The requested queue, or ``None`` if no match was found.
457        """
458        params = {'QueueName': queue_name}
459        if owner_acct_id:
460            params['QueueOwnerAWSAccountId']=owner_acct_id
461        try:
462            return self.get_object('GetQueueUrl', params, Queue)
463        except SQSError:
464            return None
465
466    lookup = get_queue
467
468    def get_dead_letter_source_queues(self, queue):
469        """
470        Retrieves the dead letter source queues for a given queue.
471
472        :type queue: A :class:`boto.sqs.queue.Queue` object.
473        :param queue: The queue for which to get DL source queues
474        :rtype: list
475        :returns: A list of :py:class:`boto.sqs.queue.Queue` instances.
476        """
477        params = {'QueueUrl': queue.url}
478        return self.get_list('ListDeadLetterSourceQueues', params,
479                             [('QueueUrl', Queue)])
480
481    #
482    # Permissions methods
483    #
484
485    def add_permission(self, queue, label, aws_account_id, action_name):
486        """
487        Add a permission to a queue.
488
489        :type queue: :class:`boto.sqs.queue.Queue`
490        :param queue: The queue object
491
492        :type label: str or unicode
493        :param label: A unique identification of the permission you are setting.
494            Maximum of 80 characters ``[0-9a-zA-Z_-]``
495            Example, AliceSendMessage
496
497        :type aws_account_id: str or unicode
498        :param principal_id: The AWS account number of the principal
499            who will be given permission.  The principal must have an
500            AWS account, but does not need to be signed up for Amazon
501            SQS. For information about locating the AWS account
502            identification.
503
504        :type action_name: str or unicode
505        :param action_name: The action.  Valid choices are:
506            * *
507            * SendMessage
508            * ReceiveMessage
509            * DeleteMessage
510            * ChangeMessageVisibility
511            * GetQueueAttributes
512
513        :rtype: bool
514        :return: True if successful, False otherwise.
515
516        """
517        params = {'Label': label,
518                  'AWSAccountId' : aws_account_id,
519                  'ActionName' : action_name}
520        return self.get_status('AddPermission', params, queue.id)
521
522    def remove_permission(self, queue, label):
523        """
524        Remove a permission from a queue.
525
526        :type queue: :class:`boto.sqs.queue.Queue`
527        :param queue: The queue object
528
529        :type label: str or unicode
530        :param label: The unique label associated with the permission
531                      being removed.
532
533        :rtype: bool
534        :return: True if successful, False otherwise.
535        """
536        params = {'Label': label}
537        return self.get_status('RemovePermission', params, queue.id)
538