Encapsulates scaffolding functions to deploy and destroy resources used by the demo.
| 15 | |
| 16 | |
| 17 | class Scaffold: |
| 18 | """ |
| 19 | Encapsulates scaffolding functions to deploy and destroy resources used by the demo. |
| 20 | """ |
| 21 | |
| 22 | def __init__(self, dyn_resource): |
| 23 | """ |
| 24 | :param dyn_resource: A Boto3 DynamoDB resource. |
| 25 | """ |
| 26 | self.dyn_resource = dyn_resource |
| 27 | self.table = None |
| 28 | |
| 29 | def create_table(self, table_name): |
| 30 | """ |
| 31 | Creates a DynamoDB table that can be used to store movie data. |
| 32 | The table uses the release year of the movie as the partition key and the |
| 33 | title as the sort key. |
| 34 | |
| 35 | :param table_name: The name of the table to create. |
| 36 | :return: The newly created table. |
| 37 | """ |
| 38 | try: |
| 39 | self.table = self.dyn_resource.create_table( |
| 40 | TableName=table_name, |
| 41 | KeySchema=[ |
| 42 | {"AttributeName": "year", "KeyType": "HASH"}, # Partition key |
| 43 | {"AttributeName": "title", "KeyType": "RANGE"}, # Sort key |
| 44 | ], |
| 45 | AttributeDefinitions=[ |
| 46 | {"AttributeName": "year", "AttributeType": "N"}, |
| 47 | {"AttributeName": "title", "AttributeType": "S"}, |
| 48 | ], |
| 49 | BillingMode='PAY_PER_REQUEST', |
| 50 | ) |
| 51 | self.table.wait_until_exists() |
| 52 | except ClientError as err: |
| 53 | if err.response["Error"]["Code"] == "ResourceInUseException": |
| 54 | logger.info("Table %s already exists.", table_name) |
| 55 | else: |
| 56 | logger.error( |
| 57 | "Couldn't create table %s. Here's why: %s: %s", |
| 58 | table_name, |
| 59 | err.response["Error"]["Code"], |
| 60 | err.response["Error"]["Message"], |
| 61 | ) |
| 62 | raise |
| 63 | |
| 64 | def delete_table(self): |
| 65 | """ |
| 66 | Deletes a table, if one was created for the demo. |
| 67 | """ |
| 68 | try: |
| 69 | if self.table is not None: |
| 70 | self.table.delete() |
| 71 | self.table = None |
| 72 | else: |
| 73 | logger.warning( |
| 74 | "Not deleting table because it wasn't created by this demo." |
no outgoing calls