Propagate stack-level tags to resources

According to
http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-resource-tags.html
"All stack-level tags, including automatically created tags, are
propagated to resources that AWS CloudFormation supports. Currently,
tags are not propagated to Amazon EBS volumes that are created from
block device mappings."
This commit is contained in:
Lars Fronius 2016-03-15 15:36:02 +01:00
parent 66032ad37c
commit 519b8e59aa
3 changed files with 17 additions and 7 deletions

View File

@ -26,7 +26,7 @@ class FakeStack(object):
self.output_map = self._create_output_map() self.output_map = self._create_output_map()
def _create_resource_map(self): def _create_resource_map(self):
resource_map = ResourceMap(self.stack_id, self.name, self.parameters, self.region_name, self.template_dict) resource_map = ResourceMap(self.stack_id, self.name, self.parameters, self.tags, self.region_name, self.template_dict)
resource_map.create() resource_map.create()
return resource_map return resource_map

View File

@ -2,6 +2,7 @@ from __future__ import unicode_literals
import collections import collections
import functools import functools
import logging import logging
import copy
from moto.autoscaling import models as autoscaling_models from moto.autoscaling import models as autoscaling_models
from moto.awslambda import models as lambda_models from moto.awslambda import models as lambda_models
@ -264,11 +265,12 @@ class ResourceMap(collections.Mapping):
each resources is passed this lazy map that it can grab dependencies from. each resources is passed this lazy map that it can grab dependencies from.
""" """
def __init__(self, stack_id, stack_name, parameters, region_name, template): def __init__(self, stack_id, stack_name, parameters, tags, region_name, template):
self._template = template self._template = template
self._resource_json_map = template['Resources'] self._resource_json_map = template['Resources']
self._region_name = region_name self._region_name = region_name
self.input_parameters = parameters self.input_parameters = parameters
self.tags = copy.deepcopy(tags)
self.resolved_parameters = {} self.resolved_parameters = {}
# Create the default resources # Create the default resources
@ -339,13 +341,12 @@ class ResourceMap(collections.Mapping):
# Since this is a lazy map, to create every object we just need to # Since this is a lazy map, to create every object we just need to
# iterate through self. # iterate through self.
tags = {'aws:cloudformation:stack-name': self.get('AWS::StackName'), self.tags.update({'aws:cloudformation:stack-name': self.get('AWS::StackName'),
'aws:cloudformation:stack-id': self.get('AWS::StackId')} 'aws:cloudformation:stack-id': self.get('AWS::StackId')})
for resource in self.resources: for resource in self.resources:
self[resource]
if isinstance(self[resource], ec2_models.TaggedEC2Resource): if isinstance(self[resource], ec2_models.TaggedEC2Resource):
tags['aws:cloudformation:logical-id'] = resource self.tags['aws:cloudformation:logical-id'] = resource
ec2_models.ec2_backends[self._region_name].create_tags([self[resource].physical_resource_id], tags) ec2_models.ec2_backends[self._region_name].create_tags([self[resource].physical_resource_id], self.tags)
def update(self, template): def update(self, template):
self.load_mapping() self.load_mapping()

View File

@ -363,6 +363,12 @@ def test_stack_security_groups():
"Type": "AWS::EC2::SecurityGroup", "Type": "AWS::EC2::SecurityGroup",
"Properties": { "Properties": {
"GroupDescription": "My security group", "GroupDescription": "My security group",
"Tags": [
{
"Key": "bar",
"Value": "baz"
}
],
"SecurityGroupIngress": [{ "SecurityGroupIngress": [{
"IpProtocol": "tcp", "IpProtocol": "tcp",
"FromPort": "22", "FromPort": "22",
@ -384,6 +390,7 @@ def test_stack_security_groups():
conn.create_stack( conn.create_stack(
"security_group_stack", "security_group_stack",
template_body=security_group_template_json, template_body=security_group_template_json,
tags={"foo":"bar"}
) )
ec2_conn = boto.ec2.connect_to_region("us-west-1") ec2_conn = boto.ec2.connect_to_region("us-west-1")
@ -395,6 +402,8 @@ def test_stack_security_groups():
ec2_instance.groups[0].id.should.equal(instance_group.id) ec2_instance.groups[0].id.should.equal(instance_group.id)
instance_group.description.should.equal("My security group") instance_group.description.should.equal("My security group")
instance_group.tags.should.have.key('foo').which.should.equal('bar')
instance_group.tags.should.have.key('bar').which.should.equal('baz')
rule1, rule2 = instance_group.rules rule1, rule2 = instance_group.rules
int(rule1.to_port).should.equal(22) int(rule1.to_port).should.equal(22)
int(rule1.from_port).should.equal(22) int(rule1.from_port).should.equal(22)