From 1441087e5c47a982468fc608a9a437bbba8c9d11 Mon Sep 17 00:00:00 2001 From: Declan Shanaghy Date: Mon, 15 Aug 2016 10:57:40 -0700 Subject: [PATCH 1/3] Add S3 parsing for CloudFormation --- moto/cloudformation/parsing.py | 2 ++ moto/s3/models.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/moto/cloudformation/parsing.py b/moto/cloudformation/parsing.py index f569f6a88..690b95631 100644 --- a/moto/cloudformation/parsing.py +++ b/moto/cloudformation/parsing.py @@ -14,6 +14,7 @@ from moto.kms import models as kms_models from moto.rds import models as rds_models from moto.redshift import models as redshift_models from moto.route53 import models as route53_models +from moto.s3 import models as s3_models from moto.sns import models as sns_models from moto.sqs import models as sqs_models from .utils import random_suffix @@ -57,6 +58,7 @@ MODEL_MAP = { "AWS::Route53::RecordSet": route53_models.RecordSet, "AWS::Route53::RecordSetGroup": route53_models.RecordSetGroup, "AWS::SNS::Topic": sns_models.Topic, + "AWS::S3::Bucket": s3_models.FakeBucket, "AWS::SQS::Queue": sqs_models.Queue, } diff --git a/moto/s3/models.py b/moto/s3/models.py index ec1ac46dd..e9afa6173 100644 --- a/moto/s3/models.py +++ b/moto/s3/models.py @@ -288,6 +288,16 @@ class FakeBucket(object): def set_acl(self, acl): self.acl = acl + @property + def physical_resource_id(self): + return self.name + + @classmethod + def create_from_cloudformation_json( + cls, resource_name, cloudformation_json, region_name): + bucket = s3_backend.create_bucket(resource_name, region_name) + return bucket + class S3Backend(BaseBackend): From 659efe1c21ef9961f028e689ba638b3204ab3679 Mon Sep 17 00:00:00 2001 From: Declan Shanaghy Date: Mon, 15 Aug 2016 11:28:07 -0700 Subject: [PATCH 2/3] Add tests --- .gitignore | 1 + tests/test_cloudformation/test_stack_parsing.py | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1893cbac8..efee854dd 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ build/ .idea/ *.swp .DS_Store +python_env \ No newline at end of file diff --git a/tests/test_cloudformation/test_stack_parsing.py b/tests/test_cloudformation/test_stack_parsing.py index 29d8282fd..1174a4bee 100644 --- a/tests/test_cloudformation/test_stack_parsing.py +++ b/tests/test_cloudformation/test_stack_parsing.py @@ -7,6 +7,7 @@ import sure # noqa from moto.cloudformation.models import FakeStack from moto.cloudformation.parsing import resource_class_from_type, parse_condition from moto.sqs.models import Queue +from moto.s3.models import FakeBucket from boto.cloudformation.stack import Output from boto.exception import BotoServerError @@ -23,6 +24,10 @@ dummy_template = { "VisibilityTimeout": 60, } }, + "S3Bucket": { + "Type": "AWS::S3::Bucket", + "DeletionPolicy": "Retain" + }, }, } @@ -85,12 +90,18 @@ def test_parse_stack_resources(): parameters={}, region_name='us-west-1') - stack.resource_map.should.have.length_of(1) + stack.resource_map.should.have.length_of(2) list(stack.resource_map.keys())[0].should.equal('Queue') + list(stack.resource_map.keys())[1].should.equal('S3Bucket') + queue = list(stack.resource_map.values())[0] queue.should.be.a(Queue) queue.name.should.equal("my-queue") + bucket = list(stack.resource_map.values())[1] + bucket.should.be.a(FakeBucket) + bucket.physical_resource_id.should.equal(bucket.name) + @patch("moto.cloudformation.parsing.logger") def test_missing_resource_logs(logger): From e8d245f6a10128fa3ea4bab9ebeee103421b6ffe Mon Sep 17 00:00:00 2001 From: Declan Shanaghy Date: Mon, 15 Aug 2016 12:36:39 -0700 Subject: [PATCH 3/3] Fix tests for python3 --- tests/test_cloudformation/test_stack_parsing.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_cloudformation/test_stack_parsing.py b/tests/test_cloudformation/test_stack_parsing.py index 1174a4bee..42208810f 100644 --- a/tests/test_cloudformation/test_stack_parsing.py +++ b/tests/test_cloudformation/test_stack_parsing.py @@ -91,14 +91,12 @@ def test_parse_stack_resources(): region_name='us-west-1') stack.resource_map.should.have.length_of(2) - list(stack.resource_map.keys())[0].should.equal('Queue') - list(stack.resource_map.keys())[1].should.equal('S3Bucket') - queue = list(stack.resource_map.values())[0] + queue = stack.resource_map['Queue'] queue.should.be.a(Queue) queue.name.should.equal("my-queue") - bucket = list(stack.resource_map.values())[1] + bucket = stack.resource_map['S3Bucket'] bucket.should.be.a(FakeBucket) bucket.physical_resource_id.should.equal(bucket.name)