forked from cloudflare/python-cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
Martin J. Levy edited this page May 5, 2016
·
10 revisions
#Examples
There is an example folder with many more coding examples included.
This short example prints the name of all your zones and if IPv6 is enabled for that specific zone. This code can be found at example-are-zones-ipv6-simple.py
#!/usr/bin/env python
import CloudFlare
def main():
cf = CloudFlare.CloudFlare()
zones = cf.zones.get(params={'per_page':50})
for zone in zones:
zone_name = zone['name']
zone_id = zone['id']
settings_ipv6 = cf.zones.settings.ipv6.get(zone_id)
ipv6_on = settings_ipv6['value']
print zone_id, ipv6_on, zone_name
exit(0)
if __name__ == '__main__':
main()Here's a DNS example showing how to set values. This code can be found at example-create-zone-and-populate.py
#!/usr/bin/env python
import os
import sys
sys.path.insert(0, os.path.abspath('..'))
import CloudFlare
def main():
try:
zone_name = sys.argv[1]
except:
exit('usage: provide a zone name as an argument on the command line')
cf = CloudFlare.CloudFlare()
# Create zone - which will only work if ... 1) The zone is not on CloudFlare. 2) The zone passes a whois test
print 'Create zone %s ...' % (zone_name)
try:
zone_info = cf.zones.post(data={'jump_start':False, 'name': zone_name})
except CloudFlare.CloudFlareAPIError as e:
exit('/zones.post %s - %d %s' % (zone_name, e, e))
except Exception as e:
exit('/zones.post %s - %s' % (zone_name, e))
zone_id = zone_info['id']
if 'email' in zone_info['owner']:
zone_owner = zone_info['owner']['email']
else:
zone_owner = '"' + zone_info['owner']['name'] + '"'
zone_plan = zone_info['plan']['name']
zone_status = zone_info['status']
print '\t%s name=%s owner=%s plan=%s status=%s\n' % (zone_id, zone_name, zone_owner, zone_plan, zone_status)
# DNS records to create
dns_records = [
{'name':'ding', 'type':'A', 'content':'216.58.194.206'},
{'name':'foo', 'type':'AAAA', 'content':'2001:d8b::1'},
{'name':'foo', 'type':'A', 'content':'192.168.0.1'},
{'name':'duh', 'type':'A', 'content':'10.0.0.1', 'ttl':120},
{'name':'bar', 'type':'CNAME', 'content':'foo.mahtin.net'},
{'name':'shakespeare', 'type':'TXT', 'content':"What's in a name? That which we call a rose by any other name would smell as sweet."}
]
print 'Create DNS records ...'
for dns_record in dns_records:
# Create DNS record
try:
r = cf.zones.dns_records.post(zone_id, data=dns_record)
except CloudFlare.CloudFlareAPIError as e:
exit('/zones.dns_records.post %s %s - %d %s' % (zone_name, record['name'], e, e))
# Print respose info - they should be the same
dns_record = r
print '\t%s %30s %6d %-5s %s ; proxied=%s proxiable=%s' % (dns_record['id'], dns_record['name'], dns_record['ttl'], dns_record['type'], dns_record['content'], dns_record['proxied'], dns_record['proxiable'])
# set proxied flag to false - for example
dns_record_id = dns_record['id']
new_dns_record = {
# Must have type/name/content (even if they don't change)
'type':dns_record['type'],
'name':dns_record['name'],
'content':dns_record['content'],
# now add new values you want to change
'proxied':False
}
try:
dns_record = cf.zones.dns_records.put(zone_id, dns_record_id, data=new_dns_record)
except CloudFlare.CloudFlareAPIError as e:
exit('/zones/dns_records.put %d %s - api call failed' % (e, e))
print ''
# Now read back all the DNS records
print 'Read back DNS records ...'
try:
dns_records = cf.zones.dns_records.get(zone_id)
except CloudFlare.CloudFlareAPIError as e:
exit('/zones.dns_records.get %s - %d %s' % (zone_name, e, e))
for dns_record in sorted(dns_records, key=lambda v: v['name']):
print '\t%s %30s %6d %-5s %s ; proxied=%s proxiable=%s' % (dns_record['id'], dns_record['name'], dns_record['ttl'], dns_record['type'], dns_record['content'], dns_record['proxied'], dns_record['proxiable'])
print ''
exit(0)
if __name__ == '__main__':
main()