add exeception handling for invalid commands over nxapi

This commit will catch invalid commands being send over nxapi and call
fail_json on the module.  The nxos shared module will now return the
failure
pull/14234/head
Peter Sprygada 9 years ago
parent 49c15c8137
commit 633b16d116

@ -100,8 +100,8 @@ class Nxapi(object):
headers = {'Content-Type': 'application/json'} headers = {'Content-Type': 'application/json'}
response, headers = fetch_url(self.module, self.url, data=data, headers=headers, response, headers = fetch_url(self.module, self.url, data=data,
method='POST') headers=headers, method='POST')
if headers['status'] != 200: if headers['status'] != 200:
self.module.fail_json(**headers) self.module.fail_json(**headers)
@ -109,20 +109,22 @@ class Nxapi(object):
response = self.module.from_json(response.read()) response = self.module.from_json(response.read())
result = list() result = list()
output = response['ins_api']['outputs']['output'] try:
if isinstance(output, list): output = response['ins_api']['outputs']['output']
for item in response['ins_api']['outputs']['output']: if isinstance(output, list):
if item['code'] != '200': for item in response['ins_api']['outputs']['output']:
self.module.fail_json(msg=item['msg'], command=item['input'], if item['code'] != '200':
code=item['code']) self.module.fail_json(msg=item['msg'], command=item['input'],
else: code=item['code'])
result.append(item['body']) else:
elif output['code'] != '200': result.append(item['body'])
self.module.fail_json(msg=item['msg'], command=item['input'], elif output['code'] != '200':
code=item['code']) self.module.fail_json(msg=item['msg'], command=item['input'],
else: code=item['code'])
result.append(output['body']) else:
result.append(output['body'])
except Exception:
self.module.fail_json(**headers)
return result return result
@ -200,6 +202,7 @@ class NetworkModule(AnsibleModule):
cmd = 'show running-config' cmd = 'show running-config'
if self.params.get('include_defaults'): if self.params.get('include_defaults'):
cmd += ' all' cmd += ' all'
if self.params['transport'] == 'cli': if self.params['transport'] == 'cli':
return self.execute(cmd)[0] return self.execute(cmd)[0]
else: else:

Loading…
Cancel
Save