From 9c54a10f9477232ea733ff99be07fd26ad7f21a0 Mon Sep 17 00:00:00 2001 From: Martin Levy Date: Thu, 29 Dec 2016 20:24:05 -0800 Subject: [PATCH] moved converts into seperate file. corrected zone name converter to be simpler --- cli4/cli4.py | 156 ++++++++++----------------------------------- cli4/converters.py | 120 ++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+), 124 deletions(-) create mode 100644 cli4/converters.py diff --git a/cli4/cli4.py b/cli4/cli4.py index dc3157c..38b3d73 100644 --- a/cli4/cli4.py +++ b/cli4/cli4.py @@ -11,121 +11,12 @@ except ImportError: yaml = None +import converters + sys.path.insert(0, os.path.abspath('..')) import CloudFlare import CloudFlare.exceptions -def convert_zones_to_identifier(cf, zone_name): - """zone names to numbers""" - params = {'name':zone_name, 'per_page':1} - try: - zones = cf.zones.get(params=params) - except CloudFlare.exceptions.CloudFlareAPIError as e: - exit('cli4: %s - %d %s' % (zone_name, e, e)) - except Exception as e: - exit('cli4: %s - %s' % (zone_name, e)) - - for zone in zones: - if zone_name == zone['name']: - return zone['id'] - - exit('cli4: %s - zone not found' % (zone_name)) - -def convert_dns_record_to_identifier(cf, zone_id, dns_name): - """dns record names to numbers""" - # this can return an array of results as there can be more than one DNS entry for a name. - params = {'name':dns_name} - try: - dns_records = cf.zones.dns_records.get(zone_id, params=params) - except CloudFlare.exceptions.CloudFlareAPIError as e: - exit('cli4: %s - %d %s' % (dns_name, e, e)) - except Exception as e: - exit('cli4: %s - %s' % (dns_name, e)) - - r = [] - for dns_record in dns_records: - if dns_name == dns_record['name']: - r.append(dns_record['id']) - if len(r) > 0: - return r - - exit('cli4: %s - dns name not found' % (dns_name)) - -def convert_certificates_to_identifier(cf, certificate_name): - """certificate names to numbers""" - try: - certificates = cf.certificates.get() - except CloudFlare.exceptions.CloudFlareAPIError as e: - exit('cli4: %s - %d %s' % (certificate_name, e, e)) - except Exception as e: - exit('cli4: %s - %s' % (certificate_name, e)) - - for certificate in certificates: - if certificate_name in certificate['hostnames']: - return certificate['id'] - - exit('cli4: %s - no zone certificates found' % (certificate_name)) - -def convert_organizations_to_identifier(cf, organization_name): - """organizations names to numbers""" - try: - organizations = cf.user.organizations.get() - except CloudFlare.exceptions.CloudFlareAPIError as e: - exit('cli4: %s - %d %s' % (organization_name, e, e)) - except Exception as e: - exit('cli4: %s - %s' % (organization_name, e)) - - for organization in organizations: - if organization_name == organization['name']: - return organization['id'] - - exit('cli4: %s - no organizations found' % (organization_name)) - -def convert_invites_to_identifier(cf, invite_name): - """invite names to numbers""" - try: - invites = cf.user.invites.get() - except CloudFlare.exceptions.CloudFlareAPIError as e: - exit('cli4: %s - %d %s' % (invite_name, e, e)) - except Exception as e: - exit('cli4: %s - %s' % (invite_name, e)) - - for invite in invites: - if invite_name == invite['organization_name']: - return invite['id'] - - exit('cli4: %s - no invites found' % (invite_name)) - -def convert_virtual_dns_to_identifier(cf, virtual_dns_name): - """virtual dns names to numbers""" - try: - virtual_dnss = cf.user.virtual_dns.get() - except CloudFlare.exceptions.CloudFlareAPIError as e: - exit('cli4: %s - %d %s\n' % (virtual_dns_name, e, e)) - except Exception as e: - exit('cli4: %s - %s\n' % (virtual_dns_name, e)) - - for virtual_dns in virtual_dnss: - if virtual_dns_name == virtual_dns['name']: - return virtual_dns['id'] - - exit('cli4: %s - no virtual_dns found' % (virtual_dns_name)) - -def convert_load_balancers_pool_to_identifier(cf, pool_name): - """load balancer pool names to numbers""" - try: - pools = cf.user.load_balancers.pools.get() - except CloudFlare.exceptions.CloudFlareAPIError as e: - exit('cli4: %s - %d %s' % (pool_name, e, e)) - except Exception as e: - exit('cli4: %s - %s' % (pool_name, e)) - - for p in pools: - if pool_name == p['description']: - return p['id'] - - exit('cli4: %s - no pools found' % (pool_name)) - def dump_commands(cf): """dump a tree of all the known API commands""" w = cf.api_list() @@ -282,19 +173,19 @@ def cli4(args): identifier1 = element elif cmd[0] == 'certificates': # identifier1 = convert_certificates_to_identifier(cf, element) - identifier1 = convert_zones_to_identifier(cf, element) + identifier1 = converters.convert_zones_to_identifier(cf, element) elif cmd[0] == 'zones': - identifier1 = convert_zones_to_identifier(cf, element) + identifier1 = converters.convert_zones_to_identifier(cf, element) elif cmd[0] == 'organizations': - identifier1 = convert_organizations_to_identifier(cf, element) + identifier1 = converters.convert_organizations_to_identifier(cf, element) elif (cmd[0] == 'user') and (cmd[1] == 'organizations'): - identifier1 = convert_organizations_to_identifier(cf, element) + identifier1 = converters.convert_organizations_to_identifier(cf, element) elif (cmd[0] == 'user') and (cmd[1] == 'invites'): - identifier1 = convert_invites_to_identifier(cf, element) + identifier1 = converters.convert_invites_to_identifier(cf, element) elif (cmd[0] == 'user') and (cmd[1] == 'virtual_dns'): - identifier1 = convert_virtual_dns_to_identifier(cf, element) + identifier1 = converters.convert_virtual_dns_to_identifier(cf, element) elif (cmd[0] == 'user') and (cmd[1] == 'load_balancers') and (cmd[2] == 'pools'): - identifier1 = convert_load_balancers_pool_to_identifier(cf, element) + identifier1 = converters.convert_load_balancers_pool_to_identifier(cf, element) else: exit("/%s/%s :NOT CODED YET 1" % ('/'.join(cmd), element)) cmd.append(':' + identifier1) @@ -303,7 +194,9 @@ def cli4(args): # raw identifier - lets just use it as-is identifier2 = element elif (cmd[0] and cmd[0] == 'zones') and (cmd[2] and cmd[2] == 'dns_records'): - identifier2 = convert_dns_record_to_identifier(cf, identifier1, element) + identifier2 = converters.convert_dns_record_to_identifier(cf, + identifier1, + element) else: exit("/%s/%s :NOT CODED YET 2" % ('/'.join(cmd), element)) # identifier2 may be an array - this needs to be dealt with later @@ -337,15 +230,30 @@ def cli4(args): for i2 in identifier2: try: if method is 'GET': - r = m.get(identifier1=identifier1, identifier2=i2, identifier3=identifier3, params=params) + r = m.get(identifier1=identifier1, + identifier2=i2, + identifier3=identifier3, + params=params) elif method is 'PATCH': - r = m.patch(identifier1=identifier1, identifier2=i2, identifier3=identifier3, data=params) + r = m.patch(identifier1=identifier1, + identifier2=i2, + identifier3=identifier3, + data=params) elif method is 'POST': - r = m.post(identifier1=identifier1, identifier2=i2, identifier3=identifier3, data=params) + r = m.post(identifier1=identifier1, + identifier2=i2, + identifier3=identifier3, + data=params) elif method is 'PUT': - r = m.put(identifier1=identifier1, identifier2=i2, identifier3=identifier3, data=params) + r = m.put(identifier1=identifier1, + identifier2=i2, + identifier3=identifier3, + data=params) elif method is 'DELETE': - r = m.delete(identifier1=identifier1, identifier2=i2, identifier3=identifier3, data=params) + r = m.delete(identifier1=identifier1, + identifier2=i2, + identifier3=identifier3, + data=params) else: pass except CloudFlare.exceptions.CloudFlareAPIError as e: diff --git a/cli4/converters.py b/cli4/converters.py new file mode 100644 index 0000000..be821de --- /dev/null +++ b/cli4/converters.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python +"""Cloudflare API via command line""" + +import os +import sys + +sys.path.insert(0, os.path.abspath('..')) +import CloudFlare +import CloudFlare.exceptions + +def convert_zones_to_identifier(cf, zone_name): + """zone names to numbers""" + params = {'name':zone_name, 'per_page':1} + try: + zones = cf.zones.get(params=params) + except CloudFlare.exceptions.CloudFlareAPIError as e: + exit('cli4: %s - %d %s' % (zone_name, e, e)) + except Exception as e: + exit('cli4: %s - %s' % (zone_name, e)) + + if len(zones) == 1: + return zones[0]['id'] + + exit('cli4: %s - zone not found' % (zone_name)) + +def convert_dns_record_to_identifier(cf, zone_id, dns_name): + """dns record names to numbers""" + # this can return an array of results as there can be more than one DNS entry for a name. + params = {'name':dns_name} + try: + dns_records = cf.zones.dns_records.get(zone_id, params=params) + except CloudFlare.exceptions.CloudFlareAPIError as e: + exit('cli4: %s - %d %s' % (dns_name, e, e)) + except Exception as e: + exit('cli4: %s - %s' % (dns_name, e)) + + r = [] + for dns_record in dns_records: + if dns_name == dns_record['name']: + r.append(dns_record['id']) + if len(r) > 0: + return r + + exit('cli4: %s - dns name not found' % (dns_name)) + +def convert_certificates_to_identifier(cf, certificate_name): + """certificate names to numbers""" + try: + certificates = cf.certificates.get() + except CloudFlare.exceptions.CloudFlareAPIError as e: + exit('cli4: %s - %d %s' % (certificate_name, e, e)) + except Exception as e: + exit('cli4: %s - %s' % (certificate_name, e)) + + for certificate in certificates: + if certificate_name in certificate['hostnames']: + return certificate['id'] + + exit('cli4: %s - no zone certificates found' % (certificate_name)) + +def convert_organizations_to_identifier(cf, organization_name): + """organizations names to numbers""" + try: + organizations = cf.user.organizations.get() + except CloudFlare.exceptions.CloudFlareAPIError as e: + exit('cli4: %s - %d %s' % (organization_name, e, e)) + except Exception as e: + exit('cli4: %s - %s' % (organization_name, e)) + + for organization in organizations: + if organization_name == organization['name']: + return organization['id'] + + exit('cli4: %s - no organizations found' % (organization_name)) + +def convert_invites_to_identifier(cf, invite_name): + """invite names to numbers""" + try: + invites = cf.user.invites.get() + except CloudFlare.exceptions.CloudFlareAPIError as e: + exit('cli4: %s - %d %s' % (invite_name, e, e)) + except Exception as e: + exit('cli4: %s - %s' % (invite_name, e)) + + for invite in invites: + if invite_name == invite['organization_name']: + return invite['id'] + + exit('cli4: %s - no invites found' % (invite_name)) + +def convert_virtual_dns_to_identifier(cf, virtual_dns_name): + """virtual dns names to numbers""" + try: + virtual_dnss = cf.user.virtual_dns.get() + except CloudFlare.exceptions.CloudFlareAPIError as e: + exit('cli4: %s - %d %s\n' % (virtual_dns_name, e, e)) + except Exception as e: + exit('cli4: %s - %s\n' % (virtual_dns_name, e)) + + for virtual_dns in virtual_dnss: + if virtual_dns_name == virtual_dns['name']: + return virtual_dns['id'] + + exit('cli4: %s - no virtual_dns found' % (virtual_dns_name)) + +def convert_load_balancers_pool_to_identifier(cf, pool_name): + """load balancer pool names to numbers""" + try: + pools = cf.user.load_balancers.pools.get() + except CloudFlare.exceptions.CloudFlareAPIError as e: + exit('cli4: %s - %d %s' % (pool_name, e, e)) + except Exception as e: + exit('cli4: %s - %s' % (pool_name, e)) + + for p in pools: + if pool_name == p['description']: + return p['id'] + + exit('cli4: %s - no pools found' % (pool_name)) +