diff --git a/lib/fog/aws/dynamodb.rb b/lib/fog/aws/dynamodb.rb index 00343dfa6..787c8acae 100644 --- a/lib/fog/aws/dynamodb.rb +++ b/lib/fog/aws/dynamodb.rb @@ -16,8 +16,8 @@ module Fog request :get_item request :list_tables request :put_item - #request :query - #request :scan + request :query + request :scan request :update_item request :update_table diff --git a/lib/fog/aws/requests/dynamodb/query.rb b/lib/fog/aws/requests/dynamodb/query.rb new file mode 100644 index 000000000..d45fd6a78 --- /dev/null +++ b/lib/fog/aws/requests/dynamodb/query.rb @@ -0,0 +1,44 @@ +module Fog + module AWS + class DynamoDB + class Real + + # Query DynamoDB items + # + # ==== Parameters + # * 'table_name'<~String> - name of table to query + # * 'hash_key'<~Hash> - hash key to query + # * options<~Hash>: + # * 'AttributesToGet'<~Array> - Array of attributes to get for each item, defaults to all + # * 'ConsistentRead'<~Boolean> - Whether to wait for consistency, defaults to false + # * 'Count'<~Boolean> - If true, returns only a count of such items rather than items themselves, defaults to false + # * 'Limit'<~Integer> - limit of total items to return + # * 'RangeKeyCondition'<~Hash>: value to compare against range key + # * 'AttributeValueList'<~Hash>: one or more values to compare against + # * 'ComparisonOperator'<~String>: comparison operator to use with attribute value list, in %w{BETWEEN BEGINS_WITH EQ LE LT GE GT} + # * 'ScanIndexForward'<~Boolean>: Whether to scan from start or end of index, defaults to start + # * 'ExclusiveStartKey'<~Hash>: Key to start listing from, can be taken from LastEvaluatedKey in response + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'ConsumedCapacityUnits'<~Integer> - number of capacity units used for query + # * 'Count'<~Integer> - number of items in response + # * 'Items'<~Array> - array of items returned + # * 'LastEvaluatedKey'<~Hash> - last key scanned, can be passed to ExclusiveStartKey for pagination + def query(table_name, hash_key, options = {}) + body = { + 'TableName' => table_name, + 'HashKeyValue' => hash_key + }.merge(options) + + request( + :body => MultiJson.encode(body), + :headers => {'x-amz-target' => 'DynamoDB_20111205.Query'} + ) + end + + end + end + end +end diff --git a/lib/fog/aws/requests/dynamodb/scan.rb b/lib/fog/aws/requests/dynamodb/scan.rb new file mode 100644 index 000000000..a249b930f --- /dev/null +++ b/lib/fog/aws/requests/dynamodb/scan.rb @@ -0,0 +1,44 @@ +module Fog + module AWS + class DynamoDB + class Real + + # Scan DynamoDB items + # + # ==== Parameters + # * 'table_name'<~String> - name of table to query + # * options<~Hash>: + # * 'AttributesToGet'<~Array> - Array of attributes to get for each item, defaults to all + # * 'ConsistentRead'<~Boolean> - Whether to wait for consistency, defaults to false + # * 'Count'<~Boolean> - If true, returns only a count of such items rather than items themselves, defaults to false + # * 'Limit'<~Integer> - limit of total items to return + # * 'ScanFilter'<~Hash>: value to compare against + # * attribute_name<~Hash>: + # * 'AttributeValueList'<~Hash>: one or more values to compare against + # * 'ComparisonOperator'<~String>: comparison operator to use with attribute value list, in %w{BETWEEN BEGINS_WITH EQ LE LT GE GT} + # * 'ScanIndexForward'<~Boolean>: Whether to scan from start or end of index, defaults to start + # * 'ExclusiveStartKey'<~Hash>: Key to start listing from, can be taken from LastEvaluatedKey in response + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'ConsumedCapacityUnits'<~Integer> - number of capacity units used for scan + # * 'Count'<~Integer> - number of items in response + # * 'Items'<~Array> - array of items returned + # * 'LastEvaluatedKey'<~Hash> - last key scanned, can be passed to ExclusiveStartKey for pagination + # * 'ScannedCount'<~Integer> - number of items scanned before applying filters + def scan(table_name, options = {}) + body = { + 'TableName' => table_name + }.merge(options) + + request( + :body => MultiJson.encode(body), + :headers => {'x-amz-target' => 'DynamoDB_20111205.Scan'} + ) + end + + end + end + end +end diff --git a/lib/fog/aws/requests/dynamodb/update_item.rb b/lib/fog/aws/requests/dynamodb/update_item.rb index 21e0ee060..6a2a95b37 100644 --- a/lib/fog/aws/requests/dynamodb/update_item.rb +++ b/lib/fog/aws/requests/dynamodb/update_item.rb @@ -37,12 +37,9 @@ module Fog 'TableName' => table_name }.merge(options) - idempotent = attribute_updates.has_key?('Action') - request( - :body => MultiJson.encode(body), - :headers => {'x-amz-target' => 'DynamoDB_20111205.UpdateItem'}, - :idempotent => idempotent + :body => MultiJson.encode(body), + :headers => {'x-amz-target' => 'DynamoDB_20111205.UpdateItem'} ) end diff --git a/tests/aws/requests/dynamodb/item_tests.rb b/tests/aws/requests/dynamodb/item_tests.rb index ca79f1f95..afc34ed66 100644 --- a/tests/aws/requests/dynamodb/item_tests.rb +++ b/tests/aws/requests/dynamodb/item_tests.rb @@ -60,6 +60,29 @@ Shindo.tests('Fog::AWS[:dynamodb] | item requests', ['aws']) do Fog::AWS[:dynamodb].get_item(@table_name, {'HashKeyElement' => {'S' => 'notakey'}}).body end + @query_format = { + 'ConsumedCapacityUnits' => Integer, + 'Count' => Integer, + 'Items' => [{ + 'key' => { 'S' => String }, + 'value' => { 'S' => String } + }], + 'LastEvaluatedKey' => NilClass + } + + tests("#query('#{@table_name}', {'S' => 'key'}").formats(@query_format) do + pending if Fog.mocking? + pending # requires a table with range key + Fog::AWS[:dynamodb].query(@table_name, {'S' => 'key'}).body + end + + @scan_format = @query_format.merge('ScannedCount' => Integer) + + tests("scan('#{@table_name}')").formats(@scan_format) do + pending if Fog.mocking? + Fog::AWS[:dynamodb].scan(@table_name).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 diff --git a/tests/vcloud/models/compute/conn_helper.rb b/tests/vcloud/models/compute/conn_helper.rb index 2472a76ff..165fb62c6 100644 --- a/tests/vcloud/models/compute/conn_helper.rb +++ b/tests/vcloud/models/compute/conn_helper.rb @@ -1,17 +1,19 @@ module Fog module Vcloud - class Fog::Connection - def request(params, &block) - path = File.expand_path(File.join(File.dirname(__FILE__),'..','..','data',params[:path].gsub(/^\//,'').gsub('/','_+_'))) - if File.exists?(path) - body = File.read(path) - else - '' + if false + class Fog::Connection + def request(params, &block) + path = File.expand_path(File.join(File.dirname(__FILE__),'..','..','data',params[:path].gsub(/^\//,'').gsub('/','_+_'))) + if File.exists?(path) + body = File.read(path) + else + '' + end + Excon::Response.new( + :body => body, + :status => 200, + :header => '') end - Excon::Response.new( - :body => body, - :status => 200, - :header => '') end end end