]> git.cworth.org Git - turbot/blobdiff - turbot/interaction.py
Create the hunts table if it doesn't already exist
[turbot] / turbot / interaction.py
index 947a01f67a48fee942f672551bd03f196dcdad2e..f634cbcdadf6120af0787f194883a573eb2cbdf8 100644 (file)
@@ -6,6 +6,7 @@ import turbot.slack
 import json
 import re
 import requests
+from botocore.exceptions import ClientError
 
 TURBOT_USER_ID = 'U01B9QM4P9R'
 
@@ -95,6 +96,33 @@ def new_hunt_submission(turb, payload, metadata):
                                 "Hunt ID can only contain letters, "
                                 + "numbers, and underscores")
 
+    # Check to see if the hunts table exists
+    hunts_table = turb.db.Table("hunts")
+
+    try:
+        exists = hunts_table.table_status in ("CREATING", "UPDATING",
+                                              "ACTIVE")
+    except ClientError:
+        exists = False
+
+    # Create the hunts table if necessary.
+    if not exists:
+        hunts_table = turb.db.create_table(
+            TableName='hunts',
+            KeySchema=[
+                {'AttributeName': 'channel_id', 'KeyType': 'HASH'},
+            ],
+            AttributeDefinitions=[
+                {'AttributeName': 'channel_id', 'AttributeType': 'S'},
+            ],
+            ProvisionedThroughput={
+                'ReadCapacityUnits': 5,
+                'WriteCapacityUnits': 5
+            }
+        )
+        return submission_error("hunt_id",
+                                "Still bootstrapping hunts table. Try again.")
+
     # Create a channel for the hunt
     try:
         response = turb.slack_client.conversations_create(name=hunt_id)
@@ -114,8 +142,9 @@ def new_hunt_submission(turb, payload, metadata):
     # Create a sheet for the channel
     sheet = turbot.sheets.sheets_create(turb, hunt_id)
 
+    channel_id = response['channel']['id']
+
     # Insert the newly-created hunt into the database
-    hunts_table = turb.db.Table("hunts")
     hunts_table.put_item(
         Item={
             'channel_id': channel_id,