def update_soa_serial(server, token, soa_content): """Update SOA serial Argument: server: TonicDNS API server token: TonicDNS API authentication token soa_content: SOA record data x-authentication-token: token Get SOA record `cur_soa` is current SOA record. `new_soa` is incremental serial SOA record. """ method = 'GET' uri = 'https://' + server + '/zone/' + soa_content.get('domain') cur_soa, new_soa = connect.tonicdns_client(uri, method, token, data=False, keyword='serial', content=soa_content) # set JSON domain = soa_content.get('domain') cur_o = JSONConverter(domain) new_o = JSONConverter(domain) cur_o.records = [cur_soa] new_o.records = [new_soa] cur_o.generata_data(False) new_o.generata_data(True) # Create new SOA record uri = 'https://' + server + '/zone/' + domain method = 'PUT' connect.tonicdns_client(uri, method, token, new_o.dict_records[0]) # Delete current SOA record why zone has only one SOA record. method = 'DELETE' uri = 'https://' + server + '/zone' connect.tonicdns_client(uri, method, token, cur_o.dict_records[0])
def create_zone(server, token, domain, identifier, dtype, master=None): """Create zone records. Arguments: server: TonicDNS API server token: TonicDNS API authentication token domain: Specify domain name identifier: Template ID dtype: MASTER|SLAVE|NATIVE (default: MASTER) master: master server ip address when dtype is SLAVE (default: None) ContentType: application/json x-authentication-token: token """ method = 'PUT' uri = 'https://' + server + '/zone' obj = JSONConverter(domain) obj.generate_zone(domain, identifier, dtype, master) connect.tonicdns_client(uri, method, token, obj.zone)
def create_zone(args): """Create zone. Argument: args: arguments object """ action = True password = get_password(args) token = connect.get_token(args.username, password, args.server) domain = args.domain template = args.domain.replace('.', '_') master = None dnsaddr = args.dnsaddr if args.__dict__.get('S'): dtype = 'SLAVE' master = dnsaddr elif args.__dict__.get('N'): dtype = 'NATIVE' else: dtype = 'MASTER' # generate template data o = JSONConverter(domain) o.generate_template(domain, dnsaddr, desc='') # create template processing.create_template(args.server, token, template, o.record) # create zone processing.create_zone(args.server, token, domain, template, dtype, master) # delete template processing.delete_template(args.server, token, template)
def delete(args): """Delete records. Argument: args: arguments object """ # for DELETE HTTP method action = False if ((args.__dict__.get('domain') and args.__dict__.get('name') and args.__dict__.get('rtype') and args.__dict__.get('content'))): # for delete sub-command domain = args.domain o = JSONConverter(domain) name, rtype, content, ttl, priority = get_record_params(args) record_dict = o.set_record(name, rtype, content, ttl, priority) json = set_json(domain, action, record=record_dict) else: # for bulk_delete sub-command if args.__dict__.get('domain'): domain = args.domain else: domain = check_infile(args.infile) json = set_json(domain, action, filename=args.infile) password = get_password(args) token = connect.get_token(args.username, password, args.server) processing.delete_records(args.server, token, json) if args.auto_update_soa == 'True': update_soa_serial(args)
def update_soa_serial(server, token, soa_content): """Update SOA serial Argument: server: TonicDNS API server token: TonicDNS API authentication token soa_content: SOA record data x-authentication-token: token Get SOA record `cur_soa` is current SOA record. `new_soa` is incremental serial SOA record. """ method = 'GET' uri = 'https://' + server + '/zone/' + soa_content.get('domain') cur_soa, new_soa = connect.tonicdns_client( uri, method, token, data=False, keyword='serial', content=soa_content) # set JSON domain = soa_content.get('domain') cur_o = JSONConverter(domain) new_o = JSONConverter(domain) cur_o.records = [cur_soa] new_o.records = [new_soa] cur_o.generata_data(False) new_o.generata_data(True) # Create new SOA record uri = 'https://' + server + '/zone/' + domain method = 'PUT' connect.tonicdns_client(uri, method, token, new_o.dict_records[0]) # Delete current SOA record why zone has only one SOA record. method = 'DELETE' uri = 'https://' + server + '/zone' connect.tonicdns_client(uri, method, token, cur_o.dict_records[0])
def set_json(domain, action, filename=False, record=False): """Convert text file to JSON. Arguments: domain: domain name of updating target action: True ; for PUT/POST HTTP method False; for DELETE HTTP method filename: text file of bulk updating (default is False) record: json record of updating single record (default is False) """ o = JSONConverter(domain) if filename: # for 'bulk_create/bulk_delete' with open(filename, 'r') as f: o.separate_input_file(f) for item in o.separated_list: o.read_records(item.splitlines()) o.generata_data(action) elif record: # for 'create/delete' o.read_records(record) o.generata_data(action) return o.dict_records
def response(uri, method, res, token='', keyword='', content='', raw_flag=False): """Response of tonicdns_client request Arguments: uri: TonicDNS API URI method: TonicDNS API request method res: Response of against request to TonicDNS API token: TonicDNS API token keyword: Processing keyword content: JSON data raw_flag: True is return responsed raw data, False is pretty print """ if method == 'GET' or (method == 'PUT' and not token): # response body data = res.read() data_utf8 = data.decode('utf-8') if token: datas = json.loads(data_utf8) else: token = json.loads(data_utf8)['hash'] return token if keyword == 'serial': # filtering with keyword record = search_record(datas, 'SOA')[0] # if SOA record, remove priority unnecessary del record['priority'] # override ttl record['ttl'] = int(record['ttl']) c = JSONConverter(content['domain']) new_record = c.get_soa(record, content) return record, new_record elif keyword: # '--search' option of 'get' subcommand records = search_record(datas, keyword) datas.update({"records": records}) if uri.split('/')[3] == 'template': # 'tmpl_get' subcommand if len(uri.split('/')) == 5: # when specify template identfier #print_formatted(datas) utils.pretty_print(datas) else: # when get all templates for data in datas: #print_formatted(data) utils.pretty_print(datas) else: # 'get' subcommand if raw_flag: return datas else: #print_formatted(datas) if len(uri.split('zone/')) > 1: domain = uri.split('zone/')[1] else: domain = '' utils.pretty_print(datas, keyword, domain) else: # response non JSON data data = res.read() print(data)