Skip to content

Commit

Permalink
Cleanup of code and confirmed functionality with A/AAAA records present
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed May 18, 2016
1 parent ca88ab7 commit 3225f64
Showing 1 changed file with 58 additions and 48 deletions.
106 changes: 58 additions & 48 deletions examples/example-update-dynamic-dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,14 @@ def my_ip_address():

return ip_address, ip_address_type

def main():
try:
dns_name = sys.argv[1]
except:
exit('usage: example-update-dynamic-dns.py fqdn-hostname')

host_name, zone_name = dns_name.split('.', 1)

ip_address, ip_address_type = my_ip_address()

print 'MY IP: %s %s' % (dns_name, ip_address)

cf = CloudFlare.CloudFlare()

# grab the zone identifier
try:
params = {'name':zone_name}
zones = cf.zones.get(params=params)
except CloudFlare.CloudFlareAPIError as e:
exit('/zones %d %s - api call failed' % (e, e))
except Exception as e:
exit('/zones.get - %s - api call failed' % (e))

if len(zones) != 1:
exit('/zones.get - %s - api call returned %d items' % (zone_name, len(zones)))

zone = zones[0]

zone_name = zone['name']
zone_id = zone['id']

def do_dns_update(cf, zone_name, zone_id, dns_name, ip_address, ip_address_type):
try:
params = {'name':dns_name,'match':'all','type':ip_address_type}
dns_records = cf.zones.dns_records.get(zone_id, params=params)
except CloudFlare.CloudFlareAPIError as e:
exit('/zones/dns_records %s - %d %s - api call failed' % (dns_name, e, e))

did_update = False
updated = False

# update the record - unless it's already correct
for dns_record in dns_records:
Expand All @@ -79,16 +49,18 @@ def main():

if ip_address_type != old_ip_address_type:
# only update the correct address type (A or AAAA)
# we don't see this becuase of the search params above
print 'IGNORED: %s %s ; wrong address family' % (dns_name, old_ip_address)
continue

if ip_address == old_ip_address:
print 'UNCHANGED: %s %s' % (dns_name, ip_address)
did_update = True
updated = True
continue

# Yes, we need to update this record - we know it's the same address type

dns_record_id = dns_record['id']
# update this record - we know it's the same address type
dns_record = {
'name':dns_name,
'type':ip_address_type,
Expand All @@ -99,21 +71,59 @@ def main():
except CloudFlare.CloudFlareAPIError as e:
exit('/zones.dns_records.put %s - %d %s - api call failed' % (dns_name, e, e))
print 'UPDATED: %s %s -> %s' % (dns_name, old_ip_address, ip_address)
did_update = True
updated = True

if did_update == False:
# nothing found - so create record
dns_record = {
'name':host_name,
'type':ip_address_type,
'content':ip_address
}
try:
dns_record = cf.zones.dns_records.post(zone_id, data=dns_record)
except CloudFlare.CloudFlareAPIError as e:
exit('/zones.dns_records.post %s - %d %s - api call failed' % (dns_name, e, e))
print 'CREATED: %s %s' % (dns_name, ip_address)
exit(0)
if updated:
return

# no exsiting dns record to update - so create dns record
dns_record = {
'name':dns_name,
'type':ip_address_type,
'content':ip_address
}
try:
dns_record = cf.zones.dns_records.post(zone_id, data=dns_record)
except CloudFlare.CloudFlareAPIError as e:
exit('/zones.dns_records.post %s - %d %s - api call failed' % (dns_name, e, e))
print 'CREATED: %s %s' % (dns_name, ip_address)

def main():
try:
dns_name = sys.argv[1]
except:
exit('usage: example-update-dynamic-dns.py fqdn-hostname')

host_name, zone_name = dns_name.split('.', 1)

ip_address, ip_address_type = my_ip_address()

print 'MY IP: %s %s' % (dns_name, ip_address)

cf = CloudFlare.CloudFlare()

# grab the zone identifier
try:
params = {'name':zone_name}
zones = cf.zones.get(params=params)
except CloudFlare.CloudFlareAPIError as e:
exit('/zones %d %s - api call failed' % (e, e))
except Exception as e:
exit('/zones.get - %s - api call failed' % (e))

if len(zones) == 0:
exit('/zones.get - %s - zone not found' % (zone_name))

if len(zones) != 1:
exit('/zones.get - %s - api call returned %d items' % (zone_name, len(zones)))

zone = zones[0]

zone_name = zone['name']
zone_id = zone['id']

do_dns_update(cf, zone_name, zone_id, dns_name, ip_address, ip_address_type)
exit(0)

if __name__ == '__main__':
main()
Expand Down

0 comments on commit 3225f64

Please sign in to comment.