Add cloudformation tags.

This commit is contained in:
Steve Pulec 2015-08-31 16:48:36 -04:00
parent 2a9085dfc6
commit 1dcfcbca57
3 changed files with 28 additions and 3 deletions

View File

@ -10,7 +10,7 @@ from .exceptions import ValidationError
class FakeStack(object): 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.stack_id = stack_id
self.name = name self.name = name
self.template = template self.template = template
@ -18,6 +18,7 @@ class FakeStack(object):
self.parameters = parameters self.parameters = parameters
self.region_name = region_name self.region_name = region_name
self.notification_arns = notification_arns if notification_arns else [] self.notification_arns = notification_arns if notification_arns else []
self.tags = tags if tags else {}
self.status = 'CREATE_COMPLETE' self.status = 'CREATE_COMPLETE'
self.description = self.template_dict.get('Description') self.description = self.template_dict.get('Description')
@ -58,7 +59,7 @@ class CloudFormationBackend(BaseBackend):
self.stacks = {} self.stacks = {}
self.deleted_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) stack_id = generate_stack_id(name)
new_stack = FakeStack( new_stack = FakeStack(
stack_id=stack_id, stack_id=stack_id,
@ -67,6 +68,7 @@ class CloudFormationBackend(BaseBackend):
parameters=parameters, parameters=parameters,
region_name=region_name, region_name=region_name,
notification_arns=notification_arns, notification_arns=notification_arns,
tags=tags,
) )
self.stacks[stack_id] = new_stack self.stacks[stack_id] = new_stack
return new_stack return new_stack

View File

@ -27,6 +27,7 @@ class CloudFormationResponse(BaseResponse):
stack_body = self._get_param('TemplateBody') stack_body = self._get_param('TemplateBody')
template_url = self._get_param('TemplateURL') template_url = self._get_param('TemplateURL')
parameters_list = self._get_list_prefix("Parameters.member") 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 # Hack dict-comprehension
parameters = dict([ parameters = dict([
@ -43,7 +44,8 @@ class CloudFormationResponse(BaseResponse):
template=stack_body, template=stack_body,
parameters=parameters, parameters=parameters,
region_name=self.region, region_name=self.region,
notification_arns=stack_notification_arns notification_arns=stack_notification_arns,
tags=tags,
) )
stack_body = { stack_body = {
'CreateStackResponse': { 'CreateStackResponse': {
@ -150,6 +152,14 @@ DESCRIBE_STACKS_TEMPLATE = """<DescribeStacksResult>
</member> </member>
{% endfor %} {% endfor %}
</Parameters> </Parameters>
<Tags>
{% for tag_key, tag_value in stack.tags.items() %}
<member>
<Key>{{ tag_key }}</Key>
<Value>{{ tag_value }}</Value>
</member>
{% endfor %}
</Tags>
</member> </member>
{% endfor %} {% endfor %}
</Stacks> </Stacks>

View File

@ -219,6 +219,19 @@ def test_cloudformation_params():
param.value.should.equal('testing123') 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 # @mock_cloudformation
# def test_update_stack(): # def test_update_stack():
# conn = boto.connect_cloudformation() # conn = boto.connect_cloudformation()