aws-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,39 @@
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
+ module AWS
15
+
16
+ # @private
17
+ class ResourceCache
18
+
19
+ def initialize
20
+ @cache = {}
21
+ end
22
+
23
+ def store(key, attributes)
24
+ (@cache[key] ||= {}).merge!(attributes)
25
+ end
26
+
27
+ def cached?(key, attribute)
28
+ attributes = @cache[key] and
29
+ attributes.key?(attribute)
30
+ end
31
+
32
+ def get(key, attribute)
33
+ raise "No cached value for attribute :#{attribute} of #{key}" unless
34
+ cached?(key, attribute)
35
+ @cache[key][attribute]
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,113 @@
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/async_handle'
15
+
16
+ module AWS
17
+
18
+ # @private
19
+ class Response
20
+
21
+ include AsyncHandle
22
+
23
+ # @return [AWS::Error] Returns nil unless the request failed.
24
+ # Normally this will be nil unless you are using the Asynchronous
25
+ # interface.
26
+ attr_accessor :error
27
+
28
+ # @return [Hash] The hash of options passed to the low level request
29
+ # method that generated this response.
30
+ attr_accessor :request_options
31
+
32
+ # @return [Symbol] The low-level request method that generated
33
+ # this response
34
+ attr_accessor :request_type
35
+
36
+ # @return [Http::Request] the HTTP request object
37
+ attr_accessor :http_request
38
+
39
+ # @return [Http::Response] the HTTP response object
40
+ attr_accessor :http_response
41
+
42
+ # @return [Boolean] true if the response is cached
43
+ attr_accessor :cached
44
+
45
+ # @param [Http::Request] http_request
46
+ # @param [Http::Response] http_request
47
+ def initialize http_request = nil, http_response = nil
48
+ @http_request = http_request
49
+ @http_response = http_response
50
+ end
51
+
52
+ # @return [Boolean] Teturns true unless there is a response error.
53
+ def successful?
54
+ error.nil?
55
+ end
56
+
57
+ # @return [Boolean] Returns true if the http request was throttled
58
+ # by AWS.
59
+ def throttled?
60
+ !successful? and
61
+ parsed_body = XmlGrammar.parse(http_response.body) and
62
+ parsed_body.respond_to?(:code) and
63
+ parsed_body.code == "Throttling"
64
+ end
65
+
66
+ # @return [Boolean] Returns true if the http request timed out.
67
+ def timeout?
68
+ http_response.timeout?
69
+ end
70
+
71
+ # @private
72
+ def inspect
73
+ "<#{self.class}>"
74
+ end
75
+
76
+ def cache_key
77
+ [http_request.access_key_id,
78
+ http_request.host,
79
+ request_type,
80
+ serialized_options].join(":")
81
+ end
82
+
83
+ def serialized_options
84
+ serialize_options_hash(request_options)
85
+ end
86
+
87
+ private
88
+ def serialize_options_hash(hash)
89
+ "(" + hash.keys.sort_by(&:to_s).map do |key|
90
+ "#{key}=#{serialize_options_value(hash[key])}"
91
+ end.join(" ") + ")"
92
+ end
93
+
94
+ private
95
+ def serialize_options_value(value)
96
+ case value
97
+ when Hash
98
+ serialize_options_hash(value)
99
+ when Array
100
+ serialize_options_array(value)
101
+ else
102
+ value.inspect
103
+ end
104
+ end
105
+
106
+ private
107
+ def serialize_options_array(ary)
108
+ "[" + ary.map { |v| serialize_options_value(v) }.join(" ") +
109
+ "]"
110
+ end
111
+
112
+ end
113
+ 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/resource_cache'
15
+
16
+ module AWS
17
+
18
+ # @private
19
+ class ResponseCache
20
+
21
+ attr_reader :cached_responses
22
+
23
+ attr_reader :resource_cache
24
+
25
+ def initialize
26
+ @cached_responses = []
27
+ @indexed_responses = {}
28
+ @resource_cache = ResourceCache.new
29
+ end
30
+
31
+ def add(resp)
32
+ cached_responses.unshift(resp)
33
+ @indexed_responses[resp.cache_key] = resp if
34
+ resp.respond_to?(:cache_key)
35
+ @resource_cache = ResourceCache.new
36
+ end
37
+
38
+ def select(*types, &block)
39
+ cached_responses.select do |resp|
40
+ types.map { |t| t.to_s }.include?(resp.request_type.to_s) and
41
+ (block.nil? || block.call(resp))
42
+ end
43
+ end
44
+
45
+ def cached(resp)
46
+ @indexed_responses[resp.cache_key]
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,109 @@
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/s3/client'
17
+ require 'aws/s3/errors'
18
+ require 'aws/s3/bucket_collection'
19
+
20
+ module AWS
21
+
22
+ # Provides an expressive, object-oriented interface to Amazon S3.
23
+ #
24
+ # To use Amazon S3 you must first
25
+ # {sign up here}[http://aws.amazon.com/s3/].
26
+ #
27
+ # For more information about Amazon S3, see:
28
+ #
29
+ # * {Amazon S3}[http://aws.amazon.com/s3/]
30
+ # * {Amazon S3 Documentation}[http://aws.amazon.com/documentation/s3/]
31
+ #
32
+ # == Credentials
33
+ #
34
+ # You can setup default credentials for all AWS services via
35
+ # AWS.config:
36
+ #
37
+ # AWS.config(
38
+ # :access_key_id => 'YOUR_ACCESS_KEY_ID',
39
+ # :secret_access_key => 'YOUR_SECRET_ACCESS_KEY')
40
+ #
41
+ # Or you can set them directly on the S3 interface:
42
+ #
43
+ # s3 = AWS::S3.new(
44
+ # :access_key_id => 'YOUR_ACCESS_KEY_ID',
45
+ # :secret_access_key => 'YOUR_SECRET_ACCESS_KEY')
46
+ #
47
+ # == Buckets Keys and Objects
48
+ #
49
+ # S3 stores objects in buckets.
50
+ #
51
+ # To create a bucket:
52
+ #
53
+ # bucket = s3.buckets.create('mybucket')
54
+ #
55
+ # To get a bucket:
56
+ #
57
+ # bucket = s3.buckets[:mybucket]
58
+ # bucket = s3.buckets['mybucket']
59
+ #
60
+ # Listing buckets:
61
+ #
62
+ # s3.buckets.each do |bucket|
63
+ # puts bucket.name
64
+ # end
65
+ #
66
+ # See {Bucket} and {BucketCollection} for more information on working
67
+ # with S3 buckets.
68
+ #
69
+ # == Listing Objects
70
+ #
71
+ # Enumerating objects in a bucket:
72
+ #
73
+ # bucket.objects.each do |object|
74
+ # puts object.key #=> no data is fetched from s3, just a list of keys
75
+ # end
76
+ #
77
+ # You can limit the list of objects with a prefix, or explore the objects
78
+ # in a bucket as a tree. See {ObjectCollection} for more information.
79
+ #
80
+ # == Reading and Writing to S3
81
+ #
82
+ # Each object in a bucket has a unique key.
83
+ #
84
+ # photo = s3.buckets['mybucket'].objects['photo.jpg']
85
+ #
86
+ # Writing to an S3Object:
87
+ #
88
+ # photo.write(File.read('/some/photo.jpg'))
89
+ #
90
+ # Reading from an S3Object:
91
+ #
92
+ # File.open("/some/path/on/disk.jpg", "w") do |f|
93
+ # f.write(photo.read)
94
+ # end
95
+ #
96
+ # See {S3Object} for more information on reading and writing to S3.
97
+ #
98
+ class S3
99
+
100
+ include ServiceInterface
101
+
102
+ # @return [BucketCollection] Returns a collection that represents all
103
+ # buckets in the account.
104
+ def buckets
105
+ BucketCollection.new(:config => @config)
106
+ end
107
+
108
+ end
109
+ end
@@ -0,0 +1,252 @@
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/s3/acl_object'
15
+
16
+ module AWS
17
+ class S3
18
+
19
+ # Represents an access control list for S3 objects and buckets. For example:
20
+ #
21
+ # acl = AccessControlList.new
22
+ # acl.grant(:full_control).
23
+ # to(:canonical_user_id => "8a6925ce4adf588a4f21c32aa379004fef")
24
+ # acl.to_xml # => '<AccessControlPolicy>...'
25
+ #
26
+ # You can also construct an AccessControlList from a hash:
27
+ #
28
+ # AccessControlList.new(
29
+ # :owner => { :id => "8a6925ce4adf588a4f21c32aa379004fef" },
30
+ # :grants => [{ :grantee => {
31
+ # :canonical_user_id => "8a6925ce4adf588a4f21c32aa379004fef",
32
+ # },
33
+ # :permission => :full_control }]
34
+ # )
35
+ #
36
+ # @see ACLObject
37
+ #
38
+ # @attr [AccessControlList::Owner] owner The owner of the access
39
+ # control list. You can set this as a hash, for example:
40
+ # acl.owner = { :id => '8a6925ce4adf588a4f21c32aa379004fef' }
41
+ # This attribute is required when setting an ACL.
42
+ #
43
+ # @attr [list of AccessControlList::Grant] grants The list of
44
+ # grants. You can set this as a list of hashes, for example:
45
+ # acl.grants = [{ :grantee => { :canonical_user_id =>
46
+ # "8a6925ce4adf588a4f21c32aa379004fef" },
47
+ # :permission => :full_control }]
48
+ class AccessControlList
49
+
50
+ # Represents an ACL owner. In the default ACL, this is the
51
+ # bucket owner.
52
+ #
53
+ # @attr [String] id The canonical user ID of the ACL owner.
54
+ # This attribute is required when setting an ACL.
55
+ #
56
+ # @attr [String] display_name The display name of the ACL
57
+ # owner. This value is ignored when setting an ACL.
58
+ class Owner
59
+ include ACLObject
60
+
61
+ string_attr "ID", :required => true
62
+ string_attr "DisplayName"
63
+ end
64
+
65
+ # Represents a user who is granted some kind of permission
66
+ # through a Grant. There are three ways to specify a grantee:
67
+ #
68
+ # * You can specify the canonical user ID, for example. When
69
+ # you read an ACL from S3, all grantees will be identified
70
+ # this way, and the display_name attribute will also be provided.
71
+ #
72
+ # Grantee.new(:canonical_user_id => "8a6925ce4adf588a4f21c32aa379004fef")
73
+ #
74
+ # * You can specify the e-mail address of an AWS customer, for example:
75
+ # Grantee.new(:amazon_customer_email => 'foo@example.com')
76
+ #
77
+ # * You can specify a group URI, for example:
78
+ # Grantee.new(:group_uri => 'http://acs.amazonaws.com/groups/global/AllUsers')
79
+ # For more details about group URIs, see:
80
+ # http://docs.amazonwebservices.com/AmazonS3/latest/dev/ACLOverview.html
81
+ #
82
+ # When constructing a grantee, you must provide a value for
83
+ # exactly one of the following attributes:
84
+ #
85
+ # * +amazon_customer_email+
86
+ # * +canonical_user_id+
87
+ # * +group_uri+
88
+ #
89
+ # @attr [String] amazon_customer_email The e-mail address of
90
+ # an AWS customer.
91
+ #
92
+ # @attr [String] canonical_user_id The canonical user ID of an
93
+ # AWS customer.
94
+ #
95
+ # @attr [String] group_uri A URI that identifies a particular
96
+ # group of users.
97
+ #
98
+ # @attr [String] display_name The display name associated with
99
+ # the grantee. This is provided by S3 when reading an ACL.
100
+ class Grantee
101
+ include ACLObject
102
+
103
+ SIGNAL_ATTRIBUTES = [:amazon_customer_email,
104
+ :canonical_user_id,
105
+ :group_uri]
106
+
107
+ string_attr "EmailAddress", :method_name => "amazon_customer_email"
108
+ string_attr "ID", :method_name => "canonical_user_id"
109
+ string_attr "URI", :method_name => "group_uri"
110
+ string_attr "DisplayName"
111
+
112
+ # (see ACLObject#validate!)
113
+ def validate!
114
+ attr = signal_attribute
115
+ raise "missing amazon_customer_email, canonical_user_id, "+
116
+ "or group_uri" unless attr
117
+ raise "display_name is invalid with #{attr}" if
118
+ attr != :canonical_user_id and display_name
119
+ end
120
+
121
+ # @private
122
+ def stag
123
+ if attr = signal_attribute
124
+ super + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
125
+ " xsi:type=\"#{type_for_attr(attr)}\""
126
+ else
127
+ super
128
+ end
129
+ end
130
+
131
+ # @private
132
+ def signal_attribute
133
+ SIGNAL_ATTRIBUTES.find { |att| send(att) }
134
+ end
135
+
136
+ # @private
137
+ def type_for_attr(attr)
138
+ { :amazon_customer_email => "AmazonCustomerByEmail",
139
+ :canonical_user_id => "CanonicalUser",
140
+ :group_uri => "Group" }[attr]
141
+ end
142
+
143
+ end
144
+
145
+ # Represents the permission being granted in a Grant object.
146
+ # Typically you will not need to construct an instance of this
147
+ # class directly.
148
+ # @see Grant#permission
149
+ class Permission
150
+ include ACLObject
151
+
152
+ # The permission expressed as a symbol following Ruby
153
+ # conventions. For example, S3's FULL_CONTROL permission
154
+ # will be returned as +:full_control+.
155
+ attr_reader :name
156
+
157
+ # @private
158
+ def initialize(name)
159
+ raise "expected string or symbol" unless
160
+ name.respond_to?(:to_str) or name.respond_to?(:to_sym)
161
+ @name = name.to_sym
162
+ end
163
+
164
+ def body_xml
165
+ name.to_s.upcase
166
+ end
167
+
168
+ end
169
+
170
+ # Represents a single grant in an ACL. Both +grantee+ and
171
+ # +permission+ are required for each grant when setting an
172
+ # ACL.
173
+ #
174
+ # See
175
+ # http://docs.amazonwebservices.com/AmazonS3/latest/dev/ACLOverview.html
176
+ # for more information on how grantees and permissions are
177
+ # interpreted by S3.
178
+ #
179
+ # @attr [Grantee] grantee The user or users who are granted
180
+ # access according to this grant. You can specify this as a
181
+ # hash:
182
+ # grant.grantee = { :amazon_customer_email => "foo@example.com" }
183
+ #
184
+ # @attr [Permission or Symbol] permission The type of
185
+ # permission that is granted by this grant. Valid values are:
186
+ # * +:read+
187
+ # * +:write+
188
+ # * +:read_acp+
189
+ # * +:write_acp+
190
+ # * +:full_control+
191
+ class Grant
192
+
193
+ include ACLObject
194
+
195
+ object_attr Grantee, :required => true
196
+ object_attr Permission, :required => true, :cast => Symbol
197
+
198
+ end
199
+
200
+ include ACLObject
201
+
202
+ # @private
203
+ def stag
204
+ super()+" xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\""
205
+ end
206
+
207
+ # @private
208
+ def element_name
209
+ "AccessControlPolicy"
210
+ end
211
+
212
+ class GrantBuilder
213
+
214
+ # @private
215
+ def initialize(acl, grant)
216
+ @acl = acl
217
+ @grant = grant
218
+ end
219
+
220
+ # Specifies the grantee.
221
+ #
222
+ # @param [Grantee or Hash] grantee A Grantee object or hash;
223
+ # for example:
224
+ # acl.grant(:full_control).to(:amazon_customer_email => "foo@example.com")
225
+ def to(grantee)
226
+ @grant.grantee = grantee
227
+ @acl.grants << @grant
228
+ end
229
+
230
+ end
231
+
232
+ # Convenience method for constructing a new grant and adding
233
+ # it to the ACL. Example usage:
234
+ #
235
+ # acl.grants.size # => 0
236
+ # acl.grant(:full_control).
237
+ # to(:canonical_user_id => "8a6925ce4adf588a4f21c32aa379004fef")
238
+ # acl.grants.size # => 1
239
+ #
240
+ # @return [GrantBuilder]
241
+ def grant(permission)
242
+ GrantBuilder.new(self, Grant.new(:permission => permission))
243
+ end
244
+
245
+ object_attr Owner, :required => true
246
+ object_list_attr("AccessControlList", Grant,
247
+ :required => true, :method_name => :grants)
248
+
249
+ end
250
+
251
+ end
252
+ end