def parse_acl_file(file_name): # check if all ecs-subnets match the normal ones subnets = [] split_pattern = re.compile(r'[ ;]') with open(file_name) as f: for line in f: if ('ecs' in line) or ("{" in line) or ('}' in line) or ('#' in line): continue net = IP(split_pattern.split(line)[0]) subnets.append((str(net), net.ip, net.broadcast().int())) overlap_subnets = _check_subnet_overlap_subnet(subnets) return overlap_subnets, subnets
def _validate_args(subnet, region, colo): try: sub = IP(subnet) except: raise BadParam('Invalid subnet.', msg_ch=u'错误的网段格式') prefix = sub.prefixlen() if prefix < 16 or prefix > 32: raise BadParam('Invalid subnet.', msg_ch=u'掩码长度在[16-32]之间') _is_valide_region(region) if colo not in SubnetIpDal.get_colo_by_group('subnet'): raise BadParam('Invalid colo', msg_ch=u'请先配置机房')
def add_subnet(subnet, region, colo, comment, username): subnet = IP(subnet) intranet = subnet.iptype() == 'PRIVATE' net_id = subnet.net() broadcast_ip = subnet.broadcast() ips_dict_list = [] for i in subnet: if i == net_id or i == broadcast_ip: continue ips_dict_list.append({'region': region, 'fixed_ip': str(i)}) try: with db.session.begin(subtransactions=True): subnet_item = Subnets(region_name=region, subnet=subnet.strCompressed(), create_user=username, intranet=intranet, colo=colo) if comment: subnet_item.comment = comment db.session.add(subnet_item) db.session.bulk_insert_mappings(IpPool, ips_dict_list) except Exception: raise BadParam('Ip conflict with other regions', msg_ch=u'和已有的网段有交叉,请检查后重试')