Some cleanups to nxos module_utils (#32804)

* When splitting, use rsplit and 1 just in case the string has more pipe
  characters than anticipated
* When converting to text, make decoding errors an error instead of
  silently corrupting data
* When converting to text for message strings, use the default error
  handler as that is already surrogate_then_replace
pull/32839/head
Toshio Kuratomi 7 years ago committed by Trishna Guha
parent 10866bfb8b
commit d97080174e

@ -127,8 +127,12 @@ class Cli:
except KeyError: except KeyError:
rc, out, err = self.exec_command(cmd) rc, out, err = self.exec_command(cmd)
if rc != 0: if rc != 0:
self._module.fail_json(msg=to_text(err, errors='surrogate_then_replace')) self._module.fail_json(msg=to_text(err))
cfg = to_text(out, errors='surrogate_then_replace').strip() try:
cfg = to_text(out, errors='surrogate_or_strict').strip()
except UnicodeError as e:
self._module.fail_json(msg=u'Failed to decode config: %s' % to_text(out))
self._device_configs[cmd] = cfg self._device_configs[cmd] = cfg
return cfg return cfg
@ -141,25 +145,29 @@ class Cli:
if item['output'] == 'json' and not is_json(item['command']): if item['output'] == 'json' and not is_json(item['command']):
cmd = '%s | json' % item['command'] cmd = '%s | json' % item['command']
elif item['output'] == 'text' and is_json(item['command']): elif item['output'] == 'text' and is_json(item['command']):
cmd = item['command'].split('|')[0] cmd = item['command'].rsplit('|', 1)[0]
else: else:
cmd = item['command'] cmd = item['command']
rc, out, err = self.exec_command(cmd) rc, out, err = self.exec_command(cmd)
out = to_text(out, errors='surrogate_then_replace') try:
out = to_text(out, errors='surrogate_or_strict')
except UnicodeError:
self._module.fail_json(msg=u'Failed to decode output from %s: %s' % (cmd, to_text(out)))
if check_rc and rc != 0: if check_rc and rc != 0:
self._module.fail_json(msg=to_text(err, errors='surrogate_then_replace')) self._module.fail_json(msg=to_text(err))
if not check_rc and rc != 0: if not check_rc and rc != 0:
try: try:
out = self._module.from_json(err) out = self._module.from_json(err)
except ValueError: except ValueError:
out = to_text(err, errors='surrogate_then_replace').strip() out = to_text(err).strip()
else: else:
try: try:
out = self._module.from_json(out) out = self._module.from_json(out)
except ValueError: except ValueError:
out = to_text(out, errors='surrogate_then_replace').strip() out = to_text(out).strip()
responses.append(out) responses.append(out)
return responses return responses
@ -170,11 +178,9 @@ class Cli:
if opts is None: if opts is None:
opts = {} opts = {}
errors = 'surrogate_then_replace'
rc, out, err = self.exec_command('configure') rc, out, err = self.exec_command('configure')
if rc != 0: if rc != 0:
msg = 'unable to enter configuration mode' self._module.fail_json(msg='unable to enter configuration mode', output=to_text(err))
self._module.fail_json(msg=msg, output=to_text(err, errors=errors))
msgs = [] msgs = []
for cmd in config: for cmd in config:
@ -183,7 +189,7 @@ class Cli:
msgs.append(err) msgs.append(err)
return msgs return msgs
elif rc != 0: elif rc != 0:
self._module.fail_json(msg=to_text(err, errors=errors)) self._module.fail_json(msg=to_text(err))
elif out: elif out:
msgs.append(out) msgs.append(out)
@ -349,7 +355,7 @@ class Nxapi:
for item in to_list(commands): for item in to_list(commands):
if is_json(item['command']): if is_json(item['command']):
item['command'] = str(item['command']).split('|')[0] item['command'] = str(item['command']).rsplit('|', 1)[0]
item['output'] = 'json' item['output'] = 'json'
if all((output == 'json', item['output'] == 'text')) or all((output == 'text', item['output'] == 'json')): if all((output == 'json', item['output'] == 'text')) or all((output == 'text', item['output'] == 'json')):

Loading…
Cancel
Save