forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscaffold.py
More file actions
82 lines (71 loc) · 2.64 KB
/
scaffold.py
File metadata and controls
82 lines (71 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Scaffolding for the Amazon DynamoDB PartiQL scenarios.
* Creates and deletes a DynamoDB movie table.
"""
import logging
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
class Scaffold:
"""
Encapsulates scaffolding functions to deploy and destroy resources used by the demo.
"""
def __init__(self, dyn_resource):
"""
:param dyn_resource: A Boto3 DynamoDB resource.
"""
self.dyn_resource = dyn_resource
self.table = None
def create_table(self, table_name):
"""
Creates a DynamoDB table that can be used to store movie data.
The table uses the release year of the movie as the partition key and the
title as the sort key.
:param table_name: The name of the table to create.
:return: The newly created table.
"""
try:
self.table = self.dyn_resource.create_table(
TableName=table_name,
KeySchema=[
{"AttributeName": "year", "KeyType": "HASH"}, # Partition key
{"AttributeName": "title", "KeyType": "RANGE"}, # Sort key
],
AttributeDefinitions=[
{"AttributeName": "year", "AttributeType": "N"},
{"AttributeName": "title", "AttributeType": "S"},
],
BillingMode='PAY_PER_REQUEST',
)
self.table.wait_until_exists()
except ClientError as err:
if err.response["Error"]["Code"] == "ResourceInUseException":
logger.info("Table %s already exists.", table_name)
else:
logger.error(
"Couldn't create table %s. Here's why: %s: %s",
table_name,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
def delete_table(self):
"""
Deletes a table, if one was created for the demo.
"""
try:
if self.table is not None:
self.table.delete()
self.table = None
else:
logger.warning(
"Not deleting table because it wasn't created by this demo."
)
except ClientError as err:
logger.error(
"Couldn't delete table. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise