#NEWLINE# def subnet_present( name , cidr_block , vpc_name = None , vpc_id = None , availability_zone = None , tags = None , region = None , key = None , keyid = None , profile = None , route_table_id = None , route_table_name = None ) : #NEWLINE# #INDENT# '\n    Ensure a subnet exists.\n\n    name\n        Name of the subnet.\n\n    cidr_block\n        The range if IPs for the subnet, in CIDR format. For example:\n        10.0.0.0/24. Block size must be between /16 and /28 netmask.\n\n    vpc_name\n        Name of the VPC in which the subnet should be placed. Either\n        vpc_name or vpc_id must be provided.\n\n    vpc_id\n        Id of the VPC in which the subnet should be placed. Either vpc_name\n        or vpc_id must be provided.\n\n    availability_zone\n        AZ in which the subnet should be placed.\n\n    tags\n        A list of tags.\n\n    route_table_id\n        A route table ID to explicitly associate the subnet with.  If both route_table_id\n        and route_table_name are specified, route_table_id will take precedence.\n\n        .. versionadded:: Carbon\n\n    route_table_name\n        A route table name to explicitly associate the subnet with.  If both route_table_id\n        and route_table_name are specified, route_table_id will take precedence.\n\n        .. versionadded:: Carbon\n\n    region\n        Region to connect to.\n\n    key\n        Secret key to be used.\n\n    keyid\n        Access key to be used.\n\n    profile\n        A dict with region, key and keyid, or a pillar key (string) that\n        contains a dict with region, key and keyid.\n    ' #NEWLINE# ret = { #NEWLINE# #INDENT# 'name' : name , #NEWLINE# 'result' : True , #NEWLINE# 'comment' : '' , #NEWLINE# 'changes' : { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } , #NEWLINE# #UNINDENT# } #NEWLINE# r = __salt__ [ 'boto_vpc.subnet_exists' ] ( subnet_name = name , tags = tags , region = region , key = key , keyid = keyid , profile = profile ) #NEWLINE# if ( 'error' in r ) : #NEWLINE# #INDENT# ret [ 'result' ] = False #NEWLINE# ret [ 'comment' ] = 'Failed to create subnet: {0}.' . format ( r [ 'error' ] [ 'message' ] ) #NEWLINE# return ret #NEWLINE# #UNINDENT# route_table_desc = None #NEWLINE# _describe = None #NEWLINE# rtid = None #NEWLINE# if ( route_table_id or route_table_name ) : #NEWLINE# #INDENT# rt = None #NEWLINE# route_table_found = False #NEWLINE# if route_table_id : #NEWLINE# #INDENT# rtid = route_table_id #NEWLINE# rt = __salt__ [ 'boto_vpc.route_table_exists' ] ( route_table_id = route_table_id , region = region , key = key , keyid = keyid , profile = profile ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if route_table_name : #NEWLINE# #INDENT# rtid = route_table_name #NEWLINE# rt = __salt__ [ 'boto_vpc.route_table_exists' ] ( route_table_name = route_table_name , region = region , key = key , keyid = keyid , profile = profile ) #NEWLINE# #UNINDENT# #UNINDENT# if rt : #NEWLINE# #INDENT# if ( 'exists' in rt ) : #NEWLINE# #INDENT# if rt [ 'exists' ] : #NEWLINE# #INDENT# if route_table_id : #NEWLINE# #INDENT# route_table_found = True #NEWLINE# route_table_desc = __salt__ [ 'boto_vpc.describe_route_table' ] ( route_table_id = route_table_id , region = region , key = key , keyid = keyid , profile = profile ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if route_table_name : #NEWLINE# #INDENT# route_table_found = True #NEWLINE# route_table_desc = __salt__ [ 'boto_vpc.describe_route_table' ] ( route_table_name = route_table_name , region = region , key = key , keyid = keyid , profile = profile ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( not route_table_found ) : #NEWLINE# #INDENT# ret [ 'result' ] = False #NEWLINE# ret [ 'comment' ] = 'The specified route table {0} could not be found.' . format ( rtid ) #NEWLINE# return ret #NEWLINE# #UNINDENT# #UNINDENT# if ( not r . get ( 'exists' ) ) : #NEWLINE# #INDENT# if __opts__ [ 'test' ] : #NEWLINE# #INDENT# ret [ 'comment' ] = 'Subnet {0} is set to be created.' . format ( name ) #NEWLINE# ret [ 'result' ] = None #NEWLINE# return ret #NEWLINE# #UNINDENT# r = __salt__ [ 'boto_vpc.create_subnet' ] ( subnet_name = name , cidr_block = cidr_block , availability_zone = availability_zone , vpc_name = vpc_name , vpc_id = vpc_id , tags = tags , region = region , key = key , keyid = keyid , profile = profile ) #NEWLINE# if ( not r . get ( 'created' ) ) : #NEWLINE# #INDENT# ret [ 'result' ] = False #NEWLINE# ret [ 'comment' ] = 'Failed to create subnet: {0}' . format ( r [ 'error' ] [ 'message' ] ) #NEWLINE# return ret #NEWLINE# #UNINDENT# _describe = __salt__ [ 'boto_vpc.describe_subnet' ] ( subnet_id = r [ 'id' ] , region = region , key = key , keyid = keyid , profile = profile ) #NEWLINE# ret [ 'changes' ] [ 'old' ] = { #NEWLINE# #INDENT# 'subnet' : None , #NEWLINE# #UNINDENT# } #NEWLINE# ret [ 'changes' ] [ 'new' ] = _describe #NEWLINE# ret [ 'comment' ] = 'Subnet {0} created.' . format ( name ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ret [ 'comment' ] = 'Subnet present.' #NEWLINE# #UNINDENT# if route_table_desc : #NEWLINE# #INDENT# if ( not _describe ) : #NEWLINE# #INDENT# _describe = __salt__ [ 'boto_vpc.describe_subnet' ] ( subnet_name = name , region = region , key = key , keyid = keyid , profile = profile ) #NEWLINE# #UNINDENT# if ( not _verify_subnet_association ( route_table_desc , _describe [ 'subnet' ] [ 'id' ] ) ) : #NEWLINE# #INDENT# if __opts__ [ 'test' ] : #NEWLINE# #INDENT# msg = 'Subnet is set to be associated with route table {0}' . format ( rtid ) #NEWLINE# ret [ 'comment' ] = ' ' . join ( [ ret [ 'comment' ] , msg ] ) #NEWLINE# ret [ 'result' ] = None #NEWLINE# return ret #NEWLINE# #UNINDENT# if ( 'explicit_route_table_association_id' in _describe [ 'subnet' ] ) : #NEWLINE# #INDENT# log . debug ( 'Need to disassociate from existing route table' ) #NEWLINE# drt_ret = __salt__ [ 'boto_vpc.disassociate_route_table' ] ( _describe [ 'subnet' ] [ 'explicit_route_table_association_id' ] , region = region , key = key , keyid = keyid , profile = profile ) #NEWLINE# if ( not drt_ret [ 'disassociated' ] ) : #NEWLINE# #INDENT# msg = 'Unable to disassociate subnet {0} with its current route table.' . format ( name ) #NEWLINE# ret [ 'comment' ] = ' ' . join ( [ ret [ 'comment' ] , msg ] ) #NEWLINE# ret [ 'result' ] = False #NEWLINE# return ret #NEWLINE# #UNINDENT# #UNINDENT# if ( 'old' not in ret [ 'changes' ] ) : #NEWLINE# #INDENT# ret [ 'changes' ] [ 'old' ] = _describe #NEWLINE# #UNINDENT# art_ret = __salt__ [ 'boto_vpc.associate_route_table' ] ( route_table_id = route_table_desc [ 'id' ] , subnet_name = name , region = region , key = key , keyid = keyid , profile = profile ) #NEWLINE# if ( 'error' in art_ret ) : #NEWLINE# #INDENT# msg = 'Failed to associate subnet {0} with route table {1}: {2}.' . format ( name , rtid , art_ret [ 'error' ] [ 'message' ] ) #NEWLINE# ret [ 'comment' ] = ' ' . join ( [ ret [ 'comment' ] , msg ] ) #NEWLINE# ret [ 'result' ] = False #NEWLINE# return ret #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# msg = 'Subnet successfully associated with route table {0}.' . format ( rtid ) #NEWLINE# ret [ 'comment' ] = ' ' . join ( [ ret [ 'comment' ] , msg ] ) #NEWLINE# if ( 'new' not in ret [ 'changes' ] ) : #NEWLINE# #INDENT# ret [ 'changes' ] [ 'new' ] = __salt__ [ 'boto_vpc.describe_subnet' ] ( subnet_name = name , region = region , key = key , keyid = keyid , profile = profile ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ret [ 'changes' ] [ 'new' ] [ 'subnet' ] [ 'explicit_route_table_association_id' ] = art_ret [ 'association_id' ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# ret [ 'comment' ] = ' ' . join ( [ ret [ 'comment' ] , 'Subnet is already associated with route table {0}' . format ( rtid ) ] ) #NEWLINE# #UNINDENT# #UNINDENT# return ret