diff --git a/moto/dynamodb2/models/__init__.py b/moto/dynamodb2/models/__init__.py index 233c4001f..fc178dd4e 100644 --- a/moto/dynamodb2/models/__init__.py +++ b/moto/dynamodb2/models/__init__.py @@ -452,6 +452,17 @@ class Table(BaseModel): ) return table + @classmethod + def delete_from_cloudformation_json( + cls, resource_name, cloudformation_json, region_name + ): + properties = cloudformation_json["Properties"] + + table = dynamodb_backends[region_name].delete_table( + name=properties["TableName"] + ) + return table + def _generate_arn(self, name): return "arn:aws:dynamodb:us-east-1:123456789011:table/" + name @@ -902,6 +913,9 @@ class Table(BaseModel): return None return ret + def delete(self, region_name): + dynamodb_backends[region_name].delete_table(self.name) + class DynamoDBBackend(BaseBackend): def __init__(self, region_name=None): diff --git a/tests/test_cloudformation/test_cloudformation_stack_crud.py b/tests/test_cloudformation/test_cloudformation_stack_crud.py index 29faa11cf..d7e26e85d 100644 --- a/tests/test_cloudformation/test_cloudformation_stack_crud.py +++ b/tests/test_cloudformation/test_cloudformation_stack_crud.py @@ -4,6 +4,7 @@ import os import json import boto +import boto3 import boto.iam import boto.s3 import boto.s3.key @@ -21,6 +22,8 @@ from moto import ( mock_s3_deprecated, mock_route53_deprecated, mock_iam_deprecated, + mock_dynamodb2, + mock_cloudformation, ) from moto.cloudformation import cloudformation_backends @@ -45,6 +48,30 @@ dummy_template3 = { }, } +dummy_template4 = { + "AWSTemplateFormatVersion": "2010-09-09", + "Resources": { + "myDynamoDBTable": { + "Type": "AWS::DynamoDB::Table", + "Properties": { + "AttributeDefinitions": [ + {"AttributeName": "Name", "AttributeType": "S"}, + {"AttributeName": "Age", "AttributeType": "S"}, + ], + "KeySchema": [ + {"AttributeName": "Name", "KeyType": "HASH"}, + {"AttributeName": "Age", "KeyType": "RANGE"}, + ], + "ProvisionedThroughput": { + "ReadCapacityUnits": 5, + "WriteCapacityUnits": 5, + }, + "TableName": "Person", + }, + } + }, +} + dummy_template_json = json.dumps(dummy_template) dummy_template_json2 = json.dumps(dummy_template2) dummy_template_json3 = json.dumps(dummy_template3) @@ -188,6 +215,34 @@ def test_describe_stack_by_stack_id(): stack_by_id.stack_name.should.equal("test_stack") +@mock_dynamodb2 +@mock_cloudformation_deprecated +def test_delete_stack_dynamo_template(): + conn = boto.connect_cloudformation() + dynamodb_client = boto3.client("dynamodb", region_name="us-east-1") + conn.create_stack("test_stack", template_body=dummy_template4) + table_desc = dynamodb_client.list_tables() + len(table_desc.get("TableNames")).should.equal(1) + conn.delete_stack("test_stack") + table_desc = dynamodb_client.list_tables() + len(table_desc.get("TableNames")).should.equal(0) + conn.create_stack("test_stack", template_body=dummy_template4) + + +@mock_dynamodb2 +@mock_cloudformation +def test_delete_stack_dynamo_template(): + conn = boto3.client("cloudformation", region_name="us-east-1") + dynamodb_client = boto3.client("dynamodb", region_name="us-east-1") + conn.create_stack(StackName="test_stack", TemplateBody=json.dumps(dummy_template4)) + table_desc = dynamodb_client.list_tables() + len(table_desc.get("TableNames")).should.equal(1) + conn.delete_stack(StackName="test_stack") + table_desc = dynamodb_client.list_tables() + len(table_desc.get("TableNames")).should.equal(0) + conn.create_stack(StackName="test_stack", TemplateBody=json.dumps(dummy_template4)) + + @mock_cloudformation_deprecated def test_describe_deleted_stack(): conn = boto.connect_cloudformation()