本文整理汇总了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)