Skip to content

Commit

Permalink
moved converts into seperate file. corrected zone name converter to b…
Browse files Browse the repository at this point in the history
…e simpler
  • Loading branch information
mahtin committed Dec 30, 2016
1 parent 9381a70 commit 9c54a10
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 124 deletions.
156 changes: 32 additions & 124 deletions cli4/cli4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
120 changes: 120 additions & 0 deletions cli4/converters.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 9c54a10

Please sign in to comment.