forked from cloudflare/python-cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example of how to adjust proxy flag on the fly
- Loading branch information
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#!/usr/bin/env python | ||
|
||
import os | ||
import sys | ||
sys.path.insert(0, os.path.abspath('..')) | ||
import CloudFlare | ||
|
||
import re | ||
|
||
def main(): | ||
try: | ||
zone_name = sys.argv[1] | ||
dns_name = sys.argv[2] | ||
if sys.argv[3] == 'false': | ||
new_r_proxied_flag = False | ||
elif sys.argv[3] == 'true': | ||
new_r_proxied_flag = True | ||
else: | ||
raise('bad arg') | ||
except: | ||
exit('usage: ./example-make-zone-proxied.py zone dns_record [true|false]') | ||
|
||
cf = CloudFlare.CloudFlare() | ||
|
||
# grab the zone identifier | ||
try: | ||
params = {'name':zone_name,'per_page':1} | ||
zones = cf.zones.get(params=params) | ||
except CloudFlare.CloudFlareAPIError as e: | ||
exit('/zones.get %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))) | ||
|
||
# there should only be one zone | ||
zone = zones[0] | ||
|
||
zone_name = zone['name'] | ||
zone_id = zone['id'] | ||
|
||
print "Zone:\t%s %s" % (zone_id, zone_name) | ||
|
||
try: | ||
params = {'name':dns_name} | ||
dns_records = cf.zones.dns_records.get(zone_id, params=params) | ||
except CloudFlare.CloudFlareAPIError as e: | ||
exit('/zones/dns_records.get %d %s - api call failed' % (e, e)) | ||
|
||
if len(dns_records) == 0: | ||
exit('/zones.dns_records.get - %s - api call returned %d items' % (dns_name, len(dns_records))) | ||
|
||
for dns_record in dns_records: | ||
r_zone_id = dns_record['zone_id'] | ||
r_id = dns_record['id'] | ||
r_name = dns_record['name'] | ||
r_type = dns_record['type'] | ||
r_content = dns_record['content'] | ||
r_ttl = dns_record['ttl'] | ||
r_proxied = dns_record['proxied'] | ||
r_proxiable = dns_record['proxiable'] | ||
print 'Record:\t%s %s %60s %6d %-5s %s ; proxied=%s proxiable=%s' % (r_zone_id, r_id, r_name, r_ttl, r_type, r_content, r_proxied, r_proxiable) | ||
|
||
dns_record_id = dns_record['id'] | ||
|
||
new_dns_record = {'zone_id':r_zone_id,'id':r_id,'type':r_type,'name':r_name,'content':r_content,'ttl':r_ttl,'proxied':new_r_proxied_flag} | ||
|
||
try: | ||
dns_record = cf.zones.dns_records.put(zone_id, dns_record_id, data=new_dns_record) | ||
except CloudFlare.CloudFlareAPIError as e: | ||
exit('/zones/dns_records.put %d %s - api call failed' % (e, e)) | ||
|
||
r_name = dns_record['name'] | ||
r_type = dns_record['type'] | ||
r_content = dns_record['content'] | ||
r_ttl = dns_record['ttl'] | ||
r_id = dns_record['id'] | ||
r_zone_id = dns_record['zone_id'] | ||
r_proxied = dns_record['proxied'] | ||
r_proxiable = dns_record['proxiable'] | ||
print 'Record:\t%s %s %60s %6d %-5s %s ; proxied=%s proxiable=%s' % (r_zone_id, r_id, r_name, r_ttl, r_type, r_content, r_proxied, r_proxiable) | ||
|
||
exit(0) | ||
|
||
if __name__ == '__main__': | ||
main() | ||
|