aci_rest: Fix error handling and improve documentation (#36295)

This PR includes:
- A fix for a recently introduced issue wrt. error handling
- Added integration tests for provoked errors
- Influence standard return values using aci library for aci_rest
- Add proxy support documentation
- Documentation update related to #34175
This commit is contained in:
Dag Wieers 2018-02-19 12:01:14 +01:00 committed by GitHub
commit 79d00adc52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 279 additions and 82 deletions

View file

@ -930,33 +930,36 @@ class ACIModule(object):
def exit_json(self, **kwargs):
if self.params['state'] in ('absent', 'present'):
if self.params['output_level'] in ('debug', 'info'):
self.result['previous'] = self.existing
if 'state' in self.params:
if self.params['state'] in ('absent', 'present'):
if self.params['output_level'] in ('debug', 'info'):
self.result['previous'] = self.existing
# Return the gory details when we need it
if self.params['output_level'] == 'debug':
self.result['filter_string'] = self.filter_string
if 'state' in self.params:
self.result['filter_string'] = self.filter_string
self.result['method'] = self.method
# self.result['path'] = self.path # Adding 'path' in result causes state: absent in output
self.result['response'] = self.response
self.result['status'] = self.status
self.result['url'] = self.url
self.original = self.existing
if self.params['state'] in ('absent', 'present'):
self.get_existing()
if 'state' in self.params:
self.original = self.existing
if self.params['state'] in ('absent', 'present'):
self.get_existing()
# if self.module._diff and self.original != self.existing:
# self.result['diff'] = dict(
# before=json.dumps(self.original, sort_keys=True, indent=4),
# after=json.dumps(self.existing, sort_keys=True, indent=4),
# )
self.result['current'] = self.existing
self.result['current'] = self.existing
if self.params['output_level'] in ('debug', 'info'):
self.result['sent'] = self.config
self.result['proposed'] = self.proposed
if self.params['output_level'] in ('debug', 'info'):
self.result['sent'] = self.config
self.result['proposed'] = self.proposed
self.result.update(**kwargs)
self.module.exit_json(**self.result)
@ -967,27 +970,31 @@ class ACIModule(object):
if self.error['code'] is not None and self.error['text'] is not None:
self.result['error'] = self.error
if self.params['state'] in ('absent', 'present'):
if self.params['output_level'] in ('debug', 'info'):
self.result['previous'] = self.existing
if 'state' in self.params:
if self.params['state'] in ('absent', 'present'):
if self.params['output_level'] in ('debug', 'info'):
self.result['previous'] = self.existing
# Return the gory details when we need it
if self.params['output_level'] == 'debug':
if self.imdata is not None:
self.result['imdata'] = self.imdata
self.result['totalCount'] = self.totalCount
# Return the gory details when we need it
if self.params['output_level'] == 'debug':
if self.imdata is not None:
self.result['imdata'] = self.imdata
self.result['totalCount'] = self.totalCount
if self.url is not None:
self.result['filter_string'] = self.filter_string
if 'state' in self.params:
self.result['filter_string'] = self.filter_string
self.result['method'] = self.method
# self.result['path'] = self.path # Adding 'path' in result causes state: absent in output
self.result['response'] = self.response
self.result['status'] = self.status
self.result['url'] = self.url
if self.params['output_level'] in ('debug', 'info'):
self.result['sent'] = self.config
self.result['proposed'] = self.proposed
if 'state' in self.params:
if self.params['output_level'] in ('debug', 'info'):
self.result['sent'] = self.config
self.result['proposed'] = self.proposed
self.result.update(**kwargs)
self.module.fail_json(msg=msg, **self.result)

View file

@ -285,7 +285,7 @@ class ACIRESTModule(ACIModule):
return False
def response_any(self, rawoutput, rest_type='xml'):
def response_type(self, rawoutput, rest_type='xml'):
''' Handle APIC response output '''
if rest_type == 'json':
@ -388,31 +388,27 @@ def main():
timeout=aci.params['timeout'],
use_proxy=aci.params['use_proxy'])
if aci.params['output_level'] == 'debug':
aci.result['filter_string'] = aci.filter_string
aci.result['method'] = aci.params['method'].upper()
# aci.result['path'] = aci.path # Adding 'path' in result causes state: absent in output
aci.result['response'] = info['msg']
aci.result['status'] = info['status']
aci.result['url'] = aci.url
aci.method = aci.params['method'].upper()
aci.response = info['msg']
aci.status = info['status']
# Report failure
if info['status'] != 200:
try:
# APIC error
aci.response(info['body'], rest_type)
aci.fail_json(msg='Request failed: %(code)s %(text)s' % aci.error)
aci.response_type(info['body'], rest_type)
aci.fail_json(msg='APIC Error %(code)s: %(text)s' % aci.error)
except KeyError:
# Connection error
aci.fail_json(msg='Request connection failed for %(url)s. %(msg)s' % info)
aci.fail_json(msg='Connection failed for %(url)s. %(msg)s' % info)
aci.response_any(resp.read(), rest_type)
aci.response_type(resp.read(), rest_type)
aci.result['imdata'] = aci.imdata
aci.result['totalCount'] = aci.totalCount
# Report success
module.exit_json(**aci.result)
aci.exit_json(**aci.result)
if __name__ == '__main__':