def records(self, domain): i_print('getting domain records') headers = self.headers url = self.baseUrl + '/v1/domains/' + domain.upper() + '/records' d_print('request url: ', url) i_print('sending request') r = requests.get(url=url, headers=headers) d_print('encoded url: ', r.url) if r.status_code == requests.codes.ok: d_print('result', r.json()) i_print('succeeded') return r.json() else: e_print('get domain records failed') r.raise_for_status()
def add_records(self, domain, records): i_print('adding records by domain') headers = self.headers url = self.baseUrl + '/v1/domains/' + domain.upper() + '/records' d_print('request url: ', url) data = records d_print('data: ', data) i_print('sending request') r = requests.patch(url=url, headers=headers, json=data) d_print('encoded url: ', r.url) if r.status_code == requests.codes.ok: i_print('succeeded') else: e_print('add records by domain failed') r.raise_for_status()
def update_record(self, domain, type, name, records): i_print('updating record') headers = self.headers url = self.baseUrl + '/v1/domains/' + domain.upper() + '/records/' + \ type.upper() + '/' + name.upper() d_print('request url: ', url) data = records d_print('data: ', data) i_print('sending requests') r = requests.put(url=url, headers=headers, json=data) d_print('encoded url: ', r.url) if r.status_code == requests.codes.ok: i_print('succeeded') else: e_print('add records by type name failed') r.raise_for_status()
def list(self, statuses=None): i_print('getting domains') headers = self.headers url = self.baseUrl + '/v1/domains' d_print('request url: ', url) data = dict() data['statuses'] = statuses i_print('sending request') r = requests.get(url, headers=headers, params=data) d_print('encoded url: ', r.url) if r.status_code == requests.codes.ok: d_print('result', r.json()) i_print('succeeded') return r.json() else: e_print('list domains failed') r.raise_for_status()
def try_update(): config = Config.load() gd = Godaddy.load() domain = config['domain'] local_records = config['records'] local_record_names = list(map(lambda rec: rec['name'], local_records)) records = gd.records(domain) for record in records: record_name = record['name'] if record_name in local_record_names: i_print('trying ', record_name, '.', domain) local_record = local_records[local_record_names.index(record_name)] i_print('record: ', record) i_print('local record: ', local_record) record_type = record['type'] if record_type != local_record['type']: e_print('remote record type does not match local record type') continue record_addr = record['data'] record_ttl = local_record['ttl'] if record_type == 'AAAA': host_addr = get_nic_ipv6_addr(local_record['nic']) elif record_type == 'A': host_addr = get_nic_ipv4_addr() if not host_addr: e_print('get host addr failed') continue i_print('record_addr: ', record_addr) i_print('host_addr: ', host_addr) if record_addr == host_addr: i_print('addr did not change') else: new_records = [ { "data": host_addr, "ttl": record_ttl, } ] try: gd.update_record(domain, record_type, record_name, new_records) except Exception as err: e_print('update record failed, exception: ', err) traceback.print_exc() continue i_print('addr renewed') Config.store(config)