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

Loading…
Cancel
Save