def add_dns_record(ali_api_id, ali_api_secret, ip, request_domain, request_sub_domain): client = AcsClient(ali_api_id, ali_api_secret, 'cn-hangzhou') request = AddDomainRecordRequest() request.set_accept_format('json') request.set_DomainName(request_domain) request.set_RR(request_sub_domain) request.set_Type("A") request.set_Value(ip) try: response = client.do_action_with_exception(request) except (ClientException, ServerException) as reason: log.error("add dns failed. do_action_with_exception " + reason) return False result = str(response, encoding='utf-8') log.info("add dns record result: \r\n" + result) json_obj = json.loads(result) if len(json_obj['RecordId']) == 0: log.error("add dns failed. error code " + json_obj['code']) return False return True
def updateDomainRecord(client, rr, domain, ip, type): records = getRecords(client, rr, domain) record_id = None for record in records: if record['RR'] == rr and record['DomainName'] == domain and record[ 'Type'] == type: record_id = record['RecordId'] avail_ip = record['Value'] avail_record = record break if record_id is None: print("不存在记录,正在添加记录") addDomainRecord(client, rr, domain, ip, type) else: print("存在记录:" + str(avail_record)) if avail_ip == ip: print("ip未改变,不更改记录,原ip=" + avail_ip + ",现ip=" + ip) else: print("ip已改变,准备更改记录,原ip=" + avail_ip + ",现ip=" + ip) request = UpdateDomainRecordRequest() request.set_action_name("UpdateDomainRecord") request.set_RR(rr) request.set_RecordId(record_id) request.set_Type(type) request.set_Value(ip) request.set_TTL(600) request.set_accept_format('json') response = client.do_action_with_exception(request) print(str(response, encoding='utf-8')) return json.loads(response)['RecordId']
def Dns(_rr): request = UpdateDomainRecordRequest() request.set_accept_format('json') logging.info('Update %s.%s DNS' % (_rr, domain)) logging.info("Getting RecordId....") record = getRecord(_rr) recordId = record['RecordId'] logging.info("RecordId:%s" % recordId) logging.info("Getting IP....") ip = getIP() logging.info("IP:%s" % ip) if ip == record['Value']: return "IP consistency does not need to be updated." logging.info("Updating DNS....") request.set_Value(ip) request.set_RecordId(recordId) request.set_RR(_rr) request.set_Type("A") response = client.do_action_with_exception(request) return json.loads(response.decode("UTF-8"))
def addDomainRecord(client, rr, domain, ip, type): request = AddDomainRecordRequest() request.set_accept_format('json') request.set_Value(ip) request.set_Type(type) request.set_RR(rr) request.set_DomainName(domain) response = client.do_action_with_exception(request) print(str(response, encoding='utf-8')) return json.loads(response)['RecordId']
def Describe_SubDomain_Records(client, record_type, subdomain): request = DescribeSubDomainRecordsRequest() request.set_accept_format('json') request.set_Type(record_type) request.set_SubDomain(subdomain) response = client.do_action_with_exception(request) response = str(response, encoding='utf-8') relsult = json.loads(response) return relsult
def update_dns(dns_rr, dns_type, dns_value, dns_record_id, dns_ttl, dns_format): clt = client.AcsClient(access_key_id, access_key_secret, 'cn-hangzhou') request = UpdateDomainRecordRequest.UpdateDomainRecordRequest() request.set_RR(dns_rr) request.set_Type(dns_type) request.set_Value(dns_value) request.set_RecordId(dns_record_id) request.set_TTL(dns_ttl) request.set_accept_format(dns_format) result = clt.do_action(request) return result
def update_record(client, priority, ttl, record_type, value, rr, record_id): request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_Priority(priority) request.set_TTL(ttl) request.set_Value(value) request.set_Type(record_type) request.set_RR(rr) request.set_RecordId(record_id) response = client.do_action_with_exception(request) response = str(response, encoding='utf-8') return response
def add_record(client, priority, ttl, record_type, value, rr, domainname): request = AddDomainRecordRequest() request.set_accept_format('json') request.set_Priority(priority) request.set_TTL(ttl) request.set_Value(value) request.set_Type(record_type) request.set_RR(rr) request.set_DomainName(domainname) response = client.do_action_with_exception(request) response = str(response, encoding='utf-8') relsult = json.loads(response) return relsult
def UpdateDomainRecord(client, yaml_data, RecordId, ip): try: dnsData = yaml_data['DnsData'] request = UpdateDomainRecordRequest() request.set_accept_format('json') DomainValue = ip request.set_Value(DomainValue) request.set_Type(dnsData['DomainType']) request.set_RR(dnsData['RR']) request.set_RecordId(RecordId) response = client.do_action_with_exception(request) print("更新域名解析成功") print("域名:" + dnsData['DomainName'] + " 主机:" + dnsData['RR'] + " 记录类型:" + dnsData['DomainType'] + " 记录值:" + DomainValue) except Exception as e: print("更新域名解析失败") print(e)
def update_record(self, RR, value): request = UpdateDomainRecordRequest.UpdateDomainRecordRequest() request.set_action_name("UpdateDomainRecord") request.set_accept_format('json') request.set_TTL('600') sRecordid = self.get_record_id(RR) if sRecordid != None and WordsCheck.RegexChecker.judgeLegalIpv4(value): request.set_RecordId(sRecordid) request.set_Type(self.record_type) request.set_RR(RR) request.set_Value(value) else: return False pass try: # jsonReturn = json.load(self.client.do_action_with_exception(request)) # rc = self.client.do_action_with_exception(request) if None != json.loads( self.client.do_action_with_exception(request).decode( 'utf-8')): return True except: return False