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

[AWS|DynamoDB] item requests

This commit is contained in:
geemus 2012-01-22 17:12:07 -06:00
parent 54036aeb1d
commit bf0c720370
7 changed files with 321 additions and 5 deletions

View file

@ -8,17 +8,17 @@ module Fog
recognizes :aws_session_token, :host, :path, :port, :scheme, :persistent, :region
request_path 'fog/aws/requests/dynamodb'
#request :batch_get_item
request :batch_get_item
request :create_table
#request :delete_item
request :delete_item
request :delete_table
request :describe_table
#request :get_item
request :get_item
request :list_tables
#request :put_item
request :put_item
#request :query
#request :scan
#request :update_item
request :update_item
request :update_table
class Mock

View file

@ -0,0 +1,43 @@
module Fog
module AWS
class DynamoDB
class Real
# Get DynamoDB items
#
# ==== Parameters
# * 'request_items'<~Hash>:
# * 'table_name'<~Hash>:
# * 'Keys'<~Array>: array of keys
# * 'HashKeyElement'<~Hash>: info for primary key
# * 'AttributeType'<~String> - type of attribute
# * 'AttributeName'<~String> - name of attribute
# * 'RangeKeyElement'<~Hash>: optional, info for range key
# * 'AttributeType'<~String> - type of attribute
# * 'AttributeName'<~String> - name of attribute
# * 'AttributesToGet'<~Array> - optional attributes to return, defaults to all
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'Responses'<~Hash>:
# * 'table_name'<~Hash>:
# * 'Items'<~Array> - Matching items
# * 'ConsumedCapacityUnits'<~Float> - Capacity units used in read
# * 'UnprocessedKeys':<~Hash> - tables and keys in excess of per request limit, pass this to subsequent batch get for pseudo-pagination
def batch_get_item(request_items)
body = {
'RequestItems' => request_items
}
request(
:body => MultiJson.encode(body),
:headers => {'x-amz-target' => 'DynamoDB_20111205.BatchGetItem'},
:idempotent => true
)
end
end
end
end
end

View file

@ -0,0 +1,45 @@
module Fog
module AWS
class DynamoDB
class Real
# Delete DynamoDB item
#
# ==== Parameters
# * 'table_name'<~String> - name of table for item
# * 'key'<~Hash>:
# * 'HashKeyElement'<~Hash>: info for primary key
# * 'AttributeName'<~String> - name of attribute
# * 'AttributeType'<~String> - type of attribute
# * 'RangeKeyElement'<~Hash>: optional, info for range key
# * 'AttributeName'<~String> - name of attribute
# * 'AttributeType'<~String> - type of attribute
# * 'options'<~Hash>:
# * 'Expected'<~Hash>: data to check against
# * 'AttributeName'<~String> - name of attribute
# * 'Value'<~Hash> - a value to check for the value of
# or
# * 'Exists'<~Boolean> - set as false to only allow update if attribute doesn't exist
# * 'ReturnValues'<~String> - data to return in %w{ALL_NEW ALL_OLD NONE UPDATED_NEW UPDATED_OLD}, defaults to NONE
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# varies based on ReturnValues param, see: http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/API_UpdateItem.html
def delete_item(table_name, key, options = {})
body = {
'Key' => key,
'TableName' => table_name
}.merge(options)
request(
:body => MultiJson.encode(body),
:headers => {'x-amz-target' => 'DynamoDB_20111205.DeleteItem'},
:idempotent => true
)
end
end
end
end
end

View file

