Example #1
0
def record_exist(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()
        rrtype = req.get('rrtype', '').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)
        items = zone.get_domains(domain, rrtype)
        exists = True if items and rid not in [item.id
                                               for item in items] else False
        return Response(exists, status=status.HTTP_200_OK)
Example #2
0
         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)
 except Exception, e:
     return {'success': False, 'msg': e.args}
Example #3
0
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)
Example #4
0
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)
Example #5
0
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)