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

Commit 62eb860

Browse files
committed
example of purge_cache for free or enterprise accounts
1 parent 64ee01b commit 62eb860

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
method = 'POST'
14+
15+
try:
16+
if sys.argv[1] == '--delete':
17+
del sys.argv[1]
18+
method = 'DELETE'
19+
except IndexError:
20+
pass
21+
22+
try:
23+
zone_name = sys.argv[1]
24+
except IndexError:
25+
exit('usage: example_zone_purge_cache.py zone')
26+
27+
cf = CloudFlare.CloudFlare()
28+
29+
# grab the zone identifier
30+
try:
31+
params = {'name': zone_name}
32+
zones = cf.zones.get(params=params)
33+
except CloudFlare.exceptions.CloudFlareAPIError as e:
34+
exit('/zones %d %s - api call failed' % (e, e))
35+
except Exception as e:
36+
exit('/zones.get - %s - api call failed' % (e))
37+
38+
if len(zones) == 0:
39+
exit('/zones.get - %s - zone not found' % (zone_name))
40+
41+
if len(zones) != 1:
42+
exit('/zones.get - %s - api call returned %d items' % (zone_name, len(zones)))
43+
44+
zone_id = zones[0]['id']
45+
zone_type = zones[0]['plan']['name']
46+
47+
if 'Enterprise' in zone_type:
48+
# Enterprise accounts can do all things ...
49+
data = {
50+
# 'purge_everything': True,
51+
'hosts': [zone_name],
52+
'tags': ['random-tag'],
53+
'prefixes': [zone_name + '/' + 'index.html'],
54+
}
55+
else:
56+
# Free, Pro, Business accounts can only do this ...
57+
data = {'purge_everything': True}
58+
59+
print('%s: zone type="%s" and hence using data="%s" method="%s"' % (zone_name, zone_type, data, method))
60+
61+
try:
62+
if method == 'DELETE':
63+
# delete method is not in documents; however, it works
64+
r = cf.zones.purge_cache.delete(zone_id, data=data)
65+
else:
66+
r = cf.zones.purge_cache.post(zone_id, data=data)
67+
except CloudFlare.exceptions.CloudFlareAPIError as e:
68+
exit('/zones/purge_cache %s - %d %s - api call failed' % (zone_name, e, e))
69+
70+
if 'id' not in r or r['id'] != zone_id:
71+
print('%s: weird response: result="%s"' % (zone_name, r))
72+
73+
exit(0)
74+
75+
if __name__ == '__main__':
76+
main()
77+

0 commit comments

Comments
 (0)