From c1f5432cc02364a01b1b2191aca077548d5c972d Mon Sep 17 00:00:00 2001 From: Ner'zhul Date: Mon, 9 Jan 2017 13:40:53 +0100 Subject: [PATCH] Better user report when OMAPI error occurs or key is invalid (#20035) * Move imports after header --- lib/ansible/modules/network/omapi_host.py | 36 +++++++++++++---------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/ansible/modules/network/omapi_host.py b/lib/ansible/modules/network/omapi_host.py index 8797ef1f66..a67fae4660 100644 --- a/lib/ansible/modules/network/omapi_host.py +++ b/lib/ansible/modules/network/omapi_host.py @@ -136,6 +136,21 @@ lease: sample: 'mydesktop' ''' +# import module snippets +from ansible.module_utils.basic import AnsibleModule, get_exception, to_bytes +from ansible.module_utils.six import iteritems +import socket +import struct +import binascii + +try: + from pypureomapi import Omapi, OmapiMessage, OmapiError, OmapiErrorNotFound + from pypureomapi import pack_ip, unpack_ip, pack_mac, unpack_mac + from pypureomapi import OMAPI_OP_STATUS, OMAPI_OP_UPDATE + pureomapi_found = True +except ImportError: + pureomapi_found = False + class OmapiHostManager: def __init__(self, module): @@ -147,6 +162,12 @@ class OmapiHostManager: try: self.omapi = Omapi(self.module.params['host'], self.module.params['port'], self.module.params['key_name'], self.module.params['key']) + except binascii.Error: + self.module.fail_json(msg="Unable to open OMAPI connection. 'key' is not a valid base64 key.") + except OmapiError: + e = get_exception() + self.module.fail_json(msg="Unable to open OMAPI connection. Ensure 'host', 'port', 'key' and 'key_name' " + "are valid. Exception was: %s" % e) except socket.error: e = get_exception() self.module.fail_json(msg="Unable to connect to OMAPI server: %s" % e) @@ -301,20 +322,5 @@ def main(): e = get_exception() module.fail_json(msg="OMAPI input value error: %s" % e) - -# import module snippets -from ansible.module_utils.basic import AnsibleModule, get_exception, to_bytes -from ansible.module_utils.six import iteritems -import socket -import struct - -try: - from pypureomapi import Omapi, OmapiMessage, OmapiError, OmapiErrorNotFound - from pypureomapi import pack_ip, unpack_ip, pack_mac, unpack_mac - from pypureomapi import OMAPI_OP_STATUS, OMAPI_OP_UPDATE - pureomapi_found = True -except ImportError: - pureomapi_found = False - if __name__ == '__main__': main()