1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[simpledb] cleaner mocking/dependencies

This commit is contained in:
geemus (Wesley Beary) 2010-03-14 20:11:43 -07:00
parent 46ea4a4a99
commit 293811ad42
15 changed files with 212 additions and 271 deletions

View file

@ -1,12 +1,12 @@
require 'fog/aws/s3.rb' require 'fog/aws/s3'
require 'fog/aws/simpledb'
module Fog module Fog
module AWS module AWS
def self.dependencies def self.dependencies
[ [
'fog/aws/ec2.rb', 'fog/aws/ec2.rb'
'fog/aws/simpledb.rb'
] ]
end end

View file

@ -1,3 +1,5 @@
require 'fog/aws/parsers/simpledb/basic'
module Fog module Fog
module Parsers module Parsers
module AWS module AWS

View file

@ -1,3 +1,5 @@
require 'fog/aws/parsers/simpledb/basic'
module Fog module Fog
module Parsers module Parsers
module AWS module AWS

View file

@ -1,3 +1,5 @@
require 'fog/aws/parsers/simpledb/basic'
module Fog module Fog
module Parsers module Parsers
module AWS module AWS

View file

@ -1,3 +1,5 @@
require 'fog/aws/parsers/simpledb/basic'
module Fog module Fog
module Parsers module Parsers
module AWS module AWS

View file

@ -1,8 +1,7 @@
unless Fog.mocking? module Fog
module AWS
module Fog module SimpleDB
module AWS class Real
class SimpleDB
# Put items attributes into a SimpleDB domain # Put items attributes into a SimpleDB domain
# #
@ -29,27 +28,21 @@ unless Fog.mocking?
end end
end end
end
end
else class Mock
module Fog
module AWS
class SimpleDB
def batch_put_attributes(domain_name, items, replace_attributes = Hash.new([])) def batch_put_attributes(domain_name, items, replace_attributes = Hash.new([]))
response = Excon::Response.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 item_name, attributes in items do
for key, value in attributes 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) 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 else
Fog::AWS::SimpleDB.data[:domains][domain_name][item_name][key.to_s] ||= [] @data[:domains][domain_name][item_name][key.to_s] ||= []
end 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
end end
response.status = 200 response.status = 200
@ -67,5 +60,4 @@ else
end end
end end
end end
end end

View file

