From 293811ad4224577b5cf0cc93c4dca456d7477cae Mon Sep 17 00:00:00 2001 From: "geemus (Wesley Beary)" Date: Sun, 14 Mar 2010 20:11:43 -0700 Subject: [PATCH] [simpledb] cleaner mocking/dependencies --- lib/fog/aws.rb | 6 +- .../aws/parsers/simpledb/domain_metadata.rb | 2 + .../aws/parsers/simpledb/get_attributes.rb | 2 + lib/fog/aws/parsers/simpledb/list_domains.rb | 2 + lib/fog/aws/parsers/simpledb/select.rb | 2 + .../requests/simpledb/batch_put_attributes.rb | 28 +- .../aws/requests/simpledb/create_domain.rb | 20 +- .../requests/simpledb/delete_attributes.rb | 28 +- .../aws/requests/simpledb/delete_domain.rb | 20 +- .../aws/requests/simpledb/domain_metadata.rb | 24 +- .../aws/requests/simpledb/get_attributes.rb | 33 +-- lib/fog/aws/requests/simpledb/list_domains.rb | 22 +- .../aws/requests/simpledb/put_attributes.rb | 18 +- lib/fog/aws/requests/simpledb/select.rb | 18 +- lib/fog/aws/simpledb.rb | 258 +++++++++--------- 15 files changed, 212 insertions(+), 271 deletions(-) diff --git a/lib/fog/aws.rb b/lib/fog/aws.rb index 265396606..1e9c756a7 100644 --- a/lib/fog/aws.rb +++ b/lib/fog/aws.rb @@ -1,12 +1,12 @@ -require 'fog/aws/s3.rb' +require 'fog/aws/s3' +require 'fog/aws/simpledb' module Fog module AWS def self.dependencies [ - 'fog/aws/ec2.rb', - 'fog/aws/simpledb.rb' + 'fog/aws/ec2.rb' ] end diff --git a/lib/fog/aws/parsers/simpledb/domain_metadata.rb b/lib/fog/aws/parsers/simpledb/domain_metadata.rb index fbda04c55..3d9df66ea 100644 --- a/lib/fog/aws/parsers/simpledb/domain_metadata.rb +++ b/lib/fog/aws/parsers/simpledb/domain_metadata.rb @@ -1,3 +1,5 @@ +require 'fog/aws/parsers/simpledb/basic' + module Fog module Parsers module AWS diff --git a/lib/fog/aws/parsers/simpledb/get_attributes.rb b/lib/fog/aws/parsers/simpledb/get_attributes.rb index 2e07a873d..9cda80a60 100644 --- a/lib/fog/aws/parsers/simpledb/get_attributes.rb +++ b/lib/fog/aws/parsers/simpledb/get_attributes.rb @@ -1,3 +1,5 @@ +require 'fog/aws/parsers/simpledb/basic' + module Fog module Parsers module AWS diff --git a/lib/fog/aws/parsers/simpledb/list_domains.rb b/lib/fog/aws/parsers/simpledb/list_domains.rb index 8bebba14d..8b1fa3e86 100644 --- a/lib/fog/aws/parsers/simpledb/list_domains.rb +++ b/lib/fog/aws/parsers/simpledb/list_domains.rb @@ -1,3 +1,5 @@ +require 'fog/aws/parsers/simpledb/basic' + module Fog module Parsers module AWS diff --git a/lib/fog/aws/parsers/simpledb/select.rb b/lib/fog/aws/parsers/simpledb/select.rb index f8ddb9cdc..eb02576c8 100644 --- a/lib/fog/aws/parsers/simpledb/select.rb +++ b/lib/fog/aws/parsers/simpledb/select.rb @@ -1,3 +1,5 @@ +require 'fog/aws/parsers/simpledb/basic' + module Fog module Parsers module AWS diff --git a/lib/fog/aws/requests/simpledb/batch_put_attributes.rb b/lib/fog/aws/requests/simpledb/batch_put_attributes.rb index f792e742a..94016c4c4 100644 --- a/lib/fog/aws/requests/simpledb/batch_put_attributes.rb +++ b/lib/fog/aws/requests/simpledb/batch_put_attributes.rb @@ -1,8 +1,7 @@ -unless Fog.mocking? - - module Fog - module AWS - class SimpleDB +module Fog + module AWS + module SimpleDB + class Real # Put items attributes into a SimpleDB domain # @@ -29,27 +28,21 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class SimpleDB + class Mock def batch_put_attributes(domain_name, items, replace_attributes = Hash.new([])) response = Excon::Response.new - if Fog::AWS::SimpleDB.data[:domains][domain_name] + if @data[:domains][domain_name] for item_name, attributes in items do for key, value in attributes do - Fog::AWS::SimpleDB.data[:domains][domain_name][item_name] ||= {} + @data[:domains][domain_name][item_name] ||= {} if replace_attributes[item_name] && replace_attributes[item_name].include?(key) - Fog::AWS::SimpleDB.data[:domains][domain_name][item_name][key.to_s] = [] + @data[:domains][domain_name][item_name][key.to_s] = [] else - Fog::AWS::SimpleDB.data[:domains][domain_name][item_name][key.to_s] ||= [] + @data[:domains][domain_name][item_name][key.to_s] ||= [] end - Fog::AWS::SimpleDB.data[:domains][domain_name][item_name][key.to_s] << value.to_s + @data[:domains][domain_name][item_name][key.to_s] << value.to_s end end response.status = 200 @@ -67,5 +60,4 @@ else end end end - end diff --git a/lib/fog/aws/requests/simpledb/create_domain.rb b/lib/fog/aws/requests/simpledb/create_domain.rb index 5ca9560a7..ccad6bbf1 100644 --- a/lib/fog/aws/requests/simpledb/create_domain.rb +++ b/lib/fog/aws/requests/simpledb/create_domain.rb @@ -1,8 +1,7 @@ -unless Fog.mocking? - - module Fog - module AWS - class SimpleDB +module Fog + module AWS + module SimpleDB + class Real # Create a SimpleDB domain # @@ -23,18 +22,12 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class SimpleDB + class Mock def create_domain(domain_name) response = Excon::Response.new - Fog::AWS::SimpleDB.data[:domains][domain_name] = {} + @data[:domains][domain_name] = {} response.status = 200 response.body = { 'BoxUsage' => Fog::AWS::Mock.box_usage, @@ -46,5 +39,4 @@ else end end end - end diff --git a/lib/fog/aws/requests/simpledb/delete_attributes.rb b/lib/fog/aws/requests/simpledb/delete_attributes.rb index 9f70b6371..92a84f38a 100644 --- a/lib/fog/aws/requests/simpledb/delete_attributes.rb +++ b/lib/fog/aws/requests/simpledb/delete_attributes.rb @@ -1,8 +1,7 @@ -unless Fog.mocking? - - module Fog - module AWS - class SimpleDB +module Fog + module AWS + module SimpleDB + class Real # List metadata for SimpleDB domain # @@ -32,26 +31,20 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class SimpleDB + class Mock def delete_attributes(domain_name, item_name, attributes = nil) response = Excon::Response.new - if Fog::AWS::SimpleDB.data[:domains][domain_name] + if @data[:domains][domain_name] if attributes for key, value in attributes - if Fog::AWS::SimpleDB.data[:domains][domain_name][key] - Fog::AWS::SimpleDB.data[:domains][domain_name][key].delete('value') + if @data[:domains][domain_name][key] + @data[:domains][domain_name][key].delete('value') end end else - Fog::AWS::SimpleDB.data[:domains].delete(domain_name) + @data[:domains].delete(domain_name) end response.status = 200 response.body = { @@ -68,5 +61,4 @@ else end end end - -end \ No newline at end of file +end diff --git a/lib/fog/aws/requests/simpledb/delete_domain.rb b/lib/fog/aws/requests/simpledb/delete_domain.rb index e4b8e9a74..d5e9dbf14 100644 --- a/lib/fog/aws/requests/simpledb/delete_domain.rb +++ b/lib/fog/aws/requests/simpledb/delete_domain.rb @@ -1,8 +1,7 @@ -unless Fog.mocking? - - module Fog - module AWS - class SimpleDB +module Fog + module AWS + module SimpleDB + class Real # Delete a SimpleDB domain # @@ -23,18 +22,12 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class SimpleDB + class Mock def delete_domain(domain_name) response = Excon::Response.new - if Fog::AWS::SimpleDB.data[:domains].delete(domain_name) + if @data[:domains].delete(domain_name) response.status = 200 response.body = { 'BoxUsage' => Fog::AWS::Mock.box_usage, @@ -47,5 +40,4 @@ else end end end - end diff --git a/lib/fog/aws/requests/simpledb/domain_metadata.rb b/lib/fog/aws/requests/simpledb/domain_metadata.rb index 9852b10a3..5a245f18c 100644 --- a/lib/fog/aws/requests/simpledb/domain_metadata.rb +++ b/lib/fog/aws/requests/simpledb/domain_metadata.rb @@ -1,8 +1,9 @@ -unless Fog.mocking? +module Fog + module AWS + module SimpleDB + class Real - module Fog - module AWS - class SimpleDB + require 'fog/aws/parsers/simpledb/domain_metadata' # List metadata for SimpleDB domain # @@ -30,20 +31,14 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class SimpleDB + class Mock def domain_metadata(domain_name) response = Excon::Response.new - if domain = Fog::AWS::SimpleDB.data[:domains][domain_name] + if domain = @data[:domains][domain_name] response.status = 200 - + attribute_names = [] attribute_values = [] for item in domain.values @@ -54,7 +49,7 @@ else end end end - + response.body = { 'AttributeNameCount' => attribute_names.length, 'AttributeNamesSizeBytes' => attribute_names.join('').length, @@ -76,5 +71,4 @@ else end end end - end diff --git a/lib/fog/aws/requests/simpledb/get_attributes.rb b/lib/fog/aws/requests/simpledb/get_attributes.rb index 67692e5f4..e43b2f159 100644 --- a/lib/fog/aws/requests/simpledb/get_attributes.rb +++ b/lib/fog/aws/requests/simpledb/get_attributes.rb @@ -1,8 +1,9 @@ -unless Fog.mocking? +module Fog + module AWS + module SimpleDB + class Real - module Fog - module AWS - class SimpleDB + require 'fog/aws/parsers/simpledb/get_attributes' # List metadata for SimpleDB domain # @@ -13,7 +14,7 @@ unless Fog.mocking? # in xml. Control characters and sequences not allowed in xml are not # valid. Can be up to 1024 bytes long. # * attributes<~Array> - Attributes to return from the item. Defaults to - # nil, which will return all attributes. Attribute names and values may use + # {}, which will return all attributes. Attribute names and values may use # any UTF-8 characters valid in xml. Control characters and sequences not # allowed in xml are not valid. Each name and value can be up to 1024 # bytes long. @@ -24,7 +25,8 @@ unless Fog.mocking? # * 'Attributes' - list of attribute name/values for the item # * 'BoxUsage' # * 'RequestId' - def get_attributes(domain_name, item_name, attributes = nil) + def get_attributes(domain_name, item_name, attributes = {}) + request({ 'Action' => 'GetAttributes', 'DomainName' => domain_name, @@ -33,27 +35,21 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class SimpleDB + class Mock def get_attributes(domain_name, item_name, attributes = nil) response = Excon::Response.new - if Fog::AWS::SimpleDB.data[:domains][domain_name] + if @data[:domains][domain_name] object = {} if attributes for attribute in attributes - if Fog::AWS::SimpleDB.data[:domains][domain_name][item_name] && Fog::AWS::SimpleDB.data[:domains][domain_name][item_name] - object[attribute] = Fog::AWS::SimpleDB.data[:domains][domain_name][item_name][attribute] + if @data[:domains][domain_name][item_name] && @data[:domains][domain_name][item_name] + object[attribute] = @data[:domains][domain_name][item_name][attribute] end end - elsif Fog::AWS::SimpleDB.data[:domains][domain_name][item_name] - object = Fog::AWS::SimpleDB.data[:domains][domain_name][item_name] + elsif @data[:domains][domain_name][item_name] + object = @data[:domains][domain_name][item_name] end response.status = 200 response.body = { @@ -71,5 +67,4 @@ else end end end - end diff --git a/lib/fog/aws/requests/simpledb/list_domains.rb b/lib/fog/aws/requests/simpledb/list_domains.rb index 14af18725..6804829c1 100644 --- a/lib/fog/aws/requests/simpledb/list_domains.rb +++ b/lib/fog/aws/requests/simpledb/list_domains.rb @@ -1,8 +1,9 @@ -unless Fog.mocking? +module Fog + module AWS + module SimpleDB + class Real - module Fog - module AWS - class SimpleDB + require 'fog/aws/parsers/simpledb/list_domains' # List SimpleDB domains # @@ -26,22 +27,16 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class SimpleDB + class Mock def list_domains(options = {}) response = Excon::Response.new - keys = Fog::AWS::SimpleDB.data[:domains].keys + keys = @data[:domains].keys max = options['MaxNumberOfDomains'] || keys.size offset = options['NextToken'] || 0 domains = [] - for key, value in Fog::AWS::SimpleDB.data[:domains].keys[offset...max] + for key, value in @data[:domains].keys[offset...max] domains << key end response.status = 200 @@ -59,5 +54,4 @@ else end end end - end diff --git a/lib/fog/aws/requests/simpledb/put_attributes.rb b/lib/fog/aws/requests/simpledb/put_attributes.rb index 9a0e6453e..20b4c1988 100644 --- a/lib/fog/aws/requests/simpledb/put_attributes.rb +++ b/lib/fog/aws/requests/simpledb/put_attributes.rb @@ -1,8 +1,7 @@ -unless Fog.mocking? - - module Fog - module AWS - class SimpleDB +module Fog + module AWS + module SimpleDB + class Real # Put item attributes into a SimpleDB domain # @@ -27,14 +26,8 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class SimpleDb + class Mock def put_attributes(domain_name, item_name, attributes, replace_attributes = []) batch_put_attributes(domain_name, { item_name => attributes }, { item_name => replace_attributes }) @@ -43,5 +36,4 @@ else end end end - end diff --git a/lib/fog/aws/requests/simpledb/select.rb b/lib/fog/aws/requests/simpledb/select.rb index af5a1701f..d2578d988 100644 --- a/lib/fog/aws/requests/simpledb/select.rb +++ b/lib/fog/aws/requests/simpledb/select.rb @@ -1,8 +1,9 @@ -unless Fog.mocking? +module Fog + module AWS + module SimpleDB + class Real - module Fog - module AWS - class SimpleDB + require 'fog/aws/parsers/simpledb/select' # Select item data from SimpleDB # @@ -27,14 +28,8 @@ unless Fog.mocking? end end - end - end -else - - module Fog - module AWS - class SimpleDB + class Mock def select(select_expression, next_token = nil) raise MockNotImplemented.new("Contributions welcome!") @@ -43,5 +38,4 @@ else end end end - end diff --git a/lib/fog/aws/simpledb.rb b/lib/fog/aws/simpledb.rb index 9a8643228..fe9aed288 100644 --- a/lib/fog/aws/simpledb.rb +++ b/lib/fog/aws/simpledb.rb @@ -1,159 +1,155 @@ +require 'fog/aws/requests/simpledb/batch_put_attributes' +require 'fog/aws/requests/simpledb/create_domain' +require 'fog/aws/requests/simpledb/delete_attributes' +require 'fog/aws/requests/simpledb/delete_domain' +require 'fog/aws/requests/simpledb/domain_metadata' +require 'fog/aws/requests/simpledb/get_attributes' +require 'fog/aws/requests/simpledb/list_domains' +require 'fog/aws/requests/simpledb/put_attributes' +require 'fog/aws/requests/simpledb/select' + module Fog module AWS - class SimpleDB + module SimpleDB - if Fog.mocking? - def self.reset_data + def self.new(options={}) + if Fog.mocking? + Fog::AWS::SimpleDB::Mock.new(options) + else + Fog::AWS::SimpleDB::Real.new(options) + end + end + + class Mock + + def reset_data @data = { :domains => {} } end - def self.data - @data - end - end - def self.dependencies - [ - "fog/aws/parsers/simpledb/basic.rb", - "fog/aws/parsers/simpledb/domain_metadata.rb", - "fog/aws/parsers/simpledb/get_attributes.rb", - "fog/aws/parsers/simpledb/list_domains.rb", - "fog/aws/parsers/simpledb/select.rb", - "fog/aws/requests/simpledb/batch_put_attributes.rb", - "fog/aws/requests/simpledb/create_domain.rb", - "fog/aws/requests/simpledb/delete_attributes.rb", - "fog/aws/requests/simpledb/delete_domain.rb", - "fog/aws/requests/simpledb/domain_metadata.rb", - "fog/aws/requests/simpledb/get_attributes.rb", - "fog/aws/requests/simpledb/list_domains.rb", - "fog/aws/requests/simpledb/put_attributes.rb", - "fog/aws/requests/simpledb/select.rb" - ] - end - - def self.reload - self.dependencies.each {|dependency| load(dependency)} - if Fog.mocking? + def initialize(options={}) reset_data end + end - # Initialize connection to SimpleDB - # - # ==== Notes - # options parameter must include values for :aws_access_key_id and - # :aws_secret_access_key in order to create a connection - # - # ==== Examples - # sdb = SimpleDB.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 {}. - # - # ==== Returns - # * SimpleDB 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 = HMAC::SHA256.new(@aws_secret_access_key) - @host = options[:host] || 'sdb.amazonaws.com' - @nil_string = options[:nil_string]|| 'nil' - @port = options[:port] || 443 - @scheme = options[:scheme] || 'https' - end + class Real - private - - def encode_attributes(attributes, replace_attributes = []) - encoded_attributes = {} - if attributes - index = 0 - for key in attributes.keys - for value in Array(attributes[key]) - encoded_attributes["Attribute.#{index}.Name"] = key.to_s - if replace_attributes.include?(key) - encoded_attributes["Attribute.#{index}.Replace"] = 'true' - end - encoded_attributes["Attribute.#{index}.Value"] = sdb_encode(value) - index += 1 - end - end + # Initialize connection to SimpleDB + # + # ==== Notes + # options parameter must include values for :aws_access_key_id and + # :aws_secret_access_key in order to create a connection + # + # ==== Examples + # sdb = SimpleDB.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 {}. + # + # ==== Returns + # * SimpleDB 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 = HMAC::SHA256.new(@aws_secret_access_key) + @host = options[:host] || 'sdb.amazonaws.com' + @nil_string = options[:nil_string]|| 'nil' + @port = options[:port] || 443 + @scheme = options[:scheme] || 'https' end - encoded_attributes - end - def encode_attribute_names(attributes) - AWS.indexed_param('AttributeName', attributes.map {|attribute| attributes.to_s}) - end + private - def encode_batch_attributes(items, replace_attributes = Hash.new([])) - encoded_attributes = {} - if items - item_index = 0 - for item_key in items.keys - encoded_attributes["Item.#{item_index}.ItemName"] = item_key.to_s - for attribute_key in items[item_key].keys - attribute_index = 0 - for value in Array(items[item_key][attribute_key]) - encoded_attributes["Item.#{item_index}.Attribute.#{attribute_index}.Name"] = attribute_key.to_s - if replace_attributes[item_key].include?(attribute_key) - encoded_attributes["Item.#{item_index}.Attribute.#{attribute_index}.Replace"] = 'true' + def encode_attributes(attributes, replace_attributes = []) + encoded_attributes = {} + if attributes + index = 0 + for key in attributes.keys + for value in Array(attributes[key]) + encoded_attributes["Attribute.#{index}.Name"] = key.to_s + if replace_attributes.include?(key) + encoded_attributes["Attribute.#{index}.Replace"] = 'true' end - encoded_attributes["Item.#{item_index}.Attribute.#{attribute_index}.Value"] = sdb_encode(value) - attribute_index += 1 + encoded_attributes["Attribute.#{index}.Value"] = sdb_encode(value) + index += 1 end - item_index += 1 end end + encoded_attributes end - encoded_attributes - end - def request(params, parser) - @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}") - params.merge!({ - 'AWSAccessKeyId' => @aws_access_key_id, - 'SignatureMethod' => 'HmacSHA256', - 'SignatureVersion' => '2', - 'Timestamp' => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"), - 'Version' => '2007-11-07' - }) + def encode_attribute_names(attributes) + AWS.indexed_param('AttributeName', attributes.map {|attribute| attributes.to_s}) + end - body = '' - for key in params.keys.sort - unless (value = params[key]).nil? - body << "#{key}=#{CGI.escape(value.to_s).gsub(/\+/, '%20')}&" + def encode_batch_attributes(items, replace_attributes = Hash.new([])) + encoded_attributes = {} + if items + item_index = 0 + for item_key in items.keys + encoded_attributes["Item.#{item_index}.ItemName"] = item_key.to_s + for attribute_key in items[item_key].keys + attribute_index = 0 + for value in Array(items[item_key][attribute_key]) + encoded_attributes["Item.#{item_index}.Attribute.#{attribute_index}.Name"] = attribute_key.to_s + if replace_attributes[item_key].include?(attribute_key) + encoded_attributes["Item.#{item_index}.Attribute.#{attribute_index}.Replace"] = 'true' + end + encoded_attributes["Item.#{item_index}.Attribute.#{attribute_index}.Value"] = sdb_encode(value) + attribute_index += 1 + end + item_index += 1 + end + end + end + encoded_attributes + end + + def request(params, parser) + @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}") + params.merge!({ + 'AWSAccessKeyId' => @aws_access_key_id, + 'SignatureMethod' => 'HmacSHA256', + 'SignatureVersion' => '2', + 'Timestamp' => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"), + 'Version' => '2007-11-07' + }) + + body = '' + for key in params.keys.sort + unless (value = params[key]).nil? + body << "#{key}=#{CGI.escape(value.to_s).gsub(/\+/, '%20')}&" + 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, + :expects => 200, + :headers => { 'Content-Type' => 'application/x-www-form-urlencoded' }, + :host => @host, + :method => 'POST', + :parser => parser + }) + + response + end + + def sdb_encode(value) + if value.nil? + @nil_string + else + value.to_s 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, - :expects => 200, - :headers => { 'Content-Type' => 'application/x-www-form-urlencoded' }, - :host => @host, - :method => 'POST', - :parser => parser - }) - - response end - - def sdb_encode(value) - if value.nil? - @nil_string - else - value.to_s - end - end - end end end - -Fog::AWS::SimpleDB.reload