mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Begin SQS support
* Add SQS object * Add create_queue * Add list_queues * Add delete_queue
This commit is contained in:
parent
b3b446f0b6
commit
750e0e8050
8 changed files with 274 additions and 0 deletions
26
lib/fog/aws/parsers/sqs/create_queue.rb
Normal file
26
lib/fog/aws/parsers/sqs/create_queue.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
module Fog
|
||||
module Parsers
|
||||
module AWS
|
||||
module SQS
|
||||
|
||||
class CreateQueue < Fog::Parsers::Base
|
||||
|
||||
def reset
|
||||
@response = { 'ResponseMetadata' => {} }
|
||||
end
|
||||
|
||||
def end_element(name)
|
||||
case name
|
||||
when 'RequestId'
|
||||
@response['ResponseMetadata'][name] = @value
|
||||
when 'QueueUrl'
|
||||
@response['QueueUrl'] = @value
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
23
lib/fog/aws/parsers/sqs/delete_queue.rb
Normal file
23
lib/fog/aws/parsers/sqs/delete_queue.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
module Fog
|
||||
module Parsers
|
||||
module AWS
|
||||
module SQS
|
||||
class DeleteQueue < Fog::Parsers::Base
|
||||
|
||||
def reset
|
||||
@response = { 'ResponseMetadata' => {} }
|
||||
end
|
||||
|
||||
def end_element(name)
|
||||
case name
|
||||
when 'RequestId'
|
||||
@response['ResponseMetadata'][name] = @value
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
25
lib/fog/aws/parsers/sqs/list_queues.rb
Normal file
25
lib/fog/aws/parsers/sqs/list_queues.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
module Fog
|
||||
module Parsers
|
||||
module AWS
|
||||
module SQS
|
||||
class ListQueues < Fog::Parsers::Base
|
||||
|
||||
def reset
|
||||
@response = { 'QueueUrls' => [], 'ResponseMetadata' => {} }
|
||||
end
|
||||
|
||||
def end_element(name)
|
||||
case name
|
||||
when 'RequestId'
|
||||
@response['ResponseMetadata'][name] = @value
|
||||
when 'QueueUrl'
|
||||
@response['QueueUrls'] << @value
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
28
lib/fog/aws/requests/sqs/create_queue.rb
Normal file
28
lib/fog/aws/requests/sqs/create_queue.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
module Fog
|
||||
module AWS
|
||||
class SQS
|
||||
class Real
|
||||
|
||||
require 'fog/aws/parsers/sqs/create_queue'
|
||||
|
||||
def create_queue(name)
|
||||
request({
|
||||
'Action' => 'CreateQueue',
|
||||
'QueueName' => name,
|
||||
:parser => Fog::Parsers::AWS::SQS::CreateQueue.new
|
||||
})
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def create_queue(name)
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
40
lib/fog/aws/requests/sqs/delete_queue.rb
Normal file
40
lib/fog/aws/requests/sqs/delete_queue.rb
Normal file
|
@ -0,0 +1,40 @@
|
|||
module Fog
|
||||
module AWS
|
||||
class SQS
|
||||
class Real
|
||||
|
||||
require 'fog/aws/parsers/sqs/list_queues'
|
||||
require 'fog/aws/parsers/sqs/delete_queue'
|
||||
|
||||
def delete_queue(name)
|
||||
url = extract_url_with_name_from_list(name)
|
||||
path = extract_path_from_url(url)
|
||||
|
||||
request({
|
||||
'Action' => 'DeleteQueue',
|
||||
:parser => Fog::Parsers::AWS::SQS::DeleteQueue.new,
|
||||
:path => path
|
||||
})
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def extract_url_with_name_from_list(name)
|
||||
list_queues.body['QueueUrls'].detect { |url| url.match(/\/#{name}$/) }
|
||||
end
|
||||
|
||||
def extract_path_from_url(url)
|
||||
url.gsub(/.*\.com/, '')
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def delete_queue(name)
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
26
lib/fog/aws/requests/sqs/list_queues.rb
Normal file
26
lib/fog/aws/requests/sqs/list_queues.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
module Fog
|
||||
module AWS
|
||||
class SQS
|
||||
class Real
|
||||
|
||||
require 'fog/aws/parsers/sqs/list_queues'
|
||||
|
||||
def list_queues
|
||||
request({
|
||||
'Action' => 'ListQueues',
|
||||
:parser => Fog::Parsers::AWS::SQS::ListQueues.new
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def list_queues
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
105
lib/fog/aws/sqs.rb
Normal file
105
lib/fog/aws/sqs.rb
Normal file
|
@ -0,0 +1,105 @@
|
|||
module Fog
|
||||
module AWS
|
||||
class SQS < Fog::Service
|
||||
|
||||
requires :aws_access_key_id, :aws_secret_access_key
|
||||
recognizes :region, :host, :path, :port, :scheme, :persistent
|
||||
|
||||
request_path 'fog/aws/requests/sqs'
|
||||
request :create_queue
|
||||
request :list_queues
|
||||
request :delete_queue
|
||||
|
||||
class Mock
|
||||
|
||||
def initialize(options={})
|
||||
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 {}.
|
||||
# * region<~String> - optional region to use, in ['eu-west-1', 'us-east-1', 'us-west-1', 'ap-southeast-1']
|
||||
#
|
||||
# ==== 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]
|
||||
@hmac = Fog::HMAC.new('sha256', @aws_secret_access_key)
|
||||
options[:region] ||= 'us-east-1'
|
||||
@host = options[:host] || case options[:region]
|
||||
when 'ap-southeast-1'
|
||||
'ap-southeast-1.queue.amazonaws.com'
|
||||
when 'eu-west-1'
|
||||
'eu-west-1.queue.amazonaws.com'
|
||||
when 'us-east-1'
|
||||
'queue.amazonaws.com'
|
||||
when 'us-west-1'
|
||||
'us-west-1.queue.amazonaws.com'
|
||||
else
|
||||
raise ArgumentError, "Unknown region: #{options[:region].inspect}"
|
||||
end
|
||||
@path = options[:path] || '/'
|
||||
@port = options[:port] || 443
|
||||
@scheme = options[:scheme] || 'https'
|
||||
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", options[:persistent])
|
||||
end
|
||||
|
||||
def reload
|
||||
@connection.reset
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
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,
|
||||
: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
|
|
@ -19,6 +19,7 @@ module Fog
|
|||
service(:rds, 'aws/rds')
|
||||
service(:ses, 'aws/ses')
|
||||
service(:simpledb, 'aws/simpledb')
|
||||
service(:sqs, 'aws/sqs')
|
||||
service(:storage, 'storage/aws')
|
||||
|
||||
def self.indexed_param(key, values)
|
||||
|
|
Loading…
Reference in a new issue