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,276 @@
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/tagged_collection'
16
+ require 'aws/ec2/collection'
17
+ require 'aws/ec2/instance'
18
+ require 'aws/ec2/block_device_mappings'
19
+ require 'base64'
20
+ require 'uuidtools'
21
+
22
+ module AWS
23
+ class EC2
24
+
25
+ ##
26
+ # Represents a collection of EC2 instances. Typically you
27
+ # should get an instance of this class by calling
28
+ # {EC2#instances}.
29
+ #
30
+ # To run an instance:
31
+ #
32
+ # ec2.instances.create(:image_id => "ami-8c1fece5")
33
+ #
34
+ # To get an instance by ID:
35
+ #
36
+ # i = ec2.instances["i-12345678"]
37
+ # i.exists?
38
+ #
39
+ # To get a map of instance IDs to instance status:
40
+ #
41
+ # ec2.instances.inject({}) { |m, i| m[i.id] = i.status; m }
42
+ # # => { "i-12345678" => :running, "i-87654321" => :shutting_down }
43
+ #
44
+ class InstanceCollection < Collection
45
+
46
+ include TaggedCollection
47
+ include BlockDeviceMappings
48
+
49
+ ##
50
+ # Runs one or more EC2 instances.
51
+ #
52
+ # @example Running a single instance
53
+ # i = ec2.instances.create(:image_id => "ami-8c1fece5")
54
+ # sleep 1 while i.status == :pending
55
+ #
56
+ # @example Running multiple instances with the same parameters
57
+ # instances =
58
+ # ec2.instances.create(:image_id => "ami-8c1fece5",
59
+ # :count => 10)
60
+ # sleep 1 while instances.any? { |i| i.status == :pending }
61
+ #
62
+ # @example Specifying block device mappings
63
+ # ec2.instances.create(:image_id => "ami-8c1fece5",
64
+ # :block_device_mappings => {
65
+ # "/dev/sda2" => {
66
+ # # 15 GiB
67
+ # :volume_size => 15,
68
+ # :delete_on_termination => true
69
+ # }
70
+ # })
71
+ #
72
+ # @return [Instance or Array] If a single instance is being created, this returns
73
+ # an instance of {EC2::Instance} to represent the newly
74
+ # created instance. Otherwise it returns an array of
75
+ # EC2::Instance objects.
76
+ #
77
+ # @param [Hash] opts Options for new instance. +:image_id+ is
78
+ # the only required option.
79
+ #
80
+ # @option opts :count How many instances to request. By
81
+ # default one instance is requested. You can specify this
82
+ # either as an integer or as a Range, to indicate the
83
+ # minimum and maximum number of instances to run. Note that
84
+ # for a new account you can request at most 20 instances at
85
+ # once.
86
+ #
87
+ # @option opts [Hash] :block_device_mappings This must be a
88
+ # hash; the keys are device names to map, and the value for
89
+ # each entry determines how that device is mapped. Valid
90
+ # values include:
91
+ #
92
+ # * A string, which is interpreted as a virtual device name.
93
+ #
94
+ # * The symbol :no_device, which overrides the default
95
+ # mapping for a device so that it is not mapped to anything.
96
+ #
97
+ # * A hash with any of the following options. One of
98
+ # +:snapshot+, +:snapshot_id+ or +:volume_size+ is
99
+ # required.
100
+ #
101
+ # [:snapshot] A snapshot to use when creating the block
102
+ # device.
103
+ #
104
+ # [:snapshot_id] The ID of a snapshot to use when creating
105
+ # the block device.
106
+ #
107
+ # [:volume_size] The size of volume to create, in gigabytes.
108
+ #
109
+ # [:delete_on_termination] Setting this to true causes EC2
110
+ # to delete the volume when the
111
+ # instance is terminated.
112
+ #
113
+ # @option opts [Boolean] :monitoring Setting this to true
114
+ # enables CloudWatch monitoring on the instances once they
115
+ # are started.
116
+ #
117
+ # @option opts [String] :availability_zone Specifies the
118
+ # availability zone where the instance should run. Without
119
+ # this option, EC2 will choose an availability zone for you.
120
+ #
121
+ # @option opts [String] :image_id ID of the AMI you want to
122
+ # launch.
123
+ #
124
+ # @option opts [String] :key_name The name of the key pair to
125
+ # use. Note: Launching public images without a key pair ID
126
+ # will leave them inaccessible.
127
+ #
128
+ # @option opts [Array] :security_groups The names of the
129
+ # security groups that will be used to determine network
130
+ # access rules for the instances. You may pass instances of
131
+ # {SecurityGroup} as well.
132
+ #
133
+ # @option opts [String] :user_data Arbitrary user data. You
134
+ # do not need to encode this value.
135
+ #
136
+ # @option opts [String] :instance_type The type of instance to
137
+ # launch, for example "m1.small".
138
+ #
139
+ # @option opts [String] :kernel_id The ID of the kernel with
140
+ # which to launch the instance.
141
+ #
142
+ # @option opts [String] :ramdisk_id The ID of the RAM disk to
143
+ # select. Some kernels require additional drivers at
144
+ # launch. Check the kernel requirements for information on
145
+ # whether you need to specify a RAM disk. To find kernel
146
+ # requirements, refer to the Resource Center and search for
147
+ # the kernel ID.
148
+ #
149
+ # @option opts [Boolean] :disable_api_termination Specifies
150
+ # whether you can terminate the instance using the EC2
151
+ # API. A value of true means you can't terminate the
152
+ # instance using the API (i.e., the instance is "locked"); a
153
+ # value of false means you can. If you set this to true, and
154
+ # you later want to terminate the instance, you must first
155
+ # enable API termination. For example:
156
+ #
157
+ # i = ec2.instances.create(:image_id => "ami-8c1fece5",
158
+ # :disable_api_termination => true)
159
+ # i.api_termination_disabled? # => true
160
+ # i.terminate # raises an exception
161
+ # i.api_termination_disabled = false
162
+ # i.terminate # terminates the instance
163
+ #
164
+ # @option opts [String] :instance_initiated_shutdown_behavior
165
+ # Determines whether the instance stops or terminates on
166
+ # instance-initiated shutdown.
167
+ def create(opts = {})
168
+ if image = opts.delete(:image)
169
+ opts[:image_id] = image.id
170
+ end
171
+
172
+ if kernel = opts.delete(:kernel)
173
+ opts[:kernel_id] = kernel.id
174
+ end
175
+
176
+ if ramdisk = opts.delete(:ramdisk)
177
+ opts[:ramdisk_id] = ramdisk.id
178
+ end
179
+
180
+ if key_pair = opts.delete(:key_pair)
181
+ opts[:key_name] = key_pair.name
182
+ end
183
+
184
+ opts = count_opts(opts).merge(opts)
185
+ opts.delete(:count)
186
+
187
+ opts[:user_data] = Base64.encode64(opts[:user_data]).strip if
188
+ opts[:user_data]
189
+
190
+ opts[:block_device_mappings] =
191
+ translate_block_device_mappings(opts[:block_device_mappings]) if
192
+ opts[:block_device_mappings]
193
+
194
+ opts[:monitoring] = { :enabled => true } if
195
+ opts[:monitoring_enabled]
196
+ opts.delete(:monitoring_enabled)
197
+
198
+ opts[:placement] = {
199
+ :availability_zone => opts[:availability_zone].to_s
200
+ } if opts[:availability_zone]
201
+ opts.delete(:availability_zone)
202
+
203
+ opts[:security_groups] = group_opts(opts[:security_groups]) if
204
+ opts[:security_groups]
205
+
206
+ opts[:client_token] = UUIDTools::UUID.timestamp_create.to_s
207
+
208
+ resp = client.run_instances(opts)
209
+
210
+ if opts[:min_count] == opts[:max_count] and
211
+ opts[:min_count] == 1
212
+ self[resp.instances_set.first.instance_id]
213
+ else
214
+ resp.instances_set.map do |i|
215
+ self[i.instance_id]
216
+ end
217
+ end
218
+ end
219
+ alias_method :run, :create
220
+
221
+ # @yield [Instance] Yields each instance in the collection.
222
+ def each(&block)
223
+ response = filtered_request(:describe_instances)
224
+ response.reservation_set.each do |r|
225
+ r.instances_set.each do |i|
226
+ yield(Instance.new(i.instance_id, :config => config))
227
+ end
228
+ end
229
+ end
230
+
231
+ # @return [Instance] Returns an object representing the EC2 instance
232
+ # with the given ID.
233
+ def [] id
234
+ super
235
+ end
236
+
237
+ # @private
238
+ protected
239
+ def member_class
240
+ Instance
241
+ end
242
+
243
+ # @private
244
+ private
245
+ def count_opts(opts)
246
+ min = max = 1
247
+ count = opts[:count]
248
+ case count
249
+ when Range
250
+ min = count.begin
251
+ max = (count.exclude_end? ? count.end-1 : count.end)
252
+ when Integer
253
+ min = max = count
254
+ end
255
+ { :min_count => min,
256
+ :max_count => max }
257
+ end
258
+
259
+ # @private
260
+ private
261
+ def group_opts(groups)
262
+ [groups].flatten.map do |g|
263
+ case g
264
+ when SecurityGroup then g.name
265
+ when String then g
266
+ else
267
+ raise ArgumentError.new("members of security_groups must be "+
268
+ "strings or SecurityGroup objects")
269
+ end
270
+ end
271
+ end
272
+
273
+ end
274
+
275
+ end
276
+ end
@@ -0,0 +1,86 @@
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/ec2/resource'
15
+
16
+ module AWS
17
+ class EC2
18
+
19
+ # Represents an EC2 key pair.
20
+ class KeyPair < Resource
21
+
22
+ def initialize name, options = {}
23
+ @name = name.to_s
24
+ @fingerprint = options[:fingerprint]
25
+ @private_key = options[:private_key]
26
+ super
27
+ end
28
+
29
+ # @return [String] The name of the key pair.
30
+ attr_reader :name
31
+
32
+ # @return [Boolean] True if the key pair exists.
33
+ def exists?
34
+ !client.describe_key_pairs(:filters => [{ :name => "key-name",
35
+ :values => [name] }]).
36
+ key_set.empty?
37
+ end
38
+
39
+ # @return [String] A SHA-1 digest of the DER encoded private key
40
+ def fingerprint; end
41
+ describe_call_attribute(:key_fingerprint,
42
+ :getter => :fingerprint,
43
+ :memoize => true)
44
+
45
+ # Returns the private key. Raises an exception if called
46
+ # against an existing key. You can only get the private key
47
+ # at the time of creation.
48
+ #
49
+ # @see KeyPairCollection#import
50
+ # @note Only call this method on newly created keys.
51
+ # @return [String] An unencrypted PEM encoded RSA private key.
52
+ def private_key
53
+ unless @private_key
54
+ raise 'you can only get the private key for just-created keypairs'
55
+ end
56
+ @private_key
57
+ end
58
+
59
+ # Deletes this key pair from EC2.
60
+ # @return [true]
61
+ def delete
62
+ client.delete_key_pair(:key_name => name)
63
+ true
64
+ end
65
+
66
+ [:create_key_pair,
67
+ :import_key_pair].each do |op|
68
+ populate_from op do |resp|
69
+ attributes_from_response_object(resp) if
70
+ resp.key_name == name
71
+ end
72
+ end
73
+
74
+ protected
75
+ def response_id_method
76
+ :key_name
77
+ end
78
+
79
+ protected
80
+ def find_in_response(resp)
81
+ resp.key_index[name]
82
+ end
83
+
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,102 @@
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/key_pair'
17
+ require 'digest/md5'
18
+ require 'base64'
19
+
20
+ module AWS
21
+ class EC2
22
+
23
+ # Represents all key pairs in your account. You can use this collection
24
+ # to create, import and find key pairs.
25
+ class KeyPairCollection < Collection
26
+
27
+ # @param [String] key_name A name for the key pair.
28
+ # @return [KeyPair] Returns a new key pair.
29
+ def create key_name
30
+ create_or_import(:create_key_pair, :key_name => key_name)
31
+ end
32
+
33
+ # Imports the public key from an RSA key pair that you created with
34
+ # a third-party tool. Compare this with {#create}, in which EC2
35
+ # creates the key pair and gives the keys to you (EC2 keeps a copy
36
+ # of the public key). With ImportKeyPair, you create the key pair
37
+ # and give EC2 just the public key. The private key is never
38
+ # transferred between you and EC2.
39
+ #
40
+ # === Supported formats:
41
+ #
42
+ # * OpenSSH public key format (e.g., the format in
43
+ # ~/.ssh/authorized_keys)
44
+ # * Base64 encoded DER format
45
+ # * SSH public key file format as specified in RFC4716
46
+ #
47
+ # DSA keys are *not* supported. Make sure your key generator is
48
+ # set up to create RSA keys. Supported lengths: 1024, 2048, and 4096.
49
+ #
50
+ # @param [String] key_name A name for this key pair.
51
+ # @param [String] public_key The RSA public key.
52
+ # @return [KeyPair] Returns a new key pair.
53
+ def import key_name, public_key
54
+ create_or_import(:import_key_pair,
55
+ :key_name => key_name,
56
+ :public_key_material => Base64.encode64(public_key.to_s))
57
+ end
58
+
59
+ # @return [KeyPair] key_name The name of the key pair.
60
+ def [] key_name
61
+ super
62
+ end
63
+
64
+ # Yields once for each key pair in your account.
65
+ # @return [nil]
66
+ def each &block
67
+ response = filtered_request(:describe_key_pairs)
68
+ response.key_set.each do |kp|
69
+ yield(KeyPair.new(kp.key_name,
70
+ :fingerprint => kp.key_fingerprint,
71
+ :config => config))
72
+ end
73
+ nil
74
+ end
75
+
76
+ # @private
77
+ protected
78
+ def member_class
79
+ KeyPair
80
+ end
81
+
82
+ # @private
83
+ private
84
+ def create_or_import client_method, options
85
+
86
+ # stringify option values
87
+ options = options.inject({}) {|h,v| h[v.first] = v.last.to_s; h }
88
+ response = client.send(client_method, options)
89
+
90
+ options = {}
91
+ options[:fingerprint] = response.key_fingerprint
92
+ if response.respond_to?(:key_material)
93
+ options[:private_key] = response.key_material
94
+ end
95
+
96
+ KeyPair.new(response.key_name, options)
97
+
98
+ end
99
+
100
+ end
101
+ end
102
+ end