Adding describe table end point

This commit is contained in:
zmsmith 2013-02-19 20:39:04 -05:00
parent 25734f0c85
commit 7e3aa7c8ee
4 changed files with 99 additions and 3 deletions

View File

@ -1,4 +1,47 @@
import datetime
from moto.core import BaseBackend
from .utils import unix_time
class Table(object):
def __init__(self, name, hash_key_attr=None, hash_key_type=None,
range_key_attr=None, range_key_type=None, read_capacity=None,
write_capacity=None):
self.name = name
self.hash_key_attr = hash_key_attr
self.hash_key_type = hash_key_type
self.range_key_attr = range_key_attr
self.range_key_type = range_key_type
self.read_capacity = read_capacity
self.write_capacity = write_capacity
self.created_at = datetime.datetime.now()
@property
def describe(self):
return {"Table": {
"CreationDateTime": unix_time(self.created_at),
"KeySchema": {
"HashKeyElement": {
"AttributeName": self.hash_key_attr,
"AttributeType": self.hash_key_type
},
"RangeKeyElement": {
"AttributeName": self.range_key_attr,
"AttributeType": self.range_key_type
}
},
"ProvisionedThroughput": {
"ReadCapacityUnits": self.read_capacity,
"WriteCapacityUnits": self.write_capacity
},
"TableName": self.name,
"TableStatus": "ACTIVE",
"ItemCount": 0,
"TableSizeBytes": 0,
}
}
class DynamoDBBackend(BaseBackend):
@ -6,7 +49,7 @@ class DynamoDBBackend(BaseBackend):
def __init__(self):
self.tables = {}
def create_table(self, name):
self.tables[name] = None
def create_table(self, name, **params):
self.tables[name] = Table(name, **params)
dynamodb_backend = DynamoDBBackend()

View File

@ -1,5 +1,6 @@
import re
import json
from .models import dynamodb_backend
@ -28,6 +29,11 @@ class DynamoHandler(object):
response = {"TableNames": tables}
return json.dumps(response)
def DescribeTable(self, uri, body, headers):
name = json.loads(body)['TableName']
table = dynamodb_backend.tables[name]
return json.dumps(table.describe)
def handler(uri, body, headers):
return DynamoHandler(uri, body, headers).dispatch()

6
moto/dynamodb/utils.py Normal file
View File

@ -0,0 +1,6 @@
import datetime
def unix_time(dt):
epoch = datetime.datetime.utcfromtimestamp(0)
delta = dt - epoch
return delta.total_seconds()

View File

@ -1,12 +1,53 @@
import boto
from freezegun import freeze_time
from moto import mock_dynamodb
from moto.dynamodb import dynamodb_backend
@mock_dynamodb
def test_list_tables():
name = "TestTable"
name = 'TestTable'
dynamodb_backend.create_table(name)
conn = boto.connect_dynamodb('the_key', 'the_secret')
assert conn.list_tables() == ['TestTable']
@freeze_time("2012-01-14")
@mock_dynamodb
def test_describe_table():
dynamodb_backend.create_table('messages',
hash_key_attr='forum_name',
hash_key_type='S',
range_key_attr='subject',
range_key_type='S',
read_capacity=10,
write_capacity=10,
)
conn = boto.connect_dynamodb('the_key', 'the_secret')
expected = {
'Table': {
'CreationDateTime': 1326499200.0,
'ItemCount': 0,
'KeySchema': {
'HashKeyElement': {
'AttributeName': 'forum_name',
'AttributeType': 'S'
},
'RangeKeyElement': {
'AttributeName': 'subject',
'AttributeType': 'S'
}
},
'ProvisionedThroughput': {
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
},
'TableName': 'messages',
'TableSizeBytes': 0,
'TableStatus': 'ACTIVE'
}
}
import pdb; pdb.set_trace()
assert conn.describe_table('messages') == expected