From 1dcfcbca5711ac50f9c3314b8a7e8812e3025156 Mon Sep 17 00:00:00 2001 From: Steve Pulec Date: Mon, 31 Aug 2015 16:48:36 -0400 Subject: [PATCH] Add cloudformation tags. --- moto/cloudformation/models.py | 6 ++++-- moto/cloudformation/responses.py | 12 +++++++++++- .../test_cloudformation_stack_crud.py | 13 +++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/moto/cloudformation/models.py b/moto/cloudformation/models.py index 9fd913ca4..cd99856f5 100644 --- a/moto/cloudformation/models.py +++ b/moto/cloudformation/models.py @@ -10,7 +10,7 @@ from .exceptions import ValidationError class FakeStack(object): - def __init__(self, stack_id, name, template, parameters, region_name, notification_arns=None): + def __init__(self, stack_id, name, template, parameters, region_name, notification_arns=None, tags=None): self.stack_id = stack_id self.name = name self.template = template @@ -18,6 +18,7 @@ class FakeStack(object): self.parameters = parameters self.region_name = region_name self.notification_arns = notification_arns if notification_arns else [] + self.tags = tags if tags else {} self.status = 'CREATE_COMPLETE' self.description = self.template_dict.get('Description') @@ -58,7 +59,7 @@ class CloudFormationBackend(BaseBackend): self.stacks = {} self.deleted_stacks = {} - def create_stack(self, name, template, parameters, region_name, notification_arns=None): + def create_stack(self, name, template, parameters, region_name, notification_arns=None, tags=None): stack_id = generate_stack_id(name) new_stack = FakeStack( stack_id=stack_id, @@ -67,6 +68,7 @@ class CloudFormationBackend(BaseBackend): parameters=parameters, region_name=region_name, notification_arns=notification_arns, + tags=tags, ) self.stacks[stack_id] = new_stack return new_stack diff --git a/moto/cloudformation/responses.py b/moto/cloudformation/responses.py index e0afa5fa3..28db697cf 100644 --- a/moto/cloudformation/responses.py +++ b/moto/cloudformation/responses.py @@ -27,6 +27,7 @@ class CloudFormationResponse(BaseResponse): stack_body = self._get_param('TemplateBody') template_url = self._get_param('TemplateURL') parameters_list = self._get_list_prefix("Parameters.member") + tags = dict((item['key'], item['value']) for item in self._get_list_prefix("Tags.member")) # Hack dict-comprehension parameters = dict([ @@ -43,7 +44,8 @@ class CloudFormationResponse(BaseResponse): template=stack_body, parameters=parameters, region_name=self.region, - notification_arns=stack_notification_arns + notification_arns=stack_notification_arns, + tags=tags, ) stack_body = { 'CreateStackResponse': { @@ -150,6 +152,14 @@ DESCRIBE_STACKS_TEMPLATE = """ {% endfor %} + + {% for tag_key, tag_value in stack.tags.items() %} + + {{ tag_key }} + {{ tag_value }} + + {% endfor %} + {% endfor %} diff --git a/tests/test_cloudformation/test_cloudformation_stack_crud.py b/tests/test_cloudformation/test_cloudformation_stack_crud.py index 5bdccb434..236c33e7d 100644 --- a/tests/test_cloudformation/test_cloudformation_stack_crud.py +++ b/tests/test_cloudformation/test_cloudformation_stack_crud.py @@ -219,6 +219,19 @@ def test_cloudformation_params(): param.value.should.equal('testing123') +@mock_cloudformation +def test_stack_tags(): + conn = boto.connect_cloudformation() + conn.create_stack( + "test_stack", + template_body=dummy_template_json, + tags={"foo": "bar", "baz": "bleh"}, + ) + + stack = conn.describe_stacks()[0] + dict(stack.tags).should.equal({"foo": "bar", "baz": "bleh"}) + + # @mock_cloudformation # def test_update_stack(): # conn = boto.connect_cloudformation()