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 AWS
def self.dependencies
[
'fog/aws/ec2.rb',
'fog/aws/simpledb.rb'
'fog/aws/ec2.rb'
]
end

View file

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

View file

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

View file

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

View file

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

View file

@ -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

View file

@ -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

View file

@ -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
end

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

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 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