def list(self, request): dnsinfo = DnsApiKey.objects.get(name=request.GET['dnsname']) domain = request.GET['domain'] dnsapi = GodaddyApi(dnsinfo.key, dnsinfo.secret) query = dnsapi.get_records(domain) serializer = GodaddyRecordSerializer(query, many=True) return Response(serializer.data)
def post(self, request): dnsname = request.data['dnsname'] dnsinfo = DnsApiKey.objects.get(name=dnsname) # post_godaddy_domain.delay(dnsinfo.key, dnsinfo.secret, dnsname) dnsapi = GodaddyApi(dnsinfo.key, dnsinfo.secret) query = dnsapi.get_domains() query_domains = [] for item in query: if 'deletedAt' not in item.keys(): print(f'sync_domain: {item}') dnsdomain = dict() dnsdomain['dnsname'] = dnsname dnsdomain['type'] = 'godaddy' dnsdomain['name'] = item['domain'] DnsDomain.objects.update_or_create(name=dnsdomain['name'], defaults=dnsdomain) # 把api查询出来的域名放入列表中 query_domains.append(dnsdomain['name']) # 把数据库查询出来的域名放入列表中 db_domains = DnsDomain.objects.filter(dnsname=dnsname).values("name") print(db_domains) # 两两对比,得到需要删除的列表 delete_domains = [ domain['name'] for domain in db_domains if domain['name'] not in query_domains ] print(f'delete_domains: {delete_domains}') # 循环列表并删除 for domain in delete_domains: DnsDomain.objects.filter(dnsname=dnsname, name=domain).delete() return Response({'status': 'success'})
def post_godaddy_domain(key, secret, dnsname): dnsapi = GodaddyApi(key, secret) query = dnsapi.get_domains() query_domains = [] for item in query: if item['status'] == 'ACTIVE': from time import sleep sleep(1) print(f'sync_domain: {item}') dnsdomain = dict() dnsdomain['dnsname'] = dnsname dnsdomain['type'] = 'godaddy' dnsdomain['name'] = item['domain'] dnsdomain['expire_time'] = datetime.strptime( item['expires'][:10], "%Y-%m-%d") + timedelta(hours=8) DnsDomain.objects.update_or_create(name=dnsdomain['name'], defaults=dnsdomain) # 把api查询出来的域名放入列表中 query_domains.append(dnsdomain['name']) # 把数据库查询出来的域名放入列表中 db_domains = DnsDomain.objects.filter(dnsname=dnsname).values("name") # 两两对比,得到需要删除的列表 delete_domains = [ domain['name'] for domain in db_domains if domain['name'] not in query_domains ] print(f'delete_domains: {delete_domains}') # 循环列表并删除 for domain in delete_domains: DnsDomain.objects.filter(dnsname=dnsname, name=domain).delete()
def post(self, request): dnsinfo = DnsApiKey.objects.get(name=request.data['dnsname']) dnsapi = GodaddyApi(dnsinfo.key, dnsinfo.secret) query = dnsapi.get_domains() for item in query: dnsdomain = dict() dnsdomain['dnsname'] = request.data['dnsname'] dnsdomain['type'] = 'godaddy' dnsdomain['name'] = item['domain'] d, create = DnsDomain.objects.update_or_create( name=dnsdomain['name'], defaults=dnsdomain) return Response({'status': create})
def update(self, request, *args, **kwargs): domain = request.data['domain'] instance = self.get_object() serializer = self.get_serializer(instance, data=request.data, partial=False) serializer.is_valid(raise_exception=True) self.perform_update(serializer) domaininfo = DnsDomain.objects.get(name=domain) domain_type = domaininfo.type name = request.data['name'] value = request.data['value'] type = request.data['type'] ttl = request.data['ttl'] dnsinfo = DnsApiKey.objects.get(name=domaininfo.dnsname) if domain_type == 'dnspod': dnsapi = DnspodApi(dnsinfo.key, dnsinfo.secret, '%s,%s' % (dnsinfo.key, dnsinfo.secret)) record_id = request.data['record_id'] dnsapi.update_record(record_id, domain, name, value, type, ttl=ttl) elif domain_type == 'godaddy': dnsapi = GodaddyApi(dnsinfo.key, dnsinfo.secret) dnsapi.update_record(domain, name, value, type, ttl=ttl) elif domain_type == 'bind': dnsapi = BindApi(user=dnsinfo.key, pwd=None, token=dnsinfo.secret) record_id = request.data['record_id'] dnsapi.update_record(record_id, domain, name, value, type, ttl=ttl) return Response(serializer.data)
def create(self, request, *args, **kwargs): dnsinfo = DnsApiKey.objects.get(name=request.data['dnsname']) domain_type = dnsinfo.type domain = request.data['domain'] name = request.data['name'] value = request.data['value'] type = request.data['type'] ttl = request.data['ttl'] if domain_type == 'dnspod': dnsapi = DnspodApi(dnsinfo.key, dnsinfo.secret, '%s,%s' % (dnsinfo.key, dnsinfo.secret)) query = dnsapi.add_record(domain, name, value, type, ttl) request.data['record_id'] = query['id'] elif domain_type == 'godaddy': dnsapi = GodaddyApi(dnsinfo.key, dnsinfo.secret) query = dnsapi.add_record(domain, name, value, type, ttl) elif domain_type == 'bind': tan = request.data['tan'] dnsapi = BindApi(user=dnsinfo.key, pwd=None, token=dnsinfo.secret) query = dnsapi.add_record(domain, name, value, type, ttl, tan) request.data['record_id'] = query['id'] serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) return Response(query)
def post(self, request, record_type="A", ttl=600): dnsinfo = DnsApiKey.objects.get(name=request.data['dnsname']) domain = request.data['domain'] dnsapi = GodaddyApi(dnsinfo.key, dnsinfo.secret) if request.data['action'] == 'create': sub_domain = request.data['sub_domain'] value = request.data['value'] record_type = request.data.get('record_type', record_type) ttl = request.data.get('ttl', ttl) record = { 'domain': DnsDomain.objects.get(name=domain), 'name': sub_domain, 'type': record_type, 'value': value, 'ttl': ttl, } DnsRecord.objects.update_or_create(**record) query = dnsapi.add_record(domain, sub_domain, value, record_type, ttl=ttl) elif request.data['action'] == 'update': sub_domain = request.data['sub_domain'] value = request.data['value'] record_type = request.data.get('record_type', record_type) ttl = request.data.get('ttl', ttl) record = { 'value': value, 'ttl': ttl, } domainquery = DnsDomain.objects.get(name=domain) DnsRecord.objects.update_or_create(domain=domainquery, name=sub_domain, type=record_type, **record) query = dnsapi.update_record(domain, sub_domain, value, record_type, ttl=ttl) elif request.data['action'] == 'sync': query = dnsapi.get_records(domain) domainquery = DnsDomain.objects.get(name=domain) for item in query: dnsrecord = dict() dnsrecord['name'] = item['domain'] dnsrecord['type'] = item['type'] dnsrecord['value'] = item['data'] dnsrecord['ttl'] = item['ttl'] dnsrecord['title'] = '{}-{}-{}-{}'.format( domainquery, dnsrecord['name'], dnsrecord['type'], dnsrecord['value']) d, create = DnsRecord.objects.update_or_create( domain=domainquery, name=item['name'], type=item['type'], value=dnsrecord['value'], defaults=dnsrecord) return Response({'status': create}) return Response(query)
def list(self, request): dnsinfo = DnsApiKey.objects.get(name=request.GET['dnsname']) dnsapi = GodaddyApi(dnsinfo.key, dnsinfo.secret) query = dnsapi.get_domains() serializer = CustomDomainSerializer(query, many=True) return Response(serializer.data)