rax-dns-backup: fix parsing

It looks like at some point the RAX bind output changed format
slightly, which messed up our backup script.  Rework it to parse the
current output.

This parsing is obviously a little fragile ... it is nice to have the
output sorted and lined up nicely (like our manually maintained
opendev.org bind files...).  If the format changes again and this
becomes a problem, maybe we switch to dumping the RAX output directly
and forget about formatting it nicely.

Change-Id: I742dd6ef9ffdb377274b384b847625c98dd5ff16
This commit is contained in:
Ian Wienand 2022-11-21 11:44:07 +11:00
parent 95c9cf6ec6
commit 7146cdebaf
No known key found for this signature in database

View File

@ -124,16 +124,20 @@ def do_bind_export(session, token, domain_id, outfile):
query = {'showDetails': 'true'} query = {'showDetails': 'true'}
final_response = session.get(callback_url, params=query, headers=headers) final_response = session.get(callback_url, params=query, headers=headers)
bind_output = final_response.json()['response']['contents'] bind_output = final_response.json()['response']['contents'].split('\n')
output = [] output = []
for line in bind_output.split('\n'):
# Read and parse the record lines for sorting; the skip is because
# the first 3 lines are comments and the rest are SOA records
# (written separately below).
for line in bind_output[10:]:
if line == '': if line == '':
continue continue
fields = line.split(' ') fields = line.split(' ')
output.append(fields) output.append(fields)
# find padding space for the first column # find padding space for the first column so everything lines up nice
max_first = max([len(x[0]) for x in output]) max_first = max([len(x[0]) for x in output])
# create a dict keyed by domain with each record # create a dict keyed by domain with each record
@ -143,10 +147,10 @@ def do_bind_export(session, token, domain_id, outfile):
outstr = '' outstr = ''
# first output SOA then get rid of it # first output comments and SOA from original
outstr += ("%-*s\t%s\n\n" % for line in bind_output[:10]:
(max_first+1, '@', '\t'.join(out_dict['@'][0]) )) outstr += "%s\n" % line
del(out_dict['@']) outstr += '\n'
# print out the rest of the entries, with individual records # print out the rest of the entries, with individual records
# sorted and grouped # sorted and grouped