From 5086e6e590582d1e6a89cdcf39b5db9805749de9 Mon Sep 17 00:00:00 2001 From: zmsmith Date: Tue, 19 Feb 2013 08:26:05 -0500 Subject: [PATCH] Adding intitial DynamoDB setup and implementing ListTables target --- moto/__init__.py | 1 + moto/dynamodb/__init__.py | 2 ++ moto/dynamodb/models.py | 11 ++++++++++ moto/dynamodb/responses.py | 32 ++++++++++++++++++++++++++++ moto/dynamodb/urls.py | 5 +++++ tests/test_dynamodb/test_dynamodb.py | 12 +++++++++++ 6 files changed, 63 insertions(+) create mode 100644 moto/dynamodb/__init__.py create mode 100644 moto/dynamodb/models.py create mode 100644 moto/dynamodb/responses.py create mode 100644 moto/dynamodb/urls.py create mode 100644 tests/test_dynamodb/test_dynamodb.py diff --git a/moto/__init__.py b/moto/__init__.py index 8da1c85e7..511cd1d40 100644 --- a/moto/__init__.py +++ b/moto/__init__.py @@ -1,2 +1,3 @@ +from .dynamodb import mock_dynamodb from .ec2 import mock_ec2 from .s3 import mock_s3 diff --git a/moto/dynamodb/__init__.py b/moto/dynamodb/__init__.py new file mode 100644 index 000000000..3312203f5 --- /dev/null +++ b/moto/dynamodb/__init__.py @@ -0,0 +1,2 @@ +from .models import dynamodb_backend +mock_dynamodb = dynamodb_backend.decorator diff --git a/moto/dynamodb/models.py b/moto/dynamodb/models.py new file mode 100644 index 000000000..bb7b9d49a --- /dev/null +++ b/moto/dynamodb/models.py @@ -0,0 +1,11 @@ +from moto.core import BaseBackend + +class DynamoDBBackend(BaseBackend): + + def __init__(self): + self.tables = {} + + def create_table(self, name): + self.tables[name] = None + +dynamodb_backend = DynamoDBBackend() \ No newline at end of file diff --git a/moto/dynamodb/responses.py b/moto/dynamodb/responses.py new file mode 100644 index 000000000..980312519 --- /dev/null +++ b/moto/dynamodb/responses.py @@ -0,0 +1,32 @@ +import re +import json +from .models import dynamodb_backend + +class DynamoHandler(object): + + def __init__(self, uri, body, headers): + self.uri = uri + self.body = body + self.headers = headers + + def get_method_name(self, headers): + """Parses request headers and extracts part od the X-Amz-Target + that corresponds to a method of DynamoHandler + + ie: X-Amz-Target: DynamoDB_20111205.ListTables -> ListTables + """ + match = re.search(r'X-Amz-Target: \w+\.(\w+)', headers) + return match.groups()[0] + + def dispatch(self): + method = self.get_method_name(self.headers) + return getattr(self, method)(self.uri, self.body, self.headers) + + def ListTables(self, uri, body, headers): + tables = dynamodb_backend.tables.keys() + response = {"TableNames": tables} + return json.dumps(response) + + +def handler(uri, body, headers): + return DynamoHandler(uri, body, headers).dispatch() \ No newline at end of file diff --git a/moto/dynamodb/urls.py b/moto/dynamodb/urls.py new file mode 100644 index 000000000..a42f1ee92 --- /dev/null +++ b/moto/dynamodb/urls.py @@ -0,0 +1,5 @@ +from .responses import handler + +urls = { + "https://dynamodb.us-east-1.amazonaws.com/": handler, +} \ No newline at end of file diff --git a/tests/test_dynamodb/test_dynamodb.py b/tests/test_dynamodb/test_dynamodb.py new file mode 100644 index 000000000..219e8ad8e --- /dev/null +++ b/tests/test_dynamodb/test_dynamodb.py @@ -0,0 +1,12 @@ +import boto + +from moto import mock_dynamodb +from moto.dynamodb import dynamodb_backend + + +@mock_dynamodb +def test_create_table(): + name = "TestTable" + dynamodb_backend.create_table(name) + conn = boto.connect_dynamodb('the_key', 'the_secret') + assert conn.list_tables() == ['TestTable']