From 527295b8832076ac19851963e29cc40b93278944 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 14 Feb 2014 21:36:13 -0500 Subject: [PATCH 1/5] Added subnet tagging. --- cloud/ec2_vpc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cloud/ec2_vpc b/cloud/ec2_vpc index 9b9fb95a0b2..10a52533df1 100644 --- a/cloud/ec2_vpc +++ b/cloud/ec2_vpc @@ -277,7 +277,10 @@ def create_vpc(module, vpc_conn): add_subnet = False if add_subnet: try: - vpc_conn.create_subnet(vpc.id, subnet['cidr'], subnet.get('az', None)) + created_subnet = vpc_conn.create_subnet(vpc.id, subnet['cidr'], subnet.get('az', None)) + subnet_tags = subnet.get('tags', None) + if subnet_tags: + vpc_conn.create_tags(created_subnet.id, subnet_tags) changed = True except EC2ResponseError, e: module.fail_json(msg='Unable to create subnet {0}, error: {1}'.format(subnet['cidr'], e)) From 847680a74aba99d919d86ad14525d0b3e84b71d6 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 14 Feb 2014 21:46:05 -0500 Subject: [PATCH 2/5] Added documentation for subnets: tags: option --- cloud/ec2_vpc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cloud/ec2_vpc b/cloud/ec2_vpc index 10a52533df1..e92b9f77ed3 100644 --- a/cloud/ec2_vpc +++ b/cloud/ec2_vpc @@ -46,7 +46,7 @@ options: choices: [ "yes", "no" ] subnets: description: - - "A dictionary array of subnets to add of the form: { cidr: ..., az: ... }. Where az is the desired availability zone of the subnet, but it is not required. All VPC subnets not in this list will be removed." + - "A dictionary array of subnets to add of the form: { cidr: ..., az: ... , tags: ... }. Where az is the desired availability zone of the subnet, but it is not required. Tags (i.e.: tags) is also optional and use dictionary form: { "Environment":"Dev", "Tier":"Web", ...}. All VPC subnets not in this list will be removed." required: false default: null aliases: [] @@ -137,10 +137,13 @@ EXAMPLES = ''' subnets: - cidr: 172.22.1.0/24 az: us-west-2c + tags: { "Environment":"Dev", "Tier" : "Web" } - cidr: 172.22.2.0/24 az: us-west-2b + tags: { "Environment":"Dev", "Tier" : "App" } - cidr: 172.22.3.0/24 az: us-west-2a + tags: { "Environment":"Dev", "Tier" : "DB" } internet_gateway: True route_tables: - subnets: From e00d365630d87d49958384e3500e9cff54f3f613 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Wed, 19 Feb 2014 16:26:40 -0500 Subject: [PATCH 3/5] Refactored subnet tagging to account for AWS delays; added 'tags' attribute to 'subnet' node in the returned json. --- cloud/ec2_vpc | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/cloud/ec2_vpc b/cloud/ec2_vpc index e92b9f77ed3..911cb4125df 100644 --- a/cloud/ec2_vpc +++ b/cloud/ec2_vpc @@ -280,10 +280,18 @@ def create_vpc(module, vpc_conn): add_subnet = False if add_subnet: try: - created_subnet = vpc_conn.create_subnet(vpc.id, subnet['cidr'], subnet.get('az', None)) - subnet_tags = subnet.get('tags', None) - if subnet_tags: - vpc_conn.create_tags(created_subnet.id, subnet_tags) + new_subnet = vpc_conn.create_subnet(vpc.id, subnet['cidr'], subnet.get('az', None)) + new_subnet_tags = subnet.get('tags', None) + if new_subnet_tags: + # Sometimes AWS takes its time to create a subnet and so using new subnets's id + # to create tags results in exception. + # boto doesn't seem to refresh 'state' of the newly created subnet, i.e.: it's always 'pending' + # so i resorted to polling vpc_conn.get_all_subnets with the id of the newly added subnet + while len(vpc_conn.get_all_subnets(filters={ 'subnet-id': new_subnet.id })) == 0: + time.sleep(0.1) + + vpc_conn.create_tags(new_subnet.id, new_subnet_tags) + changed = True except EC2ResponseError, e: module.fail_json(msg='Unable to create subnet {0}, error: {1}'.format(subnet['cidr'], e)) @@ -411,14 +419,15 @@ def create_vpc(module, vpc_conn): created_vpc_id = vpc.id returned_subnets = [] current_subnets = vpc_conn.get_all_subnets(filters={ 'vpc_id': vpc.id }) + for sn in current_subnets: returned_subnets.append({ + 'tags': dict((t.name, t.value) for t in vpc_conn.get_all_tags(filters={'resource-id': sn.id})), 'cidr': sn.cidr_block, 'az': sn.availability_zone, 'id': sn.id, }) - return (vpc_dict, created_vpc_id, returned_subnets, changed) def terminate_vpc(module, vpc_conn, vpc_id=None, cidr=None): From e0894d50952659993bd229c69822c975cc5e811f Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Mon, 24 Feb 2014 23:50:12 -0500 Subject: [PATCH 4/5] Renamed subnet's 'tags' attribute into 'instance_tags' to distinguish it from Ansible's own 'tags' and to conform to ec2 module naming for AWS tags. --- cloud/ec2_vpc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cloud/ec2_vpc b/cloud/ec2_vpc index 911cb4125df..439a20ddc68 100644 --- a/cloud/ec2_vpc +++ b/cloud/ec2_vpc @@ -46,7 +46,7 @@ options: choices: [ "yes", "no" ] subnets: description: - - "A dictionary array of subnets to add of the form: { cidr: ..., az: ... , tags: ... }. Where az is the desired availability zone of the subnet, but it is not required. Tags (i.e.: tags) is also optional and use dictionary form: { "Environment":"Dev", "Tier":"Web", ...}. All VPC subnets not in this list will be removed." + - "A dictionary array of subnets to add of the form: { cidr: ..., az: ... , instance_tags: ... }. Where az is the desired availability zone of the subnet, but it is not required. Tags (i.e.: instance_tags) is also optional and use dictionary form: { "Environment":"Dev", "Tier":"Web", ...}. All VPC subnets not in this list will be removed." required: false default: null aliases: [] @@ -137,13 +137,13 @@ EXAMPLES = ''' subnets: - cidr: 172.22.1.0/24 az: us-west-2c - tags: { "Environment":"Dev", "Tier" : "Web" } + instance_tags: { "Environment":"Dev", "Tier" : "Web" } - cidr: 172.22.2.0/24 az: us-west-2b - tags: { "Environment":"Dev", "Tier" : "App" } + instance_tags: { "Environment":"Dev", "Tier" : "App" } - cidr: 172.22.3.0/24 az: us-west-2a - tags: { "Environment":"Dev", "Tier" : "DB" } + instance_tags: { "Environment":"Dev", "Tier" : "DB" } internet_gateway: True route_tables: - subnets: @@ -281,7 +281,7 @@ def create_vpc(module, vpc_conn): if add_subnet: try: new_subnet = vpc_conn.create_subnet(vpc.id, subnet['cidr'], subnet.get('az', None)) - new_subnet_tags = subnet.get('tags', None) + new_subnet_tags = subnet.get('instance_tags', None) if new_subnet_tags: # Sometimes AWS takes its time to create a subnet and so using new subnets's id # to create tags results in exception. @@ -422,7 +422,7 @@ def create_vpc(module, vpc_conn): for sn in current_subnets: returned_subnets.append({ - 'tags': dict((t.name, t.value) for t in vpc_conn.get_all_tags(filters={'resource-id': sn.id})), + 'instance_tags': dict((t.name, t.value) for t in vpc_conn.get_all_tags(filters={'resource-id': sn.id})), 'cidr': sn.cidr_block, 'az': sn.availability_zone, 'id': sn.id, From 46ee6417154eda7c3b0735ee21d25fe4fac2aa04 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Sat, 1 Mar 2014 20:41:24 -0500 Subject: [PATCH 5/5] Renamed instance_tags to resource_tags based on community feedback. --- cloud/ec2_vpc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cloud/ec2_vpc b/cloud/ec2_vpc index 439a20ddc68..7427fc2c905 100644 --- a/cloud/ec2_vpc +++ b/cloud/ec2_vpc @@ -46,7 +46,7 @@ options: choices: [ "yes", "no" ] subnets: description: - - "A dictionary array of subnets to add of the form: { cidr: ..., az: ... , instance_tags: ... }. Where az is the desired availability zone of the subnet, but it is not required. Tags (i.e.: instance_tags) is also optional and use dictionary form: { "Environment":"Dev", "Tier":"Web", ...}. All VPC subnets not in this list will be removed." + - "A dictionary array of subnets to add of the form: { cidr: ..., az: ... , resource_tags: ... }. Where az is the desired availability zone of the subnet, but it is not required. Tags (i.e.: resource_tags) is also optional and use dictionary form: { "Environment":"Dev", "Tier":"Web", ...}. All VPC subnets not in this list will be removed." required: false default: null aliases: [] @@ -137,13 +137,13 @@ EXAMPLES = ''' subnets: - cidr: 172.22.1.0/24 az: us-west-2c - instance_tags: { "Environment":"Dev", "Tier" : "Web" } + resource_tags: { "Environment":"Dev", "Tier" : "Web" } - cidr: 172.22.2.0/24 az: us-west-2b - instance_tags: { "Environment":"Dev", "Tier" : "App" } + resource_tags: { "Environment":"Dev", "Tier" : "App" } - cidr: 172.22.3.0/24 az: us-west-2a - instance_tags: { "Environment":"Dev", "Tier" : "DB" } + resource_tags: { "Environment":"Dev", "Tier" : "DB" } internet_gateway: True route_tables: - subnets: @@ -281,7 +281,7 @@ def create_vpc(module, vpc_conn): if add_subnet: try: new_subnet = vpc_conn.create_subnet(vpc.id, subnet['cidr'], subnet.get('az', None)) - new_subnet_tags = subnet.get('instance_tags', None) + new_subnet_tags = subnet.get('resource_tags', None) if new_subnet_tags: # Sometimes AWS takes its time to create a subnet and so using new subnets's id # to create tags results in exception. @@ -422,7 +422,7 @@ def create_vpc(module, vpc_conn): for sn in current_subnets: returned_subnets.append({ - 'instance_tags': dict((t.name, t.value) for t in vpc_conn.get_all_tags(filters={'resource-id': sn.id})), + 'resource_tags': dict((t.name, t.value) for t in vpc_conn.get_all_tags(filters={'resource-id': sn.id})), 'cidr': sn.cidr_block, 'az': sn.availability_zone, 'id': sn.id,