diff --git a/lib/fog/aws/parsers/sqs/create_queue.rb b/lib/fog/aws/parsers/sqs/create_queue.rb new file mode 100644 index 000000000..516feaed5 --- /dev/null +++ b/lib/fog/aws/parsers/sqs/create_queue.rb @@ -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 diff --git a/lib/fog/aws/parsers/sqs/delete_queue.rb b/lib/fog/aws/parsers/sqs/delete_queue.rb new file mode 100644 index 000000000..0eb113c8f --- /dev/null +++ b/lib/fog/aws/parsers/sqs/delete_queue.rb @@ -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 diff --git a/lib/fog/aws/parsers/sqs/list_queues.rb b/lib/fog/aws/parsers/sqs/list_queues.rb new file mode 100644 index 000000000..8f3664b94 --- /dev/null +++ b/lib/fog/aws/parsers/sqs/list_queues.rb @@ -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 diff --git a/lib/fog/aws/requests/sqs/create_queue.rb b/lib/fog/aws/requests/sqs/create_queue.rb new file mode 100644 index 000000000..bb151ae62 --- /dev/null +++ b/lib/fog/aws/requests/sqs/create_queue.rb @@ -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 diff --git a/lib/fog/aws/requests/sqs/delete_queue.rb b/lib/fog/aws/requests/sqs/delete_queue.rb new file mode 100644 index 000000000..3271e0ef0 --- /dev/null +++ b/lib/fog/aws/requests/sqs/delete_queue.rb @@ -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 diff --git a/lib/fog/aws/requests/sqs/list_queues.rb b/lib/fog/aws/requests/sqs/list_queues.rb new file mode 100644 index 000000000..59536164a --- /dev/null +++ b/lib/fog/aws/requests/sqs/list_queues.rb @@ -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 diff --git a/lib/fog/aws/sqs.rb b/lib/fog/aws/sqs.rb new file mode 100644 index 000000000..4ca4bb45d --- /dev/null +++ b/lib/fog/aws/sqs.rb @@ -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 diff --git a/lib/fog/providers/aws.rb b/lib/fog/providers/aws.rb index fa804e483..61ed67899 100644 --- a/lib/fog/providers/aws.rb +++ b/lib/fog/providers/aws.rb @@ -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)