|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Cloudflare API code - example""" |
| 3 | + |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import re |
| 7 | +import json |
| 8 | +import requests |
| 9 | + |
| 10 | +sys.path.insert(0, os.path.abspath('..')) |
| 11 | +import CloudFlare |
| 12 | + |
| 13 | +def main(): |
| 14 | + """Cloudflare API code - example""" |
| 15 | + |
| 16 | + try: |
| 17 | + zone_name = sys.argv[1] |
| 18 | + dns_name = sys.argv[2] |
| 19 | + except IndexError: |
| 20 | + exit('usage: example_delete_zone_entry.py zone dns_record') |
| 21 | + |
| 22 | + cf = CloudFlare.CloudFlare() |
| 23 | + |
| 24 | + # grab the zone identifier |
| 25 | + try: |
| 26 | + params = {'name':zone_name} |
| 27 | + zones = cf.zones.get(params=params) |
| 28 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 29 | + exit('/zones %d %s - api call failed' % (e, e)) |
| 30 | + except Exception as e: |
| 31 | + exit('/zones.get - %s - api call failed' % (e)) |
| 32 | + |
| 33 | + if len(zones) == 0: |
| 34 | + exit('/zones.get - %s - zone not found' % (zone_name)) |
| 35 | + |
| 36 | + if len(zones) != 1: |
| 37 | + exit('/zones.get - %s - api call returned %d items' % (zone_name, len(zones))) |
| 38 | + |
| 39 | + zone = zones[0] |
| 40 | + |
| 41 | + zone_id = zone['id'] |
| 42 | + zone_name = zone['name'] |
| 43 | + |
| 44 | + print 'ZONE:', zone_id, zone_name |
| 45 | + |
| 46 | + try: |
| 47 | + params = {'name':dns_name + '.' + zone_name} |
| 48 | + dns_records = cf.zones.dns_records.get(zone_id, params=params) |
| 49 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 50 | + exit('/zones/dns_records %s - %d %s - api call failed' % (dns_name, e, e)) |
| 51 | + |
| 52 | + found = False |
| 53 | + for dns_record in dns_records: |
| 54 | + dns_record_id = dns_record['id'] |
| 55 | + dns_record_name = dns_record['name'] |
| 56 | + dns_record_type = dns_record['type'] |
| 57 | + dns_record_value = dns_record['content'] |
| 58 | + print 'DNS RECORD:', dns_record_id, dns_record_name, dns_record_type, dns_record_value |
| 59 | + |
| 60 | + try: |
| 61 | + dns_record = cf.zones.dns_records.delete(zone_id, dns_record_id) |
| 62 | + print 'DELETED' |
| 63 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 64 | + exit('/zones.dns_records.delete %s - %d %s - api call failed' % (dns_name, e, e)) |
| 65 | + found = True |
| 66 | + |
| 67 | + if not found: |
| 68 | + print 'RECORD NOT FOUND' |
| 69 | + |
| 70 | + exit(0) |
| 71 | + |
| 72 | +if __name__ == '__main__': |
| 73 | + main() |
| 74 | + |
0 commit comments