Skip to content

Commit 806b626

Browse files
committed
added uuid support and converter for custom_hostnames
1 parent 9dcb55f commit 806b626

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

cli4/cli4.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def run_command(cf, method, command, params=None, content=None, files=None):
5252

5353
hex_only = re.compile('^[0-9a-fA-F]+$')
5454
waf_rules = re.compile('^[0-9]+[A-Z]*$')
55+
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
5556

5657
m = cf
5758
for element in parts:
@@ -61,6 +62,9 @@ def run_command(cf, method, command, params=None, content=None, files=None):
6162
if len(element) in [32, 40, 48] and hex_only.match(element):
6263
# raw identifier - lets just use it as-is
6364
identifier1 = element
65+
if len(element) == 36 and uuid_value.match(element):
66+
# uuid identifier - lets just use it as-is
67+
identifier1 = element
6468
elif element[0] == ':':
6569
# raw string - used for workers script_name - use ::script_name
6670
identifier1 = element[1:]
@@ -93,6 +97,9 @@ def run_command(cf, method, command, params=None, content=None, files=None):
9397
if len(element) in [32, 40, 48] and hex_only.match(element):
9498
# raw identifier - lets just use it as-is
9599
identifier2 = element
100+
if len(element) == 36 and uuid_value.match(element):
101+
# uuid identifier - lets just use it as-is
102+
identifier2 = element
96103
elif element[0] == ':':
97104
# raw string - used for workers script_names
98105
identifier2 = element[1:]
@@ -102,6 +109,10 @@ def run_command(cf, method, command, params=None, content=None, files=None):
102109
identifier2 = converters.convert_dns_record_to_identifier(cf,
103110
identifier1,
104111
element)
112+
elif (cmd[0] and cmd[0] == 'zones') and (cmd[2] and cmd[2] == 'custom_hostnames'):
113+
identifier2 = converters.convert_custom_hostnames_to_identifier(cf,
114+
identifier1,
115+
element)
105116
else:
106117
raise Exception("/%s/%s :NOT CODED YET" % ('/'.join(cmd), element))
107118
except Exception as e:
@@ -117,6 +128,9 @@ def run_command(cf, method, command, params=None, content=None, files=None):
117128
if len(element) in [32, 40, 48] and hex_only.match(element):
118129
# raw identifier - lets just use it as-is
119130
identifier3 = element
131+
if len(element) == 36 and uuid_value.match(element):
132+
# uuid identifier - lets just use it as-is
133+
identifier3 = element
120134
elif waf_rules.match(element):
121135
identifier3 = element
122136
else:

cli4/converters.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,23 @@ def convert_load_balancers_pool_to_identifier(cf, pool_name):
130130
return p['id']
131131

132132
raise ConverterError('%s: not found' % (pool_name))
133+
134+
def convert_custom_hostnames_to_identifier(cf, zone_id, custom_hostname):
135+
"""custom_hostnames to numbers"""
136+
# this can return an array of results
137+
params = {'name':custom_hostname}
138+
try:
139+
custom_hostnames_records = cf.zones.custom_hostnames.get(zone_id, params=params)
140+
except CloudFlare.exceptions.CloudFlareAPIError as e:
141+
raise ConverterError(int(e), '%s - %d %s' % (dns_name, e, e))
142+
except Exception as e:
143+
raise ConverterError(0, '%s - %s' % (dns_name, e))
144+
145+
r = []
146+
for custom_hostnames_record in custom_hostnames_records:
147+
if custom_hostname == custom_hostnames_record['hostname']:
148+
r.append(custom_hostnames_record['id'])
149+
if len(r) > 0:
150+
return r
151+
152+
raise ConverterError('%s: not found' % (custom_hostname))

0 commit comments

Comments
 (0)