Skip to content

Commit 06b4c6b

Browse files
committed
added example python code for dns_records/export
1 parent b596a90 commit 06b4c6b

3 files changed

Lines changed: 101 additions & 0 deletions

File tree

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,30 @@ $
671671

672672
The **jq -r** option is used to convert newlines and tabs within the JSON response data. The egrep is used for documentation brevity.
673673

674+
This can also be done via Python code with the following example.
675+
```
676+
#!/usr/bin/env python
677+
import sys
678+
import CloudFlare
679+
680+
def main():
681+
zone_name = sys.argv[1]
682+
cf = CloudFlare.CloudFlare()
683+
684+
zones = cf.zones.get(params={'name': zone_name})
685+
zone_id = zones[0]['id']
686+
687+
dns_records = cf.zones.dns_records.export.get(zone_id)
688+
for l in dns_records.splitlines():
689+
if len(l) == 0 or l[0] == ';':
690+
continue
691+
print l
692+
exit(0)
693+
694+
if __name__ == '__main__':
695+
main()
696+
```
697+
674698
## Implemented API calls
675699

676700
The **--dump** argument to cli4 will produce a list of all the call implemented within the library.

README.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,31 @@ page within the Cloudflare portal.
739739
The **jq -r** option is used to convert newlines and tabs within the
740740
JSON response data. The egrep is used for documentation brevity.
741741

742+
This can also be done via Python code with the following example.
743+
744+
::
745+
746+
#!/usr/bin/env python
747+
import sys
748+
import CloudFlare
749+
750+
def main():
751+
zone_name = sys.argv[1]
752+
cf = CloudFlare.CloudFlare()
753+
754+
zones = cf.zones.get(params={'name': zone_name})
755+
zone_id = zones[0]['id']
756+
757+
dns_records = cf.zones.dns_records.export.get(zone_id)
758+
for l in dns_records.splitlines():
759+
if len(l) == 0 or l[0] == ';':
760+
continue
761+
print l
762+
exit(0)
763+
764+
if __name__ == '__main__':
765+
main()
766+
742767
Implemented API calls
743768
---------------------
744769

examples/example_dns_export.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python
2+
"""Cloudflare API code - example"""
3+
4+
import os
5+
import sys
6+
sys.path.insert(0, os.path.abspath('..'))
7+
8+
import CloudFlare
9+
10+
def main():
11+
"""Cloudflare API code - example"""
12+
13+
try:
14+
zone_name = sys.argv[1]
15+
except IndexError:
16+
exit('usage: example_dns_export.py zone')
17+
18+
cf = CloudFlare.CloudFlare()
19+
20+
# grab the zone identifier
21+
try:
22+
params = {'name': zone_name}
23+
zones = cf.zones.get(params=params)
24+
except CloudFlare.exceptions.CloudFlareAPIError as e:
25+
exit('/zones %d %s - api call failed' % (e, e))
26+
except Exception as e:
27+
exit('/zones.get - %s - api call failed' % (e))
28+
29+
if len(zones) == 0:
30+
exit('/zones.get - %s - zone not found' % (zone_name))
31+
32+
if len(zones) != 1:
33+
exit('/zones.get - %s - api call returned %d items' % (zone_name, len(zones)))
34+
35+
zone_id = zones[0]['id']
36+
37+
try:
38+
dns_records = cf.zones.dns_records.export.get(zone_id)
39+
except CloudFlare.exceptions.CloudFlareAPIError as e:
40+
exit('/zones/dns_records/export %s - %d %s - api call failed' % (dns_name, e, e))
41+
42+
for l in dns_records.splitlines():
43+
if len(l) == 0 or l[0] == ';':
44+
# blank line or comment line are skipped - to make example easy to see
45+
continue
46+
print l
47+
48+
exit(0)
49+
50+
if __name__ == '__main__':
51+
main()
52+

0 commit comments

Comments
 (0)