2014-02-01 21:13:17 -05:00
|
|
|
require 'fog/aws/core'
|
2011-08-31 16:52:53 -04:00
|
|
|
|
2011-02-05 17:06:44 -05:00
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
class SQS < Fog::Service
|
2012-06-18 04:44:51 -04:00
|
|
|
extend Fog::AWS::CredentialFetcher::ServiceMethods
|
2014-02-19 07:30:59 -05:00
|
|
|
|
2011-02-05 17:06:44 -05:00
|
|
|
requires :aws_access_key_id, :aws_secret_access_key
|
2012-06-18 04:44:51 -04:00
|
|
|
recognizes :region, :host, :path, :port, :scheme, :persistent, :aws_session_token, :use_iam_profile, :aws_credentials_expire_at
|
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
|
2014-02-19 07:30:59 -05:00
|
|
|
|
2011-09-18 16:00:58 -04:00
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
2011-02-05 17:06:44 -05:00
|
|
|
|
|
|
|
def initialize(options={})
|
2012-06-18 04:44:51 -04:00
|
|
|
@use_iam_profile = options[:use_iam_profile]
|
|
|
|
setup_credentials(options)
|
2011-09-18 16:00:58 -04:00
|
|
|
@region = options[:region] || 'us-east-1'
|
|
|
|
|
2012-11-13 21:32:07 -05:00
|
|
|
unless ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2', 'sa-east-1'].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
|
|
|
|
|
2012-06-18 04:44:51 -04:00
|
|
|
def setup_credentials(options)
|
|
|
|
@aws_access_key_id = options[:aws_access_key_id]
|
|
|
|
end
|
2011-02-05 17:06:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
2012-06-18 04:44:51 -04:00
|
|
|
include Fog::AWS::CredentialFetcher::ConnectionMethods
|
2011-02-05 17:06:44 -05:00
|
|
|
# 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={})
|
2012-06-18 04:44:51 -04:00
|
|
|
@use_iam_profile = options[:use_iam_profile]
|
|
|
|
setup_credentials(options)
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
2011-02-05 17:06:44 -05:00
|
|
|
options[:region] ||= 'us-east-1'
|
2012-07-30 10:23:59 -04:00
|
|
|
@host = options[:host] || "sqs.#{options[:region]}.amazonaws.com"
|
|
|
|
|
2011-09-12 11:01:48 -04:00
|
|
|
@path = options[:path] || '/'
|
|
|
|
@persistent = options[:persistent] || false
|
|
|
|
@port = options[:port] || 443
|
|
|
|
@scheme = options[:scheme] || 'https'
|
2014-02-26 19:50:35 -05:00
|
|
|
@connection = Fog::XML::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", @persistent, @connection_options)
|
2011-02-05 17:06:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2012-06-18 04:44:51 -04:00
|
|
|
def setup_credentials(options)
|
|
|
|
@aws_access_key_id = options[:aws_access_key_id]
|
|
|
|
@aws_secret_access_key = options[:aws_secret_access_key]
|
|
|
|
@aws_session_token = options[:aws_session_token]
|
|
|
|
@aws_credentials_expire_at = options[:aws_credentials_expire_at]
|
|
|
|
@hmac = Fog::HMAC.new('sha256', @aws_secret_access_key)
|
|
|
|
end
|
|
|
|
|
2011-07-19 16:10:26 -04:00
|
|
|
def path_from_queue_url(queue_url)
|
2012-09-22 12:19:42 -04:00
|
|
|
queue_url.split('.com', 2).last.sub(/^:[0-9]+/, '')
|
2011-02-05 23:17:47 -05:00
|
|
|
end
|
|
|
|
|
2011-02-05 17:06:44 -05:00
|
|
|
def request(params)
|
2013-10-14 09:36:41 -04:00
|
|
|
refresh_credentials_if_expired
|
|
|
|
|
2011-02-05 17:06:44 -05:00
|
|
|
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,
|
2014-01-08 14:05:58 -05:00
|
|
|
:host => @host,
|
2011-02-05 17:06:44 -05:00
|
|
|
:path => path || @path,
|
2014-01-08 14:05:58 -05:00
|
|
|
:port => @port,
|
2011-02-05 17:06:44 -05:00
|
|
|
:version => '2009-02-01'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
args = {
|
|
|
|
:body => body,
|
|
|
|
:expects => 200,
|
|
|
|
:idempotent => idempotent,
|
|
|
|
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
|
|
|
|
:method => 'POST',
|
|
|
|
:parser => parser
|
|
|
|
}
|
|
|
|
args.merge!(:path => path) if path
|
|
|
|
|
|
|
|
response = @connection.request(args)
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|