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,92 @@
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/naming'
15
+ require 'aws/meta_utils'
16
+
17
+ module AWS
18
+
19
+ # @private
20
+ module Cacheable
21
+
22
+ def local_cache_key
23
+ raise NotImplementedError
24
+ end
25
+
26
+ def cache_key
27
+ endpoint_method = self.class.service_ruby_name + "_endpoint"
28
+ config.signer.access_key_id + ":" +
29
+ config.send(endpoint_method) + ":" +
30
+ local_cache_key
31
+ end
32
+
33
+ def attributes_from_response(response)
34
+ method = "populate_from_#{response.request_type}"
35
+ if respond_to?(method)
36
+ send(method, response)
37
+ end
38
+ end
39
+
40
+ def request_types
41
+ self.class.request_types
42
+ end
43
+
44
+ class NoData < StandardError; end
45
+
46
+ def retrieve_attribute(attribute)
47
+ if cache = AWS.response_cache
48
+ if cache.resource_cache.cached?(cache_key, attribute)
49
+ return cache.resource_cache.get(cache_key, attribute)
50
+ end
51
+ cache.select(*request_types).each do |resp|
52
+ if attributes = attributes_from_response(resp)
53
+ cache.resource_cache.store(cache_key, attributes)
54
+ return attributes[attribute] if attributes.key?(attribute)
55
+ end
56
+ end
57
+ end
58
+ resp = yield
59
+ if attributes = attributes_from_response(resp)
60
+ if cache = AWS.response_cache
61
+ cache.resource_cache.store(cache_key, attributes)
62
+ end
63
+ attributes[attribute] if attributes.key?(attribute)
64
+ else
65
+ raise NoData.new("no data for #{self} in response to #{resp.request_type}")
66
+ end
67
+ end
68
+
69
+ module ClassMethods
70
+
71
+ def request_types
72
+ []
73
+ end
74
+
75
+ def populate_from(type, &block)
76
+ define_method("populate_from_#{type}", &block)
77
+ new_request_types = request_types + [type]
78
+ new_request_types.uniq!
79
+ MetaUtils.extend_method(self, :request_types) { new_request_types }
80
+ end
81
+
82
+ end
83
+
84
+ def self.included(mod)
85
+ mod.extend ClassMethods
86
+ mod.extend Naming unless
87
+ mod.respond_to?(:service_ruby_name)
88
+ end
89
+
90
+ end
91
+
92
+ end
@@ -0,0 +1,228 @@
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/configuration'
15
+ require 'aws/resource_cache'
16
+ require 'aws/response_cache'
17
+
18
+ # Root namespace for the AWS SDK for Ruby.
19
+ module AWS
20
+ class << self
21
+
22
+ # @private
23
+ @@config = nil
24
+
25
+ # The global configuration for AWS. Generally you set your prefered
26
+ # configuration operations once after loading the aws-sdk gem.
27
+ #
28
+ # AWS.config({
29
+ # :access_key_id => 'ACCESS_KEY_ID',
30
+ # :secret_access_key => 'SECRET_ACCESS_KEY',
31
+ # :simple_db_endpoint => 'sdb.us-west-1.amazonaws.com',
32
+ # :max_retries => 2,
33
+ # })
34
+ #
35
+ # When using AWS classes they will always default to use configuration
36
+ # values defined in {AWS.config}.
37
+ #
38
+ # AWS.config(:max_retries => 2)
39
+ #
40
+ # sqs = AWS::SQS.new
41
+ # sqs.config.max_retries #=> 2
42
+ #
43
+ # If you want to change a configuration value for a single instance you
44
+ # pass the new configuration value to that objects initializer:
45
+ #
46
+ # AWS::SQS.new(:max_retries => 0)
47
+ #
48
+ # @note Changing the global configuration does not affect objects
49
+ # that have already been constructed.
50
+ # @param [Hash] options
51
+ # @option options [String] :access_key_id Your account access key
52
+ # id credential.
53
+ # @option options [String] :secret_access_key Your account secret
54
+ # access key credential.
55
+ # @option options [Integer] :max_retries (3) The maximum number of times
56
+ # service errors (500) should be retried. There is an exponential
57
+ # backoff in between service request retries, so the more retries the
58
+ # longer it can take to fail.
59
+ # @option options [String] :ec2_endpoint ('ec2.amazonaws.com') The
60
+ # service endpoint to use when communicating with Amazon EC2.
61
+ # @option options :http_handler The request/response handler
62
+ # for all service requests. The default handler uses HTTParty to
63
+ # send requests.
64
+ # @option options :logger (nil) A logger instance that should receive log
65
+ # messages generated by service requets. A logger needs to respond to
66
+ # #log and must accept a severity (e.g. :info, :error, etc) and
67
+ # a string message.
68
+ # @option options [String] :s3_endpoint ('s3.amazonaws.com') The
69
+ # service endpoint to use when communicating with Amazon S3.
70
+ # @option [Integer] :s3_multipart_threshold (16777216) When uploading
71
+ # data to S3, if the number of bytes to send exceedes
72
+ # +:s3_multipart_threshold+ then a multi part session is automatically
73
+ # started and the data is sent up in chunks. The size of each part
74
+ # is specified by +:s3_multipart_min_part_size+.
75
+ # @option [Integer] :s3_multipart_min_part_size (5242880)
76
+ # @option [Integer] :s3_multipart_max_parts (1000)
77
+ # @option options [Boolean] :simple_db_consistent_reads (false) When true
78
+ # all read operations against SimpleDB will be consistent reads (slower).
79
+ # @option options [String] :simple_db_endpoint ('sdb.amazonaws.com') The
80
+ # service endpoint to use when communicating with Amazon SimpleDB.
81
+ # @option options [String] :simple_email_service_endpoint ('email.us-east-1.amazonaws.com')
82
+ # The service endpoint to use when communicating with Amazon
83
+ # SimpleEmailService.
84
+ # @option options :signer The request signer. Defaults to a
85
+ # {DefaultSigner}.
86
+ # @option options [String] :sns_endpoint ('sns.us-east-1.amazonaws.com')
87
+ # The service endpoint to use when communicating with Amazon SNS.
88
+ # @option options [String] :sqs_endpoint ('sqs.us-east-1.amazonaws.com')
89
+ # The service endpoint to use when communicating with Amazon SQS.
90
+ # @option options :stub_requests (false) When true no requests will be
91
+ # made against the live service. Responses returned will have empty
92
+ # values. This is primarily used for writing tests.
93
+ # @option options [Boolean] :use_ssl (true) When true, all requests are
94
+ # sent over SSL.
95
+ # @option options [String] :user_agent_prefix (nil) A string prefix to
96
+ # append to all requets against AWS services. This should be set
97
+ # for clients and applications built ontop of the aws-sdk gem.
98
+ # @return [Configuration] Returns the new configuration.
99
+ def config options = {}
100
+ @@config ||= Configuration.new
101
+ @@config = @@config.with(options) unless options.empty?
102
+ @@config
103
+ end
104
+
105
+ # @note Memoization is currently only supported for the EC2 APIs;
106
+ # other APIs are unaffected by the status of memoization. To
107
+ # protect your code from future changes in memoization support,
108
+ # you should not enable memoization while calling non-EC2 APIs.
109
+ #
110
+ # Starts memoizing service requests made in the current thread.
111
+ # See {memoize} for a full discussion of the memoization feature.
112
+ # This has no effect if memoization is already enabled.
113
+ def start_memoizing
114
+ Thread.current[:aws_memoization] ||= {}
115
+ nil
116
+ end
117
+
118
+ # @note Memoization is currently only supported for the EC2 APIs;
119
+ # other APIs are unaffected by the status of memoization. To
120
+ # protect your code from future changes in memoization support,
121
+ # you should not enable memoization while calling non-EC2 APIs.
122
+ #
123
+ # Stops memoizing service requests made in the current thread.
124
+ # See {memoize} for a full discussion of the memoization feature.
125
+ # This has no effect if memoization is already disabled.
126
+ def stop_memoizing
127
+ Thread.current[:aws_memoization] = nil
128
+ end
129
+
130
+ # @note Memoization is currently only supported for the EC2 APIs;
131
+ # other APIs are unaffected by the status of memoization. To
132
+ # protect your code from future changes in memoization support,
133
+ # you should not enable memoization while calling non-EC2 APIs.
134
+ #
135
+ # @return [Boolean] True if memoization is enabled for the current
136
+ # thread. See {memoize} for a full discussion of the
137
+ # memoization feature.
138
+ def memoizing?
139
+ !Thread.current[:aws_memoization].nil?
140
+ end
141
+
142
+ # @note Memoization is currently only supported for the EC2 APIs;
143
+ # other APIs are unaffected by the status of memoization. To
144
+ # protect your code from future changes in memoization support,
145
+ # you should not enable memoization while calling non-EC2 APIs.
146
+ #
147
+ # Enables memoization for the current thread, within a block.
148
+ # Memoization lets you avoid making multiple requests for the same
149
+ # data by reusing the responses which have already been received.
150
+ # For example, consider the following code to get the most
151
+ # recently launched EC2 instance:
152
+ #
153
+ # latest = ec2.instances.sort_by(&:launch_time).last
154
+ #
155
+ # The above code would make N+1 requests (where N is the number of
156
+ # instances in the account); iterating the collection of instances
157
+ # is one request, and +Enumerable#sort_by+ calls
158
+ # {AWS::EC2::Instance#launch_time} for each instance, causing
159
+ # another request per instance. We can rewrite the code as
160
+ # follows to make only one request:
161
+ #
162
+ # latest = AWS.memoize do
163
+ # ec2.instances.sort_by(&:launch_time).last
164
+ # end
165
+ #
166
+ # Iterating the collection still causes a request, but each
167
+ # subsequent call to {AWS::EC2::Instance#launch_time} uses the
168
+ # results from that first request rather than making a new request
169
+ # for the same data.
170
+ #
171
+ # While memoization is enabled, every response that is received
172
+ # from the service is retained in memory. Therefore you should
173
+ # use memoization only for short-lived blocks of code that make
174
+ # relatively small numbers of requests. The cached responses are
175
+ # used in two ways while memoization is enabled:
176
+ #
177
+ # 1. Before making a request, the SDK checks the cache for a
178
+ # response to a request with the same signature (credentials,
179
+ # service endpoint, operation name, and parameters). If such a
180
+ # response is found, it is used instead of making a new
181
+ # request.
182
+ #
183
+ # 2. Before retrieving data for an attribute of a resource
184
+ # (e.g. {AWS::EC2::Instance#launch_time}), the SDK attempts to
185
+ # find a cached response that contains the requested data. If
186
+ # such a response is found, the cached data is returned instead
187
+ # of making a new request.
188
+ #
189
+ # When memoization is disabled, all previously cached responses
190
+ # are discarded.
191
+ def memoize
192
+ return yield if memoizing?
193
+
194
+ begin
195
+ start_memoizing
196
+ yield if block_given?
197
+ ensure
198
+ stop_memoizing
199
+ end
200
+ end
201
+
202
+ # @private
203
+ def resource_cache
204
+ if memoizing?
205
+ Thread.current[:aws_memoization][:resource_cache] ||=
206
+ ResourceCache.new
207
+ end
208
+ end
209
+
210
+ # @private
211
+ def response_cache
212
+ if memoizing?
213
+ Thread.current[:aws_memoization][:response_cache] ||=
214
+ ResponseCache.new
215
+ end
216
+ end
217
+
218
+ # Causes all requests to return empty responses without making any
219
+ # requests against the live services. This does not attempt to
220
+ # mock the services.
221
+ # @return [nil]
222
+ def stub!
223
+ config(:stub_requests => true)
224
+ nil
225
+ end
226
+
227
+ end
228
+ 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/configuration'
15
+
16
+ module AWS
17
+
18
+ # @private
19
+ module Configurable
20
+
21
+ module ClassMethods
22
+
23
+ def make_configurable(name, opts = {})
24
+ Configuration.add_parameter_type(self, name, opts)
25
+ end
26
+
27
+ end
28
+
29
+ def self.included(m)
30
+ m.extend ClassMethods
31
+ super
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,272 @@
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/default_signer'
15
+ require 'aws/http/httparty_handler'
16
+ require 'set'
17
+
18
+ module AWS
19
+
20
+ # A configuration object for AWS interfaces and clients.
21
+ #
22
+ # == Configuring Credential
23
+ #
24
+ # In order to do anything with AWS you will need to assign credentials.
25
+ # The simplest method is to assing your credentials into the default
26
+ # configuration:
27
+ #
28
+ # AWS.config(:access_key_id => 'KEY', :secret_access_key => 'SECRET')
29
+ #
30
+ # You can also export them into your environment and they will be picked up
31
+ # automatically:
32
+ #
33
+ # export AWS_ACCESS_KEY_ID='YOUR_KEY_ID_HERE'
34
+ # export AWS_SECRET_ACCESS_KEY='YOUR_SECRET_KEY_HERE'
35
+ #
36
+ # For compatability with other AWS gems, the credentials can also be
37
+ # exported like:
38
+ #
39
+ # export AMAZON_ACCESS_KEY_ID='YOUR_KEY_ID_HERE'
40
+ # export AMAZON_SECRET_ACCESS_KEY='YOUR_SECRET_KEY_HERE'
41
+ #
42
+ # == Modifying a Configuration
43
+ #
44
+ # Configuration objects are read-only. If you need a different set of
45
+ # configuration values, call {#with}, passing in the updates
46
+ # and a new configuration object will be returned.
47
+ #
48
+ # config = Configuration.new(:max_retires => 3)
49
+ # new_config = config.with(:max_retries => 2)
50
+ #
51
+ # config.max_retries #=> 3
52
+ # new_config.max_retries #=> 2
53
+ #
54
+ # == Global Configuration
55
+ #
56
+ # The global default configuration can be found at {AWS.config}
57
+ #
58
+ class Configuration
59
+
60
+ # Creates a new Configuration object.
61
+ # @param options (see AWS.config)
62
+ # @option options (see AWS.config)
63
+ def initialize options = {}
64
+
65
+ @create_options = options.delete(:__create_options__) || {}
66
+
67
+ @overridden = options.delete(:__overridden__) ||
68
+ Set.new(options.keys.map { |k| k.to_sym })
69
+
70
+ @options = {
71
+ :ec2_endpoint => 'ec2.amazonaws.com',
72
+ :http_handler => Http::HTTPartyHandler.new,
73
+ :max_retries => 3,
74
+ :s3_endpoint => 's3.amazonaws.com',
75
+ :s3_multipart_threshold => 16 * 1024 * 1024,
76
+ :s3_multipart_min_part_size => 5 * 1024 * 1024,
77
+ :s3_multipart_max_parts => 10000,
78
+ :simple_db_endpoint => 'sdb.amazonaws.com',
79
+ :simple_db_consistent_reads => false,
80
+ :simple_email_service_endpoint => 'email.us-east-1.amazonaws.com',
81
+ :sns_endpoint => 'sns.us-east-1.amazonaws.com',
82
+ :sqs_endpoint => 'sqs.us-east-1.amazonaws.com',
83
+ :stub_requests => false,
84
+ :use_ssl => true,
85
+ :user_agent_prefix => nil,
86
+ }
87
+
88
+ {
89
+ 'AWS_ACCESS_KEY_ID' => :access_key_id,
90
+ 'AWS_SECRET_ACCESS_KEY' => :secret_access_key,
91
+ 'AMAZON_ACCESS_KEY_ID' => :access_key_id,
92
+ 'AMAZON_SECRET_ACCESS_KEY' => :secret_access_key,
93
+ }.each_pair do |env_key, opt_key|
94
+ if ENV[env_key]
95
+ @options[opt_key] = ENV[env_key]
96
+ end
97
+ end
98
+
99
+ options.each do |(k,v)|
100
+ @options[k.to_sym] = v
101
+ end
102
+
103
+ end
104
+
105
+ # @return [Boolean] Returns true if web service requets should be
106
+ # made with HTTPS.
107
+ def use_ssl?
108
+ @options[:use_ssl]
109
+ end
110
+ alias_method :use_ssl, :use_ssl?
111
+
112
+ # @return [String] Your AWS account access key id credential.
113
+ def access_key_id
114
+ @options[:access_key_id]
115
+ end
116
+
117
+ # @return [String] Your AWS secret access key credential.
118
+ def secret_access_key
119
+ @options[:secret_access_key]
120
+ end
121
+
122
+ # Used to create a new Configuration object with the given modifications.
123
+ # The current configuration object is not modified.
124
+ #
125
+ # AWS.config(:max_retries => 2)
126
+ #
127
+ # no_retries_config = AWS.config.with(:max_retries => 0)
128
+ #
129
+ # AWS.config.max_retries #=> 2
130
+ # no_retries_config.max_retries #=> 0
131
+ #
132
+ # You can use these configuration objects returned by #with to create
133
+ # AWS objects:
134
+ #
135
+ # AWS::S3.new(:config => no_retries_config)
136
+ # AWS::SQS.new(:config => no_retries_config)
137
+ #
138
+ # @param options (see AWS.config)
139
+ # @option options (see AWS.config)
140
+ # @return [Configuration] Copies the current configuration and returns
141
+ # a new one with modifications as provided in +:options+.
142
+ def with options = {}
143
+ overridden = @overridden + options.keys.map { |k| k.to_sym }
144
+ self.class.new(@options.merge(options).
145
+ merge(:__create_options__ => @create_options,
146
+ :__overridden__ => overridden))
147
+ end
148
+
149
+ # @return [Integer] Maximum number of times to retry server errors.
150
+ def max_retries
151
+ @options[:max_retries]
152
+ end
153
+
154
+ # @return [String] The service endpoint for Amazon S3.
155
+ def s3_endpoint
156
+ @options[:s3_endpoint]
157
+ end
158
+
159
+ # @return [String] The default service endpoint for Amazon EC2.
160
+ def ec2_endpoint
161
+ @options[:ec2_endpoint]
162
+ end
163
+
164
+ # @return [String] The service endpoint for Amazon SimpleDB.
165
+ def simple_db_endpoint
166
+ @options[:simple_db_endpoint]
167
+ end
168
+
169
+ # @return [String] The service endpoint for Amazon SimpleEmailService.
170
+ def simple_email_service_endpoint
171
+ @options[:simple_email_service_endpoint]
172
+ end
173
+
174
+ # @return [String] The service endpoint for Amazon SNS.
175
+ def sns_endpoint
176
+ @options[:sns_endpoint]
177
+ end
178
+
179
+ # @return [String] The service endpoint for Amazon SQS.
180
+ def sqs_endpoint
181
+ @options[:sqs_endpoint]
182
+ end
183
+
184
+ # @return [Boolean] Returns true if all reads to SimpleDB default to
185
+ # consistent reads.
186
+ def simple_db_consistent_reads?
187
+ @options[:simple_db_consistent_reads]
188
+ end
189
+
190
+ # @return [String,nil] Returns the prefix that is appended to the
191
+ # user agent string that is sent with all requests to AWS.
192
+ def user_agent_prefix
193
+ @options[:user_agent_prefix]
194
+ end
195
+
196
+ # @return [Object] Returns the current http handler.
197
+ def http_handler
198
+ @options[:http_handler]
199
+ end
200
+
201
+ # @return [Object] Returns the current request signer.
202
+ def signer
203
+ return @options[:signer] if @options[:signer]
204
+ raise "Missing credentials" unless access_key_id and secret_access_key
205
+ @options[:signer] ||= DefaultSigner.new(access_key_id, secret_access_key)
206
+ end
207
+
208
+ # @return [Object,nil] Returns the current logger.
209
+ def logger
210
+ @options[:logger]
211
+ end
212
+
213
+ # @return [Boolean] Returns true if this configuration causes
214
+ # all AWS requests to return stubbed (empty) responses without making a
215
+ # request to the actual service.
216
+ def stub_requests?
217
+ @options[:stub_requests]
218
+ end
219
+
220
+ # @return [Integer] Returns the number of bytes where files larger
221
+ # are uploaded to S3 in multiple parts.
222
+ def s3_multipart_threshold
223
+ @options[:s3_multipart_threshold]
224
+ end
225
+
226
+ # @return [Integer] The absolute minimum size (in bytes) each S3 multipart
227
+ # segment should be.
228
+ def s3_multipart_min_part_size
229
+ @options[:s3_multipart_min_part_size]
230
+ end
231
+
232
+ # @return [Integer] The maximum number of parts to split a file into
233
+ # when uploading to S3.
234
+ def s3_multipart_max_parts
235
+ @options[:s3_multipart_max_parts]
236
+ end
237
+
238
+ # @private
239
+ def inspect
240
+ "<#{self.class}>"
241
+ end
242
+
243
+ class << self
244
+
245
+ # @private
246
+ def add_parameter_type(klass, name, options = {})
247
+ if options[:needs]
248
+ define_method(name) do
249
+ unless @overridden.include?(name)
250
+ create_options = { :config => self }
251
+ options[:needs].each do |need|
252
+ create_options[need] = send(need)
253
+ end
254
+ end
255
+
256
+ return @options[name] if @options[name] and
257
+ (@overridden.include?(name) or
258
+ @create_options[name] == create_options)
259
+
260
+ @create_options[name] = create_options
261
+ @options[name] = klass.new(create_options)
262
+ end
263
+ else
264
+ define_method(name) do
265
+ @options[name] ||= klass.new(:config => self)
266
+ end
267
+ end
268
+ end
269
+
270
+ end
271
+ end
272
+ end