From e0eb323a713698be0e10f1d5066e982eace511f0 Mon Sep 17 00:00:00 2001 From: Martin Levy Date: Wed, 5 Feb 2020 18:17:00 -0800 Subject: [PATCH] restored extras= config file functions --- CloudFlare/api_extras.py | 82 ++++++++++++++++++++------------------ CloudFlare/cloudflare.py | 8 ++++ CloudFlare/read_configs.py | 7 +++- 3 files changed, 57 insertions(+), 40 deletions(-) diff --git a/CloudFlare/api_extras.py b/CloudFlare/api_extras.py index fced7b4..e7eda85 100644 --- a/CloudFlare/api_extras.py +++ b/CloudFlare/api_extras.py @@ -2,55 +2,61 @@ import re +from .exceptions import CloudFlareAPIError + def api_extras(self, extras=None): """ API extras for Cloudflare API""" + count = 0; for extra in extras: - if extra == '': - continue extra = re.sub(r"^.*/client/v4/", '/', extra) extra = re.sub(r"^.*/v4/", '/', extra) extra = re.sub(r"^/", '', extra) + if extra == '': + continue # build parts of the extra command parts = [] - nn = 0 + part = None for element in extra.split('/'): if element[0] == ':': - nn += 1 - continue - try: - parts[nn] - except IndexError: - parts.append([]) - parts[nn].append(element) - - # insert extra command into class - element_path = [] - current = self - for element in parts[0]: - element_path.append(element) - try: - m = getattr(current, element) - # exists - but still add it there's a second part - if element == parts[0][-1] and len(parts) > 1: - api_call_part1 = '/'.join(element_path) - api_call_part2 = '/'.join(parts[1]) - setattr(m, parts[1][0], - self._AddWithAuth(self._base, api_call_part1, api_call_part2)) - current = m + parts.append(part) + part = None continue - except: - pass - # does not exist - if element == parts[0][-1] and len(parts) > 1: - # last element - api_call_part1 = '/'.join(element_path) - api_call_part2 = '/'.join(parts[1]) - setattr(current, element, - self._AddWithAuth(self._base, api_call_part1, api_call_part2)) + if part: + part += '/' + element else: - api_call_part1 = '/'.join(element_path) - setattr(current, element, - self._AddWithAuth(self._base, api_call_part1)) - current = getattr(current, element) + part = element + if part: + parts.append(part) + + if len(parts) > 1: + p = parts[1].split('/') + for nn in range(0, len(p)): + try: + self.add('VOID', parts[0], '/'.join(p[0:nn])) + except CloudFlareAPIError: + # already exists - this is ok + pass + + if len(parts) > 2: + p = parts[2].split('/') + for nn in range(0, len(p)): + try: + self.add('VOID', parts[0], parts[1], '/'.join(p[0:nn])) + except CloudFlareAPIError: + # already exists - this is ok + pass + + while len(parts) < 3: + parts.append(None) + + # we can only add AUTH elements presently + try: + self.add('AUTH', parts[0], parts[1], parts[2]) + count += 1 + except CloudFlareAPIError: + # this is silently dropped - however, that could change + pass + + return count diff --git a/CloudFlare/cloudflare.py b/CloudFlare/cloudflare.py index 30312f2..27011f6 100644 --- a/CloudFlare/cloudflare.py +++ b/CloudFlare/cloudflare.py @@ -814,6 +814,14 @@ def add(self, t, p1, p2=None, p3=None): raise CloudFlareAPIError(0, 'api load name failed') name = a[-1] + try: + f = getattr(branch, name) + # already exists - don't let it overwrite + raise CloudFlareAPIError(0, 'api duplicate name found: %s/**%s**' % ('/'.join(a[0:-1]), name)) + except AttributeError: + # this is the required behavior - i.e. it's a new node to create + pass + if t == 'VOID': f = self._AddUnused(self._base, p1, p2, p3) elif t == 'OPEN': diff --git a/CloudFlare/read_configs.py b/CloudFlare/read_configs.py index 95243b2..c6aab1f 100644 --- a/CloudFlare/read_configs.py +++ b/CloudFlare/read_configs.py @@ -45,7 +45,10 @@ def read_configs(profile=None): for option in ['email', 'token', 'certtoken', 'extras']: if option not in config or config[option] is None: try: - config[option] = re.sub(r"\s+", '', section.get(option)) + if option == 'extras': + config[option] = re.sub(r"\s+", ' ', section.get(option)) + else: + config[option] = re.sub(r"\s+", '', section.get(option)) if config[option] == '': config.pop(option) except (configparser.NoOptionError, configparser.NoSectionError): @@ -66,7 +69,7 @@ def read_configs(profile=None): # do any final cleanup - only needed for extras (which are multiline) if 'extras' in config and config['extras'] is not None: - config['extras'] = config['extras'].split(' ') + config['extras'] = config['extras'].strip().split(' ') # remove blank entries for x in sorted(config.keys()):