|
1 | 1 | #!/usr/bin/env python |
| 2 | +"""CloudFlare API code - example""" |
2 | 3 |
|
3 | 4 | import os |
4 | 5 | import sys |
| 6 | +import json |
| 7 | + |
5 | 8 | sys.path.insert(0, os.path.abspath('..')) |
6 | 9 | import CloudFlare |
7 | 10 |
|
8 | | -import json |
9 | | - |
10 | 11 | def main(): |
11 | | - # Grab the first argument, if there is one |
12 | | - try: |
13 | | - zone_name = sys.argv[1] |
14 | | - params = {'name':zone_name,'per_page':1} |
15 | | - except: |
16 | | - params = {'per_page':50} |
17 | | - |
18 | | - cf = CloudFlare.CloudFlare() |
19 | | - |
20 | | - # grab the zone identifier |
21 | | - try: |
22 | | - zones = cf.zones.get(params=params) |
23 | | - except CloudFlare.CloudFlareAPIError as e: |
24 | | - exit('/zones %d %s - api call failed' % (e, e)) |
25 | | - except Exception as e: |
26 | | - exit('/zones - %s - api call failed' % (e)) |
27 | | - |
28 | | - # there should only be one zone |
29 | | - for zone in sorted(zones, key=lambda v: v['name']): |
30 | | - zone_name = zone['name'] |
31 | | - zone_id = zone['id'] |
32 | | - try: |
33 | | - certificates = cf.zones.ssl.certificate_packs.get(zone_id) |
34 | | - except CloudFlare.CloudFlareAPIError as e: |
35 | | - exit('/zones.ssl.certificate_packs %d %s - api call failed' % (e, e)) |
36 | | - |
37 | | - for certificate in certificates: |
38 | | - certificate_type = certificate['type'] |
39 | | - primary_certificate = certificate['primary_certificate'] |
40 | | - certificate_hosts = certificate['hosts'] |
41 | | - certificate_sig = certificate['certificates'][0]['signature'] |
42 | | - certificate_sig_count = len(certificate['certificates']) |
43 | | - if certificate_sig_count > 1: |
44 | | - c = certificate['certificates'][0] |
45 | | - print '%-40s %-10s %-32s %-15s [ %s ]' %(zone_name, certificate_type, primary_certificate, c['signature'], ','.join(certificate_hosts)) |
46 | | - nn = 0 |
47 | | - for c in certificate['certificates']: |
48 | | - nn += 1 |
49 | | - if nn == 1: |
50 | | - next |
51 | | - print '%-40s %-10s %-32s %2d:%-15s [ %s ]' %('', '', '', nn, c['signature'], '') |
52 | | - else: |
53 | | - for c in certificate['certificates']: |
54 | | - print '%-40s %-10s %-32s %-15s [ %s ]' %(zone_name, certificate_type, primary_certificate, c['signature'], ','.join(certificate_hosts)) |
55 | | - |
56 | | - exit(0) |
| 12 | + """CloudFlare API code - example""" |
| 13 | + |
| 14 | + # Grab the first argument, if there is one |
| 15 | + try: |
| 16 | + zone_name = sys.argv[1] |
| 17 | + params = {'name':zone_name, 'per_page':1} |
| 18 | + except IndexError: |
| 19 | + params = {'per_page':50} |
| 20 | + |
| 21 | + cf = CloudFlare.CloudFlare() |
| 22 | + |
| 23 | + # grab the zone identifier |
| 24 | + try: |
| 25 | + zones = cf.zones.get(params=params) |
| 26 | + except CloudFlare.CloudFlareAPIError as e: |
| 27 | + exit('/zones %d %s - api call failed' % (e, e)) |
| 28 | + except Exception as e: |
| 29 | + exit('/zones - %s - api call failed' % (e)) |
| 30 | + |
| 31 | + # there should only be one zone |
| 32 | + for zone in sorted(zones, key=lambda v: v['name']): |
| 33 | + zone_name = zone['name'] |
| 34 | + zone_id = zone['id'] |
| 35 | + try: |
| 36 | + certificates = cf.zones.ssl.certificate_packs.get(zone_id) |
| 37 | + except CloudFlare.CloudFlareAPIError as e: |
| 38 | + exit('/zones.ssl.certificate_packs %d %s - api call failed' % (e, e)) |
| 39 | + |
| 40 | + for certificate in certificates: |
| 41 | + certificate_type = certificate['type'] |
| 42 | + primary_certificate = certificate['primary_certificate'] |
| 43 | + certificate_hosts = certificate['hosts'] |
| 44 | + certificate_sig = certificate['certificates'][0]['signature'] |
| 45 | + certificate_sig_count = len(certificate['certificates']) |
| 46 | + if certificate_sig_count > 1: |
| 47 | + c = certificate['certificates'][0] |
| 48 | + print '%-40s %-10s %-32s %-15s [ %s ]' % ( |
| 49 | + zone_name, |
| 50 | + certificate_type, |
| 51 | + primary_certificate, |
| 52 | + c['signature'], |
| 53 | + ','.join(certificate_hosts) |
| 54 | + ) |
| 55 | + nn = 0 |
| 56 | + for c in certificate['certificates']: |
| 57 | + nn += 1 |
| 58 | + if nn == 1: |
| 59 | + next |
| 60 | + print '%-40s %-10s %-32s %2d:%-15s [ %s ]' % ( |
| 61 | + '', |
| 62 | + '', |
| 63 | + '', |
| 64 | + nn, |
| 65 | + c['signature'], |
| 66 | + '' |
| 67 | + ) |
| 68 | + else: |
| 69 | + for c in certificate['certificates']: |
| 70 | + print '%-40s %-10s %-32s %-15s [ %s ]' % ( |
| 71 | + zone_name, |
| 72 | + certificate_type, |
| 73 | + primary_certificate, |
| 74 | + c['signature'], |
| 75 | + ','.join(certificate_hosts) |
| 76 | + ) |
| 77 | + |
| 78 | + exit(0) |
57 | 79 |
|
58 | 80 | if __name__ == '__main__': |
59 | | - main() |
| 81 | + main() |
60 | 82 |
|
0 commit comments