fixes conditional processing with junos and xml data structures (#17801)

The conditional processing was failing due for two reasons:

1) The xml to json conversion string was not happening before the runner
   was processing the results
2) The Conditional instance was not parsing conditionals encoded with []

This fix address both issues.
pull/17803/head
Peter Sprygada 8 years ago committed by GitHub
parent fb50698da3
commit c534f8f9fc

@ -136,7 +136,7 @@ class Netconf(object):
for index, cmd in enumerate(commands): for index, cmd in enumerate(commands):
if cmd.output == 'xml': if cmd.output == 'xml':
responses[index] = etree.tostring(responses[index]) responses[index] = xml_to_json(responses[index])
elif cmd.args.get('command_type') == 'rpc': elif cmd.args.get('command_type') == 'rpc':
responses[index] = str(responses[index].text).strip() responses[index] = str(responses[index].text).strip()

@ -171,7 +171,7 @@ class CommandRunner(object):
self.retries -= 1 self.retries -= 1
else: else:
failed_conditions = [item.raw for item in self.conditionals] failed_conditions = [item.raw for item in self.conditionals]
errmsg = 'One or more conditional statements have not be satisfied' errmsg = 'One or more conditional statements have not been satisfied'
raise FailedConditionsError(errmsg, failed_conditions) raise FailedConditionsError(errmsg, failed_conditions)
class Conditional(object): class Conditional(object):
@ -189,9 +189,9 @@ class Conditional(object):
'matches': ['matches'] 'matches': ['matches']
} }
def __init__(self, conditional, encoding='json'): def __init__(self, conditional, encoding=None):
self.raw = conditional self.raw = conditional
self.encoding = encoding self.encoding = encoding or 'json'
try: try:
key, op, val = shlex.split(conditional) key, op, val = shlex.split(conditional)
@ -230,7 +230,7 @@ class Conditional(object):
return self.get_json(result) return self.get_json(result)
except (IndexError, TypeError): except (IndexError, TypeError):
msg = 'unable to apply conditional to result' msg = 'unable to apply conditional to result'
raise FailedConditionalError(msg, self.key) raise FailedConditionalError(msg, self.raw)
elif self.encoding == 'xml': elif self.encoding == 'xml':
return self.get_xml(result.get('result')) return self.get_xml(result.get('result'))
@ -250,14 +250,16 @@ class Conditional(object):
path += '/text()' path += '/text()'
index = int(re.match(r'result\[(\d+)\]', parts[0]).group(1)) index = int(re.match(r'result\[(\d+)\]', parts[0]).group(1))
values = result[index].xpath(path) values = result[index].findall(path)
if value_index is not None: if value_index is not None:
return values[value_index].strip() return values[value_index].strip()
return [v.strip() for v in values] return [v.strip() for v in values]
def get_json(self, result): def get_json(self, result):
parts = re.split(r'\.(?=[^\]]*(?:\[|$))', self.key) string = re.sub(r"\[[\'|\"]", ".", self.key)
string = re.sub(r"[\'|\"]\]", ".", string)
parts = re.split(r'\.(?=[^\]]*(?:\[|$))', string)
for part in parts: for part in parts:
match = re.findall(r'\[(\S+?)\]', part) match = re.findall(r'\[(\S+?)\]', part)
if match: if match:

Loading…
Cancel
Save