def record_save_multi(request): if request.method == 'POST': req = request.REQUEST dns_zone_id = req.get('dns_zone_id', 0) dns_zone_id = int( dns_zone_id) if dns_zone_id and dns_zone_id.isdigit() else 0 lists = req.get('lists', '').strip() owner = req.get('owner', 0) owner = int(owner) if owner and owner.isdigit() else 0 # return Response(lists.splitlines(), status=status.HTTP_200_OK) try: model = DnsZone.objects.get(id=dns_zone_id) except DnsZone.DoesNotExist: return Response('Zone文件不存在,可能已经删除', status=status.HTTP_404_NOT_FOUND) zone = Zone(model) zone.owner = owner zone.user = request.user.username for line in lists.splitlines(): if not line.strip(): continue try: domain, rrtype, rrdata = line.split() zone.save_record(domain, '', rrtype, rrdata, 0, owner) except ZoneException as e: return Response(str(e), status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response('保存成功', status=status.HTTP_200_OK)
def zone_write(request): if request.method == 'POST': dns_zone_id = request.data.get('dns_zone_id', 0) dns_reload = request.data.get('reload', 0) dns_reload = True if dns_reload else False dns_backup = request.data.get('backup', 0) dns_backup = True if dns_backup else False force = 1 owner = 0 if is_dba(request): owner = 3 force = 0 try: model = DnsZone.objects.get(id=dns_zone_id) except DnsZone.DoesNotExist: return Response('Zone文件不存在,可能已经删除', status=status.HTTP_404_NOT_FOUND) zone = Zone(model) zone.owner = owner zone.user = request.user.username try: zone.validate(dns_reload, dns_backup, force=force) except ZoneException as e: response = Response(str(e), status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: response = Response('生效Zone文件成功', status=status.HTTP_200_OK) return response
def record_save(request): if request.method == 'POST': req = request.data rid = req.get('id', 0) rid = int(rid) if rid and rid.isdigit() else 0 dns_zone_id = req.get('dns_zone_id', 0) dns_zone_id = int( dns_zone_id) if dns_zone_id and dns_zone_id.isdigit() else 0 domain = req.get('domain', '').strip() ttl = req.get('ttl', '').strip() rrtype = req.get('rrtype', '').strip() rrdata = req.get('rrdata', '').strip() owner = req.get('owner', 0) owner = int(owner) if owner and owner.isdigit() else 0 try: model = DnsZone.objects.get(id=dns_zone_id) except DnsZone.DoesNotExist: return Response('Zone文件不存在,可能已经删除', status=status.HTTP_404_NOT_FOUND) zone = Zone(model) zone.owner = owner zone.user = request.user.username try: zone.save_record(domain, ttl, rrtype, rrdata, rid, owner) except ZoneException as e: return Response(str(e), status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response('保存成功', status=status.HTTP_200_OK)
def domain_add(request): ''' 增加域名列表 ''' if request.method == 'POST': key = request.REQUEST.get('key', 'merchant_shop') intime = request.REQUEST.get('intime', '1') intime = int(intime) if intime and intime.isdigit() else 1 lists = [ item.strip() for item in request.REQUEST.get('lists', '').split(',') ] if not lists: response = simplejson.dumps({'success': False, 'msg': u'请输入域名列表'}) return HttpResponse(response) pat = r'^[a-zA-Z0-9]{4,20}(\.yhd\.com)?$' for domain in lists: m = re.match(pat, domain) if not m: response = simplejson.dumps({ 'success': False, 'msg': u'域名不符合规则, 请检查' }) return HttpResponse(response) try: item = DnsApiZone.objects.get(key=key) except DnsApiZone.DoesNotExist: response = simplejson.dumps({'success': False, 'msg': u'无效的请求'}) return HttpResponse(response) model = item.zone if not model: response = simplejson.dumps({'success': False, 'msg': u'Zone不存在'}) return HttpResponse(response) zone = Zone(model) zone.user = '******' zone.owner = 9 # owner = 9 表示属于API exists = [] dones = [] for domain in lists: domain = domain.strip() if zone.domain_exists(domain): exists.append(domain) else: try: zone.save_record(domain, zone.ttl, 'CNAME', 'merchant-shop.yhd.com.yhcdn.cn.') except ZoneException as e: pass else: dones.append(domain) '''if intime and dones: zone.validate(reload=True, backup=False) # 生效''' response = simplejson.dumps({'success': True, 'exists': exists}) return HttpResponse(response)
def domain_sync(request): if request.method == 'GET': zone_id = request.REQUEST.get('zone_id') owner = request.REQUEST.get('owner') zone_id = int(zone_id) if zone_id and zone_id.isdigit() else 0 owner = int(owner) if owner and owner.isdigit() else 9 try: model = DnsZone.objects.get(id=zone_id) except DnsZone.DoesNotExist: return Response({'msg': 'Zone文件不存在,可能已经删除'}, status=status.HTTP_404_NOT_FOUND) zone = Zone(model) zone.owner = owner zone.sync_records(True if owner == 0 else False) return Response({'msg': 'Zone同步成功'}, status=status.HTTP_200_OK)
def domain_validate(request): if request.method == 'GET': zone_id = request.REQUEST.get('zone_id') owner = request.REQUEST.get('owner') force = request.REQUEST.get('force') zone_id = int(zone_id) if zone_id and zone_id.isdigit() else 0 owner = int(owner) if owner and owner.isdigit() else 9 force = True if force else False try: model = DnsZone.objects.get(id=zone_id) except DnsZone.DoesNotExist: return Response({'msg': 'Zone文件不存在,可能已经删除'}, status=status.HTTP_404_NOT_FOUND) zone = Zone(model) zone.owner = owner zone.validate(reload=True, backup=False, force=force) return Response({'msg': 'Zone生效成功'}, status=status.HTTP_200_OK)
def record_list(request): if request.method == 'GET': dns_zone_id = request.GET.get('dns_zone_id', 0) owner = None if is_dba(request): # DB管理员所属组 owner = 3 # owner = 3 # debug search = request.GET.get('search', '').strip() try: model = DnsZone.objects.get(id=dns_zone_id) except DnsZone.DoesNotExist: return Response('Zone文件不存在', status=status.HTTP_404_NOT_FOUND) zone = Zone(model) if owner is not None: zone.owner = owner records = zone.get_records(search, whole=False) else: records = zone.get_records(search) id_ = request.GET.get('id', None) id_ = int(id_) if id_ and id_.isdigit() else None if id_ is not None: result = None for record in records: if record['id'] == id_: result = record break if result is not None: return Response(result, status=status.HTTP_200_OK) else: # 可能有问题!!!!! return Response('记录不存在,可能已被删除', status=status.HTTP_404_NOT_FOUND) else: return Response({'count': len(records), 'results': records})
if len(new_domain_tmp_list) == 2: domain = new_domain_tmp_list[1] dns_zone_obj = dns_zone_queryset.filter(domain=domain).first() if dns_zone_obj is not None: break new_domain_tmp = domain else: break if dns_zone_obj is None: return { 'success': False, 'msg': '%s is not a valid domain_name' % new_domain } zone = Zone(dns_zone_obj) zone.user = '******' zone.owner = owner # 判断待更改的记录是否合法 dns_record_queryset = zone.get_domains(old_domain, 'A') dns_record_obj = dns_record_queryset.filter(rrdata=old_ip).first() if dns_record_obj is None: return { 'success': False, 'msg': '%s(%s) is not a valid record' % (old_domain, old_ip) } elif dns_record_obj.owner != owner: return { 'success': False, 'msg': 'the record does not belong to you' % (old_domain, old_ip) } try: zone.save_record(new_domain, zone.ttl, 'A', new_ip, dns_record_obj.id)
def domain_dba(request): if request.method == 'GET': action = request.REQUEST.get('action', 'add').strip() if action not in ('add', 'edit'): response = simplejson.dumps({ 'success': False, 'msg': u'action invalid.' }) return HttpResponse(response) domain_old = request.REQUEST.get('oldDomain', '').strip() domain_new = request.REQUEST.get('newDomain', '').strip() ipaddr_old = request.REQUEST.get('oldIP', '').strip() ipaddr_new = request.REQUEST.get('newIP', '').strip() pat = r'^\d{1,3}(\.\d{1,3}){3}$' if not re.match(pat, ipaddr_new): response = simplejson.dumps({ 'success': False, 'msg': u'IP invalid.' }) return HttpResponse(response) env = request.REQUEST.get('env', 'prod') env_id = {'stag': 0, 'prod': 1}.get(env, -1) items = DnsOwner.objects.filter(owner=3) zone_ids = [int(item.dns_zone_id) for item in items] models = DnsZone.objects.filter(id__in=zone_ids, dns_zone_env_id=env_id) model = None for item in models: if domain_new.endswith(item.domain): model = item break if not model: response = simplejson.dumps({ 'success': False, 'msg': 'zone not exists, maybe new domain invalid.' }) return HttpResponse(response) zone = Zone(model) zone.user = '******' zone.owner = 3 # owner = 3 表示属于DBA record_id = 0 if action == 'edit': deny = True domains = zone.get_domains(domain_old, 'A') for domain in domains: if domain.rrdata == ipaddr_old: record_id = domain.id if domain.owner == 3: deny = False break if not record_id: response = simplejson.dumps({ 'success': False, 'msg': 'old domain not exists.' }) return HttpResponse(response) if deny: response = simplejson.dumps({ 'success': False, 'msg': 'this domain does not own to dba, permission deny.' }) return HttpResponse(response) try: zone.save_record(domain_new, zone.ttl, 'A', ipaddr_new, record_id) except ZoneException as e: response = simplejson.dumps({ 'success': False, 'msg': 'error when save record, maybe record duplicated.' }) return HttpResponse(response) try: zone.validate(reload=True, backup=False, force=False) except Exception, e: response = simplejson.dumps({ 'success': False, 'msg': 'error when validate record:' + str(e.args) }) return HttpResponse(response) response = simplejson.dumps({'success': True, 'msg': 'success saved.'}) return HttpResponse(response)
def domain_del(request): ''' 删除域名列表 ''' if request.methed == 'POST': key = request.REQUEST.get('key', 'merchant_shop') intime = request.REQUEST.get('intime', '1') intime = int(intime) if intime and intime.isdigit() else 1 lists = [ item.strip() for item in request.REQUEST.get('lists', '').split(',') ] if not lists: response = simplejson.dumps({'success': False, 'msg': u'请输入域名列表'}) return HttpResponse(response) pat = r'^[a-zA-Z0-9]{4,20}(\.yhd\.com)?$' for domain in lists: m = re.match(pat, domain) if not m: response = simplejson.dumps({ 'success': False, 'msg': u'域名不符合规则, 请检查' }) return HttpResponse(response) try: item = DnsApiZone.objects.get(key=key) except DnsApiZone.DoesNotExist: response = simplejson.dumps({'success': False, 'msg': u'无效的请求'}) return HttpResponse(response) model = item.zone if not model: response = simplejson.dumps({'success': False, 'msg': u'Zone不存在'}) return HttpResponse(response) zone = Zone(model) zone.user = '******' zone.owner = 9 # owner = 9 表示属于API result = [] rlists = [] for domain in lists: domain = domain.strip() domains = zone.get_domains(domain, 'CNAME') if not domains: result.append({'domainName': domain, 'msg': u'域名不存在,无法删除'}) rlists.append(domain) continue o = domains[0] if o.owner != 9: # 只能删除API添加的域名 result.append({ 'domainName': domain, 'msg': u'没有权限删除该域名,请联系SA' }) rlists.append(domain) continue try: zone.delete_record(o.id) except ZoneException as e: result.append({'domainName': domain, 'msg': str(e)}) rlists.append(domain) else: result.append({'domainName': domain, 'msg': 'success'}) response = simplejson.dumps({ 'success': True, 'lists': rlists, 'result': result }) return HttpResponse(response)
def domain_edit(request): ''' 修改域名列表 ''' if request.method == 'POST': key = request.REQUEST.get('key', 'merchant_shop') intime = request.REQUEST.get('intime', '1') intime = int(intime) if intime and intime.isdigit() else 1 lists = request.REQUEST.get('lists') if not lists: response = simplejson.dumps({ 'success': False, 'msg': u'请输入需要修改的域名列表' }) return HttpResponse(response) lists = simplejson.loads(lists) pat = r'^[a-zA-Z0-9]{4,20}(\.yhd\.com)?$' for dict_domain in lists: if not dict_domain: continue old_domain = dict_domain['oldDomainName'] new_domain = dict_domain['newDomainName'] m = re.match(pat, new_domain) if not m: response = simplejson.dumps({ 'success': False, 'msg': u'新域名{0}不符合规则, 请检查'.format(new_domain) }) return HttpResponse(response) try: item = DnsApiZone.objects.get(key=key) except DnsApiZone.DoesNotExist: response = simplejson.dumps({'success': False, 'msg': u'无效的请求'}) return HttpResponse(response) model = item.zone if not model: response = simplejson.dumps({'success': False, 'msg': u'Zone不存在'}) return HttpResponse(response) zone = Zone(model) zone.user = '******' zone.owner = 9 # owner = 9 表示属于API result = [] rlists = [] for dict_domain in lists: if not dict_domain: continue old_domain = dict_domain['oldDomainName'] new_domain = dict_domain['newDomainName'] domains = zone.get_domains(old_domain, 'CNAME') if not domains: # 旧的域名不存在 result.append({ 'oldDomainName': old_domain, 'newDomainName': new_domain, 'msg': u'域名不存在,无法修改' }) rlists.append({ 'oldDomainName': old_domain, 'newDomainName': new_domain }) continue domain = domains[0] if domain.owner != 9: # 只能修改API添加的域名 result.append({ 'oldDomainName': old_domain, 'newDomainName': new_domain, 'msg': u'没有权限修改该域名,请联系SA' }) rlists.append({ 'oldDomainName': old_domain, 'newDomainName': new_domain }) continue try: zone.save_record(new_domain, zone.ttl, 'CNAME', 'merchant-shop.yhd.com.yhcdn.cn.', domain.id) except ZoneException as e: result.append({ 'oldDomainName': old_domain, 'newDomainName': new_domain, 'msg': str(e) }) rlists.append({ 'oldDomainName': old_domain, 'newDomainName': new_domain }) else: result.append({ 'oldDomainName': old_domain, 'newDomainName': new_domain, 'msg': 'success' }) response = simplejson.dumps({ 'success': True, 'result': result, 'lists': rlists }) return HttpResponse(response)
def history(request): if request.method == 'GET': req = request.REQUEST limit = req.get('limit', 10) zone_id = req.get('dns_zone_id', 0) owner = None if is_dba(request): owner = 3 try: model = DnsZone.objects.get(id=zone_id) except DnsZone.DoesNotExist: return Response('Zone文件不存在,可能已经删除' + str(zone_id), status=status.HTTP_404_NOT_FOUND) zone = Zone(model) if owner is None: items = zone.get_history(limit=limit) else: zone.owner = owner items = zone.get_history(limit=limit, whole=False) lists = [] for item in items: old_data = item['old_data'] or {} new_data = item['new_data'] or {} old_domain = old_data.get('domain', '') old_ttl = old_data.get('ttl', '') old_rrtype = old_data.get('rrtype', '') old_rrdata = old_data.get('rrdata', '') new_domain = new_data.get('domain', '') new_ttl = new_data.get('ttl', '') new_rrtype = new_data.get('rrtype', '') new_rrdata = new_data.get('rrdata', '') diff = '' if old_domain != new_domain: diff += '域名: {0}\t=>\t{1}\n'.format(old_domain, new_domain) if old_ttl != new_ttl: diff += 'TTL: {0}\t=>\t{1}\n'.format(old_ttl, new_ttl) if old_rrtype != new_rrtype: diff += '类型: {0}\t=>\t{1}\n'.format(old_rrtype, new_rrtype) if old_rrdata != new_rrdata: diff += '值: {0}\t=>\t{1}\n'.format(old_rrdata, new_rrdata) lists.append({ 'id': item['id'], 'dns_zone_id': item['dns_zone_id'], 'dns_record_id': item['dns_record_id'], 'old_domain': old_domain, 'new_domain': new_domain, 'action': item['action'], 'username': item['username'], 'ctime': item['ctime_date'], 'stamp': stamp2str(item['ctime'], '%Y%m%d%H%M'), 'diff': diff }) return Response({ 'results': lists, 'count': len(lists) }, status=status.HTTP_200_OK)