2009-06-30 12:43:39 -04:00
|
|
|
require 'rubygems'
|
|
|
|
require 'base64'
|
|
|
|
require 'cgi'
|
|
|
|
require 'hmac-sha2'
|
|
|
|
|
2009-07-13 21:22:42 -04:00
|
|
|
current_directory = File.dirname(__FILE__)
|
|
|
|
require "#{current_directory}/../connection"
|
|
|
|
require "#{current_directory}/../parser"
|
|
|
|
require "#{current_directory}/../response"
|
|
|
|
|
|
|
|
parsers_directory = "#{current_directory}/parsers/ec2"
|
2009-07-11 16:41:21 -04:00
|
|
|
require "#{parsers_directory}/allocate_address"
|
|
|
|
require "#{parsers_directory}/basic"
|
|
|
|
require "#{parsers_directory}/create_key_pair"
|
|
|
|
require "#{parsers_directory}/create_snapshot"
|
|
|
|
require "#{parsers_directory}/create_volume"
|
|
|
|
require "#{parsers_directory}/describe_addresses"
|
2009-07-13 12:58:08 -04:00
|
|
|
require "#{parsers_directory}/describe_availability_zones"
|
2009-07-11 16:41:21 -04:00
|
|
|
require "#{parsers_directory}/describe_images"
|
|
|
|
require "#{parsers_directory}/describe_instances"
|
|
|
|
require "#{parsers_directory}/describe_key_pairs"
|
|
|
|
require "#{parsers_directory}/describe_security_groups"
|
|
|
|
require "#{parsers_directory}/describe_snapshots"
|
|
|
|
require "#{parsers_directory}/describe_volumes"
|
2009-07-17 03:36:42 -04:00
|
|
|
require "#{parsers_directory}/run_instances"
|
|
|
|
require "#{parsers_directory}/terminate_instances"
|
2009-07-11 16:41:21 -04:00
|
|
|
|
2009-07-13 22:14:59 -04:00
|
|
|
requests_directory = "#{current_directory}/requests/ec2"
|
|
|
|
require "#{requests_directory}/allocate_address"
|
|
|
|
require "#{requests_directory}/create_key_pair"
|
|
|
|
require "#{requests_directory}/create_security_group"
|
|
|
|
require "#{requests_directory}/create_snapshot"
|
|
|
|
require "#{requests_directory}/create_volume"
|
|
|
|
require "#{requests_directory}/delete_key_pair"
|
|
|
|
require "#{requests_directory}/delete_security_group"
|
|
|
|
require "#{requests_directory}/delete_snapshot"
|
|
|
|
require "#{requests_directory}/delete_volume"
|
|
|
|
require "#{requests_directory}/describe_addresses"
|
|
|
|
require "#{requests_directory}/describe_availability_zones"
|
|
|
|
require "#{requests_directory}/describe_images"
|
|
|
|
require "#{requests_directory}/describe_instances"
|
|
|
|
require "#{requests_directory}/describe_key_pairs"
|
|
|
|
require "#{requests_directory}/describe_security_groups"
|
|
|
|
require "#{requests_directory}/describe_snapshots"
|
|
|
|
require "#{requests_directory}/describe_volumes"
|
|
|
|
require "#{requests_directory}/release_address"
|
|
|
|
require "#{requests_directory}/run_instances"
|
2009-07-17 03:36:42 -04:00
|
|
|
require "#{requests_directory}/terminate_instances"
|
|
|
|
|
2009-07-13 22:14:59 -04:00
|
|
|
|
2009-06-30 12:43:39 -04:00
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
class EC2
|
|
|
|
|
|
|
|
# Initialize connection to EC2
|
|
|
|
#
|
|
|
|
# ==== Notes
|
|
|
|
# options parameter must include values for :aws_access_key_id and
|
|
|
|
# :aws_secret_access_key in order to create a connection
|
|
|
|
#
|
|
|
|
# ==== Examples
|
2009-07-07 22:20:54 -04:00
|
|
|
# sdb = SimpleDB.new(
|
|
|
|
# :aws_access_key_id => your_aws_access_key_id,
|
|
|
|
# :aws_secret_access_key => your_aws_secret_access_key
|
|
|
|
# )
|
2009-06-30 12:43:39 -04:00
|
|
|
#
|
|
|
|
# ==== Parameters
|
2009-07-07 22:20:54 -04:00
|
|
|
# * options<~Hash> - config arguments for connection. Defaults to {}.
|
2009-06-30 12:43:39 -04:00
|
|
|
#
|
|
|
|
# ==== Returns
|
2009-07-07 22:20:54 -04:00
|
|
|
# * EC2 object with connection to aws.
|
2009-06-30 12:43:39 -04:00
|
|
|
def initialize(options={})
|
|
|
|
@aws_access_key_id = options[:aws_access_key_id]
|
|
|
|
@aws_secret_access_key = options[:aws_secret_access_key]
|
|
|
|
@hmac = HMAC::SHA256.new(@aws_secret_access_key)
|
|
|
|
@host = options[:host] || 'ec2.amazonaws.com'
|
|
|
|
@port = options[:port] || 443
|
|
|
|
@scheme = options[:scheme] || 'https'
|
2009-07-13 21:22:42 -04:00
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}")
|
2009-06-30 12:43:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2009-07-04 13:00:36 -04:00
|
|
|
def indexed_params(name, params)
|
|
|
|
indexed, index = {}, 1
|
|
|
|
for param in [*params]
|
|
|
|
indexed["#{name}.#{index}"] = param
|
|
|
|
index += 1
|
|
|
|
end
|
|
|
|
indexed
|
|
|
|
end
|
|
|
|
|
2009-06-30 12:43:39 -04:00
|
|
|
def request(params, parser)
|
|
|
|
params.merge!({
|
|
|
|
'AWSAccessKeyId' => @aws_access_key_id,
|
|
|
|
'SignatureMethod' => 'HmacSHA256',
|
|
|
|
'SignatureVersion' => '2',
|
2009-07-02 13:54:41 -04:00
|
|
|
'Timestamp' => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
|
2009-06-30 12:43:39 -04:00
|
|
|
'Version' => '2009-04-04'
|
|
|
|
})
|
|
|
|
|
|
|
|
body = ''
|
|
|
|
for key in params.keys.sort
|
|
|
|
unless (value = params[key]).nil?
|
2009-07-02 15:38:35 -04:00
|
|
|
body << "#{key}=#{CGI.escape(value.to_s).gsub(/\+/, '%20')}&"
|
2009-06-30 12:43:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
string_to_sign = "POST\n#{@host}\n/\n" << body.chop
|
|
|
|
hmac = @hmac.update(string_to_sign)
|
|
|
|
body << "Signature=#{CGI.escape(Base64.encode64(hmac.digest).chomp!).gsub(/\+/, '%20')}"
|
|
|
|
|
|
|
|
response = @connection.request({
|
|
|
|
:body => body,
|
2009-07-14 19:04:39 -04:00
|
|
|
:expects => 200,
|
2009-06-30 12:43:39 -04:00
|
|
|
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
|
|
|
|
:host => @host,
|
2009-07-10 18:40:11 -04:00
|
|
|
:method => 'POST',
|
|
|
|
:parser => parser
|
2009-06-30 12:43:39 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|