Created
March 29, 2017 21:18
-
-
Save gwax/45fafd014cd05ae0d4ea1cea02f533cb to your computer and use it in GitHub Desktop.
script to update a google cloud dns record for a host
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
#!/usr/bin/env python3 | |
"""Update Google cloud dns records for a machine.""" | |
# if testing locally, make sure that you have authenticated: https://googlecloudplatform.github.io/google-cloud-python/stable/google-cloud-auth.html#overview | |
from google.cloud import dns | |
def update_dns_a_record(zonename, hostname, ip_address): | |
"""Update the A record for a given host on a target zone.""" | |
client = dns.Client() | |
zone = client.zone(zonename) | |
zone.reload() # call api to populate zone attributes | |
fq_hostname = '{host}.{domain}'.format(host=hostname, domain=zone.dns_name) | |
# start a change transaction | |
change_set = zone.changes() | |
# remove existing records | |
for record in zone.list_resource_record_sets(): | |
if record.name == fq_hostname: | |
change_set.delete_record_set(record) | |
# add new record | |
new_record = zone.resource_record_set( | |
name=fq_hostname, | |
record_type='A', | |
ttl=300, | |
rrdatas=[ip_address], | |
) | |
change_set.add_record_set(new_record) | |
# asynchronously commit change transaction | |
change_set.create() | |
def main(): | |
zonename = 'test-zone' | |
hostname = 'hostname' | |
ipaddr = '10.0.0.100' | |
update_dns_a_record(zonename, hostname, ipaddr) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note, this hasn't been fully tested or debugged.