aws-sdk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (205) hide show
  1. data/.yardopts +6 -0
  2. data/LICENSE.txt +171 -0
  3. data/NOTICE.txt +2 -0
  4. data/README.rdoc +189 -0
  5. data/lib/aws-sdk.rb +14 -0
  6. data/lib/aws.rb +63 -0
  7. data/lib/aws/api_config.rb +45 -0
  8. data/lib/aws/api_config/.document +0 -0
  9. data/lib/aws/api_config/EC2-2011-02-28.yml +2314 -0
  10. data/lib/aws/api_config/SNS-2010-03-31.yml +171 -0
  11. data/lib/aws/api_config/SQS-2009-02-01.yml +161 -0
  12. data/lib/aws/api_config/SimpleDB-2009-04-15.yml +278 -0
  13. data/lib/aws/api_config/SimpleEmailService-2010-12-01.yml +147 -0
  14. data/lib/aws/api_config_transform.rb +32 -0
  15. data/lib/aws/async_handle.rb +90 -0
  16. data/lib/aws/authorize_v2.rb +37 -0
  17. data/lib/aws/authorize_v3.rb +37 -0
  18. data/lib/aws/base_client.rb +524 -0
  19. data/lib/aws/cacheable.rb +92 -0
  20. data/lib/aws/common.rb +228 -0
  21. data/lib/aws/configurable.rb +36 -0
  22. data/lib/aws/configuration.rb +272 -0
  23. data/lib/aws/configured_client_methods.rb +81 -0
  24. data/lib/aws/configured_grammars.rb +65 -0
  25. data/lib/aws/configured_option_grammars.rb +46 -0
  26. data/lib/aws/configured_xml_grammars.rb +47 -0
  27. data/lib/aws/default_signer.rb +38 -0
  28. data/lib/aws/ec2.rb +321 -0
  29. data/lib/aws/ec2/attachment.rb +149 -0
  30. data/lib/aws/ec2/attachment_collection.rb +57 -0
  31. data/lib/aws/ec2/availability_zone.rb +80 -0
  32. data/lib/aws/ec2/availability_zone_collection.rb +47 -0
  33. data/lib/aws/ec2/block_device_mappings.rb +53 -0
  34. data/lib/aws/ec2/client.rb +54 -0
  35. data/lib/aws/ec2/client/xml.rb +127 -0
  36. data/lib/aws/ec2/collection.rb +39 -0
  37. data/lib/aws/ec2/config_transform.rb +63 -0
  38. data/lib/aws/ec2/elastic_ip.rb +107 -0
  39. data/lib/aws/ec2/elastic_ip_collection.rb +85 -0
  40. data/lib/aws/ec2/errors.rb +29 -0
  41. data/lib/aws/ec2/filtered_collection.rb +65 -0
  42. data/lib/aws/ec2/has_permissions.rb +46 -0
  43. data/lib/aws/ec2/image.rb +245 -0
  44. data/lib/aws/ec2/image_collection.rb +235 -0
  45. data/lib/aws/ec2/instance.rb +515 -0
  46. data/lib/aws/ec2/instance_collection.rb +276 -0
  47. data/lib/aws/ec2/key_pair.rb +86 -0
  48. data/lib/aws/ec2/key_pair_collection.rb +102 -0
  49. data/lib/aws/ec2/permission_collection.rb +177 -0
  50. data/lib/aws/ec2/region.rb +81 -0
  51. data/lib/aws/ec2/region_collection.rb +55 -0
  52. data/lib/aws/ec2/request.rb +27 -0
  53. data/lib/aws/ec2/reserved_instances.rb +50 -0
  54. data/lib/aws/ec2/reserved_instances_collection.rb +44 -0
  55. data/lib/aws/ec2/reserved_instances_offering.rb +55 -0
  56. data/lib/aws/ec2/reserved_instances_offering_collection.rb +43 -0
  57. data/lib/aws/ec2/resource.rb +340 -0
  58. data/lib/aws/ec2/resource_tag_collection.rb +218 -0
  59. data/lib/aws/ec2/security_group.rb +246 -0
  60. data/lib/aws/ec2/security_group/ip_permission.rb +70 -0
  61. data/lib/aws/ec2/security_group/ip_permission_collection.rb +59 -0
  62. data/lib/aws/ec2/security_group_collection.rb +132 -0
  63. data/lib/aws/ec2/snapshot.rb +138 -0
  64. data/lib/aws/ec2/snapshot_collection.rb +90 -0
  65. data/lib/aws/ec2/tag.rb +88 -0
  66. data/lib/aws/ec2/tag_collection.rb +114 -0
  67. data/lib/aws/ec2/tagged_collection.rb +48 -0
  68. data/lib/aws/ec2/tagged_item.rb +87 -0
  69. data/lib/aws/ec2/volume.rb +190 -0
  70. data/lib/aws/ec2/volume_collection.rb +95 -0
  71. data/lib/aws/errors.rb +129 -0
  72. data/lib/aws/http/builtin_handler.rb +69 -0
  73. data/lib/aws/http/curb_handler.rb +123 -0
  74. data/lib/aws/http/handler.rb +77 -0
  75. data/lib/aws/http/httparty_handler.rb +61 -0
  76. data/lib/aws/http/request.rb +136 -0
  77. data/lib/aws/http/request_param.rb +63 -0
  78. data/lib/aws/http/response.rb +75 -0
  79. data/lib/aws/ignore_result_element.rb +38 -0
  80. data/lib/aws/indifferent_hash.rb +86 -0
  81. data/lib/aws/inflection.rb +46 -0
  82. data/lib/aws/lazy_error_classes.rb +64 -0
  83. data/lib/aws/meta_utils.rb +43 -0
  84. data/lib/aws/model.rb +57 -0
  85. data/lib/aws/naming.rb +32 -0
  86. data/lib/aws/option_grammar.rb +544 -0
  87. data/lib/aws/policy.rb +912 -0
  88. data/lib/aws/rails.rb +209 -0
  89. data/lib/aws/record.rb +79 -0
  90. data/lib/aws/record/attribute.rb +94 -0
  91. data/lib/aws/record/attribute_macros.rb +288 -0
  92. data/lib/aws/record/attributes/boolean.rb +49 -0
  93. data/lib/aws/record/attributes/datetime.rb +86 -0
  94. data/lib/aws/record/attributes/float.rb +48 -0
  95. data/lib/aws/record/attributes/integer.rb +68 -0
  96. data/lib/aws/record/attributes/sortable_float.rb +60 -0
  97. data/lib/aws/record/attributes/sortable_integer.rb +95 -0
  98. data/lib/aws/record/attributes/string.rb +69 -0
  99. data/lib/aws/record/base.rb +728 -0
  100. data/lib/aws/record/conversion.rb +38 -0
  101. data/lib/aws/record/dirty_tracking.rb +286 -0
  102. data/lib/aws/record/errors.rb +153 -0
  103. data/lib/aws/record/exceptions.rb +48 -0
  104. data/lib/aws/record/finder_methods.rb +262 -0
  105. data/lib/aws/record/naming.rb +31 -0
  106. data/lib/aws/record/scope.rb +157 -0
  107. data/lib/aws/record/validations.rb +653 -0
  108. data/lib/aws/record/validator.rb +237 -0
  109. data/lib/aws/record/validators/acceptance.rb +51 -0
  110. data/lib/aws/record/validators/block.rb +38 -0
  111. data/lib/aws/record/validators/confirmation.rb +43 -0
  112. data/lib/aws/record/validators/count.rb +108 -0
  113. data/lib/aws/record/validators/exclusion.rb +43 -0
  114. data/lib/aws/record/validators/format.rb +57 -0
  115. data/lib/aws/record/validators/inclusion.rb +56 -0
  116. data/lib/aws/record/validators/length.rb +107 -0
  117. data/lib/aws/record/validators/numericality.rb +138 -0
  118. data/lib/aws/record/validators/presence.rb +45 -0
  119. data/lib/aws/resource_cache.rb +39 -0
  120. data/lib/aws/response.rb +113 -0
  121. data/lib/aws/response_cache.rb +50 -0
  122. data/lib/aws/s3.rb +109 -0
  123. data/lib/aws/s3/access_control_list.rb +252 -0
  124. data/lib/aws/s3/acl_object.rb +266 -0
  125. data/lib/aws/s3/bucket.rb +320 -0
  126. data/lib/aws/s3/bucket_collection.rb +122 -0
  127. data/lib/aws/s3/bucket_version_collection.rb +85 -0
  128. data/lib/aws/s3/client.rb +999 -0
  129. data/lib/aws/s3/client/xml.rb +190 -0
  130. data/lib/aws/s3/data_options.rb +99 -0
  131. data/lib/aws/s3/errors.rb +43 -0
  132. data/lib/aws/s3/multipart_upload.rb +318 -0
  133. data/lib/aws/s3/multipart_upload_collection.rb +78 -0
  134. data/lib/aws/s3/object_collection.rb +159 -0
  135. data/lib/aws/s3/object_metadata.rb +67 -0
  136. data/lib/aws/s3/object_upload_collection.rb +83 -0
  137. data/lib/aws/s3/object_version.rb +141 -0
  138. data/lib/aws/s3/object_version_collection.rb +78 -0
  139. data/lib/aws/s3/paginated_collection.rb +94 -0
  140. data/lib/aws/s3/policy.rb +76 -0
  141. data/lib/aws/s3/prefix_and_delimiter_collection.rb +56 -0
  142. data/lib/aws/s3/prefixed_collection.rb +84 -0
  143. data/lib/aws/s3/presigned_post.rb +504 -0
  144. data/lib/aws/s3/request.rb +198 -0
  145. data/lib/aws/s3/s3_object.rb +794 -0
  146. data/lib/aws/s3/tree.rb +116 -0
  147. data/lib/aws/s3/tree/branch_node.rb +71 -0
  148. data/lib/aws/s3/tree/child_collection.rb +108 -0
  149. data/lib/aws/s3/tree/leaf_node.rb +99 -0
  150. data/lib/aws/s3/tree/node.rb +22 -0
  151. data/lib/aws/s3/tree/parent.rb +90 -0
  152. data/lib/aws/s3/uploaded_part.rb +82 -0
  153. data/lib/aws/s3/uploaded_part_collection.rb +86 -0
  154. data/lib/aws/service_interface.rb +60 -0
  155. data/lib/aws/simple_db.rb +202 -0
  156. data/lib/aws/simple_db/attribute.rb +159 -0
  157. data/lib/aws/simple_db/attribute_collection.rb +227 -0
  158. data/lib/aws/simple_db/client.rb +52 -0
  159. data/lib/aws/simple_db/client/options.rb +34 -0
  160. data/lib/aws/simple_db/client/xml.rb +68 -0
  161. data/lib/aws/simple_db/consistent_read_option.rb +42 -0
  162. data/lib/aws/simple_db/delete_attributes.rb +64 -0
  163. data/lib/aws/simple_db/domain.rb +118 -0
  164. data/lib/aws/simple_db/domain_collection.rb +116 -0
  165. data/lib/aws/simple_db/domain_metadata.rb +112 -0
  166. data/lib/aws/simple_db/errors.rb +46 -0
  167. data/lib/aws/simple_db/expect_condition_option.rb +45 -0
  168. data/lib/aws/simple_db/item.rb +84 -0
  169. data/lib/aws/simple_db/item_collection.rb +594 -0
  170. data/lib/aws/simple_db/item_data.rb +70 -0
  171. data/lib/aws/simple_db/put_attributes.rb +62 -0
  172. data/lib/aws/simple_db/request.rb +27 -0
  173. data/lib/aws/simple_email_service.rb +373 -0
  174. data/lib/aws/simple_email_service/client.rb +39 -0
  175. data/lib/aws/simple_email_service/client/options.rb +24 -0
  176. data/lib/aws/simple_email_service/client/xml.rb +38 -0
  177. data/lib/aws/simple_email_service/email_address_collection.rb +66 -0
  178. data/lib/aws/simple_email_service/errors.rb +29 -0
  179. data/lib/aws/simple_email_service/quotas.rb +64 -0
  180. data/lib/aws/simple_email_service/request.rb +27 -0
  181. data/lib/aws/sns.rb +69 -0
  182. data/lib/aws/sns/client.rb +37 -0
  183. data/lib/aws/sns/client/options.rb +24 -0
  184. data/lib/aws/sns/client/xml.rb +38 -0
  185. data/lib/aws/sns/errors.rb +29 -0
  186. data/lib/aws/sns/policy.rb +49 -0
  187. data/lib/aws/sns/request.rb +27 -0
  188. data/lib/aws/sns/subscription.rb +100 -0
  189. data/lib/aws/sns/subscription_collection.rb +84 -0
  190. data/lib/aws/sns/topic.rb +384 -0
  191. data/lib/aws/sns/topic_collection.rb +70 -0
  192. data/lib/aws/sns/topic_subscription_collection.rb +58 -0
  193. data/lib/aws/sqs.rb +70 -0
  194. data/lib/aws/sqs/client.rb +38 -0
  195. data/lib/aws/sqs/client/xml.rb +36 -0
  196. data/lib/aws/sqs/errors.rb +33 -0
  197. data/lib/aws/sqs/policy.rb +50 -0
  198. data/lib/aws/sqs/queue.rb +507 -0
  199. data/lib/aws/sqs/queue_collection.rb +105 -0
  200. data/lib/aws/sqs/received_message.rb +184 -0
  201. data/lib/aws/sqs/received_sns_message.rb +112 -0
  202. data/lib/aws/sqs/request.rb +44 -0
  203. data/lib/aws/xml_grammar.rb +923 -0
  204. data/rails/init.rb +15 -0
  205. metadata +298 -0
@@ -0,0 +1,70 @@
1
+ # Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+
14
+ require 'aws/model'
15
+ require 'aws/sns/topic'
16
+
17
+ module AWS
18
+ class SNS
19
+
20
+ class TopicCollection
21
+
22
+ include Model
23
+ include Enumerable
24
+
25
+
26
+ # Creates and returns a new SNS Topic.
27
+ # @return [Topic] Returns a new topic with the given name.
28
+ def create name
29
+ response = client.create_topic(:name => name)
30
+ Topic.new(response.topic_arn, :config => config)
31
+ end
32
+
33
+ # @param [String] topic_arn An AWS SNS Topic ARN. It should be
34
+ # formatted something like:
35
+ #
36
+ # arn:aws:sns:us-east-1:123456789012:TopicName
37
+ #
38
+ # @return [Topic] Returns a topic with the given Topic ARN.
39
+ def [] topic_arn
40
+ unless topic_arn =~ /^arn:aws:sns:/
41
+ raise ArgumentError, "invalid topic arn `#{topic_arn}`"
42
+ end
43
+ Topic.new(topic_arn, :config => config)
44
+ end
45
+
46
+ # Yields once for each topic.
47
+ # @yieldparam [Topic] topic
48
+ # @return [nil]
49
+ def each &block
50
+
51
+ next_token = nil
52
+
53
+ begin
54
+
55
+ list_options = next_token ? { :next_token => next_token } : {}
56
+ response = client.list_topics(list_options)
57
+
58
+ response.topics.each do |t|
59
+ topic = Topic.new(t.topic_arn, :config => config)
60
+ yield(topic)
61
+ end
62
+
63
+ end while(next_token = response.next_token)
64
+ nil
65
+
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,58 @@
1
+ # Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+
14
+ require 'aws/model'
15
+ require 'aws/sns/subscription_collection'
16
+
17
+ module AWS
18
+ class SNS
19
+
20
+ # Represents the collection of all subscriptions for a particular
21
+ # topic. For example:
22
+ #
23
+ # # get the e-mail addressess that receive plain-text
24
+ # # messages sent to the topic
25
+ # topic.subscriptions.
26
+ # select { |s| s.protocol == :email }.
27
+ # map(&:endpoint)
28
+ class TopicSubscriptionCollection < SubscriptionCollection
29
+
30
+ include Model
31
+ include Enumerable
32
+
33
+ # @return [Topic] The topic to which all the subscriptions
34
+ # belong.
35
+ attr_reader :topic
36
+
37
+ # @private
38
+ def initialize(topic, opts = {})
39
+ @topic = topic
40
+ super
41
+ end
42
+
43
+ # @private
44
+ protected
45
+ def list_request
46
+ :list_subscriptions_by_topic
47
+ end
48
+
49
+ # @private
50
+ protected
51
+ def request_opts
52
+ { :topic_arn => topic.arn }
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,70 @@
1
+ # Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+
14
+ require 'aws/common'
15
+ require 'aws/service_interface'
16
+ require 'aws/sqs/client'
17
+ require 'aws/sqs/queue_collection'
18
+
19
+ module AWS
20
+
21
+ # Provides an expressive, object-oriented interface to Amazon SQS.
22
+ #
23
+ # == Credentials
24
+ #
25
+ # You can setup default credentials for all AWS services via
26
+ # AWS.config:
27
+ #
28
+ # AWS.config(
29
+ # :access_key_id => 'YOUR_ACCESS_KEY_ID',
30
+ # :secret_access_key => 'YOUR_SECRET_ACCESS_KEY')
31
+ #
32
+ # Or you can set them directly on the SQS interface:
33
+ #
34
+ # sqs = AWS::SQS.new(
35
+ # :access_key_id => 'YOUR_ACCESS_KEY_ID',
36
+ # :secret_access_key => 'YOUR_SECRET_ACCESS_KEY')
37
+ #
38
+ # == Queues and Messages
39
+ #
40
+ # Amazon SQS is a distributed queue system that enables web
41
+ # service applications to quickly and reliably queue messages that
42
+ # one component in the application generates to be consumed by
43
+ # another component. A queue is a temporary repository for
44
+ # messages that are awaiting processing.
45
+ #
46
+ # You can access your queues using the {#queues} collection. For
47
+ # example, to create a queue, use {QueueCollection#create}:
48
+ #
49
+ # queue = sqs.queues.create("myqueue")
50
+ #
51
+ # Or to find out what queues you have in your account:
52
+ #
53
+ # pp sqs.queues.collect(&:url)
54
+ #
55
+ # See the {Queue} class for more information on how to send and
56
+ # receive messages.
57
+ #
58
+ class SQS
59
+
60
+ include ServiceInterface
61
+
62
+ # @return [QueueCollection] The collection of all {Queue}
63
+ # objects in your account.
64
+ def queues
65
+ QueueCollection.new(:config => config)
66
+ end
67
+
68
+ end
69
+
70
+ end
@@ -0,0 +1,38 @@
1
+ # Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+
14
+ require 'aws/base_client'
15
+ require 'aws/configured_client_methods'
16
+ require 'aws/sqs/request'
17
+ require 'aws/sqs/client/xml'
18
+ require 'aws/sqs/errors'
19
+ require 'aws/sqs/policy'
20
+
21
+ module AWS
22
+ class SQS
23
+
24
+ # @private
25
+ class Client < BaseClient
26
+
27
+ include ConfiguredClientMethods
28
+
29
+ API_VERSION = '2009-02-01'
30
+
31
+ # @private
32
+ REQUEST_CLASS = SQS::Request
33
+
34
+ configure_client
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,36 @@
1
+ # Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+
14
+ require 'aws/configured_xml_grammars'
15
+ require 'aws/ignore_result_element'
16
+
17
+ module AWS
18
+ class SQS
19
+ class Client < BaseClient
20
+
21
+ # @private
22
+ module XML
23
+
24
+ include ConfiguredXmlGrammars
25
+ extend IgnoreResultElement
26
+
27
+ BaseError = XmlGrammar.customize do
28
+ element("Error") { ignore }
29
+ end
30
+
31
+ define_configured_grammars
32
+
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,33 @@
1
+ # Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+
14
+ require 'aws/lazy_error_classes'
15
+ require 'aws/sqs/client/xml'
16
+
17
+ module AWS
18
+ class SQS
19
+
20
+ # @private
21
+ module Errors
22
+
23
+ BASE_ERROR_GRAMMAR = Client::XML::BaseError
24
+
25
+ include LazyErrorClasses
26
+
27
+ def self.error_class(code)
28
+ super(code.sub(/^AWS\.SimpleQueueService\./, ''))
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,50 @@
1
+ # Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+
14
+ require 'aws/policy'
15
+
16
+ module AWS
17
+ class SQS
18
+
19
+ # @private
20
+ class Policy < AWS::Policy
21
+
22
+ class Statement < AWS::Policy::Statement
23
+
24
+ ACTION_MAPPING = {
25
+ :add_permission => 'sqs:AddPermission',
26
+ :change_message_visibility => 'sqs:ChangeMessageVisibility',
27
+ :create_queue => 'sqs:CreateQueue',
28
+ :delete_message => 'sqs:DeleteMessage',
29
+ :delete_queue => 'sqs:DeleteQueue',
30
+ :get_queue_attributes => 'sqs:GetQueueAttributes',
31
+ :list_queues => 'sqs:ListQueues',
32
+ :receive_message => 'sqs:ReceiveMessage',
33
+ :remove_permission => 'sqs:RemovePermission',
34
+ :send_message => 'sqs:SendMessage',
35
+ :set_queue_attributes => 'sqs:SetQueueAttributes',
36
+ }
37
+
38
+ protected
39
+ def resource_arn resource
40
+ case resource
41
+ when Queue then URI.parse(resource.url).path
42
+ else resource.to_s
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,507 @@
1
+ # Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+
14
+ require 'aws/inflection'
15
+ require 'aws/model'
16
+ require 'aws/sqs/received_message'
17
+ require 'aws/sqs/received_sns_message'
18
+
19
+ module AWS
20
+ class SQS
21
+
22
+ # Represents an Amazon SQS Queue.
23
+ #
24
+ # @example Sending a message
25
+ # msg = queue.send_message("HELLO")
26
+ # puts "Sent message: #{msg.id}"
27
+ #
28
+ # @example Polling for messages indefinitely
29
+ # queue.poll do |msg|
30
+ # puts "Got message: #{msg.body}"
31
+ # end
32
+ #
33
+ class Queue
34
+
35
+ # The default number of seconds to wait between polling requests for
36
+ # new messages.
37
+ DEFAULT_POLL_INTERVAL = 1
38
+
39
+ include Model
40
+
41
+ # @return [String] The queue URL.
42
+ attr_reader :url
43
+
44
+ # @private
45
+ def initialize(url, opts = {})
46
+ @url = url
47
+ super
48
+ end
49
+
50
+ # Deletes the queue, regardless of whether it is empty.
51
+ #
52
+ # When you delete a queue, the deletion process takes up to 60
53
+ # seconds. Requests you send involving that queue during the
54
+ # 60 seconds might succeed. For example, calling
55
+ # {#send_message} might succeed, but after the 60 seconds, the
56
+ # queue and that message you sent no longer exist.
57
+ #
58
+ # Also, when you delete a queue, you must wait at least 60 seconds
59
+ # before creating a queue with the same name.
60
+ # @return [nil]
61
+ def delete
62
+ client.delete_queue(:queue_url => url)
63
+ nil
64
+ end
65
+
66
+ # Represents a message sent using {Queue#send_message}.
67
+ class SentMessage
68
+
69
+ # @return [String] Returns the message ID.
70
+ attr_accessor :message_id
71
+
72
+ alias_method :id, :message_id
73
+
74
+ # @return [String] Returns an MD5 digest of the message body
75
+ # string. You can use this to verify that SQS received your
76
+ # message correctly.
77
+ attr_accessor :md5
78
+
79
+ end
80
+
81
+ # Delivers a message to this queue.
82
+ #
83
+ # @param [String] body The message to send. The maximum
84
+ # allowed message size is 64 KB. The message may only
85
+ # contain Unicode characters from the following list,
86
+ # according to the W3C XML specification (for more
87
+ # information, go to
88
+ # http://www.w3.org/TR/REC-xml/#charsets). If you send any
89
+ # characters not included in the list, your request will be
90
+ # rejected.
91
+ #
92
+ # * #x9
93
+ # * #xA
94
+ # * #xD
95
+ # * #x20 to #xD7FF
96
+ # * #xE000 to #xFFFD
97
+ # * #x10000 to #x10FFFF
98
+ #
99
+ # @return [SentMessage] An object containing information about
100
+ # the message that was sent.
101
+ def send_message(body)
102
+ resp = client.send_message(:queue_url => url,
103
+ :message_body => body)
104
+ msg = SentMessage.new
105
+ msg.message_id = resp.message_id
106
+ msg.md5 = resp.md5_of_message_body
107
+ msg
108
+ end
109
+
110
+ # Retrieves one or more messages. When a block is given, each
111
+ # message is yielded to the block and then deleted as long as
112
+ # the block exits normally. When no block is given, you must
113
+ # delete the message yourself using {ReceivedMessage#delete}.
114
+ #
115
+ # @note Due to the distributed nature of the queue, a weighted
116
+ # random set of machines is sampled on a ReceiveMessage
117
+ # call. That means only the messages on the sampled machines
118
+ # are returned. If the number of messages in the queue is
119
+ # small (less than 1000), it is likely you will get fewer
120
+ # messages than you requested per call to
121
+ # {#receive_message}. If the number of messages in the queue
122
+ # is extremely small, you might not receive any messages.
123
+ # To poll continually for messages, use the {#poll} method,
124
+ # which automatically retries the request after a
125
+ # configurable delay.
126
+ #
127
+ # @param [Hash] opts Options for receiving messages.
128
+ #
129
+ # @option opts [Integer] :limit The maximum number of messages
130
+ # to receive. By default this is 1, and the return value is
131
+ # a single message object. If this options is specified and
132
+ # is not 1, the return value is an array of message objects;
133
+ # however, the array may contain fewer objects than you
134
+ # requested. Valid values: integers from 1 to 10.
135
+ #
136
+ # Not necessarily all the messages in the queue are returned
137
+ # (for more information, see the preceding note about
138
+ # machine sampling).
139
+ #
140
+ # @option opts [Integer] :visibilitiy_timeout The duration (in
141
+ # seconds) that the received messages are hidden from
142
+ # subsequent retrieve requests. Valid values: integer from
143
+ # 0 to 43200 (maximum 12 hours)
144
+ #
145
+ # @option opts [Array<Symbol, String>] :attributes The
146
+ # attributes to populate in each received message. Valid values:
147
+ #
148
+ # * +:all+ (to populate all attributes)
149
+ # * +:sender_id+
150
+ # * +:sent_at+
151
+ # * +:receive_count+
152
+ # * +:first_received_at+
153
+ #
154
+ # See {ReceivedMessage} for documentation on each
155
+ # attribute's meaning.
156
+ #
157
+ # @yieldparam [ReceivedMessage] message Each message that was received.
158
+ #
159
+ # @return [ReceivedMessage] Returns the received message (or messages)
160
+ # only if a block is not given to this method.
161
+ #
162
+ def receive_message(opts = {}, &block)
163
+ resp = client.receive_message(receive_opts(opts))
164
+
165
+ messages = resp.messages.map do |m|
166
+ ReceivedMessage.new(self, m.message_id, m.receipt_handle,
167
+ :body => m.body,
168
+ :md5 => m.md5_of_body,
169
+ :attributes => m.attributes)
170
+ end
171
+
172
+ if block
173
+ call_message_block(messages, block)
174
+ elsif opts[:limit] && opts[:limit] != 1
175
+ messages
176
+ else
177
+ messages.first
178
+ end
179
+ end
180
+ alias_method :receive_messages, :receive_message
181
+
182
+ # Polls continually for messages. For example, you can use
183
+ # this to poll indefinitely:
184
+ #
185
+ # queue.poll { |msg| puts msg.body }
186
+ #
187
+ # Or, to poll indefinitely for the first message and then
188
+ # continue polling until no message is received for a period
189
+ # of at least ten seconds:
190
+ #
191
+ # queue.poll(:initial_timeout => false,
192
+ # :idle_timeout => 10) { |msg| puts msg.body }
193
+ #
194
+ # As with the block form of {#receive_message}, this method
195
+ # automatically deletes the message then the block exits
196
+ # normally.
197
+ #
198
+ # @yieldparam [ReceivedMessage] message Each message that was received.
199
+ #
200
+ # @param [Hash] opts Options for polling.
201
+ #
202
+ # @option opts [Float, Integer] :poll_interval The number of
203
+ # seconds to wait before retrying when no messages are
204
+ # received. The default is 1 second.
205
+ #
206
+ # @option opts [Integer] :idle_timeout The maximum number of
207
+ # seconds to spend polling while no messages are being
208
+ # returned. By default this method polls indefinitely
209
+ # whether messages are received or not.
210
+ #
211
+ # @option opts [Integer] :initial_timeout The maximum number
212
+ # of seconds to spend polling before the first message is
213
+ # received. This option defaults to the value of
214
+ # +:idle_timeout+. You can specify +false+ to poll
215
+ # indefinitely for the first message when +:idle_timeout+ is
216
+ # set.
217
+ #
218
+ # @option opts [Integer] :batch_size The maximum number of
219
+ # messages to retrieve in a single request. By default
220
+ # messages are received one at a time. Valid values:
221
+ # integers from 1 to 10.
222
+ #
223
+ # @option opts [Integer] :visibilitiy_timeout The duration (in
224
+ # seconds) that the received messages are hidden from
225
+ # subsequent retrieve requests. Valid values: integer from
226
+ # 0 to 43200 (maximum 12 hours)
227
+ #
228
+ # @option opts [Array<Symbol, String>] :attributes The
229
+ # attributes to populate in each received message. Valid values:
230
+ #
231
+ # * +:all+ (to populate all attributes)
232
+ # * +:sender_id+
233
+ # * +:sent_at+
234
+ # * +:receive_count+
235
+ # * +:first_received_at+
236
+ #
237
+ # See {ReceivedMessage} for documentation on each
238
+ # attribute's meaning.
239
+ #
240
+ # @return [nil]
241
+ def poll(opts = {}, &block)
242
+ poll_interval = opts[:poll_interval] || DEFAULT_POLL_INTERVAL
243
+ opts[:limit] = opts.delete(:batch_size) if
244
+ opts.key?(:batch_size)
245
+
246
+ last_message_at = Time.now
247
+ got_first = false
248
+ loop do
249
+ got_msg = false
250
+ receive_messages(opts) do |message|
251
+ got_msg = got_first = true
252
+ last_message_at = Time.now
253
+ yield(message)
254
+ end
255
+ unless got_msg
256
+ Kernel.sleep(poll_interval) unless poll_interval == 0
257
+ return if hit_timeout?(got_first, last_message_at, opts)
258
+ end
259
+ end
260
+ nil
261
+ end
262
+
263
+ # @return [Integer] The approximate number of visible messages
264
+ # in a queue. For more information, see {Resources Required
265
+ # to Process
266
+ # Messages}[http://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/IntroductionArticle.html#ApproximateNumber]
267
+ # in the Amazon SQS Developer Guide.
268
+ def approximate_number_of_messages
269
+ get_attribute("ApproximateNumberOfMessages").to_i
270
+ end
271
+ alias_method :visible_messages, :approximate_number_of_messages
272
+
273
+ # @return [Integer] The approximate number of messages that
274
+ # are not timed-out and not deleted. For more information,
275
+ # see {Resources Required to Process
276
+ # Messages}[http://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/IntroductionArticle.html#ApproximateNumber]
277
+ # in the Amazon SQS Developer Guide.
278
+ def approximate_number_of_messages_not_visible
279
+ get_attribute("ApproximateNumberOfMessagesNotVisible").to_i
280
+ end
281
+ alias_method :invisible_messages, :approximate_number_of_messages_not_visible
282
+
283
+ # @return [Integer] Returns the visibility timeout for the
284
+ # queue. For more information about visibility timeout, see
285
+ # {Visibility
286
+ # Timeout}[http://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/IntroductionArticle.html#AboutVT]
287
+ # in the Amazon SQS Developer Guide.
288
+ def visibility_timeout
289
+ get_attribute("VisibilityTimeout").to_i
290
+ end
291
+
292
+ # Sets the visibility timeout for the queue.
293
+ #
294
+ # @param [Integer] timeout The length of time (in seconds)
295
+ # that a message received from a queue will be invisible to
296
+ # other receiving components when they ask to receive
297
+ # messages. Valid values: integers from 0 to 43200 (12
298
+ # hours).
299
+ #
300
+ # @return Returns the value passed as a timeout.
301
+ def visibility_timeout=(timeout)
302
+ set_attribute("VisibilityTimeout", timeout.to_s)
303
+ timeout
304
+ end
305
+
306
+ # @return [Time] The time when the queue was created.
307
+ def created_timestamp
308
+ Time.at(get_attribute("CreatedTimestamp").to_i)
309
+ end
310
+
311
+ # @return [Time] The time when the queue was last changed.
312
+ def last_modified_timestamp
313
+ Time.at(get_attribute("LastModifiedTimestamp").to_i)
314
+ end
315
+
316
+ # @return [Integer] The limit of how many bytes a message can
317
+ # contain before Amazon SQS rejects it.
318
+ def maximum_message_size
319
+ get_attribute("MaximumMessageSize").to_i
320
+ end
321
+
322
+ # Sets the maximum message size for the queue.
323
+ #
324
+ # @param [Integer] size The limit of how many bytes a message
325
+ # can contain before Amazon SQS rejects it. This must be an
326
+ # integer from 1024 bytes (1KB) up to 65536 bytes
327
+ # (64KB). The default for this attribute is 8192 (8KB).
328
+ # @return Retuns the passed size argument.
329
+ def maximum_message_size=(size)
330
+ set_attribute("MaximumMessageSize", size.to_s)
331
+ end
332
+
333
+ # @return [Integer] The number of seconds Amazon SQS retains a
334
+ # message.
335
+ def message_retention_period
336
+ get_attribute("MessageRetentionPeriod").to_i
337
+ end
338
+
339
+ # Sets the message retention period for the queue
340
+ #
341
+ # @param [Integer] period The number of seconds Amazon SQS
342
+ # retains a message. Must be an integer from 3600 (1 hour)
343
+ # to 1209600 (14 days). The default for this attribute is
344
+ # 345600 (4 days).
345
+ # @return Returns the passed period argument.
346
+ def message_retention_period=(period)
347
+ set_attribute("MessageRetentionPeriod", period.to_s)
348
+ period
349
+ end
350
+
351
+ # @return [String] The queue's Amazon resource name (ARN).
352
+ def arn
353
+ @arn ||= get_attribute("QueueArn")
354
+ end
355
+
356
+ # @return [Boolean] True if the queue exists.
357
+ #
358
+ # @note This may raise an exception if you don't have
359
+ # permission to access the queue attributes. Also, it may
360
+ # return true for up to 60 seconds after a queue has been
361
+ # deleted.
362
+ def exists?
363
+ client.get_queue_attributes(:queue_url => url,
364
+ :attribute_names => ["QueueArn"])
365
+ rescue Errors::NonExistentQueue, Errors::InvalidAddress
366
+ false
367
+ else
368
+ true
369
+ end
370
+
371
+ # @private
372
+ module PolicyProxy
373
+
374
+ attr_accessor :queue
375
+
376
+ def change
377
+ yield(self)
378
+ queue.policy = self
379
+ end
380
+
381
+ def delete
382
+ queue.client.send(:set_attribute, 'Policy', '')
383
+ end
384
+
385
+ end
386
+
387
+ # @return [Policy] Returns the current queue policy if there is one.
388
+ # Returns +nil+ otherwise.
389
+ def policy
390
+ if policy_json = get_attribute('Policy')
391
+ policy = SQS::Policy.from_json(policy_json)
392
+ policy.extend(PolicyProxy)
393
+ policy.queue = self
394
+ policy
395
+ else
396
+ nil
397
+ end
398
+ end
399
+
400
+ # Set the policy on this queue.
401
+ #
402
+ # If you pass nil or an empty string then it will have the same
403
+ # effect as deleting the policy.
404
+ #
405
+ # @param policy The policy to set. This policy can be a {Policy} object,
406
+ # a json policy string, or any other object that responds with a policy
407
+ # string when it received #to_json.
408
+ #
409
+ # @return [nil]
410
+ def policy= policy
411
+ policy_string = case policy
412
+ when nil, '' then ''
413
+ when String then policy
414
+ else policy.to_json
415
+ end
416
+ set_attribute('Policy', policy_string)
417
+ nil
418
+ end
419
+
420
+ # @return [Boolean] Returns true if the other queue has the same
421
+ # url.
422
+ def ==(other)
423
+ other.kind_of?(Queue) and other.url == url
424
+ end
425
+ alias_method :eql?, :==
426
+
427
+ # @private
428
+ def inspect
429
+ "<#{self.class}:#{url}>"
430
+ end
431
+
432
+ # @private
433
+ protected
434
+ def hit_timeout?(got_first, last_message_at, opts)
435
+ initial_timeout = opts[:initial_timeout]
436
+ idle_timeout = opts[:idle_timeout]
437
+
438
+ timeout = (got_first ||
439
+ # if initial_timeout is false (as opposed
440
+ # to nil) then we skip the branch and poll
441
+ # indefinitely until the first message
442
+ # comes
443
+ (!initial_timeout && initial_timeout != false) ?
444
+ idle_timeout :
445
+ initial_timeout) and
446
+ Time.now - last_message_at > timeout
447
+ end
448
+
449
+ # @private
450
+ protected
451
+ def receive_opts(opts)
452
+ receive_opts = { :queue_url => url }
453
+ receive_opts[:visibility_timeout] = opts[:visibility_timeout] if
454
+ opts[:visibility_timeout]
455
+ receive_opts[:max_number_of_messages] = opts[:limit] if
456
+ opts[:limit]
457
+ if names = opts[:attributes]
458
+ receive_opts[:attribute_names] = names.map do |name|
459
+ name = ReceivedMessage::ATTRIBUTE_ALIASES[name.to_sym] if
460
+ ReceivedMessage::ATTRIBUTE_ALIASES.key?(name.to_sym)
461
+ name = Inflection.class_name(name.to_s) if name.kind_of?(Symbol)
462
+ name
463
+ end
464
+ end
465
+ receive_opts
466
+ end
467
+
468
+ # @private
469
+ protected
470
+ def call_message_block(messages, block)
471
+ result = nil
472
+ messages.each do |message|
473
+ begin
474
+ result = block.call(message)
475
+ rescue Exception => e
476
+ raise
477
+ else
478
+ message.delete
479
+ end
480
+ end
481
+ result
482
+ end
483
+
484
+ # @private
485
+ protected
486
+ def get_attribute(name)
487
+ resp = client.get_queue_attributes(:queue_url => url,
488
+ :attribute_names =>
489
+ [name, "QueueArn"].uniq)
490
+ @arn ||= resp.attributes["QueueArn"]
491
+ resp.attributes[name]
492
+ end
493
+
494
+ # @private
495
+ protected
496
+ def set_attribute(name, value)
497
+ client.set_queue_attributes(:queue_url => url,
498
+ :attribute => {
499
+ :name => name,
500
+ :value => value
501
+ })
502
+ end
503
+
504
+ end
505
+
506
+ end
507
+ end