|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Cloudflare API code - example""" |
| 3 | + |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import json |
| 7 | +sys.path.insert(0, os.path.abspath('.')) |
| 8 | +sys.path.insert(0, os.path.abspath('..')) |
| 9 | + |
| 10 | +import CloudFlare |
| 11 | + |
| 12 | +def main(): |
| 13 | + """Cloudflare API code - example""" |
| 14 | + |
| 15 | + try: |
| 16 | + zone_name = sys.argv[1] |
| 17 | + except IndexError: |
| 18 | + exit('usage: example_bot_management.py zone_name True/False') |
| 19 | + |
| 20 | + try: |
| 21 | + enable_value = sys.argv[2] |
| 22 | + except IndexError: |
| 23 | + exit('usage: example_bot_management.py zone_name True/False') |
| 24 | + |
| 25 | + enable_value = True if enable_value in ['true','True','1'] else False |
| 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('/zone %d %s - api call failed' % (e, e)) |
| 35 | + except Exception as e: |
| 36 | + exit('/zone.get - %s - api call failed' % (e)) |
| 37 | + |
| 38 | + if len(zones) == 0: |
| 39 | + exit('/zones.get - %s - zones 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 | + |
| 46 | + settings_bot = cf.zones.bot_management.get(zone_id) |
| 47 | + print(json.dumps(settings_bot, indent=4)) |
| 48 | + |
| 49 | + try: |
| 50 | + settings_bot = cf.zones.bot_management.put(zone_id, data={'enable_js': enable_value}) |
| 51 | + except Exception as e: |
| 52 | + if int(e) == 99998: |
| 53 | + pass |
| 54 | + else: |
| 55 | + exit("Exception: %d %s" % (int(e), str(e))) |
| 56 | + |
| 57 | + settings_bot = cf.zones.bot_management.get(zone_id) |
| 58 | + print(json.dumps(settings_bot, indent=4)) |
| 59 | + |
| 60 | +if __name__ == '__main__': |
| 61 | + main() |
0 commit comments