Adjustment to update records in pdns 4.x.x

This commit is contained in:
Khanh Ngo 2016-06-07 17:05:41 +07:00
parent 23972ff09f
commit fb51bce1f8

View File

@ -667,6 +667,7 @@ class Record(object):
if NEW_SCHEMA: if NEW_SCHEMA:
rrsets = jdata['rrsets'] rrsets = jdata['rrsets']
for rrset in rrsets: for rrset in rrsets:
rrset['name'] = rrset['name'].rstrip('.')
rrset['content'] = rrset['records'][0]['content'] rrset['content'] = rrset['records'][0]['content']
rrset['disabled'] = rrset['records'][0]['disabled'] rrset['disabled'] = rrset['records'][0]['disabled']
return {'records': rrsets} return {'records': rrsets}
@ -689,23 +690,42 @@ class Record(object):
# continue if the record is ready to be added # continue if the record is ready to be added
headers = {} headers = {}
headers['X-API-Key'] = PDNS_API_KEY headers['X-API-Key'] = PDNS_API_KEY
data = {"rrsets": [
{ if NEW_SCHEMA:
"name": self.name, data = {"rrsets": [
"type": self.type, {
"changetype": "REPLACE", "name": self.name + '.',
"records": [ "type": self.type,
{ "changetype": "REPLACE",
"content": self.data, "ttl": self.ttl,
"disabled": self.status, "records": [
"name": self.name, {
"ttl": self.ttl, "content": self.data,
"type": self.type "disabled": self.status,
} }
] ]
} }
] ]
} }
else:
data = {"rrsets": [
{
"name": self.name,
"type": self.type,
"changetype": "REPLACE",
"records": [
{
"content": self.data,
"disabled": self.status,
"name": self.name,
"ttl": self.ttl,
"type": self.type
}
]
}
]
}
try: try:
jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/localhost/zones/%s' % domain), headers=headers, method='PATCH', data=data) jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/localhost/zones/%s' % domain), headers=headers, method='PATCH', data=data)
logging.debug(jdata) logging.debug(jdata)
@ -759,42 +779,60 @@ class Record(object):
records = [] records = []
for r in new_records: for r in new_records:
record = { if NEW_SCHEMA:
"name": r['name'], record = {
"type": r['type'], "name": r['name'] + '.',
"changetype": "REPLACE", "type": r['type'],
"records": [ "ttl": r['ttl'],
{ "changetype": "REPLACE",
"content": r['content'], "records": [
"disabled": r['disabled'], {
"name": r['name'], "content": r['content'],
"ttl": r['ttl'], "disabled": r['disabled'],
"type": r['type'], }
"priority": 10, # priority field for pdns 3.4.1. https://doc.powerdns.com/md/authoritative/upgrading/ ]
} }
] else:
} record = {
"name": r['name'],
"type": r['type'],
"changetype": "REPLACE",
"records": [
{
"content": r['content'],
"disabled": r['disabled'],
"name": r['name'],
"ttl": r['ttl'],
"type": r['type'],
"priority": 10, # priority field for pdns 3.4.1. https://doc.powerdns.com/md/authoritative/upgrading/
}
]
}
records.append(record) records.append(record)
# Adjustment to add multiple records which described in https://github.com/ngoduykhanh/PowerDNS-Admin/issues/5#issuecomment-181637576 # Adjustment to add multiple records which described in https://github.com/ngoduykhanh/PowerDNS-Admin/issues/5#issuecomment-181637576
final_records = [] final_records = []
records = sorted(records, key = lambda item: (item["name"], item["type"])) if NEW_SCHEMA:
for key, group in itertools.groupby(records, lambda item: (item["name"], item["type"])): final_records = records
final_records.append({ else:
"name": key[0], records = sorted(records, key = lambda item: (item["name"], item["type"]))
"type": key[1], for key, group in itertools.groupby(records, lambda item: (item["name"], item["type"])):
"changetype": "REPLACE", final_records.append({
"records": [ "name": key[0],
{ "type": key[1],
"content": item['records'][0]['content'], "changetype": "REPLACE",
"disabled": item['records'][0]['disabled'], "records": [
"name": key[0], {
"ttl": item['records'][0]['ttl'], "content": item['records'][0]['content'],
"type": key[1], "disabled": item['records'][0]['disabled'],
"priority": 10, "name": key[0],
} for item in group "ttl": item['records'][0]['ttl'],
] "type": key[1],
}) "priority": 10,
} for item in group
]
})
postdata_for_new = {"rrsets": final_records} postdata_for_new = {"rrsets": final_records}