@ -1,8 +1,7 @@
unless Fog.mocking? module Fog
module AWS
module Fog module SimpleDB
module AWS class Real
class SimpleDB
# Create a SimpleDB domain # Create a SimpleDB domain
# #
@ -23,18 +22,12 @@ unless Fog.mocking?
end end
end end
end
end
else class Mock
module Fog
module AWS
class SimpleDB
def create_domain(domain_name) def create_domain(domain_name)
response = Excon::Response.new response = Excon::Response.new
Fog::AWS::SimpleDB.data[:domains][domain_name] = {} @data[:domains][domain_name] = {}
response.status = 200 response.status = 200
response.body = { response.body = {
'BoxUsage' => Fog::AWS::Mock.box_usage, 'BoxUsage' => Fog::AWS::Mock.box_usage,
@ -46,5 +39,4 @@ else
end end
end end
end end
end end

View file

@ -1,8 +1,7 @@
unless Fog.mocking? module Fog
module AWS
module Fog module SimpleDB
module AWS class Real
class SimpleDB
# List metadata for SimpleDB domain # List metadata for SimpleDB domain
# #
@ -32,26 +31,20 @@ unless Fog.mocking?
end end
end end
end
end
else class Mock
module Fog
module AWS
class SimpleDB
def delete_attributes(domain_name, item_name, attributes = nil) def delete_attributes(domain_name, item_name, attributes = nil)
response = Excon::Response.new response = Excon::Response.new
if Fog::AWS::SimpleDB.data[:domains][domain_name] if @data[:domains][domain_name]
if attributes if attributes
for key, value in attributes for key, value in attributes
if Fog::AWS::SimpleDB.data[:domains][domain_name][key] if @data[:domains][domain_name][key]
Fog::AWS::SimpleDB.data[:domains][domain_name][key].delete('value') @data[:domains][domain_name][key].delete('value')
end end
end end
else else
Fog::AWS::SimpleDB.data[:domains].delete(domain_name) @data[:domains].delete(domain_name)
end end
response.status = 200 response.status = 200
response.body = { response.body = {
@ -68,5 +61,4 @@ else
end end
end end
end end
end
end

View file

@ -1,8 +1,7 @@
unless Fog.mocking? module Fog
module AWS
module Fog module SimpleDB
module AWS class Real
class SimpleDB
# Delete a SimpleDB domain # Delete a SimpleDB domain
# #
@ -23,18 +22,12 @@ unless Fog.mocking?
end end
end end
end
end
else class Mock
module Fog
module AWS
class SimpleDB
def delete_domain(domain_name) def delete_domain(domain_name)
response = Excon::Response.new response = Excon::Response.new
if Fog::AWS::SimpleDB.data[:domains].delete(domain_name) if @data[:domains].delete(domain_name)
response.status = 200 response.status = 200
response.body = { response.body = {
'BoxUsage' => Fog::AWS::Mock.box_usage, 'BoxUsage' => Fog::AWS::Mock.box_usage,
@ -47,5 +40,4 @@ else
end end
end end
end end
end end

View file

@ -1,8 +1,9 @@
unless Fog.mocking? module Fog
module AWS
module SimpleDB
class Real
module Fog require 'fog/aws/parsers/simpledb/domain_metadata'
module AWS
class SimpleDB
# List metadata for SimpleDB domain # List metadata for SimpleDB domain
# #
@ -30,20 +31,14 @@ unless Fog.mocking?
end end
end end
end
end
else class Mock
module Fog
module AWS
class SimpleDB
def domain_metadata(domain_name) def domain_metadata(domain_name)
response = Excon::Response.new response = Excon::Response.new
if domain = Fog::AWS::SimpleDB.data[:domains][domain_name] if domain = @data[:domains][domain_name]
response.status = 200 response.status = 200
attribute_names = [] attribute_names = []
attribute_values = [] attribute_values = []
for item in domain.values for item in domain.values
@ -54,7 +49,7 @@ else
end end
end end
end end
response.body = { response.body = {
'AttributeNameCount' => attribute_names.length, 'AttributeNameCount' => attribute_names.length,
'AttributeNamesSizeBytes' => attribute_names.join('').length, 'AttributeNamesSizeBytes' => attribute_names.join('').length,
@ -76,5 +71,4 @@ else
end end
end end
end end
end end

View file

@ -1,8 +1,9 @@
unless Fog.mocking? module Fog
module AWS
module SimpleDB
class Real
module Fog require 'fog/aws/parsers/simpledb/get_attributes'
module AWS
class SimpleDB
# List metadata for SimpleDB domain # List metadata for SimpleDB domain
# #
@ -13,7 +14,7 @@ unless Fog.mocking?
# in xml. Control characters and sequences not allowed in xml are not # in xml. Control characters and sequences not allowed in xml are not
# valid. Can be up to 1024 bytes long. # valid. Can be up to 1024 bytes long.
# * attributes<~Array> - Attributes to return from the item. Defaults to # * 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 # 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 # allowed in xml are not valid. Each name and value can be up to 1024
# bytes long. # bytes long.
@ -24,7 +25,8 @@ unless Fog.mocking?
# * 'Attributes' - list of attribute name/values for the item # * 'Attributes' - list of attribute name/values for the item
# * 'BoxUsage' # * 'BoxUsage'
# * 'RequestId' # * 'RequestId'
def get_attributes(domain_name, item_name, attributes = nil) def get_attributes(domain_name, item_name, attributes = {})
request({ request({
'Action' => 'GetAttributes', 'Action' => 'GetAttributes',
'DomainName' => domain_name, 'DomainName' => domain_name,
@ -33,27 +35,21 @@ unless Fog.mocking?
end end
end end
end
end
else class Mock
module Fog
module AWS
class SimpleDB
def get_attributes(domain_name, item_name, attributes = nil) def get_attributes(domain_name, item_name, attributes = nil)
response = Excon::Response.new response = Excon::Response.new
if Fog::AWS::SimpleDB.data[:domains][domain_name] if @data[:domains][domain_name]
object = {} object = {}
if attributes if attributes
for attribute in attributes for attribute in attributes
if Fog::AWS::SimpleDB.data[:domains][domain_name][item_name] && Fog::AWS::SimpleDB.data[:domains][domain_name][item_name] if @data[:domains][domain_name][item_name] && @data[:domains][domain_name][item_name]
object[attribute] = Fog::AWS::SimpleDB.data[:domains][domain_name][item_name][attribute] object[attribute] = @data[:domains][domain_name][item_name][attribute]
end end
end end
elsif Fog::AWS::SimpleDB.data[:domains][domain_name][item_name] elsif @data[:domains][domain_name][item_name]
object = Fog::AWS::SimpleDB.data[:domains][domain_name][item_name] object = @data[:domains][domain_name][item_name]
end end
response.status = 200 response.status = 200
response.body = { response.body = {
@ -71,5 +67,4 @@ else
end end
end end
end end
end end

View file

@ -1,8 +1,9 @@
unless Fog.mocking? module Fog
module AWS
module SimpleDB
class Real
module Fog require 'fog/aws/parsers/simpledb/list_domains'
module AWS
class SimpleDB
# List SimpleDB domains # List SimpleDB domains
# #
@ -26,22 +27,16 @@ unless Fog.mocking?
end end
end end
end
end
else class Mock
module Fog
module AWS
class SimpleDB
def list_domains(options = {}) def list_domains(options = {})
response = Excon::Response.new response = Excon::Response.new
keys = Fog::AWS::SimpleDB.data[:domains].keys keys = @data[:domains].keys
max = options['MaxNumberOfDomains'] || keys.size max = options['MaxNumberOfDomains'] || keys.size
offset = options['NextToken'] || 0 offset = options['NextToken'] || 0
domains = [] domains = []
for key, value in Fog::AWS::SimpleDB.data[:domains].keys[offset...max] for key, value in @data[:domains].keys[offset...max]
domains << key domains << key
end end
response.status = 200 response.status = 200
@ -59,5 +54,4 @@ else
end end
end end
end end
end end

View file

@ -1,8 +1,7 @@
unless Fog.mocking? module Fog
module AWS
module Fog module SimpleDB
module AWS class Real
class SimpleDB
# Put item attributes into a SimpleDB domain # Put item attributes into a SimpleDB domain
# #
@ -27,14 +26,8 @@ unless Fog.mocking?
end end
end end
end
end
else class Mock
module Fog
module AWS
class SimpleDb
def put_attributes(domain_name, item_name, attributes, replace_attributes = []) def put_attributes(domain_name, item_name, attributes, replace_attributes = [])
batch_put_attributes(domain_name, { item_name => attributes }, { item_name => replace_attributes }) batch_put_attributes(domain_name, { item_name => attributes }, { item_name => replace_attributes })
@ -43,5 +36,4 @@ else
end end
end end
end end
end end

View file

@ -1,8 +1,9 @@
unless Fog.mocking? module Fog
module AWS
module SimpleDB
class Real
module Fog require 'fog/aws/parsers/simpledb/select'
module AWS
class SimpleDB
# Select item data from SimpleDB # Select item data from SimpleDB
# #
@ -27,14 +28,8 @@ unless Fog.mocking?
end end
end end
end
end
else class Mock
module Fog
module AWS
class SimpleDB
def select(select_expression, next_token = nil) def select(select_expression, next_token = nil)
raise MockNotImplemented.new("Contributions welcome!") raise MockNotImplemented.new("Contributions welcome!")
@ -43,5 +38,4 @@ else
end end
end end
end end
end end

View file

@ -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 Fog
module AWS module AWS
class SimpleDB module SimpleDB
if Fog.mocking? def self.new(options={})
def self.reset_data 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 => {} } @data = { :domains => {} }
end end
def self.data
@data
end
end
def self.dependencies def initialize(options={})
[
"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?
reset_data reset_data
end end
end end
# Initialize connection to SimpleDB class Real
#
# ==== 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
private # Initialize connection to SimpleDB
#
def encode_attributes(attributes, replace_attributes = []) # ==== Notes
encoded_attributes = {} # options parameter must include values for :aws_access_key_id and
if attributes # :aws_secret_access_key in order to create a connection
index = 0 #
for key in attributes.keys # ==== Examples
for value in Array(attributes[key]) # sdb = SimpleDB.new(
encoded_attributes["Attribute.#{index}.Name"] = key.to_s # :aws_access_key_id => your_aws_access_key_id,
if replace_attributes.include?(key) # :aws_secret_access_key => your_aws_secret_access_key
encoded_attributes["Attribute.#{index}.Replace"] = 'true' # )
end #
encoded_attributes["Attribute.#{index}.Value"] = sdb_encode(value) # ==== Parameters
index += 1 # * options<~Hash> - config arguments for connection. Defaults to {}.
end #
end # ==== 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 end
encoded_attributes
end
def encode_attribute_names(attributes) private
AWS.indexed_param('AttributeName', attributes.map {|attribute| attributes.to_s})
end
def encode_batch_attributes(items, replace_attributes = Hash.new([])) def encode_attributes(attributes, replace_attributes = [])
encoded_attributes = {} encoded_attributes = {}
if items if attributes
item_index = 0 index = 0
for item_key in items.keys for key in attributes.keys
encoded_attributes["Item.#{item_index}.ItemName"] = item_key.to_s for value in Array(attributes[key])
for attribute_key in items[item_key].keys encoded_attributes["Attribute.#{index}.Name"] = key.to_s
attribute_index = 0 if replace_attributes.include?(key)
for value in Array(items[item_key][attribute_key]) encoded_attributes["Attribute.#{index}.Replace"] = 'true'
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 end
encoded_attributes["Item.#{item_index}.Attribute.#{attribute_index}.Value"] = sdb_encode(value) encoded_attributes["Attribute.#{index}.Value"] = sdb_encode(value)
attribute_index += 1 index += 1
end end
item_index += 1
end end
end end
encoded_attributes
end end
encoded_attributes
end
def request(params, parser) def encode_attribute_names(attributes)
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}") AWS.indexed_param('AttributeName', attributes.map {|attribute| attributes.to_s})
params.merge!({ end
'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 = '' def encode_batch_attributes(items, replace_attributes = Hash.new([]))
for key in params.keys.sort encoded_attributes = {}
unless (value = params[key]).nil? if items
body << "#{key}=#{CGI.escape(value.to_s).gsub(/\+/, '%20')}&" 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
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 end
def sdb_encode(value)
if value.nil?
@nil_string
else
value.to_s
end
end
end end
end end
end end
Fog::AWS::SimpleDB.reload