Skip to content

Commit

Permalink
restored extras= config file functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed Feb 6, 2020
1 parent 2ec542b commit e0eb323
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 40 deletions.
82 changes: 44 additions & 38 deletions CloudFlare/api_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions CloudFlare/cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
7 changes: 5 additions & 2 deletions CloudFlare/read_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()):
Expand Down

0 comments on commit e0eb323

Please sign in to comment.