Skip to content

Commit

Permalink
added a delete dns record example - issues/33
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed Aug 23, 2017
1 parent 3fc396c commit 90e3ea5
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Change Log

- 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
- 2017-08-22 05:09:21 -0700 [1e13e8d](https://github.com/cloudflare/python-cloudflare/commit/1e13e8d5cbbe0c37f0a9d563869d51d91858cd50) CHANGELOG.md pushed to github
- 2017-08-22 04:51:51 -0700 [3a03516](https://github.com/cloudflare/python-cloudflare/commit/3a035163d3613756e8bb3e4bc26fb3642091861c) CHANGELOG.md pushed to github
- 2017-08-22 04:51:16 -0700 [cae98bb](https://github.com/cloudflare/python-cloudflare/commit/cae98bba8564d95a64ac6aa293805b1e587db2c6) 1.6.0 release
- 2017-08-22 04:39:52 -0700 [4745e20](https://github.com/cloudflare/python-cloudflare/commit/4745e20cc337d7d22a4c87ccc58a21961415a603) Merge pull request #35 from Bellardia/implement-argo
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,31 @@ if __name__ == '__main__':
main()
```

## A DNS zone delete code example (be careful)

```python
#!/usr/bin/env python

import sys
import CloudFlare

def main():
zone_name = sys.argv[1]
cf = CloudFlare.CloudFlare()
zone_info = cf.zones.get(param={'name': zone_name})
zone_id = zone_info['id']

dns_name = sys.argv[2]
dns_records = cf.zones.dns_records.get(zone_id, params={'name':dns_name + '.' + zone_name})
for dns_record in dns_records:
dns_record_id = dns_record['id']
r = cf.zones.dns_records.delete(zone_id, dns_record_id)
exit(0)

if __name__ == '__main__':
main()
```

## CLI

All API calls can be called from the command line.
Expand Down
26 changes: 26 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,32 @@ A DNS zone code example
if __name__ == '__main__':
main()
A DNS zone delete code example (be careful)
-------------------------------------------

.. code:: python
#!/usr/bin/env python
import sys
import CloudFlare
def main():
zone_name = sys.argv[1]
cf = CloudFlare.CloudFlare()
zone_info = cf.zones.get(param={'name': zone_name})
zone_id = zone_info['id']
dns_name = sys.argv[2]
dns_records = cf.zones.dns_records.get(zone_id, params={'name':dns_name + '.' + zone_name})
for dns_record in dns_records:
dns_record_id = dns_record['id']
r = cf.zones.dns_records.delete(zone_id, dns_record_id)
exit(0)
if __name__ == '__main__':
main()
CLI
---

Expand Down
74 changes: 74 additions & 0 deletions examples/example_delete_zone_entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env python
"""Cloudflare API code - example"""

import os
import sys
import re
import json
import requests

sys.path.insert(0, os.path.abspath('..'))
import CloudFlare

def main():
"""Cloudflare API code - example"""

try:
zone_name = sys.argv[1]
dns_name = sys.argv[2]
except IndexError:
exit('usage: example_delete_zone_entry.py zone dns_record')

cf = CloudFlare.CloudFlare()

# grab the zone identifier
try:
params = {'name':zone_name}
zones = cf.zones.get(params=params)
except CloudFlare.exceptions.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_id = zone['id']
zone_name = zone['name']

print 'ZONE:', zone_id, zone_name

try:
params = {'name':dns_name + '.' + zone_name}
dns_records = cf.zones.dns_records.get(zone_id, params=params)
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones/dns_records %s - %d %s - api call failed' % (dns_name, e, e))

found = False
for dns_record in dns_records:
dns_record_id = dns_record['id']
dns_record_name = dns_record['name']
dns_record_type = dns_record['type']
dns_record_value = dns_record['content']
print 'DNS RECORD:', dns_record_id, dns_record_name, dns_record_type, dns_record_value

try:
dns_record = cf.zones.dns_records.delete(zone_id, dns_record_id)
print 'DELETED'
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.dns_records.delete %s - %d %s - api call failed' % (dns_name, e, e))
found = True

if not found:
print 'RECORD NOT FOUND'

exit(0)

if __name__ == '__main__':
main()

0 comments on commit 90e3ea5

Please sign in to comment.