2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/aws'
|
2011-08-31 16:52:53 -04:00
|
|
|
|
2011-02-05 17:06:44 -05:00
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
class SQS < Fog::Service
|
|
|
|
|
|
|
|
requires :aws_access_key_id, :aws_secret_access_key
|
2011-11-13 15:50:09 -05:00
|
|
|
recognizes :region, :host, :path, :port, :scheme, :persistent, :aws_session_token
|
2011-02-05 17:06:44 -05:00
|
|
|
|
|
|
|
request_path 'fog/aws/requests/sqs'
|
2011-07-19 16:10:26 -04:00
|
|
|
request :change_message_visibility
|
2011-02-05 17:06:44 -05:00
|
|
|
request :create_queue
|
2011-02-05 23:17:47 -05:00
|
|
|
request :delete_message
|
2011-02-05 17:06:44 -05:00
|
|
|
request :delete_queue
|
2011-02-05 23:33:53 -05:00
|
|
|
request :get_queue_attributes
|
2011-02-05 23:17:47 -05:00
|
|
|
request :list_queues
|
|
|
|
request :receive_message
|
|
|
|
request :send_message
|
2011-07-19 16:10:26 -04:00
|
|
|
request :set_queue_attributes
|
2011-02-05 17:06:44 -05:00
|
|
|
|
|
|
|
class Mock
|
2011-09-18 16:00:58 -04:00
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, region|
|
|
|
|
hash[region] = Hash.new do |region_hash, key|
|
|
|
|
region_hash[key] = {
|
|
|
|
:owner_id => Fog::AWS::Mock.owner_id,
|
|
|
|
:queues => {}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
2011-02-05 17:06:44 -05:00
|
|
|
|
|
|
|
def initialize(options={})
|
2011-09-18 16:00:58 -04:00
|
|
|
@aws_access_key_id = options[:aws_access_key_id]
|
|
|
|
|
|
|
|
@region = options[:region] || 'us-east-1'
|
|
|
|
|
2011-11-09 12:57:48 -05:00
|
|
|
unless ['ap-northeast-1', 'ap-southeast-1', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2'].include?(@region)
|
2011-09-18 16:00:58 -04:00
|
|
|
raise ArgumentError, "Unknown region: #{@region.inspect}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
self.class.data[@region][@aws_access_key_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
|
|
|
self.class.data[@region].delete(@aws_access_key_id)
|
2011-02-05 17:06:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
|
|
|
|
# Initialize connection to SQS
|
|
|
|
#
|
|
|
|
# ==== Notes
|
|
|
|
# options parameter must include values for :aws_access_key_id and
|
|
|
|
# :aws_secret_access_key in order to create a connection
|
|
|
|
#
|
|
|
|
# ==== Examples
|
|
|
|
# sqs = SQS.new(
|
|
|
|
# :aws_access_key_id => your_aws_access_key_id,
|
|
|
|
# :aws_secret_access_key => your_aws_secret_access_key
|
|
|
|
# )
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# * options<~Hash> - config arguments for connection. Defaults to {}.
|
2012-02-02 18:32:36 -05:00
|
|
|
# * region<~String> - optional region to use. For instance, 'eu-west-1', 'us-east-1' and etc.
|
2011-02-05 17:06:44 -05:00
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# * SQS object with connection to AWS.
|
|
|
|
def initialize(options={})
|
|
|
|
@aws_access_key_id = options[:aws_access_key_id]
|
|
|
|
@aws_secret_access_key = options[:aws_secret_access_key]
|
2011-11-13 15:50:09 -05:00
|
|
|
@aws_session_token = options[:aws_session_token]
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
2011-02-05 17:06:44 -05:00
|
|
|
@hmac = Fog::HMAC.new('sha256', @aws_secret_access_key)
|
|
|
|
options[:region] ||= 'us-east-1'
|
|
|
|
@host = options[:host] || case options[:region]
|
|
|
|
when 'us-east-1'
|
|
|
|
'queue.amazonaws.com'
|
|
|
|
else
|
2012-02-02 18:32:36 -05:00
|
|
|
"#{options[:region]}.queue.amazonaws.com"
|
2011-02-05 17:06:44 -05:00
|
|
|
end
|
2011-09-12 11:01:48 -04:00
|
|
|
@path = options[:path] || '/'
|
|
|
|
@persistent = options[:persistent] || false
|
|
|
|
@port = options[:port] || 443
|
|
|
|
@scheme = options[:scheme] || 'https'
|
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", @persistent, @connection_options)
|
2011-02-05 17:06:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2011-07-19 16:10:26 -04:00
|
|
|
def path_from_queue_url(queue_url)
|
|
|
|
queue_url.split('.com', 2).last
|
2011-02-05 23:17:47 -05:00
|
|
|
end
|
|
|
|
|
2011-02-05 17:06:44 -05:00
|
|
|
def request(params)
|
|
|
|
idempotent = params.delete(:idempotent)
|
|
|
|
parser = params.delete(:parser)
|
|
|
|
path = params.delete(:path)
|
|
|
|
|
|
|
|
body = AWS.signed_params(
|
|
|
|
params,
|
|
|
|
{
|
|
|
|
:aws_access_key_id => @aws_access_key_id,
|
2011-11-13 15:50:09 -05:00
|
|
|
:aws_session_token => @aws_session_token,
|
2011-02-05 17:06:44 -05:00
|
|
|
:hmac => @hmac,
|
|
|
|
:host => @host,
|
|
|
|
:path => path || @path,
|
|
|
|
:port => @port,
|
|
|
|
:version => '2009-02-01'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
args = {
|
|
|
|
:body => body,
|
|
|
|
:expects => 200,
|
|
|
|
:idempotent => idempotent,
|
|
|
|
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
|
|
|
|
:host => @host,
|
|
|
|
:method => 'POST',
|
|
|
|
:parser => parser
|
|
|
|
}
|
|
|
|
args.merge!(:path => path) if path
|
|
|
|
|
|
|
|
response = @connection.request(args)
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|