"""DynamoDB Table Schemas

Tables defined in this module are based on the tables
described here:  http://amzn.to/1OD8zKf

The table specifications are hard-coded other than
the read capacity and write capacity

For more details on provisioned throughput:
    http://amzn.to/1OD7vFZ

"""

import time

import botocore


def create_all(client):
    """Create all tables and wait for ACTIVE."""
    tables = [
        thread,
        reply,
        forum,
    ]
    responses = []
    for table in tables:
        kwargs = table()
        try:
            response = client.create_table(**kwargs)
        except botocore.exceptions.ClientError as err:
            if 'table already exists' in repr(err).lower():
                response = err
            else:
                raise
        responses.append(response)

    for response in responses:
        if isinstance(response, botocore.exceptions.ClientError):
            continue
        while response.table_status != 'ACTIVE':
            response.load()
            time.sleep(.5)
    return responses


def delete_all(client):

    table_names = [
        'Reply',
        'Thread',
        'Forum',
    ]
    responses = []
    for name in table_names:
        response = client.Table(name).delete()
        responses.append(response)
    return responses


def thread(read=20, write=20):
    """Return the kwargs for creating the Thread table."""
    return {
        'AttributeDefinitions': [
            {
                'AttributeName': 'ForumName',
                'AttributeType': 'S'
            },
            {
                'AttributeName': 'Subject',
                'AttributeType': 'S'
            },
        ],
        'TableName': 'Thread',
        'KeySchema': [
            {
                'AttributeName': 'ForumName',
                'KeyType': 'HASH',
            },
            {
                'AttributeName': 'Subject',
                'KeyType': 'RANGE',
            },
        ],
        'ProvisionedThroughput': {
            'ReadCapacityUnits': read,
            'WriteCapacityUnits': write,
        }
    }


def reply(read=20, write=20):
    """Return the kwargs for creating the Reply table."""
    return {
        'AttributeDefinitions': [
            {
                'AttributeName': 'Id',
                'AttributeType': 'S'
            },
            {
                'AttributeName': 'ReplyDateTime',
                'AttributeType': 'S'
            },
            {
                'AttributeName': 'PostedBy',
                'AttributeType': 'S'
            },
            {
                'AttributeName': 'Message',
                'AttributeType': 'S'
            },
        ],
        'TableName': 'Reply',
        'KeySchema': [
            {
                'AttributeName': 'Id',
                'KeyType': 'HASH',
            },
            {
                'AttributeName': 'ReplyDateTime',
                'KeyType': 'RANGE',
            },
        ],
        'ProvisionedThroughput': {
            'ReadCapacityUnits': read,
            'WriteCapacityUnits': write,
        },
        'GlobalSecondaryIndexes': [
            {
                'IndexName': 'PostedBy-Message-Index',
                'KeySchema': [
                    {
                        'AttributeName': 'PostedBy',
                        'KeyType': 'HASH'
                    },
                    {
                        'AttributeName': 'Message',
                        'KeyType': 'RANGE'
                    },
                ],
                'Projection': {
                    'ProjectionType': 'ALL',
                },
                'ProvisionedThroughput': {
                    'ReadCapacityUnits': read,
                    'WriteCapacityUnits': write,
                }
            },
        ]
    }


def forum(read=20, write=20):
    """Return the kwargs for creating the Forum table."""
    return {
        'AttributeDefinitions': [
            {
                'AttributeName': 'Name',
                'AttributeType': 'S'
            },
        ],
        'TableName': 'Forum',
        'KeySchema': [
            {
                'AttributeName': 'Name',
                'KeyType': 'HASH',
            },
        ],
        'ProvisionedThroughput': {
            'ReadCapacityUnits': read,
            'WriteCapacityUnits': write,
        }
    }

@@ -280,16 +280,65 @@
 FZ%0A%0A%22%22%22%0A
+from __future__ import print_function%0A%0Aimport sys
 %0Aimport 
@@ -858,16 +858,41 @@
 ponse)%0A%0A
+    # wait until created%0A
     for 
@@ -1106,9 +1106,9 @@
 ep(.
-5
+1
 )%0A  
@@ -1242,32 +1242,48 @@
  responses = %5B%5D%0A
+    tables = %5B%5D%0A
     for name in 
@@ -1295,39 +1295,53 @@
 _names:%0A        
-respons
+try:%0A            tabl
 e = client.Table
@@ -1350,52 +1350,730 @@
 ame)
-.delete()%0A        responses.append(response)
+%0A            response = table.delete()%0A        except botocore.exceptions.ClientError as err:%0A            if 'resource not found' in repr(err).lower():%0A                response = err%0A            else:%0A                raise%0A        else:%0A            # dont append the table if we hit a ClientError%0A            tables.append(table)%0A        responses.append(response)%0A%0A    # wait until deleted%0A    for table in tables:%0A        while True:%0A            try:%0A                status = table.table_status%0A                table.load()%0A            except botocore.exceptions.ClientError as err:%0A                if 'resource not found' in repr(err).lower():%0A                    break%0A            else:%0A                time.sleep(.1)%0A
 %0A   

update table ops