本文整理匯總了Python中ipcalc.Network.check_collision方法的典型用法代碼示例。如果您正苦於以下問題:Python Network.check_collision方法的具體用法?Python Network.check_collision怎麽用?Python Network.check_collision使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ipcalc.Network
的用法示例。
在下文中一共展示了Network.check_collision方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: add_network_cidr_mapping
# 需要導入模塊: from ipcalc import Network [as 別名]
# 或者: from ipcalc.Network import check_collision [as 別名]
def add_network_cidr_mapping(self,
network_config):
'''
Method calculates and adds a CloudFormation mapping that is used to set VPC and Subnet CIDR blocks. Calculated based on CIDR block sizes and additionally checks to ensure all network segments fit inside of the specified overall VPC CIDR
@param network_config [dict] dictionary of values containing data for creating
'''
public_subnet_count = int(network_config.get('public_subnet_count', 2))
private_subnet_count = int(network_config.get('private_subnet_count', 2))
public_subnet_size = str(network_config.get('public_subnet_size', '24'))
private_subnet_size = str(network_config.get('private_subnet_size', '22'))
network_cidr_base = str(network_config.get('network_cidr_base', '172.16.0.0'))
network_cidr_size = str(network_config.get('network_cidr_size', '20'))
first_network_address_block = str(network_config.get('first_network_address_block', network_cidr_base))
ret_val = {}
cidr_info = Network(network_cidr_base + '/' + network_cidr_size)
base_cidr = cidr_info.network().to_tuple()[0] + '/' + str(cidr_info.to_tuple()[1])
ret_val['vpcBase'] = {'cidr': base_cidr}
current_base_address = first_network_address_block
for public_subnet_id in range(0, public_subnet_count):
if not cidr_info.check_collision(current_base_address):
raise RuntimeError('Cannot continue creating network--current base address is outside the range of the master Cidr block. Found on pass ' + str(public_subnet_id + 1) + ' when creating public subnet cidrs')
ip_info = Network(current_base_address + '/' + str(public_subnet_size))
range_info = ip_info.network().to_tuple()
if 'subnet' + str(public_subnet_id) not in ret_val:
ret_val['subnet' + str(public_subnet_id)] = dict()
ret_val['subnet' + str(public_subnet_id)]['public'] = ip_info.network().to_tuple()[0] + '/' + str(ip_info.to_tuple()[1])
current_base_address = IP(int(ip_info.host_last().hex(), 16) + 2).to_tuple()[0]
range_reset = Network(current_base_address + '/' + str(private_subnet_size))
current_base_address = IP(int(range_reset.host_last().hex(), 16) + 2).to_tuple()[0]
for private_subnet_id in range(0, private_subnet_count):
if not cidr_info.check_collision(current_base_address):
raise RuntimeError('Cannot continue creating network--current base address is outside the range of the master Cidr block. Found on pass ' + str(private_subnet_id + 1) + ' when creating private subnet cidrs')
ip_info = Network(current_base_address + '/' + str(private_subnet_size))
range_info = ip_info.network().to_tuple()
if 'subnet' + str(private_subnet_id) not in ret_val:
ret_val['subnet' + str(private_subnet_id)] = dict()
ret_val['subnet' + str(private_subnet_id)]['private'] = ip_info.network().to_tuple()[0] + '/' + str(ip_info.to_tuple()[1])
current_base_address = IP(int(ip_info.host_last().hex(), 16) + 2).to_tuple()[0]
return self.template.add_mapping('networkAddresses', ret_val)