@ -0,0 +1,43 @@
module Fog
module AWS
class DynamoDB
class Real
# Get DynamoDB item
#
# ==== Parameters
# * 'table_name'<~String> - name of table for item
# * 'key'<~Hash>:
# * 'HashKeyElement'<~Hash>: info for primary key
# * 'AttributeType'<~String> - type of attribute
# * 'AttributeName'<~String> - name of attribute
# * 'RangeKeyElement'<~Hash>: optional, info for range key
# * 'AttributeType'<~String> - type of attribute
# * 'AttributeName'<~String> - name of attribute
# * 'options'<~Hash>:
# * 'AttributesToGet'<~Array>: list of array names to return, defaults to returning all
# * 'ConsistentRead'<~Boolean>: whether to wait for updates, defaults to false
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'ConsumedCapacityUnits'<~Float> - Capacity units used in read
# * 'Item':<~Hash>:
# * 'AttributeName'<~Hash>: in form of {"type":value}
def get_item(table_name, key, options = {})
body = {
'Key' => key,
'TableName' => table_name
}.merge(options)
request(
:body => MultiJson.encode(body),
:headers => {'x-amz-target' => 'DynamoDB_20111205.GetItem'},
:idempotent => true
)
end
end
end
end
end

View file

@ -0,0 +1,42 @@
module Fog
module AWS
class DynamoDB
class Real
# Update DynamoDB item
#
# ==== Parameters
# * 'table_name'<~String> - name of table for item
# * 'item'<~Hash>: data to update, must include primary key
# * 'AttributeName'<~String> - Attribute to update
# * 'Value'<~Hash> - formated as {type => value}
# * 'Action'<~String> - action to take if expects matches, in %w{ADD DELETE PUT}, defaults to PUT
# * 'options'<~Hash>:
# * 'Expected'<~Hash>: data to check against
# * 'AttributeName'<~String> - name of attribute
# * 'Value'<~Hash> - a value to check for the value of
# or
# * 'Exists'<~Boolean> - set as false to only allow update if attribute doesn't exist
# * 'ReturnValues'<~String> - data to return in %w{ALL_NEW ALL_OLD NONE UPDATED_NEW UPDATED_OLD}, defaults to NONE
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# varies based on ReturnValues param, see: http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/API_UpdateItem.html
def put_item(table_name, item, options = {})
body = {
'Item' => item,
'TableName' => table_name
}.merge(options)
request(
:body => MultiJson.encode(body),
:headers => {'x-amz-target' => 'DynamoDB_20111205.PutItem'},
:idempotent => true
)
end
end
end
end
end

View file

@ -0,0 +1,50 @@
module Fog
module AWS
class DynamoDB
class Real
# Update DynamoDB item
#
# ==== Parameters
# * 'table_name'<~String> - name of table for item
# * 'key'<~Hash>:
# * 'HashKeyElement'<~Hash>: info for primary key
# * 'AttributeName'<~String> - name of attribute
# * 'AttributeType'<~String> - type of attribute
# * 'RangeKeyElement'<~Hash>: optional, info for range key
# * 'AttributeName'<~String> - name of attribute
# * 'AttributeType'<~String> - type of attribute
# * 'attribute_updates'<~Hash>:
# * 'AttributeName'<~String> - Attribute to update
# * 'Value'<~Hash> - formated as {type => value}
# * 'Action'<~String> - action to take if expects matches, in %w{ADD DELETE PUT}, defaults to PUT
# * 'options'<~Hash>:
# * 'Expected'<~Hash>: data to check against
# * 'AttributeName'<~String> - name of attribute
# * 'Value'<~Hash> - a value to check for the value of
# or
# * 'Exists'<~Boolean> - set as false to only allow update if attribute doesn't exist
# * 'ReturnValues'<~String> - data to return in %w{ALL_NEW ALL_OLD NONE UPDATED_NEW UPDATED_OLD}, defaults to NONE
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# varies based on ReturnValues param, see: http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/API_UpdateItem.html
def update_item(table_name, key, attribute_updates, options = {})
body = {
'AttributeUpdates' => attribute_updates,
'Key' => key,
'TableName' => table_name
}.merge(options)
request(
:body => MultiJson.encode(body),
:headers => {'x-amz-target' => 'DynamoDB_20111205.UpdateItem'},
:idempotent => true
)
end
end
end
end
end

View file

