Fix : cloudFormation dynamodb : delete resource on delete stack (#3120)

* Fix :  cloudFormation dynamodb : delete resource on delete stack

* Delete function for dynamodb

* Added tests for delete stack using dynamodb.

* Added tests for non decorator

* Linting

Co-authored-by: Bert Blommers <info@bertblommers.nl>
This commit is contained in:
usmangani1 2020-07-19 16:29:19 +05:30 committed by GitHub
parent 552b1294df
commit a123a22eeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 0 deletions

View File

@ -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):

View File

@ -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()