|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Cloudflare API code - example""" |
| 3 | + |
| 4 | +import os |
| 5 | +import sys |
| 6 | + |
| 7 | +sys.path.insert(0, os.path.abspath('..')) |
| 8 | +import CloudFlare |
| 9 | + |
| 10 | +def main(): |
| 11 | + """Cloudflare API code - example""" |
| 12 | + |
| 13 | + # Grab the first argument, if there is one |
| 14 | + try: |
| 15 | + zone_name = sys.argv[1] |
| 16 | + params = {'name':zone_name, 'per_page':1} |
| 17 | + except IndexError: |
| 18 | + params = {'per_page':500} |
| 19 | + |
| 20 | + cf = CloudFlare.CloudFlare() |
| 21 | + |
| 22 | + # grab the zone identifier |
| 23 | + try: |
| 24 | + zones = cf.zones.get(params=params) |
| 25 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 26 | + exit('/zones.get %d %s - api call failed' % (e, e)) |
| 27 | + except Exception as e: |
| 28 | + exit('/zones.get - %s - api call failed' % (e)) |
| 29 | + |
| 30 | + # there should only be one zone - but handle more if needed |
| 31 | + for zone in sorted(zones, key=lambda v: v['name']): |
| 32 | + zone_name = zone['name'] |
| 33 | + zone_id = zone['id'] |
| 34 | + zone_plan = zone['plan']['name'] |
| 35 | + if zone_plan != 'Enterprise Website': |
| 36 | + print('%s %s %s - not Enterprise' % (zone_id, zone_name, zone_plan)) |
| 37 | + continue |
| 38 | + |
| 39 | + print('%s %-40s %s' % (zone_id, zone_name, zone_plan)) |
| 40 | + try: |
| 41 | + custom_info = cf.zones.custom_hostnames.get(zone_id) |
| 42 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 43 | + sys.stderr.write('/zones.custom_hostnames.get %d %s - api call failed\n' % (e, e)) |
| 44 | + continue |
| 45 | + |
| 46 | + for hostname_info in custom_info: |
| 47 | + print('\t%s %-30s %s' % (hostname_info['id'], hostname_info['hostname'], hostname_info['created_at'])) |
| 48 | + |
| 49 | + for s in sorted(hostname_info.keys()): |
| 50 | + if s in ['id', 'hostname', 'created_at'] or hostname_info[s] == None: |
| 51 | + continue |
| 52 | + print('\t%-15s = %s' % (s, hostname_info[s])) |
| 53 | + |
| 54 | + try: |
| 55 | + fallback_origin = cf.zones.custom_hostnames.fallback_origin.get(zone_id) |
| 56 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 57 | + sys.stderr.write('/zones.custom_hostnames.fallback_origin.get %d %s - api call failed\n' % (e, e)) |
| 58 | + continue |
| 59 | + |
| 60 | + print('\t%s %-30s %s %s' % ('', fallback_origin['origin'], fallback_origin['created_at'], fallback_origin['status'])) |
| 61 | + print('') |
| 62 | + |
| 63 | + exit(0) |
| 64 | + |
| 65 | +if __name__ == '__main__': |
| 66 | + main() |
| 67 | + |
0 commit comments