Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 90e3ea5

Browse files
committed
added a delete dns record example - issues/33
1 parent 3fc396c commit 90e3ea5

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Change Log
22

3+
- 2017-08-22 05:10:15 -0700 [3fc396c](https://github.com/cloudflare/python-cloudflare/commit/3fc396cb685bf57ce094299a7505216d78624ac2) missing chmod +x on examples/example_paging_thru_zones.py
4+
- 2017-08-22 05:09:21 -0700 [1e13e8d](https://github.com/cloudflare/python-cloudflare/commit/1e13e8d5cbbe0c37f0a9d563869d51d91858cd50) CHANGELOG.md pushed to github
35
- 2017-08-22 04:51:51 -0700 [3a03516](https://github.com/cloudflare/python-cloudflare/commit/3a035163d3613756e8bb3e4bc26fb3642091861c) CHANGELOG.md pushed to github
46
- 2017-08-22 04:51:16 -0700 [cae98bb](https://github.com/cloudflare/python-cloudflare/commit/cae98bba8564d95a64ac6aa293805b1e587db2c6) 1.6.0 release
57
- 2017-08-22 04:39:52 -0700 [4745e20](https://github.com/cloudflare/python-cloudflare/commit/4745e20cc337d7d22a4c87ccc58a21961415a603) Merge pull request #35 from Bellardia/implement-argo

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,31 @@ if __name__ == '__main__':
397397
main()
398398
```
399399

400+
## A DNS zone delete code example (be careful)
401+
402+
```python
403+
#!/usr/bin/env python
404+
405+
import sys
406+
import CloudFlare
407+
408+
def main():
409+
zone_name = sys.argv[1]
410+
cf = CloudFlare.CloudFlare()
411+
zone_info = cf.zones.get(param={'name': zone_name})
412+
zone_id = zone_info['id']
413+
414+
dns_name = sys.argv[2]
415+
dns_records = cf.zones.dns_records.get(zone_id, params={'name':dns_name + '.' + zone_name})
416+
for dns_record in dns_records:
417+
dns_record_id = dns_record['id']
418+
r = cf.zones.dns_records.delete(zone_id, dns_record_id)
419+
exit(0)
420+
421+
if __name__ == '__main__':
422+
main()
423+
```
424+
400425
## CLI
401426

402427
All API calls can be called from the command line.

README.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,32 @@ A DNS zone code example
445445
if __name__ == '__main__':
446446
main()
447447
448+
A DNS zone delete code example (be careful)
449+
-------------------------------------------
450+
451+
.. code:: python
452+
453+
#!/usr/bin/env python
454+
455+
import sys
456+
import CloudFlare
457+
458+
def main():
459+
zone_name = sys.argv[1]
460+
cf = CloudFlare.CloudFlare()
461+
zone_info = cf.zones.get(param={'name': zone_name})
462+
zone_id = zone_info['id']
463+
464+
dns_name = sys.argv[2]
465+
dns_records = cf.zones.dns_records.get(zone_id, params={'name':dns_name + '.' + zone_name})
466+
for dns_record in dns_records:
467+
dns_record_id = dns_record['id']
468+
r = cf.zones.dns_records.delete(zone_id, dns_record_id)
469+
exit(0)
470+
471+
if __name__ == '__main__':
472+
main()
473+
448474
CLI
449475
---
450476

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)