Allow subnet deletion by name

This commit is contained in:
Claudiu Popescu 2015-03-31 12:36:17 +02:00
parent 524b3117ac
commit af7592c741

View File

@ -339,9 +339,9 @@ def create_subnet(vpc_id, cidr_block, availability_zone=None, subnet_name=None,
return False
def delete_subnet(subnet_id, region=None, key=None, keyid=None, profile=None):
def delete_subnet(name=None, subnet_id=None, region=None, key=None, keyid=None, profile=None):
'''
Given a subnet ID, delete the subnet.
Given a subnet ID or name, delete the subnet.
Returns True if the subnet was deleted and returns False if the subnet was not deleted.
@ -357,6 +357,16 @@ def delete_subnet(subnet_id, region=None, key=None, keyid=None, profile=None):
if not conn:
return False
if not any((name, subnet_id)):
raise SaltInvocationError("Either name or subnet ID needs to be specified.")
if name:
subnets = describe_subnets(region=region, key=key, keyid=keyid, profile=profile)
for subnet in subnets:
if 'Name' in subnet['tags']:
if subnet['tags']['Name'] == name:
subnet_id = subnet['id']
try:
if conn.delete_subnet(subnet_id):
log.debug('Subnet {0} was deleted.'.format(subnet_id))