def main(): pp = pprint.PrettyPrinter(indent=4) my_acct = Account(api_key=keyring.get_password('godaddy', 'apikey'), \ api_secret=keyring.get_password('godaddy', 'apisecret')) client = Client(my_acct) domains = client.get_domains() print("{}".format(domains)) for dom in domains: r = client.get_domain_info(dom) print("{}:".format(r['domain']), end=" ") if r['status'] == 'CANCELLED': cprint("{}".format(r['status']), "red") elif r['status'] == 'ACTIVE': cprint("{}".format(r['status']), "green") records = client.get_records(r['domain']) #pp.pprint(records) has_caa = False has_mxs = False has_soa = False has_cnames = False has_as = False has_nss = False for R in records: if R['type'] == 'A': has_as = True elif R['type'] == 'SOA': has_soa = True elif R['type'] == 'CAA': has_caa = True elif R['type'] == 'CNAME': has_cnames = True elif R['type'] == 'NS': has_nss = True elif R['type'] == 'MX': has_mxs = True else: cprint("Unrecognized type: {}".format(R['type']), \ "magenta") print("\tA: {}, CNAME: {}, SOA: {}, CAA: {}, MX: {}, NS: {}"\ .format(has_as, has_cnames, has_soa, has_caa, has_mxs, \ has_nss)) else: print("Unrecognized domain status: {}: {}".format(\ r['domain'], r['status']))
class GoDaddy: def __init__(self, **config): self.domain_name = config.get('domain_name') self.alias_name = config.get('alias_name') self.__access_id = config.get('access_id') self.acct = Account(api_key=self.__access_id, api_secret=config.get('access_key')) self.__client = Client(self.acct) def describe_record(self, *args, **kwargs): return self.__client.get_records(kwargs.get('domain_name'), record_type=kwargs.get('domain_type'), name=kwargs.get('domain_rr')) def add_record(self, *args, **kwargs): params = dict(name=kwargs.get('domain_rr'), data=kwargs.get('domain_value'), type=kwargs.get('domain_type'), ttl=int(kwargs.get('domain_ttl'))) result_data = self.__client.add_record(kwargs.get('domain_name'), params) if result_data is True: result_data = str(uuid()) return result_data def update_record(self, *args, **kwargs): params = dict(name=kwargs.get('domain_rr'), data=kwargs.get('domain_value'), type=kwargs.get('domain_type'), ttl=int(kwargs.get('domain_ttl'))) result_data = self.__client.update_record(kwargs.get('domain_name'), params) return result_data def remark(self, *args, **kwargs): return dict(code=0, msg='GoDaddy不支持修改') def set_record_status(self, *args, **kwargs): if kwargs.get('status') in ['开启', '启用', 'Enable', 'enable', 'ENABLE']: self.add_record(**kwargs) elif kwargs.get('status') in ['暂停', '禁用', 'disable']: self.del_record(**kwargs) else: return False return True def del_record(self, *args, **kwargs): domain_name = kwargs.get('domain_name') name = kwargs.get('domain_rr') record_type = kwargs.get('domain_type') result_data = self.__client.delete_records(domain_name, name, record_type=record_type) return result_data def describe_domains(self): domain_list = self.__client.get_domains() if not domain_list: return False for domain in domain_list: domain_info_list = self.__client.get_domain_info(domain) domain_info_list['records'] = len( self.__client.get_records(domain)) yield domain_info_list def record_generator(self, **domain): record_info_list = self.__client.get_records(domain.get('domain')) if not record_info_list: return False for record in record_info_list: yield dict(domain_name=domain.get('domain'), data_dict=record)
class GodaddyApi: def __init__(self, api_key, api_secret): self.api_key = api_key self.api_secret = api_secret my_acct = Account(api_key, api_secret) self.__client = Client(my_acct) self.Authorization = "sso-key %s:%s" % (self.api_key, self.api_secret) self.headers = {"Authorization": self.Authorization, "accept": "application/json","Content-Type": "application/json"} def domain_list(self): Authorization = "sso-key %s:%s" % (self.api_key, self.api_secret) headers = {"Authorization": Authorization} all_domain = [] status = True marker = "" while True: url = 'https://api.godaddy.com/v1/domains?limit=1000&marker={}'.format(marker) r = requests.get(url, headers=headers) status = True if r.json() else False if not status: break for domain in r.json(): all_domain.append(domain['domain']) marker = domain['domain'] return all_domain def domain_info(self, domain): ''' :param domain: get "domain" information. :return: dict ''' return self.__client.get_domain_info(domain) # return self.__client.get_domain_info(domain)['expires'] # return self.__client.get_domain_info(domain)['nameServers'] def record_list(self, domain): ''' :param domain: get "domain" records. :return: list, each one record is a dict, include 'data' 'name' 'ttl' and 'type' field. ''' return self.__client.get_records(domain) def record_create(self,domain,data,name,type): data_dict = [{"data": data, "name": name, "ttl": 3600, "type": type}] data_dict_json = json.dumps(data_dict) Authorization = "sso-key %s:%s" % (self.api_key, self.api_secret) headers = {"Authorization": Authorization,"accept": "application/json","Content-Type": "application/json"} url = "https://api.godaddy.com/v1/domains/{}/records".format(domain) r = requests.patch(url,data_dict_json,headers=headers) return r.status_code def record_edit(self,domain, data, record_type=None, name=None): data_dict = [{"data":data}] data_dict_json = json.dumps(data_dict) url = "https://api.godaddy.com/v1/domains/{}/records/{}/{}".format(domain,record_type,name) r = requests.put(url,data_dict_json,headers=self.headers) return r.status_code #获取单个记录的值,也用来判断记录是否存在 def get_record(self,domain,record,type): """ 返回的结果为[{'data': '42.97.23.79', 'name': 'test2', 'ttl': 600, 'type': 'A'}]这种数据结构 """ url = "https://api.godaddy.com/v1/domains/{}/records/{}/{}".format(domain,type,record) r = requests.get(url,headers=self.headers) return r.json() def record_delte(self,domain,record_name): self.__client.delete_records(domain,record_name) def edit_ns(self,domain,nameservers): """nameservers为列表,元素为需要设置的nameserver域名""" data = {"locked":True,"nameServers":nameservers,"renewAuto":False} url = "https://api.godaddy.com/v1/domains/{}".format(domain) r = requests.patch(url,data=json.dumps(data),headers=self.headers) return r.text