Added the ability to tag ec2 objects.

This commit is contained in:
Omer Katz 2014-09-29 12:24:30 +03:00
parent 888e2ce35e
commit 18e913a41f
2 changed files with 51 additions and 7 deletions

View File

@ -145,7 +145,8 @@ def exists(vpc_id, region=None, key=None, keyid=None, profile=None):
return False
def create(cidr_block, instance_tenancy=None, vpc_name=None, region=None, key=None, keyid=None, profile=None):
def create(cidr_block, instance_tenancy=None, vpc_name=None, tags=None, region=None, key=None, keyid=None,
profile=None):
'''
Given a valid CIDR block, create a VPC.
@ -172,6 +173,7 @@ def create(cidr_block, instance_tenancy=None, vpc_name=None, region=None, key=No
log.debug('The newly created VPC id is {0}'.format(vpc.id))
_maybe_set_name_tag(vpc_name, vpc)
_maybe_set_tags(tags, vpc)
return True
except boto.exception.BotoServerError as e:
@ -211,8 +213,8 @@ def delete(vpc_id, region=None, key=None, keyid=None, profile=None):
return False
def create_subnet(vpc_id, cidr_block, availability_zone=None, subnet_name=None, region=None, key=None, keyid=None,
profile=None):
def create_subnet(vpc_id, cidr_block, availability_zone=None, subnet_name=None, tags=None, region=None, key=None,
keyid=None, profile=None):
'''
Given a valid VPC ID and a CIDR block, create a subnet for the VPC.
@ -240,6 +242,7 @@ def create_subnet(vpc_id, cidr_block, availability_zone=None, subnet_name=None,
vpc_id))
_maybe_set_name_tag(subnet_name, vpc_subnet)
_maybe_set_tags(tags, vpc_subnet)
return True
except boto.exception.BotoServerError as e:
@ -279,7 +282,8 @@ def delete_subnet(subnet_id, region=None, key=None, keyid=None, profile=None):
return False
def create_customer_gateway(vpn_connection_type, ip_address, bgp_asn, customer_gateway_name=None, region=None, key=None, keyid=None, profile=None):
def create_customer_gateway(vpn_connection_type, ip_address, bgp_asn, customer_gateway_name=None, tags=None,
region=None, key=None, keyid=None, profile=None):
'''
Given a valid VPN connection type, a static IP address and a customer gateways Border Gateway Protocol (BGP) Autonomous System Number, create a customer gateway.
@ -303,6 +307,7 @@ def create_customer_gateway(vpn_connection_type, ip_address, bgp_asn, customer_g
log.info('A customer gateway with id {0} was created'.format(customer_gateway.id))
_maybe_set_name_tag(customer_gateway_name, customer_gateway)
_maybe_set_tags(tags, customer_gateway)
except boto.exception.BotoServerError as e:
log.error(e)
return False
@ -341,7 +346,7 @@ def delete_customer_gateway(customer_gateway_id, region=None, key=None, keyid=No
def create_dhcp_options(domain_name=None, domain_name_servers=None, ntp_servers=None,
netbios_name_servers=None, netbios_node_type=None, dhcp_options_name=None,
netbios_name_servers=None, netbios_node_type=None, dhcp_options_name=None, tags=None,
region=None, key=None, keyid=None, profile=None):
'''
Given valid DHCP options, create a DHCP options record.
@ -367,6 +372,7 @@ def create_dhcp_options(domain_name=None, domain_name_servers=None, ntp_servers=
log.info('DHCP options with id {0} were created'.format(dhcp_options.id))
_maybe_set_name_tag(dhcp_options_name, dhcp_options)
_maybe_set_tags(tags, dhcp_options)
return True
else:
@ -480,4 +486,11 @@ def _maybe_set_name_tag(name, obj):
if name:
obj.add_tag("Name", name)
log.debug('{0} is now named as {1}'.format(obj, name))
log.debug('{0} is now named as {1}'.format(obj, name))
def _maybe_set_tags(tags, obj):
if tags:
obj.add_tags(tags)
log.debug('The following tags: {0} were added to {1}'.format(', '.join(tags), obj))

View File

@ -176,6 +176,15 @@ class BotoVpcTestCase(TestCase):
self.assertTrue(vpc_creation_result)
@mock_ec2
def test_that_when_creating_a_vpc_and_specifying_tags_succeeds_the_create_vpc_method_returns_true(self):
'''
tests True VPC created.
'''
vpc_creation_result = boto_vpc.create(cidr_block, tags={'test': 'value'}, **conn_parameters)
self.assertTrue(vpc_creation_result)
@mock_ec2
def test_that_when_creating_a_vpc_fails_the_create_vpc_method_returns_false(self):
'''
@ -209,13 +218,22 @@ class BotoVpcTestCase(TestCase):
self.assertTrue(subnet_creation_result)
@mock_ec2
def test_that_when_creating_a_subnet_and_spefiying_a_name_succeeds_the_create_subnet_method_returns_true(self):
def test_that_when_creating_a_subnet_and_specifying_a_name_succeeds_the_create_subnet_method_returns_true(self):
vpc = self._create_vpc()
subnet_creation_result = boto_vpc.create_subnet(vpc.id, '10.0.0.0/24', subnet_name='test', **conn_parameters)
self.assertTrue(subnet_creation_result)
@mock_ec2
def test_that_when_creating_a_subnet_and_specifying_tags_succeeds_the_create_subnet_method_returns_true(self):
vpc = self._create_vpc()
subnet_creation_result = boto_vpc.create_subnet(vpc.id, '10.0.0.0/24', tags={'test': 'testvalue'},
**conn_parameters)
self.assertTrue(subnet_creation_result)
@mock_ec2
def test_that_when_creating_a_subnet_fails_the_create_subnet_method_returns_false(self):
vpc = self._create_vpc()
@ -263,6 +281,19 @@ class BotoVpcTestCase(TestCase):
self.assertTrue(dhcp_options_creation_result)
@mock_ec2
def test_when_creating_dhcp_options_and_specifying_tags_succeeds_the_create_dhcp_options_method_returns_true(
self):
dhcp_options_creation_result = boto_vpc.create_dhcp_options(domain_name='example.com',
domain_name_servers=['1.2.3.4'],
ntp_servers=['5.6.7.8'],
netbios_name_servers=['10.0.0.1'],
netbios_node_type=2,
tags={'test': 'testvalue'},
**conn_parameters)
self.assertTrue(dhcp_options_creation_result)
@mock_ec2
def test_when_creating_dhcp_options_fails_the_create_dhcp_options_method_returns_false(self):
with patch('moto.ec2.models.DHCPOptionsSetBackend.create_dhcp_options',