Skip to content

Commit

Permalink
added uuid support and converter for custom_hostnames
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed Jun 23, 2020
1 parent 9dcb55f commit 806b626
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cli4/cli4.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def run_command(cf, method, command, params=None, content=None, files=None):

hex_only = re.compile('^[0-9a-fA-F]+$')
waf_rules = re.compile('^[0-9]+[A-Z]*$')
uuid_value = re.compile('^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$') # 8-4-4-4-12

m = cf
for element in parts:
Expand All @@ -61,6 +62,9 @@ def run_command(cf, method, command, params=None, content=None, files=None):
if len(element) in [32, 40, 48] and hex_only.match(element):
# raw identifier - lets just use it as-is
identifier1 = element
if len(element) == 36 and uuid_value.match(element):
# uuid identifier - lets just use it as-is
identifier1 = element
elif element[0] == ':':
# raw string - used for workers script_name - use ::script_name
identifier1 = element[1:]
Expand Down Expand Up @@ -93,6 +97,9 @@ def run_command(cf, method, command, params=None, content=None, files=None):
if len(element) in [32, 40, 48] and hex_only.match(element):
# raw identifier - lets just use it as-is
identifier2 = element
if len(element) == 36 and uuid_value.match(element):
# uuid identifier - lets just use it as-is
identifier2 = element
elif element[0] == ':':
# raw string - used for workers script_names
identifier2 = element[1:]
Expand All @@ -102,6 +109,10 @@ def run_command(cf, method, command, params=None, content=None, files=None):
identifier2 = converters.convert_dns_record_to_identifier(cf,
identifier1,
element)
elif (cmd[0] and cmd[0] == 'zones') and (cmd[2] and cmd[2] == 'custom_hostnames'):
identifier2 = converters.convert_custom_hostnames_to_identifier(cf,
identifier1,
element)
else:
raise Exception("/%s/%s :NOT CODED YET" % ('/'.join(cmd), element))
except Exception as e:
Expand All @@ -117,6 +128,9 @@ def run_command(cf, method, command, params=None, content=None, files=None):
if len(element) in [32, 40, 48] and hex_only.match(element):
# raw identifier - lets just use it as-is
identifier3 = element
if len(element) == 36 and uuid_value.match(element):
# uuid identifier - lets just use it as-is
identifier3 = element
elif waf_rules.match(element):
identifier3 = element
else:
Expand Down
20 changes: 20 additions & 0 deletions cli4/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,23 @@ def convert_load_balancers_pool_to_identifier(cf, pool_name):
return p['id']

raise ConverterError('%s: not found' % (pool_name))

def convert_custom_hostnames_to_identifier(cf, zone_id, custom_hostname):
"""custom_hostnames to numbers"""
# this can return an array of results
params = {'name':custom_hostname}
try:
custom_hostnames_records = cf.zones.custom_hostnames.get(zone_id, params=params)
except CloudFlare.exceptions.CloudFlareAPIError as e:
raise ConverterError(int(e), '%s - %d %s' % (dns_name, e, e))
except Exception as e:
raise ConverterError(0, '%s - %s' % (dns_name, e))

r = []
for custom_hostnames_record in custom_hostnames_records:
if custom_hostname == custom_hostnames_record['hostname']:
r.append(custom_hostnames_record['id'])
if len(r) > 0:
return r

raise ConverterError('%s: not found' % (custom_hostname))

0 comments on commit 806b626

Please sign in to comment.