From 4020141478298424cb8a93185bf9735177426d0d Mon Sep 17 00:00:00 2001 From: Steve Pulec Date: Mon, 13 Jul 2015 19:27:00 -0400 Subject: [PATCH] Fix cloudformation subnet tagging. --- moto/ec2/models.py | 5 ++++ tests/test_ec2/test_subnets.py | 51 ++++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index cdb891487..3f3fc4491 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -1715,6 +1715,11 @@ class Subnet(TaggedEC2Resource): cidr_block=cidr_block, availability_zone=availability_zone, ) + for tag in properties.get("Tags", []): + tag_key = tag["Key"] + tag_value = tag["Value"] + subnet.add_tag(tag_key, tag_value) + return subnet @property diff --git a/tests/test_ec2/test_subnets.py b/tests/test_ec2/test_subnets.py index 6205a50f2..311e9b295 100644 --- a/tests/test_ec2/test_subnets.py +++ b/tests/test_ec2/test_subnets.py @@ -1,14 +1,15 @@ from __future__ import unicode_literals # Ensure 'assert_raises' context manager support for Python 2.6 -import tests.backport_assert_raises +import tests.backport_assert_raises # noqa from nose.tools import assert_raises import boto import boto.vpc from boto.exception import EC2ResponseError +import json import sure # noqa -from moto import mock_ec2 +from moto import mock_cloudformation, mock_ec2 @mock_ec2 @@ -84,20 +85,20 @@ def test_get_subnets_filtering(): # Filter by VPC ID subnets_by_vpc = conn.get_all_subnets(filters={'vpc-id': vpcB.id}) subnets_by_vpc.should.have.length_of(2) - set([subnet.id for subnet in subnets_by_vpc]).should.equal(set([subnetB1.id,subnetB2.id])) + set([subnet.id for subnet in subnets_by_vpc]).should.equal(set([subnetB1.id, subnetB2.id])) # Filter by CIDR variations subnets_by_cidr1 = conn.get_all_subnets(filters={'cidr': "10.0.0.0/24"}) subnets_by_cidr1.should.have.length_of(2) - set([subnet.id for subnet in subnets_by_cidr1]).should.equal(set([subnetA.id,subnetB1.id])) + set([subnet.id for subnet in subnets_by_cidr1]).should.equal(set([subnetA.id, subnetB1.id])) subnets_by_cidr2 = conn.get_all_subnets(filters={'cidr-block': "10.0.0.0/24"}) subnets_by_cidr2.should.have.length_of(2) - set([subnet.id for subnet in subnets_by_cidr2]).should.equal(set([subnetA.id,subnetB1.id])) + set([subnet.id for subnet in subnets_by_cidr2]).should.equal(set([subnetA.id, subnetB1.id])) subnets_by_cidr3 = conn.get_all_subnets(filters={'cidrBlock': "10.0.0.0/24"}) subnets_by_cidr3.should.have.length_of(2) - set([subnet.id for subnet in subnets_by_cidr3]).should.equal(set([subnetA.id,subnetB1.id])) + set([subnet.id for subnet in subnets_by_cidr3]).should.equal(set([subnetA.id, subnetB1.id])) # Filter by VPC ID and CIDR subnets_by_vpc_and_cidr = conn.get_all_subnets(filters={'vpc-id': vpcB.id, 'cidr': "10.0.0.0/24"}) @@ -116,3 +117,41 @@ def test_get_subnets_filtering(): # Unsupported filter conn.get_all_subnets.when.called_with(filters={'not-implemented-filter': 'foobar'}).should.throw(NotImplementedError) + + +@mock_ec2 +@mock_cloudformation +def test_subnet_tags_through_cloudformation(): + vpc_conn = boto.vpc.connect_to_region('us-west-1') + vpc = vpc_conn.create_vpc("10.0.0.0/16") + + subnet_template = { + "AWSTemplateFormatVersion": "2010-09-09", + "Resources": { + "testSubnet": { + "Type": "AWS::EC2::Subnet", + "Properties": { + "VpcId": vpc.id, + "CidrBlock": "10.0.0.0/24", + "AvailabilityZone": "us-west-1b", + "Tags": [{ + "Key": "foo", + "Value": "bar", + }, { + "Key": "blah", + "Value": "baz", + }] + } + } + } + } + cf_conn = boto.cloudformation.connect_to_region("us-west-1") + template_json = json.dumps(subnet_template) + cf_conn.create_stack( + "test_stack", + template_body=template_json, + ) + + subnet = vpc_conn.get_all_subnets(filters={'cidrBlock': '10.0.0.0/24'})[0] + subnet.tags["foo"].should.equal("bar") + subnet.tags["blah"].should.equal("baz")