@ -0,0 +1,93 @@
Shindo.tests('Fog::AWS[:dynamodb] | item requests', ['aws']) do
@table_name = "fog_table_#{Time.now.to_f.to_s.gsub('.','')}"
unless Fog.mocking?
Fog::AWS[:dynamodb].create_table(
@table_name,
{'HashKeyElement' => {'AttributeName' => 'key', 'AttributeType' => 'S'}},
{'ReadCapacityUnits' => 5, 'WriteCapacityUnits' => 5}
)
Fog.wait_for { Fog::AWS[:dynamodb].describe_table(@table_name).body['Table']['TableStatus'] == 'ACTIVE' }
end
tests('success') do
tests("#put_item('#{@table_name}', {'key' => {'S' => 'key'}}, {'value' => {'S' => 'value'}})").formats('ConsumedCapacityUnits' => Float) do
pending if Fog.mocking?
Fog::AWS[:dynamodb].put_item(@table_name, {'key' => {'S' => 'key'}}, {'value' => {'S' => 'value'}}).body
end
tests("#update_item('#{@table_name}', {'HashKeyElement' => {'S' => 'key'}}, {'value' => {'Value' => {'S' => 'value'}}})").formats('ConsumedCapacityUnits' => Float) do
pending if Fog.mocking?
Fog::AWS[:dynamodb].update_item(@table_name, {'HashKeyElement' => {'S' => 'key'}}, {'value' => {'Value' => {'S' => 'value'}}}).body
end
@batch_get_item_format = {
'Responses' => {
@table_name => {
'ConsumedCapacityUnits' => Float,
'Items' => [{
'key' => { 'S' => String },
'value' => { 'S' => String }
}]
}
},
'UnprocessedKeys' => {}
}
tests("#batch_get_item({'#{@table_name}' => {'Keys' => [{'HashKeyElement' => {'S' => 'key'}}]}})").formats(@batch_get_item_format) do
Fog::AWS[:dynamodb].batch_get_item(
{@table_name => {'Keys' => [{'HashKeyElement' => {'S' => 'key'}}]}}
).body
end
@get_item_format = {
'ConsumedCapacityUnits' => Float,
'Item' => {
'key' => { 'S' => String },
'value' => { 'S' => String }
}
}
tests("#get_item('#{@table_name}', {'HashKeyElement' => {'S' => 'key'}})").formats(@get_item_format) do
pending if Fog.mocking?
Fog::AWS[:dynamodb].get_item(@table_name, {'HashKeyElement' => {'S' => 'key'}}).body
end
tests("#get_item('#{@table_name}', {'HashKeyElement' => {'S' => 'notakey'}})").formats('ConsumedCapacityUnits' => Float) do
pending if Fog.mocking?
Fog::AWS[:dynamodb].get_item(@table_name, {'HashKeyElement' => {'S' => 'notakey'}}).body
end
tests("#delete_item('#{@table_name}', {'HashKeyElement' => {'S' => 'key'}})").formats('ConsumedCapacityUnits' => Float) do
pending if Fog.mocking?
Fog::AWS[:dynamodb].delete_item(@table_name, {'HashKeyElement' => {'S' => 'key'}}).body
end
tests("#delete_item('#{@table_name}, {'HashKeyElement' => {'S' => 'key'}})").formats('ConsumedCapacityUnits' => Float) do
pending if Fog.mocking?
Fog::AWS[:dynamodb].delete_item(@table_name, {'HashKeyElement' => {'S' => 'key'}}).body
end
end
tests('failure') do
tests("#put_item('notatable', {'key' => {'S' => 'key'}}, {'value' => {'S' => 'value'}})").raises(Excon::Errors::BadRequest) do
pending if Fog.mocking?
Fog::AWS[:dynamodb].put_item('notatable', {'key' => {'S' => 'key'}}, {'value' => {'S' => 'value'}})
end
tests("#update_item('notatable', {'HashKeyElement' => {'S' => 'key'}}, {'value' => {'Value' => {'S' => 'value'}}})").raises(Excon::Errors::BadRequest) do
pending if Fog.mocking?
Fog::AWS[:dynamodb].update_item('notatable', {'HashKeyElement' => {'S' => 'key'}}, {'value' => {'Value' => {'S' => 'value'}}})
end
end
unless Fog.mocking?
Fog::AWS[:dynamodb].delete_table(@table_name)
end
end