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.
This commit is contained in:
parent
fb50698da3
commit
c534f8f9fc
2 changed files with 9 additions and 7 deletions
|
@ -136,7 +136,7 @@ class Netconf(object):
|
|||
|
||||
for index, cmd in enumerate(commands):
|
||||
if cmd.output == 'xml':
|
||||
responses[index] = etree.tostring(responses[index])
|
||||
responses[index] = xml_to_json(responses[index])
|
||||
elif cmd.args.get('command_type') == 'rpc':
|
||||
responses[index] = str(responses[index].text).strip()
|
||||
|
||||
|
|
|
@ -171,7 +171,7 @@ class CommandRunner(object):
|
|||
self.retries -= 1
|
||||
else:
|
||||
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)
|
||||
|
||||
class Conditional(object):
|
||||
|
@ -189,9 +189,9 @@ class Conditional(object):
|
|||
'matches': ['matches']
|
||||
}
|
||||
|
||||
def __init__(self, conditional, encoding='json'):
|
||||
def __init__(self, conditional, encoding=None):
|
||||
self.raw = conditional
|
||||
self.encoding = encoding
|
||||
self.encoding = encoding or 'json'
|
||||
|
||||
try:
|
||||
key, op, val = shlex.split(conditional)
|
||||
|
@ -230,7 +230,7 @@ class Conditional(object):
|
|||
return self.get_json(result)
|
||||
except (IndexError, TypeError):
|
||||
msg = 'unable to apply conditional to result'
|
||||
raise FailedConditionalError(msg, self.key)
|
||||
raise FailedConditionalError(msg, self.raw)
|
||||
|
||||
elif self.encoding == 'xml':
|
||||
return self.get_xml(result.get('result'))
|
||||
|
@ -250,14 +250,16 @@ class Conditional(object):
|
|||
path += '/text()'
|
||||
|
||||
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:
|
||||
return values[value_index].strip()
|
||||
return [v.strip() for v in values]
|
||||
|
||||
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:
|
||||
match = re.findall(r'\[(\S+?)\]', part)
|
||||
if match:
|
||||
|
|
Loading…
Reference in a new issue