kerryb-amazon-ec2 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,64 @@
1
+ #--
2
+ # Amazon Web Services EC2 Query API Ruby library
3
+ #
4
+ # Ruby Gem Name:: amazon-ec2
5
+ # Author:: Glenn Rempe (mailto:glenn@rempe.us)
6
+ # Copyright:: Copyright (c) 2007-2008 Glenn Rempe
7
+ # License:: Distributes under the same terms as Ruby
8
+ # Home:: http://github.com/grempe/amazon-ec2/tree/master
9
+ #++
10
+
11
+ # This allows us to access hash values as if they were methods
12
+ # e.g. foo[:bar] can be accessed as foo.bar
13
+
14
+ class Hash
15
+ def method_missing(meth, *args, &block)
16
+ if args.size == 0
17
+ self[meth.to_s] || self[meth.to_sym]
18
+ end
19
+ end
20
+ end
21
+
22
+ module EC2
23
+
24
+ # The make_request() and ec2_error? methods, which are shared by all, will raise any
25
+ # exceptions encountered along the way as it converses with EC2.
26
+ #
27
+ # Exception Handling: If for some reason an error occurrs when executing a method
28
+ # (e.g. its arguments were incorrect, or it simply failed) then an exception will
29
+ # be thrown. The exceptions are defined in exceptions.rb as individual classes and should
30
+ # match the exceptions that Amazon has defined for EC2. If the exception raised cannot be
31
+ # identified then a more generic exception class will be thrown.
32
+ #
33
+ # The implication of this is that you need to be prepared to handle any exceptions that
34
+ # may be raised by this library in YOUR code with a 'rescue' clauses. It is up to you
35
+ # how gracefully you want to handle these exceptions that are raised.
36
+
37
+
38
+ require 'rubygems'
39
+ begin
40
+ require 'xmlsimple' unless defined? XmlSimple
41
+ rescue Exception => e
42
+ require 'xml-simple' unless defined? XmlSimple
43
+ end
44
+
45
+ class Response
46
+
47
+ def self.parse(options = {})
48
+ options = {
49
+ :xml => "",
50
+ :parse_options => { 'forcearray' => ['item'], 'suppressempty' => nil, 'keeproot' => false }
51
+ }.merge(options)
52
+
53
+ # NOTE: Parsing the response as a nested set of Response objects was extremely
54
+ # memory intensive and appeared to leak (the memory was not freed on subsequent requests).
55
+ # It was changed to return the raw XmlSimple response.
56
+
57
+ response = XmlSimple.xml_in(options[:xml], options[:parse_options])
58
+
59
+ return response
60
+ end
61
+
62
+ end # class Response
63
+
64
+ end # module EC2
@@ -0,0 +1,232 @@
1
+ #--
2
+ # Amazon Web Services EC2 Query API Ruby library
3
+ #
4
+ # Ruby Gem Name:: amazon-ec2
5
+ # Author:: Glenn Rempe (mailto:glenn@rempe.us)
6
+ # Copyright:: Copyright (c) 2007-2008 Glenn Rempe
7
+ # License:: Distributes under the same terms as Ruby
8
+ # Home:: http://github.com/grempe/amazon-ec2/tree/master
9
+ #++
10
+
11
+ module EC2
12
+
13
+ class Base
14
+
15
+
16
+ #Amazon Developer Guide Docs:
17
+ #
18
+ # The CreateSecurityGroup operation creates a new security group. Every instance is launched
19
+ # in a security group. If none is specified as part of the launch request then instances
20
+ # are launched in the default security group. Instances within the same security group have
21
+ # unrestricted network access to one another. Instances will reject network access attempts from other
22
+ # instances in a different security group. As the owner of instances you may grant or revoke specific
23
+ # permissions using the AuthorizeSecurityGroupIngress and RevokeSecurityGroupIngress operations.
24
+ #
25
+ #Required Arguments:
26
+ #
27
+ # :group_name => String (default : "")
28
+ # :group_description => String (default : "")
29
+ #
30
+ #Optional Arguments:
31
+ #
32
+ # none
33
+ #
34
+ def create_security_group( options = {} )
35
+
36
+ options = {:group_name => "",
37
+ :group_description => ""
38
+ }.merge(options)
39
+
40
+ raise ArgumentError, "No :group_name provided" if options[:group_name].nil? || options[:group_name].empty?
41
+ raise ArgumentError, "No :group_description provided" if options[:group_description].nil? || options[:group_description].empty?
42
+
43
+ params = {
44
+ "GroupName" => options[:group_name],
45
+ "GroupDescription" => options[:group_description]
46
+ }
47
+
48
+ return response_generator(:action => "CreateSecurityGroup", :params => params)
49
+
50
+ end
51
+
52
+
53
+ #Amazon Developer Guide Docs:
54
+ #
55
+ # The DescribeSecurityGroups operation returns information about security groups owned by the
56
+ # user making the request.
57
+ #
58
+ # An optional list of security group names may be provided to request information for those security
59
+ # groups only. If no security group names are provided, information of all security groups will be
60
+ # returned. If a group is specified that does not exist a fault is returned.
61
+ #
62
+ #Required Arguments:
63
+ #
64
+ # none
65
+ #
66
+ #Optional Arguments:
67
+ #
68
+ # :group_name => Array (default : [])
69
+ #
70
+ def describe_security_groups( options = {} )
71
+
72
+ options = { :group_name => [] }.merge(options)
73
+
74
+ params = pathlist("GroupName", options[:group_name] )
75
+
76
+ return response_generator(:action => "DescribeSecurityGroups", :params => params)
77
+
78
+ end
79
+
80
+
81
+ #Amazon Developer Guide Docs:
82
+ #
83
+ # The DeleteSecurityGroup operation deletes a security group.
84
+ #
85
+ # If an attempt is made to delete a security group and any instances exist that are members of that group a
86
+ # fault is returned.
87
+ #
88
+ #Required Arguments:
89
+ #
90
+ # :group_name => String (default : "")
91
+ #
92
+ #Optional Arguments:
93
+ #
94
+ # none
95
+ #
96
+ def delete_security_group( options = {} )
97
+
98
+ options = { :group_name => "" }.merge(options)
99
+
100
+ raise ArgumentError, "No :group_name provided" if options[:group_name].nil? || options[:group_name].empty?
101
+
102
+ params = { "GroupName" => options[:group_name] }
103
+
104
+ return response_generator(:action => "DeleteSecurityGroup", :params => params)
105
+
106
+ end
107
+
108
+
109
+ #Amazon Developer Guide Docs:
110
+ #
111
+ # The AuthorizeSecurityGroupIngress operation adds permissions to a security group.
112
+ #
113
+ # Permissions are specified in terms of the IP protocol (TCP, UDP or ICMP), the source of the request (by
114
+ # IP range or an Amazon EC2 user-group pair), source and destination port ranges (for TCP and UDP),
115
+ # and ICMP codes and types (for ICMP). When authorizing ICMP, -1 may be used as a wildcard in the
116
+ # type and code fields.
117
+ #
118
+ # Permission changes are propagated to instances within the security group being modified as quickly as
119
+ # possible. However, a small delay is likely, depending on the number of instances that are members of
120
+ # the indicated group.
121
+ #
122
+ # When authorizing a user/group pair permission, GroupName, SourceSecurityGroupName and
123
+ # SourceSecurityGroupOwnerId must be specified. When authorizing a CIDR IP permission,
124
+ # GroupName, IpProtocol, FromPort, ToPort and CidrIp must be specified. Mixing these two types
125
+ # of parameters is not allowed.
126
+ #
127
+ #Required Arguments:
128
+ #
129
+ # :group_name => String (default : "")
130
+ #
131
+ #Optional Arguments:
132
+ #
133
+ # :ip_protocol => String (default : nil) : Required when authorizing CIDR IP permission
134
+ # :from_port => Integer (default : nil) : Required when authorizing CIDR IP permission
135
+ # :to_port => Integer (default : nil) : Required when authorizing CIDR IP permission
136
+ # :cidr_ip => String (default : nil): Required when authorizing CIDR IP permission
137
+ # :source_security_group_name => String (default : nil) : Required when authorizing user group pair permissions
138
+ # :source_security_group_owner_id => String (default : nil) : Required when authorizing user group pair permissions
139
+ #
140
+ def authorize_security_group_ingress( options = {} )
141
+
142
+ # defaults
143
+ options = { :group_name => nil,
144
+ :ip_protocol => nil,
145
+ :from_port => nil,
146
+ :to_port => nil,
147
+ :cidr_ip => nil,
148
+ :source_security_group_name => nil,
149
+ :source_security_group_owner_id => nil }.merge(options)
150
+
151
+ # lets not validate the rest of the possible permutations of required params and instead let
152
+ # EC2 sort it out on the server side. We'll only require :group_name as that is always needed.
153
+ raise ArgumentError, "No :group_name provided" if options[:group_name].nil? || options[:group_name].empty?
154
+
155
+ params = { "GroupName" => options[:group_name],
156
+ "IpProtocol" => options[:ip_protocol],
157
+ "FromPort" => options[:from_port].to_s,
158
+ "ToPort" => options[:to_port].to_s,
159
+ "CidrIp" => options[:cidr_ip],
160
+ "SourceSecurityGroupName" => options[:source_security_group_name],
161
+ "SourceSecurityGroupOwnerId" => options[:source_security_group_owner_id]
162
+ }
163
+
164
+ return response_generator(:action => "AuthorizeSecurityGroupIngress", :params => params)
165
+
166
+ end
167
+
168
+
169
+ #Amazon Developer Guide Docs:
170
+ #
171
+ # The RevokeSecurityGroupIngress operation revokes existing permissions that were previously
172
+ # granted to a security group. The permissions to revoke must be specified using the same values
173
+ # originally used to grant the permission.
174
+ #
175
+ # Permissions are specified in terms of the IP protocol (TCP, UDP or ICMP), the source of the request (by
176
+ # IP range or an Amazon EC2 user-group pair), source and destination port ranges (for TCP and UDP),
177
+ # and ICMP codes and types (for ICMP). When authorizing ICMP, -1 may be used as a wildcard in the
178
+ # type and code fields.
179
+ #
180
+ # Permission changes are propagated to instances within the security group being modified as quickly as
181
+ # possible. However, a small delay is likely, depending on the number of instances that are members of
182
+ # the indicated group.
183
+ #
184
+ # When revoking a user/group pair permission, GroupName, SourceSecurityGroupName and
185
+ # SourceSecurityGroupOwnerId must be specified. When authorizing a CIDR IP permission,
186
+ # GroupName, IpProtocol, FromPort, ToPort and CidrIp must be specified. Mixing these two types
187
+ # of parameters is not allowed.
188
+ #
189
+ #Required Arguments:
190
+ #
191
+ # :group_name => String (default : "")
192
+ #
193
+ #Optional Arguments:
194
+ #
195
+ # :ip_protocol => String (default : nil) : Required when revoking CIDR IP permission
196
+ # :from_port => Integer (default : nil) : Required when revoking CIDR IP permission
197
+ # :to_port => Integer (default : nil) : Required when revoking CIDR IP permission
198
+ # :cidr_ip => String (default : nil): Required when revoking CIDR IP permission
199
+ # :source_security_group_name => String (default : nil) : Required when revoking user group pair permissions
200
+ # :source_security_group_owner_id => String (default : nil) : Required when revoking user group pair permissions
201
+ #
202
+ def revoke_security_group_ingress( options = {} )
203
+
204
+ # defaults
205
+ options = { :group_name => nil,
206
+ :ip_protocol => nil,
207
+ :from_port => nil,
208
+ :to_port => nil,
209
+ :cidr_ip => nil,
210
+ :source_security_group_name => nil,
211
+ :source_security_group_owner_id => nil }.merge(options)
212
+
213
+ # lets not validate the rest of the possible permutations of required params and instead let
214
+ # EC2 sort it out on the server side. We'll only require :group_name as that is always needed.
215
+ raise ArgumentError, "No :group_name provided" if options[:group_name].nil? || options[:group_name].empty?
216
+
217
+ params = { "GroupName" => options[:group_name],
218
+ "IpProtocol" => options[:ip_protocol],
219
+ "FromPort" => options[:from_port].to_s,
220
+ "ToPort" => options[:to_port].to_s,
221
+ "CidrIp" => options[:cidr_ip],
222
+ "SourceSecurityGroupName" => options[:source_security_group_name],
223
+ "SourceSecurityGroupOwnerId" => options[:source_security_group_owner_id]
224
+ }
225
+
226
+ return response_generator(:action => "RevokeSecurityGroupIngress", :params => params)
227
+
228
+ end
229
+
230
+ end
231
+
232
+ end
@@ -0,0 +1,94 @@
1
+ #--
2
+ # Amazon Web Services EC2 Query API Ruby library, EBS snaphshots support
3
+ #
4
+ # Ruby Gem Name:: amazon-ec2
5
+ # Author:: Yann Klis (mailto:yann.klis@novelys.com)
6
+ # Copyright:: Copyright (c) 2008 Yann Klis
7
+ # License:: Distributes under the same terms as Ruby
8
+ # Home:: http://github.com/grempe/amazon-ec2/tree/master
9
+ #++
10
+
11
+ module EC2
12
+
13
+ class Base
14
+
15
+ #Amazon Developer Guide Docs:
16
+ #
17
+ # The DescribeSnapshots operation describes the status of Amazon EBS snapshots.
18
+ #
19
+ #Required Arguments:
20
+ #
21
+ # none
22
+ #
23
+ #Optional Arguments:
24
+ #
25
+ # :snapshot_id => Array (default : [])
26
+ #
27
+
28
+ def describe_snapshots( options = {} )
29
+
30
+ options = { :snapshot_id => [] }.merge(options)
31
+
32
+ params = pathlist("SnapshotId", options[:snapshot_id] )
33
+
34
+ return response_generator(:action => "DescribeSnapshots", :params => params)
35
+
36
+ end
37
+
38
+ #Amazon Developer Guide Docs:
39
+ #
40
+ # The CreateSnapshot operation creates a snapshot of an Amazon EBS volume and stores it in Amazon S3. You can use snapshots for backups, to launch instances from identical snapshots, and to save data before shutting down an instance.
41
+ #
42
+ #Required Arguments:
43
+ #
44
+ # :volume_id => String (default : '')
45
+ #
46
+ #Optional Arguments:
47
+ #
48
+ # none
49
+ #
50
+
51
+ def create_snapshot( options = {} )
52
+
53
+ # defaults
54
+ options = { :volume_id => '' }.merge(options)
55
+
56
+ raise ArgumentError, "No :volume_id provided" if options[:volume_id].nil? || options[:volume_id].empty?
57
+
58
+ params = {
59
+ "VolumeId" => options[:volume_id]
60
+ }
61
+
62
+ return response_generator(:action => "CreateSnapshot", :params => params)
63
+
64
+ end
65
+
66
+ #Amazon Developer Guide Docs:
67
+ #
68
+ # The DeleteSnapshot operation deletes a snapshot of an Amazon EBS volume that is stored in Amazon S3.
69
+ #
70
+ #Required Arguments:
71
+ #
72
+ # :snapshot_id => String (default : '')
73
+ #
74
+ #Optional Arguments:
75
+ #
76
+ # none
77
+ #
78
+
79
+ def delete_snapshot( options = {} )
80
+
81
+ options = { :snapshot_id => '' }.merge(options)
82
+
83
+ raise ArgumentError, "No :snapshot_id provided" if options[:snapshot_id].nil? || options[:snapshot_id].empty?
84
+
85
+ params = {
86
+ "SnapshotId" => options[:snapshot_id]
87
+ }
88
+
89
+ return response_generator(:action => "DeleteSnapshot", :params => params)
90
+
91
+ end
92
+
93
+ end
94
+ end
@@ -0,0 +1,170 @@
1
+ #--
2
+ # Amazon Web Services EC2 Query API Ruby library, EBS volumes support
3
+ #
4
+ # Ruby Gem Name:: amazon-ec2
5
+ # Author:: Yann Klis (mailto:yann.klis@novelys.com)
6
+ # Copyright:: Copyright (c) 2008 Yann Klis
7
+ # License:: Distributes under the same terms as Ruby
8
+ # Home:: http://github.com/grempe/amazon-ec2/tree/master
9
+ #++
10
+
11
+ module EC2
12
+
13
+ class Base
14
+
15
+ #Amazon Developer Guide Docs:
16
+ #
17
+ # The DescribeVolumes operation lists one or more Amazon EBS volumes that you own, If you do not specify any volumes, Amazon EBS returns all volumes that you own.
18
+ #
19
+ #Required Arguments:
20
+ #
21
+ # none
22
+ #
23
+ #Optional Arguments:
24
+ #
25
+ # :volume_id => Array (default : [])
26
+ #
27
+
28
+ def describe_volumes( options = {} )
29
+
30
+ options = { :volume_id => [] }.merge(options)
31
+
32
+ params = pathlist("VolumeId", options[:volume_id] )
33
+
34
+ return response_generator(:action => "DescribeVolumes", :params => params)
35
+
36
+ end
37
+
38
+ #Amazon Developer Guide Docs:
39
+ #
40
+ # The CreateVolume operation creates a new Amazon EBS volume that you can mount from any Amazon EC2 instance.
41
+ #
42
+ #Required Arguments:
43
+ #
44
+ # :availability_zone => String (default : '')
45
+ #
46
+ #Optional Arguments:
47
+ #
48
+ # :size => String (default : '')
49
+ # :snapshot_id => String (default : '')
50
+ #
51
+
52
+ def create_volume( options = {} )
53
+
54
+ # defaults
55
+ options = { :availability_zone => '' }.merge(options)
56
+
57
+ raise ArgumentError, "No :availability_zone provided" if options[:availability_zone].nil? || options[:availability_zone].empty?
58
+
59
+ options = { :size => '' }.merge(options)
60
+ options = { :snapshot_id => '' }.merge(options)
61
+
62
+ params = {
63
+ "AvailabilityZone" => options[:availability_zone],
64
+ "Size" => options[:size],
65
+ "SnapshotId" => options[:snapshot_id]
66
+ }
67
+
68
+ return response_generator(:action => "CreateVolume", :params => params)
69
+
70
+ end
71
+
72
+ #Amazon Developer Guide Docs:
73
+ #
74
+ # The DeleteVolume operation deletes an Amazon EBS volume.
75
+ #
76
+ #Required Arguments:
77
+ #
78
+ # :volume_id => String (default : '')
79
+ #
80
+ #Optional Arguments:
81
+ #
82
+ # none
83
+ #
84
+
85
+ def delete_volume( options = {} )
86
+
87
+ options = { :volume_id => '' }.merge(options)
88
+
89
+ raise ArgumentError, "No :volume_id provided" if options[:volume_id].nil? || options[:volume_id].empty?
90
+
91
+ params = {
92
+ "VolumeId" => options[:volume_id]
93
+ }
94
+
95
+ return response_generator(:action => "DeleteVolume", :params => params)
96
+
97
+ end
98
+
99
+ #Amazon Developer Guide Docs:
100
+ #
101
+ # The AttachVolume operation attaches an Amazon EBS volume to an instance.
102
+ #
103
+ #Required Arguments:
104
+ #
105
+ # :volume_id => String (default : '')
106
+ # :instance_id => String (default : '')
107
+ # :device => String (default : '')
108
+ #
109
+ #Optional Arguments:
110
+ #
111
+ # none
112
+ #
113
+
114
+ def attach_volume( options = {} )
115
+
116
+ options = { :volume_id => '' }.merge(options)
117
+ options = { :instance_id => '' }.merge(options)
118
+ options = { :device => '' }.merge(options)
119
+
120
+ raise ArgumentError, "No :volume_id provided" if options[:volume_id].nil? || options[:volume_id].empty?
121
+ raise ArgumentError, "No :instance_id provided" if options[:instance_id].nil? || options[:instance_id].empty?
122
+ raise ArgumentError, "No :device provided" if options[:device].nil? || options[:device].empty?
123
+
124
+ params = {
125
+ "VolumeId" => options[:volume_id],
126
+ "InstanceId" => options[:instance_id],
127
+ "Device" => options[:device]
128
+ }
129
+
130
+ return response_generator(:action => "AttachVolume", :params => params)
131
+
132
+ end
133
+
134
+ #Amazon Developer Guide Docs:
135
+ #
136
+ # The DetachVolume operation detaches an Amazon EBS volume from an instance.
137
+ #
138
+ #Required Arguments:
139
+ #
140
+ # :volume_id => String (default : '')
141
+ #
142
+ #Optional Arguments:
143
+ #
144
+ # :instance_id => String (default : '')
145
+ # :device => String (default : '')
146
+ # :force => Boolean (default : '')
147
+ #
148
+
149
+ def detach_volume( options = {} )
150
+
151
+ options = { :volume_id => '' }.merge(options)
152
+
153
+ raise ArgumentError, "No :volume_id provided" if options[:volume_id].nil? || options[:volume_id].empty?
154
+
155
+ options = { :instance_id => '' }.merge(options)
156
+ options = { :device => '' }.merge(options)
157
+ options = { :force => '' }.merge(options)
158
+
159
+ params = {
160
+ "VolumeId" => options[:volume_id],
161
+ "InstanceId" => options[:instance_id],
162
+ "Device" => options[:device],
163
+ "Force" => options[:force]
164
+ }
165
+
166
+ return response_generator(:action => "DetachVolume", :params => params)
167
+
168
+ end
169
+ end
170
+ end