|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +sys.path.insert(0, os.path.abspath('..')) |
| 6 | +import CloudFlare |
| 7 | + |
| 8 | +import re |
| 9 | + |
| 10 | +def main(): |
| 11 | + try: |
| 12 | + zone_name = sys.argv[1] |
| 13 | + dns_name = sys.argv[2] |
| 14 | + if sys.argv[3] == 'false': |
| 15 | + new_r_proxied_flag = False |
| 16 | + elif sys.argv[3] == 'true': |
| 17 | + new_r_proxied_flag = True |
| 18 | + else: |
| 19 | + raise('bad arg') |
| 20 | + except: |
| 21 | + exit('usage: ./example-make-zone-proxied.py zone dns_record [true|false]') |
| 22 | + |
| 23 | + cf = CloudFlare.CloudFlare() |
| 24 | + |
| 25 | + # grab the zone identifier |
| 26 | + try: |
| 27 | + params = {'name':zone_name,'per_page':1} |
| 28 | + zones = cf.zones.get(params=params) |
| 29 | + except CloudFlare.CloudFlareAPIError as e: |
| 30 | + exit('/zones.get %d %s - api call failed' % (e, e)) |
| 31 | + except Exception as e: |
| 32 | + exit('/zones.get - %s - api call failed' % (e)) |
| 33 | + |
| 34 | + if len(zones) != 1: |
| 35 | + exit('/zones.get - %s - api call returned %d items' % (zone_name, len(zones))) |
| 36 | + |
| 37 | + # there should only be one zone |
| 38 | + zone = zones[0] |
| 39 | + |
| 40 | + zone_name = zone['name'] |
| 41 | + zone_id = zone['id'] |
| 42 | + |
| 43 | + print "Zone:\t%s %s" % (zone_id, zone_name) |
| 44 | + |
| 45 | + try: |
| 46 | + params = {'name':dns_name} |
| 47 | + dns_records = cf.zones.dns_records.get(zone_id, params=params) |
| 48 | + except CloudFlare.CloudFlareAPIError as e: |
| 49 | + exit('/zones/dns_records.get %d %s - api call failed' % (e, e)) |
| 50 | + |
| 51 | + if len(dns_records) == 0: |
| 52 | + exit('/zones.dns_records.get - %s - api call returned %d items' % (dns_name, len(dns_records))) |
| 53 | + |
| 54 | + for dns_record in dns_records: |
| 55 | + r_zone_id = dns_record['zone_id'] |
| 56 | + r_id = dns_record['id'] |
| 57 | + r_name = dns_record['name'] |
| 58 | + r_type = dns_record['type'] |
| 59 | + r_content = dns_record['content'] |
| 60 | + r_ttl = dns_record['ttl'] |
| 61 | + r_proxied = dns_record['proxied'] |
| 62 | + r_proxiable = dns_record['proxiable'] |
| 63 | + 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) |
| 64 | + |
| 65 | + dns_record_id = dns_record['id'] |
| 66 | + |
| 67 | + 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} |
| 68 | + |
| 69 | + try: |
| 70 | + dns_record = cf.zones.dns_records.put(zone_id, dns_record_id, data=new_dns_record) |
| 71 | + except CloudFlare.CloudFlareAPIError as e: |
| 72 | + exit('/zones/dns_records.put %d %s - api call failed' % (e, e)) |
| 73 | + |
| 74 | + r_name = dns_record['name'] |
| 75 | + r_type = dns_record['type'] |
| 76 | + r_content = dns_record['content'] |
| 77 | + r_ttl = dns_record['ttl'] |
| 78 | + r_id = dns_record['id'] |
| 79 | + r_zone_id = dns_record['zone_id'] |
| 80 | + r_proxied = dns_record['proxied'] |
| 81 | + r_proxiable = dns_record['proxiable'] |
| 82 | + 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) |
| 83 | + |
| 84 | + exit(0) |
| 85 | + |
| 86 | +if __name__ == '__main__': |
| 87 | + main() |
| 88 | + |
0 commit comments