moomerman-aws 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +49 -0
- data/lib/aws.rb +1 -0
- data/lib/aws/ec2/api.rb +55 -0
- data/lib/aws/ec2/client.rb +47 -0
- data/lib/aws/ec2/parser.rb +20 -0
- data/lib/aws/ec2/request.rb +68 -0
- metadata +70 -0
data/README.textile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
h1. AWS - Amazon Web Services client library for Ruby
|
2
|
+
|
3
|
+
h2. Currently implemented:
|
4
|
+
|
5
|
+
EC2
|
6
|
+
- EC2 API
|
7
|
+
- Cloud Watch
|
8
|
+
- Elastic Load Balancing
|
9
|
+
- Auto Scaling
|
10
|
+
|
11
|
+
h2. Usage
|
12
|
+
|
13
|
+
Simply provide your amazon access key and amazon secret key to start making calls:
|
14
|
+
|
15
|
+
<pre><code>client = AWS::EC2::Client.new(
|
16
|
+
:access_key_id => ACCESS_KEY_ID,
|
17
|
+
:secret_access_key => SECRET_ACCESS_KEY
|
18
|
+
)
|
19
|
+
client.describe_regions
|
20
|
+
|
21
|
+
=> {"regionInfo"=>{
|
22
|
+
"item"=>[
|
23
|
+
{"regionName"=>"eu-west-1", "regionEndpoint"=>"eu-west-1.ec2.amazonaws.com"},
|
24
|
+
{"regionName"=>"us-east-1", "regionEndpoint"=>"us-east-1.ec2.amazonaws.com"}
|
25
|
+
]
|
26
|
+
},
|
27
|
+
"requestId"=>"c6b5e7b4-421d-4484-932c-dcf32e66a2ab",
|
28
|
+
"xmlns"=>"http://ec2.amazonaws.com/doc/2009-04-04/"
|
29
|
+
}
|
30
|
+
</code></pre>
|
31
|
+
|
32
|
+
You can also specify a region to make calls in a specific region:
|
33
|
+
<pre><code>client = AWS::EC2::Client.new(
|
34
|
+
:access_key_id => ACCESS_KEY_ID,
|
35
|
+
:secret_access_key => SECRET_ACCESS_KEY,
|
36
|
+
:region => 'eu-west-1'
|
37
|
+
)
|
38
|
+
client.describe_availability_zones
|
39
|
+
|
40
|
+
=> {"availabilityZoneInfo"=>{
|
41
|
+
"item"=>[
|
42
|
+
{"zoneName"=>"eu-west-1a", "regionName"=>"eu-west-1", "zoneState"=>"available"},
|
43
|
+
{"zoneName"=>"eu-west-1b", "regionName"=>"eu-west-1", "zoneState"=>"available"}
|
44
|
+
]
|
45
|
+
},
|
46
|
+
"requestId"=>"ed31f360-bb07-4417-b976-5f5bd1740e6e",
|
47
|
+
"xmlns"=>"http://ec2.amazonaws.com/doc/2009-04-04/"
|
48
|
+
}
|
49
|
+
</code></pre>
|
data/lib/aws.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'aws/ec2/client'
|
data/lib/aws/ec2/api.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
module AWS
|
2
|
+
module EC2
|
3
|
+
API = {
|
4
|
+
:ec2 => {
|
5
|
+
:host => 'ec2.amazonaws.com',
|
6
|
+
:documentation => 'http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/',
|
7
|
+
:regions => ['us-east-1', 'eu-west-1'],
|
8
|
+
:version => '2009-04-04',
|
9
|
+
:methods => [
|
10
|
+
:allocate_address, :associate_address, :attach_volume, :authorize_security_group_ingress,
|
11
|
+
:bundle_instance, :cancel_bundle_task, :confirm_product_instance, :create_key_pair,
|
12
|
+
:create_security_group, :create_snapshot, :create_volume, :delete_key_pair, :delete_security_group,
|
13
|
+
:delete_snapshot, :delete_volume, :deregister_image, :describe_addresses, :describe_availability_zones,
|
14
|
+
:describe_bundle_tasks, :describe_image_attribute, :describe_images, :describe_instances, :describe_key_pairs,
|
15
|
+
:describe_regions, :describe_reserved_instances, :describe_reserved_instances_offerings, :describe_security_groups,
|
16
|
+
:describe_snapshots, :describe_volumes, :detach_volume, :disassociate_address, :get_console_output,
|
17
|
+
:modify_image_attribute, :monitor_instances, :purchase_reserved_instances_offering, :reboot_instances,
|
18
|
+
:register_image, :release_address, :reset_image_attribute, :revoke_security_group_ingress,
|
19
|
+
:run_instances, :terminate_instances, :unmonitor_instances
|
20
|
+
]
|
21
|
+
},
|
22
|
+
|
23
|
+
:elastic_load_balancing => {
|
24
|
+
:host => 'elasticloadbalancing.amazonaws.com',
|
25
|
+
:documentation => 'http://docs.amazonwebservices.com/ElasticLoadBalancing/latest/DeveloperGuide/',
|
26
|
+
:version => '2009-05-15',
|
27
|
+
:methods => [
|
28
|
+
:configure_health_check, :create_load_balancer, :delete_load_balancer, :deregister_instances_from_load_balancer,
|
29
|
+
:describe_load_balancers, :describe_instance_health, :disable_availability_zones_for_load_balancer,
|
30
|
+
:enable_availability_zones_for_load_balancer, :register_instances_with_load_balancer
|
31
|
+
]
|
32
|
+
},
|
33
|
+
|
34
|
+
:cloud_watch => {
|
35
|
+
:host => 'monitoring.amazonaws.com',
|
36
|
+
:documentation => 'http://docs.amazonwebservices.com/AmazonCloudWatch/latest/DeveloperGuide/',
|
37
|
+
:version => '2009-05-15',
|
38
|
+
:methods => [:get_metric_statistics, :list_metrics]
|
39
|
+
},
|
40
|
+
|
41
|
+
:auto_scaling => {
|
42
|
+
:host => 'autoscaling.amazonaws.com',
|
43
|
+
:documentation => 'http://docs.amazonwebservices.com/AutoScaling/latest/DeveloperGuide/',
|
44
|
+
:version => '2009-05-15',
|
45
|
+
:methods => [
|
46
|
+
:create_auto_scaling_group, :create_launch_configuration, :create_or_update_scaling_trigger,
|
47
|
+
:delete_auto_scaling_group, :delete_launch_configuration, :delete_trigger,
|
48
|
+
:describe_auto_scaling_groups, :describe_launch_configurations, :describe_scaling_activities,
|
49
|
+
:describe_triggers, :set_desired_capacity, :terminate_instance_in_auto_scaling_group,
|
50
|
+
:update_auto_scaling_group
|
51
|
+
]
|
52
|
+
}
|
53
|
+
}
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'aws/ec2/request'
|
2
|
+
require 'aws/ec2/parser'
|
3
|
+
require 'aws/ec2/api'
|
4
|
+
|
5
|
+
module AWS
|
6
|
+
module EC2
|
7
|
+
class Client
|
8
|
+
include EC2
|
9
|
+
include EC2::Request
|
10
|
+
include EC2::Parser
|
11
|
+
|
12
|
+
def initialize(options = {})
|
13
|
+
@access_key_id = options[:access_key_id]
|
14
|
+
@secret_access_key = options[:secret_access_key]
|
15
|
+
@region = options[:region]
|
16
|
+
@debug = options[:debug] || false
|
17
|
+
end
|
18
|
+
|
19
|
+
def method_missing(method_name, *args)
|
20
|
+
action = camelize(method_name)
|
21
|
+
|
22
|
+
config = EC2::API.find{|x, y| y[:methods].include?(method_name.to_sym)}
|
23
|
+
|
24
|
+
raise ArgumentError, "Unknown Method: #{method_name}" unless config
|
25
|
+
|
26
|
+
host = config.last[:host]
|
27
|
+
regions = config.last[:regions] || []
|
28
|
+
host = @region + '.' + host if @region and regions.include?(@region)
|
29
|
+
version = config.last[:version]
|
30
|
+
|
31
|
+
user_options = args[0] || {}
|
32
|
+
user_options.merge!(:action => action, :version => version)
|
33
|
+
camelized_options = {}
|
34
|
+
user_options.each{|key, value| camelized_options[camelize(key).to_sym] = value}
|
35
|
+
|
36
|
+
puts "calling host=#{host}, params=#{camelized_options.inspect}" if @debug
|
37
|
+
|
38
|
+
xml_response = aws_request(host, camelized_options)
|
39
|
+
parse(xml_response)
|
40
|
+
end
|
41
|
+
|
42
|
+
def camelize(key)
|
43
|
+
key.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'xmlsimple'
|
3
|
+
|
4
|
+
class Hash
|
5
|
+
def method_missing(meth, *args, &block)
|
6
|
+
if args.size == 0
|
7
|
+
self[meth.to_s] || self[meth.to_sym]
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module AWS
|
13
|
+
module EC2
|
14
|
+
module Parser
|
15
|
+
def parse(xml_response)
|
16
|
+
result = XmlSimple.xml_in(xml_response, { 'forcearray' => ['item'], 'suppressempty' => nil, 'keeproot' => false })
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'time'
|
2
|
+
require 'openssl'
|
3
|
+
require 'digest/sha1'
|
4
|
+
require 'base64'
|
5
|
+
require 'uri'
|
6
|
+
require 'net/https'
|
7
|
+
require 'cgi'
|
8
|
+
|
9
|
+
module AWS
|
10
|
+
module EC2
|
11
|
+
module Request
|
12
|
+
|
13
|
+
def aws_request(host, params)
|
14
|
+
|
15
|
+
params.merge!(
|
16
|
+
:AWSAccessKeyId => @access_key_id,
|
17
|
+
:SignatureVersion => 2,
|
18
|
+
:SignatureMethod => 'HmacSHA1',
|
19
|
+
:Timestamp => Time.now.getutc.iso8601
|
20
|
+
)
|
21
|
+
request_params_string = params_to_request_string(params)
|
22
|
+
string_to_sign = prepare_sign_string(host, request_params_string)
|
23
|
+
request_params_string += "&Signature=#{sign(string_to_sign)}"
|
24
|
+
|
25
|
+
post(host, request_params_string)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def sign(str)
|
30
|
+
digest = OpenSSL::Digest::Digest.new('sha1')
|
31
|
+
b64_hmac = Base64.encode64(OpenSSL::HMAC.digest(digest, @secret_access_key, str)).gsub("\n","")
|
32
|
+
CGI::escape(b64_hmac)
|
33
|
+
end
|
34
|
+
|
35
|
+
def params_to_request_string(params)
|
36
|
+
sorted_params = params.sort {|x,y| x[0].to_s <=> y[0].to_s}
|
37
|
+
escaped_params = sorted_params.collect do |p|
|
38
|
+
encoded = (CGI::escape(p[0].to_s) + "=" + CGI::escape(p[1].to_s))
|
39
|
+
encoded.gsub('+', '%20')
|
40
|
+
end
|
41
|
+
escaped_params.join('&')
|
42
|
+
end
|
43
|
+
|
44
|
+
def post(host, params)
|
45
|
+
http = Net::HTTP.new(host, 443)
|
46
|
+
http.use_ssl = true
|
47
|
+
|
48
|
+
http.start do
|
49
|
+
req = Net::HTTP::Post.new('/')
|
50
|
+
req.content_type = 'application/x-www-form-urlencoded'
|
51
|
+
req['User-Agent'] = "moomerman-aws-gem"
|
52
|
+
|
53
|
+
response = http.request(req, params)
|
54
|
+
|
55
|
+
return response.body
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def prepare_sign_string(host, params)
|
60
|
+
string_to_sign =
|
61
|
+
'POST' + "\n" +
|
62
|
+
host + "\n" +
|
63
|
+
'/' + "\n" +
|
64
|
+
params
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: moomerman-aws
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Richard Taylor
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-20 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: xml-simple
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.0.11
|
24
|
+
version:
|
25
|
+
description: aws is a Amazon Web Services client library for Ruby.
|
26
|
+
email: moomerman@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- README.textile
|
35
|
+
- lib/aws.rb
|
36
|
+
- lib/aws
|
37
|
+
- lib/aws/ec2
|
38
|
+
- lib/aws/ec2/api.rb
|
39
|
+
- lib/aws/ec2/client.rb
|
40
|
+
- lib/aws/ec2/parser.rb
|
41
|
+
- lib/aws/ec2/request.rb
|
42
|
+
has_rdoc: false
|
43
|
+
homepage: http://github.com/moomerman/aws
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --inline-source
|
47
|
+
- --charset=UTF-8
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project: aws
|
65
|
+
rubygems_version: 1.2.0
|
66
|
+
signing_key:
|
67
|
+
specification_version: 2
|
68
|
+
summary: aws is a Amazon Web Services client library for Ruby.
|
69
|
+
test_files: []
|
70
|
+
|