def _validate_args(domain_name, param_dict): is_valid_domain_name(domain_name) if param_dict.get('ttl', 0) < 0: raise BadParam("Invalid ttl.", msg_ch='ttl必须大于等于0') record_type = param_dict.get('record_type', None) if record_type: check_record = param_dict['check_record'] record = param_dict['record'] if record_type == 'CNAME': if domain_name == param_dict['record']: raise BadParam("Self-CNAME is not allowed.", msg_ch='域名和CNAME不能相同') if check_record and not is_valid_cname(record): raise BadParam('Invalid CNAME record', msg_ch='CNAME记录的zone必须在DNSDB管理中') elif record_type == 'A': record = format_ipv4(record) if check_record and (not SubnetIpDal.is_ip_exist(record)): raise BadParam('No such ip %s' % record, msg_ch='IP在数据库中不存在') elif record_type == 'AAAA': record = format_ipv6(record) if check_record and (not SubnetIpDal.is_ip_exist(record)): raise BadParam('No such ip %s' % record, msg_ch='IP在数据库中不存在') else: raise BadParam("Invalid record type.", msg_ch='记录类型只能是[A, CNAME]') param_dict['record'] = record
def auto_add_record(domain, region, username): is_valid_domain_name(domain) if not SubnetIpDal.is_intranet_region(region): raise BadParam("Can't automatically bind an A record with public ip.", msg_ch=u'不能自动绑定A记录到公网IP') if ZoneRecordDal.get_domain_records(domain_name=domain): raise BadParam('Domain name %s has already had a record' % domain, msg_ch=u'域名已有记录, 不能自动绑定') return ZoneRecordDal.auto_add_record(domain, region, username)
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 _validate_args(subnet, region, colo): if '/' not in subnet: raise BadParam('Invalid subnet.', msg_ch=u'请使用cidr格式的网段') _is_valide_region(region) if colo not in SubnetIpDal.get_colo_by_group('subnet'): raise BadParam('Invalid colo', msg_ch=u'请先配置机房') try: sub = ipaddress.ip_network(subnet) except Exception as e: raise BadParam('Invalid subnet: %s' % e, msg_ch=u'错误的网段格式') prefix = sub.prefixlen if sub.version == 4: if prefix < 16 or prefix > 32: raise BadParam('Invalid subnet.', msg_ch=u'IPv4掩码长度在[16-32]之间') else: if prefix < 64 or prefix > 128: raise BadParam('Invalid subnet.', msg_ch=u'IPv6掩码长度在[64-128]之间')
def get_region_by_name(region): return SubnetIpDal.get_region_by_name_like(region)
def get_region_by_name(region): return [SubnetIpDal.get_region_by_name(region)]
def list_region(): return SubnetIpDal.list_region()
def get_region_by_ip(ip): return [SubnetIpDal.get_region_by_ip(ip)]
def get_subnet_colos(): return SubnetIpDal.get_colo_by_group('subnet')
def get_subnet_ip(region): return SubnetIpDal.get_subnet_ip(region)
def rename_subnet(old_region, new_region, username): _is_valide_region(new_region) return SubnetIpDal.rename_subnet(old_region, new_region, username)
def add_subnet(subnet, region, colo, comment, username): _validate_args(subnet, region, colo) return SubnetIpDal.add_subnet(subnet, region, colo, comment, username)
def delete_subnet(subnet, region): return SubnetIpDal.delete_subnet(subnet, region)