Last active
April 10, 2020 23:09
-
-
Save hslabbert/3db212f86e95dc109326fb3fbb98338f to your computer and use it in GitHub Desktop.
Add/update a PowerDNS A record via requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import logging | |
def format_dns_rrset( | |
record_name, | |
record_values, | |
domain=DEFAULT_DOMAIN, | |
operation="REPLACE", | |
ttl=86400, | |
record_type="A", | |
): | |
""" | |
Format a DNS rrset from the supplied record name & values. | |
""" | |
return { | |
"name": f"{record_name}.{domain}.", | |
"type": record_type, | |
"ttl": ttl, | |
"changetype": operation, | |
"records": [ | |
{"content": record_value, "disabled": False} | |
for record_value in record_values | |
], | |
} | |
def update_dns_a_record( | |
pdns_session, | |
device_name, | |
ip_address, | |
ttl=86400, | |
pdns_host=POWERDNS_HOST, | |
domain=DEFAULT_DOMAIN, | |
): | |
""" | |
Create a DNS A record for the provided name. | |
""" | |
endpoint = f"https://{pdns_host}/api/v1/servers/localhost/zones/{domain}." | |
rrset = format_dns_rrset( | |
record_name=device_name, record_values=[ip_address], domain=domain, ttl=ttl, | |
) | |
logging.debug("rrset is %s", rrset) | |
rrsets = {"rrsets": [rrset]} | |
payload = json.dumps(rrsets) | |
logging.debug("payload is %s", payload) | |
result = pdns_session.patch(url=endpoint, data=payload) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment