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,235 @@
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/ec2/collection'
16
+ require 'aws/ec2/tagged_collection'
17
+ require 'aws/ec2/image'
18
+ require 'aws/ec2/block_device_mappings'
19
+
20
+ module AWS
21
+ class EC2
22
+
23
+ # Represents a collection of EC2 images. You can use this to
24
+ # find out which images exist with the characteristics you are
25
+ # interested in:
26
+ #
27
+ # ec2 = EC2.new
28
+ # all_images = ec2.images
29
+ # amazon_owned_images = all_images.with_owner('amazon')
30
+ # my_images = all_images.with_owner('self')
31
+ # tagged_amis = all_images.tagged('mytag')
32
+ # tagged_amis.map(&:id) # => ["ami-123", ...]
33
+ #
34
+ # You can also use it to create new images. For example:
35
+ #
36
+ # ec2.images.create(:instance_id => "i-123",
37
+ # :name => "my-image")
38
+ #
39
+ class ImageCollection < Collection
40
+
41
+ include TaggedCollection
42
+ include BlockDeviceMappings
43
+
44
+ # @private
45
+ def initialize(options = {})
46
+ @owners = options[:owners] || []
47
+ @executable_users = options[:executable_users] || []
48
+ super(options)
49
+ end
50
+
51
+ # @return [Image] image_id The ID of the image.
52
+ def [] image_id
53
+ super
54
+ end
55
+
56
+ # @return [ImageCollection] A new collection that only includes
57
+ # images owned by one or more of the specified AWS accounts.
58
+ # The IDs +:amazon+ and +:self+ can be used to include AMIs
59
+ # owned by Amazon or AMIs owned by you, respectively.
60
+ #
61
+ # @param [Array of Strings] owners The AWS account IDs by
62
+ # which the new collection should be filtered.
63
+ def with_owner(*owners)
64
+ collection_with(:owners => @owners + owners)
65
+ end
66
+
67
+ # @return [ImageCollection] A new collection that only includes
68
+ # images for which the specified user ID has explicit launch
69
+ # permissions. The user ID can be an AWS account ID, +:self+
70
+ # to return AMIs for which the sender of the request has
71
+ # explicit launch permissions, or +:all+ to return AMIs with
72
+ # public launch permissions.
73
+ #
74
+ # @param [Array of Strings] users The AWS account IDs by which
75
+ # the new collection should be filtered.
76
+ def executable_by(*users)
77
+ collection_with(:executable_users => @executable_users + users)
78
+ end
79
+
80
+ # @yield [image] Each image in the collection.
81
+ # @return [nil]
82
+ def each(&block)
83
+ opts = {}
84
+ opts[:owners] = @owners.map { |id| id.to_s } unless
85
+ @owners.empty?
86
+ opts[:executable_users] = @executable_users.map { |id| id.to_s } unless
87
+ @executable_users.empty?
88
+ response = filtered_request(:describe_images, opts)
89
+ response.images_set.each do |i|
90
+ image = Image.new(i.image_id, :config => config)
91
+ yield(image)
92
+ end
93
+ nil
94
+ end
95
+
96
+ # Creates an AMI. There are several ways to create an AMI
97
+ # using this method; for detailed information on each strategy
98
+ # see {the EC2 Developer
99
+ # Guide}[http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/creating-an-ami.html].
100
+ #
101
+ # @param [Hash] options Options for creating the image.
102
+ # +:name+ is required, and you must also specify one of the
103
+ # following options:
104
+ #
105
+ # * +:instance_id+
106
+ # * +:image_location+
107
+ # * +:root_device_name+
108
+ #
109
+ # @option options [String] :instance_id The ID of a running
110
+ # instance. This instance will be rebooted unless
111
+ # +:no_reboot+ is set to +true+.
112
+ #
113
+ # @option options [String] :description A description of the
114
+ # new image.
115
+ #
116
+ # @option options [Boolean] :no_reboot By default this
117
+ # option is set to +false+, which means Amazon EC2
118
+ # attempts to cleanly shut down the instance before image
119
+ # creation and reboots the instance afterwards. When the
120
+ # option is set to +true+, Amazon EC2 does not shut down
121
+ # the instance before creating the image. When this option
122
+ # is used, file system integrity on the created image cannot
123
+ # be guaranteed.
124
+ #
125
+ # *Note*: This option is only valid when used with
126
+ # +:instance_id+.
127
+ #
128
+ # @option options [String] :image_location Full path to your
129
+ # AMI manifest in Amazon S3 storage. This must be of the
130
+ # form "bucket_name/key".
131
+ #
132
+ # @option options [String] :architecture The architecture of
133
+ # the image. Valid values:
134
+ #
135
+ # * +:i386+
136
+ # * +:x86_64+
137
+ #
138
+ # *Note*: This option is only valid with +:image_location+
139
+ # or +:root_device_name+
140
+ #
141
+ # @option options [String] :kernel_id The ID of the kernel to
142
+ # select.
143
+ #
144
+ # *Note*: This option is only valid with +:image_location+
145
+ # or +:root_device_name+
146
+ #
147
+ # @option options [Image] :kernel The kernel image to use.
148
+ # Equivalent to passing +:kernel_id+ with the ID of the
149
+ # image.
150
+ #
151
+ # *Note*: This option is only valid with +:image_location+
152
+ # or +:root_device_name+
153
+ #
154
+ # @option options [String] :ramdisk_id The ID of the RAM disk
155
+ # to select. Some kernels require additional drivers at
156
+ # launch. Check the kernel requirements for information on
157
+ # whether you need to specify a RAM disk. To find kernel
158
+ # requirements, refer to the Resource Center and search for
159
+ # the kernel ID.
160
+ #
161
+ # *Note*: This option is only valid with +:image_location+
162
+ # or +:root_device_name+
163
+ #
164
+ # @option options [Image] :ramdisk The ramdisk image to use.
165
+ # Equivalent to passing +:ramdisk_id+ with the ID of the
166
+ # image.
167
+ #
168
+ # *Note*: This option is only valid with +:image_location+
169
+ # or +:root_device_name+
170
+ #
171
+ # @option options [String] :root_device_name The root device
172
+ # name (e.g., /dev/sda1, or xvda).
173
+ #
174
+ # @option options [Hash] :block_device_mappings This must be a
175
+ # hash; the keys are device names to map, and the value for
176
+ # each entry determines how that device is mapped. Valid
177
+ # values include:
178
+ #
179
+ # * A string, which is interpreted as a virtual device name.
180
+ #
181
+ # * A hash with any of the following options. One of
182
+ # +:snapshot+, +:snapshot_id+ or +:volume_size+ is
183
+ # required.
184
+ #
185
+ # [:snapshot] A snapshot to use when creating the block
186
+ # device.
187
+ #
188
+ # [:snapshot_id] The ID of a snapshot to use when creating
189
+ # the block device.
190
+ #
191
+ # [:volume_size] The size of volume to create, in gigabytes.
192
+ #
193
+ # [:delete_on_termination] Setting this to true causes EC2
194
+ # to delete the volume when the
195
+ # instance is terminated.
196
+ # @return [Image]
197
+ def create(options = {})
198
+ resp = case
199
+ when options[:instance_id]
200
+ client.create_image(options)
201
+ when options[:image_location] || options[:root_device_name]
202
+ if kernel = options.delete(:kernel)
203
+ options[:kernel_id] = kernel.id
204
+ end
205
+ if ramdisk = options.delete(:ramdisk)
206
+ options[:ramdisk_id] = ramdisk.id
207
+ end
208
+ options[:block_device_mappings] =
209
+ translate_block_device_mappings(options[:block_device_mappings]) if
210
+ options[:block_device_mappings]
211
+ client.register_image(options)
212
+ else
213
+ raise(ArgumentError,
214
+ "expected instance_id, image_location, " +
215
+ "or root_device_name")
216
+ end
217
+ self[resp.image_id]
218
+ end
219
+
220
+ # @private
221
+ protected
222
+ def member_class
223
+ Image
224
+ end
225
+
226
+ # @private
227
+ protected
228
+ def preserved_options
229
+ super.merge(:owners => @owners, :executable_users => @executable_users)
230
+ end
231
+
232
+ end
233
+
234
+ end
235
+ end
@@ -0,0 +1,515 @@
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/ec2/tagged_item'
16
+ require 'aws/model'
17
+ require 'aws/cacheable'
18
+ require 'aws/ec2/resource'
19
+ require 'aws/ec2/key_pair'
20
+ require 'aws/ec2/image'
21
+
22
+ module AWS
23
+ class EC2
24
+
25
+ # Represents an EC2 instance.
26
+ #
27
+ # @attr [String] user_data Arbitrary metadata that is available
28
+ # to the instance while it is running. This interface handles
29
+ # the details of encoding the user data for transmission; you
30
+ # should set the user data exactly as you want it to be made
31
+ # available to the instance.
32
+ #
33
+ # The instance must be in a stopped state to change user data;
34
+ # for example:
35
+ #
36
+ # i.user_data # => "HELLO"
37
+ # i.status # => :running
38
+ # i.user_data = "GOODBYE" # raises an exception
39
+ # i.stop; sleep 1 until i.status == :stopped
40
+ # i.user_data = "GOODBYE" # => "GOODBYE"
41
+ #
42
+ # @attr [String] instance_type The instance type,
43
+ # e.g. "m1.small". The instance must be in a stopped state to
44
+ # change the instance type.
45
+ #
46
+ # @attr [Boolean] api_termination_disabled True if the instance
47
+ # cannot be terminated using the {#terminate} method. This
48
+ # attribute can be changed at any time.
49
+ #
50
+ # @attr [String] instance_initiated_shutdown_behavior Valid
51
+ # values are:
52
+ #
53
+ # ["stop"] When the instance shuts down, it will go into a
54
+ # "stopped" state.
55
+ #
56
+ # ["terminate"] When the instance shuts down, it will be
57
+ # terminated.
58
+ #
59
+ # @attr_reader [String] image_id Image ID of the AMI used to
60
+ # launch the instance.
61
+ #
62
+ # @attr_reader [String] key_name The name of the key pair with
63
+ # which this instance was associated at launch.
64
+ #
65
+ # @attr [String] kernel_id The ID of the kernel that the image
66
+ # currently uses. The instance must be in a stopped state to
67
+ # change this attribute.
68
+ #
69
+ # @attr [String] ramdisk_id The ID of the RAM disk that the
70
+ # image currently uses. The instance must be in a stopped
71
+ # state to change this attribute.
72
+ #
73
+ # @attr_reader [Symbol] root_device_type The root device type
74
+ # used by the AMI. The AMI can use an Amazon EBS or instance
75
+ # store root device. Valid values:
76
+ #
77
+ # * +:ebs+
78
+ # * +:instance_store+
79
+ #
80
+ # @attr_reader [String] root_device_name The name of the root
81
+ # device.
82
+ #
83
+ # @attr_reader [String] private_dns_name The DNS name of the
84
+ # instance within the EC2 network.
85
+ #
86
+ # @attr_reader [String] dns_name The DNS name of the instance on
87
+ # the internet.
88
+ #
89
+ # @attr_reader [Integer] ami_launch_index The AMI launch index,
90
+ # which can be used to find this instance within the launch
91
+ # group.
92
+ #
93
+ # @attr_reader [String] private_ip_address The private IP
94
+ # address assigned to the instance.
95
+ #
96
+ # @attr_reader [String] ip_address The IP address of the
97
+ # instance.
98
+ #
99
+ # @attr_reader [Symbol] architecture The architecture of the
100
+ # image.
101
+ #
102
+ # @attr_reader [Symbol] virtualization_type The instance's
103
+ # virtualization type. Valid values:
104
+ #
105
+ # * +:paravirtual+
106
+ # * +:hvm+
107
+ #
108
+ # @attr_reader [String] reservation_id The ID of the reservation
109
+ # in which this instance was launched.
110
+ #
111
+ # @attr_reader [String] requester_id ID of the requester that
112
+ # launched the instance on your behalf (e.g., AWS Management
113
+ # Console, Auto Scaling).
114
+ #
115
+ # @attr_reader [String] owner_id ID of the AWS account that owns
116
+ # the reservation in which the instance was launched.
117
+ #
118
+ # @attr_reader [Symbol] monitoring The status of CloudWatch
119
+ # monitoring for the instance. Valid values:
120
+ #
121
+ # * +:enabled+
122
+ # * +:disabled+
123
+ # * +:pending+
124
+ #
125
+ # @attr_reader [String] state_transition_reason A string
126
+ # describing the reason for the last state transition.
127
+ #
128
+ # @attr_reader [Time] launch_time The time at which the instance
129
+ # was launched.
130
+ #
131
+ # @attr_reader [String] platform A string describing the
132
+ # platform of the image (e.g. "windows").
133
+ #
134
+ # @attr_reader [Symbol] hypervisor The instance's hypervisor
135
+ # type. Valid values:
136
+ #
137
+ # * +:ovm+
138
+ # * +:xen+
139
+ #
140
+ # @attr_reader [String] client_token Idempotency token you
141
+ # provided when you launched the instance.
142
+ class Instance < Resource
143
+
144
+ include TaggedItem
145
+
146
+ # Creates an object that represents the instance with the
147
+ # given ID. It's usually easier to get an instance of this
148
+ # class by calling {InstanceCollection#[]} or
149
+ # {InstanceCollection#each}.
150
+ def initialize(instance_id, opts = {})
151
+ super
152
+ @id = instance_id
153
+ @reservation_attributes = {}
154
+ end
155
+
156
+ # @return [String] Returns the instance id.
157
+ attr_reader :id
158
+
159
+ # @return [Boolean] True if the instance exists according to
160
+ # EC2.
161
+ def exists?
162
+ client.describe_instances(:filters =>
163
+ [{
164
+ :name => "instance-id",
165
+ :values => [id]
166
+ }]).instance_index.key?(id)
167
+ end
168
+
169
+ # @private
170
+ def self.instance_action(name)
171
+ define_method(name) do
172
+ client.send("#{name}_instances", :instance_ids => [id])
173
+ nil
174
+ end
175
+ end
176
+
177
+ # Terminates the instance.
178
+ # @return [nil]
179
+ def terminate; end
180
+ instance_action :terminate
181
+ alias_method :delete, :terminate
182
+
183
+ # Reboots the instance.
184
+ # @return [nil]
185
+ def reboot; end
186
+ instance_action :reboot
187
+
188
+ # Starts the instance, assuming it is in a stopped state.
189
+ # @see stop
190
+ # @return [nil]
191
+ def start; end
192
+ instance_action :start
193
+
194
+ # Stops the instance, eventually putting it into a stopped state.
195
+ # @return [nil]
196
+ def stop; end
197
+ instance_action :stop
198
+
199
+ describe_call_attribute :private_dns_name
200
+ describe_call_attribute :dns_name
201
+ alias_method :public_dns_name, :dns_name
202
+ describe_call_attribute :ami_launch_index
203
+
204
+ describe_call_attribute :monitoring do
205
+ translate_output { |v| v.state.to_sym if v }
206
+ end
207
+
208
+ # @return true if CloudWatch monitoring is enabled for this
209
+ # instance.
210
+ def monitoring_enabled?
211
+ monitoring == :enabled
212
+ end
213
+
214
+ # Enables or disables monitoring for this instance.
215
+ def monitoring_enabled=(value)
216
+ if value
217
+ client.monitor_instances(:instance_ids => [id])
218
+ true
219
+ else
220
+ client.unmonitor_instances(:instance_ids => [id])
221
+ false
222
+ end
223
+ end
224
+
225
+ # @return [Array] A list of security groups
226
+ # the instance is in. Each member is an instance of
227
+ # {SecurityGroup}.
228
+ def security_groups
229
+ group_set
230
+ end
231
+ describe_call_attribute :group_set do
232
+ translate_output do |groups|
233
+ (groups || []).map do |g|
234
+ SecurityGroup.new(g.group_id,
235
+ :name => g.group_name,
236
+ :config => config)
237
+ end
238
+ end
239
+ end
240
+ alias_method :groups, :security_groups
241
+
242
+ describe_call_attribute :private_ip_address
243
+ describe_call_attribute :ip_address
244
+ alias_method :public_ip_address, :ip_address
245
+ describe_call_attribute :architecture, :to_sym => true
246
+ describe_call_attribute :root_device_type, :to_sym => true
247
+ describe_call_attribute :instance_lifecycle, :to_sym => true
248
+
249
+ # @return [Boolean] true if the instance is a Spot instance.
250
+ def spot_instance?
251
+ instance_lifecycle == :spot
252
+ end
253
+
254
+ describe_call_attribute :virtualization_type, :to_sym => true
255
+ describe_call_attribute :hypervisor, :to_sym => true
256
+ describe_call_attribute :placement
257
+
258
+ # @return [String] The availability zone where the instance is
259
+ # running.
260
+ def availability_zone
261
+ if p = placement
262
+ p.availability_zone
263
+ end
264
+ end
265
+
266
+ describe_call_attribute :reason, :getter => :state_transition_reason
267
+
268
+ describe_call_attribute :launch_time
269
+ describe_call_attribute :platform
270
+ describe_call_attribute :client_token
271
+ describe_call_attribute :image_id
272
+
273
+ # @return [Image] The AMI used to launch the instance.
274
+ def image
275
+ Image.new(image_id, :config => config)
276
+ end
277
+
278
+ describe_call_attribute :key_name
279
+
280
+ # @return [KeyPair] The key pair with which this instance was
281
+ # associated at launch.
282
+ def key_pair
283
+ KeyPair.new(key_name, :config => config) if key_name
284
+ end
285
+
286
+ # Creates an AMI from this instance.
287
+ #
288
+ # @param [String] name A name for the new image you're
289
+ # creating. Constraints: 3-128 alphanumeric characters,
290
+ # parenthesis (()), commas (,), slashes (/), dashes (-), or
291
+ # underscores(_)
292
+ #
293
+ # @param [Hash] options Additional options for creating the
294
+ # image.
295
+ #
296
+ # @option options [String] :description A description of the
297
+ # new image.
298
+ #
299
+ # @option options [Boolean] :no_reboot By default this
300
+ # option is set to +false+, which means Amazon EC2
301
+ # attempts to cleanly shut down the instance before image
302
+ # creation and reboots the instance afterwards. When the
303
+ # option is set to +true+, Amazon EC2 does not shut down
304
+ # the instance before creating the image. When this option
305
+ # is used, file system integrity on the created image cannot
306
+ # be guaranteed.
307
+ #
308
+ # @return [Image] The newly created image.
309
+ def create_image(name, options = {})
310
+ ImageCollection.new(:config => config).
311
+ create(options.merge(:instance_id => id,
312
+ :name => name))
313
+ end
314
+
315
+ # @private
316
+ def self.reservation_attribute(name)
317
+ define_method(name) do
318
+ @reservation_attributes[name] ||= retrieve_attribute(name) do
319
+ client.describe_instances(:instance_ids => [id])
320
+ end
321
+ end
322
+ MetaUtils.class_extend_method(self, :attributes_from_reservation) do |r, i|
323
+ if atts = super(r, i)
324
+ atts[name] = (r.send(name) if r.respond_to?(name))
325
+ atts
326
+ end
327
+ end
328
+ end
329
+
330
+ MetaUtils.class_extend_method(self, :attributes_from_reservation) do |r, i|
331
+ attributes_from_response_object(i) if i
332
+ end
333
+
334
+ reservation_attribute :reservation_id
335
+ reservation_attribute :owner_id
336
+ reservation_attribute :requester_id
337
+
338
+ mutable_attribute :user_data do
339
+ # mutable_attribute unwraps the ".value" unless you translate
340
+ # the output yourself
341
+ translate_output { |v| Base64.decode64(v.value) if v }
342
+ translate_input { |v| Base64.encode64(v).strip }
343
+ end
344
+
345
+ describe_call_attribute :instance_type
346
+ mutable_attribute :instance_type
347
+
348
+ mutable_attribute :source_dest_check, :getter => :source_dest_check?
349
+ mutable_attribute(:disable_api_termination,
350
+ :getter => :api_termination_disabled?,
351
+ :setter => :api_termination_disabled=)
352
+ mutable_attribute :instance_initiated_shutdown_behavior
353
+ describe_call_attribute :kernel_id
354
+ mutable_attribute :kernel, :getter => :kernel_id, :setter => :kernel_id=
355
+
356
+ ##
357
+ # Resets the kernel to its default value.
358
+ def reset_kernel_id
359
+ client.reset_instance_attribute(:instance_id => id,
360
+ :attribute => "kernel").return
361
+ end
362
+
363
+ describe_call_attribute :ramdisk_id
364
+ mutable_attribute :ramdisk, :getter => :ramdisk_id, :setter => :ramdisk_id=
365
+
366
+ ##
367
+ # Resets the RAM disk to its default value.
368
+ def reset_ramdisk_id
369
+ client.reset_instance_attribute(:instance_id => id,
370
+ :attribute => "ramdisk").return
371
+ end
372
+
373
+ describe_call_attribute :root_device_name
374
+ mutable_attribute :root_device_name, :setter => false
375
+
376
+ # @return [Hash] A hash in which each key is the name of a
377
+ # device that is mapped to an EBS volume. The value of each
378
+ # entry is an object describing the volume, including the
379
+ # following fields:
380
+ #
381
+ # [status] The status of the attachment (e.g. "attached").
382
+ #
383
+ # [volume_id] The ID of the EBS volume.
384
+ #
385
+ # [attach_time] The time at which the volume was attached.
386
+ #
387
+ # [delete_on_termination?] True if the volume will be
388
+ # automatically deleted when the
389
+ # instance terminates.
390
+ def block_device_mappings; end
391
+
392
+ describe_call_attribute(:block_device_mapping,
393
+ :getter => :block_device_mappings) do
394
+ translate_output { |v| translate_block_device_mappings(v) }
395
+ end
396
+ mutable_attribute(:block_device_mapping,
397
+ :setter => false,
398
+ :getter => :block_device_mappings) do
399
+ translate_output { |v| translate_block_device_mappings(v) }
400
+ end
401
+
402
+ # @return [Symbol] The status of the instance. Valid values:
403
+ # * +:pending+
404
+ # * +:running+
405
+ # * +:shutting_down+
406
+ # * +:terminated+
407
+ # * +:stopping+
408
+ # * +:stopped+
409
+ def status
410
+ retrieve_attribute(:status) do
411
+ client.describe_instances(:instance_ids => [id])
412
+ end
413
+ end
414
+
415
+ # Associates the elastic IP address with this instance.
416
+ #
417
+ # @param [ElasticIp,String] elastic_ip Either a public IP address
418
+ # string or an {ElasticIp} object to associate to this
419
+ # instance.
420
+ # @return [nil]
421
+ def associate_elastic_ip elastic_ip
422
+ client.associate_address(
423
+ :public_ip => elastic_ip.to_s,
424
+ :instance_id => self.id)
425
+ nil
426
+ end
427
+
428
+ alias_method :ip_address=, :associate_elastic_ip
429
+
430
+ # Disassociates an attached elastic IP address from this instance.
431
+ # Raises an exception if there is no elastic IP address associated
432
+ # with this instance.
433
+ def disassociate_elastic_ip
434
+ if ip = self.elastic_ip
435
+ ip.disassociate
436
+ else
437
+ raise "instance #{id} does not have an associated elastic ip"
438
+ end
439
+ end
440
+
441
+ # @return [Boolean] Returns true if an elastic IP address is
442
+ # associated with this instance, false otherwise.
443
+ def has_elastic_ip?
444
+ !elastic_ip.nil?
445
+ end
446
+
447
+ # @return [ElasticIp,nil] Returns an elastic IP address if one
448
+ # is associated with this instance, nil otherwise.
449
+ def elastic_ip
450
+ ElasticIpCollection.new(:config => config).
451
+ filter('instance-id', id).first
452
+ end
453
+
454
+ protected
455
+ def find_in_response(resp)
456
+ resp.instance_index[id]
457
+ end
458
+
459
+ populate_from :describe_instances do |resp|
460
+ if r = resp.reservation_index[id]
461
+ attributes_from_reservation(r, resp.instance_index[id])
462
+ end
463
+ end
464
+
465
+ populate_from :run_instances do |resp|
466
+ attributes_from_reservation(resp,
467
+ resp.instances_set.
468
+ find { |i| i.instance_id == id })
469
+ end
470
+
471
+ [:terminate_instances,
472
+ :start_instances,
473
+ :stop_instances].each do |op|
474
+ populate_from op do |resp|
475
+ if i = resp.instances_set.find { |i| i.instance_id == id }
476
+ { :status => i.current_state.name.tr("-","_").to_sym }
477
+ end
478
+ end
479
+ end
480
+
481
+ [:monitor_instances,
482
+ :unmonitor_instances].each do |op|
483
+ populate_from op do |resp|
484
+ if i = resp.instances_set.find { |i| i.instance_id == id }
485
+ { :monitoring =>
486
+ attributes_from_response_object(i)[:monitoring] }
487
+ end
488
+ end
489
+ end
490
+
491
+ protected
492
+ def translate_block_device_mappings(mappings)
493
+ mappings.inject({}) do |m, mapping|
494
+ m[mapping.device_name] =
495
+ Attachment.new(Volume.new(mapping.ebs.volume_id, :config => config),
496
+ self,
497
+ mapping.device_name,
498
+ :config => config)
499
+ m
500
+ end if mappings
501
+ end
502
+
503
+ protected
504
+ def attributes_from_response_object(obj)
505
+ if atts = super(obj)
506
+ atts[:status] = obj.instance_state.name.tr("-","_").to_sym if
507
+ obj.respond_to?(:instance_state)
508
+ atts
509
+ end
510
+ end
511
+
512
+ end
513
+
514
+ end
515
+ end