Fix undefined variables, basestring usage, and some associated python3 issues

This commit is contained in:
Toshio Kuratomi 2017-07-22 18:15:46 -07:00
parent 9f7b0dfc30
commit 225fa5d092
84 changed files with 652 additions and 963 deletions

View file

@ -251,16 +251,25 @@ EXAMPLES = """
role: librarian
"""
import traceback
try:
import psycopg2
import psycopg2.extensions
except ImportError:
psycopg2 = None
# import module snippets
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.database import pg_quote_identifier
from ansible.module_utils._text import to_native, to_text
VALID_PRIVS = frozenset(('SELECT', 'INSERT', 'UPDATE', 'DELETE', 'TRUNCATE',
'REFERENCES', 'TRIGGER', 'CREATE', 'CONNECT',
'TEMPORARY', 'TEMP', 'EXECUTE', 'USAGE', 'ALL', 'USAGE'))
class Error(Exception):
pass
@ -306,17 +315,10 @@ class Connection(object):
sslrootcert = params.ssl_rootcert
if psycopg2.__version__ < '2.4.3' and sslrootcert is not None:
module.fail_json(msg='psycopg2 must be at least 2.4.3 in order to user the ssl_rootcert parameter')
raise ValueError('psycopg2 must be at least 2.4.3 in order to user the ssl_rootcert parameter')
try:
self.connection = psycopg2.connect(**kw)
self.cursor = self.connection.cursor()
except TypeError:
e = get_exception()
if 'sslrootcert' in e.args[0]:
module.fail_json(msg='Postgresql server must be at least version 8.4 to support sslrootcert')
module.fail_json(msg="unable to connect to database: %s" % e)
self.connection = psycopg2.connect(**kw)
self.cursor = self.connection.cursor()
def commit(self):
@ -611,9 +613,15 @@ def main():
module.fail_json(msg='Python module "psycopg2" must be installed.')
try:
conn = Connection(p)
except psycopg2.Error:
e = get_exception()
module.fail_json(msg='Could not connect to database: %s' % e)
except psycopg2.Error as e:
module.fail_json(msg='Could not connect to database: %s' % to_native(e), exception=traceback.format_exc())
except TypeError as e:
if 'sslrootcert' in e.args[0]:
module.fail_json(msg='Postgresql server must be at least version 8.4 to support sslrootcert')
module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc())
except ValueError as e:
# We raise this when the psycopg library is too old
module.fail_json(msg=to_native(e))
try:
# privs
@ -652,17 +660,14 @@ def main():
schema_qualifier=p.schema
)
except Error:
e = get_exception()
except Error as e:
conn.rollback()
module.fail_json(msg=e.message)
module.fail_json(msg=e.message, exception=traceback.format_exc())
except psycopg2.Error:
e = get_exception()
except psycopg2.Error as e:
conn.rollback()
# psycopg2 errors come in connection encoding, re-encode
msg = e.message.decode(conn.encoding).encode(sys.getdefaultencoding(),
'replace')
# psycopg2 errors come in connection encoding
msg = to_text(e.message(encoding=conn.encoding))
module.fail_json(msg=msg)
if module.check_mode:
@ -672,8 +677,5 @@ def main():
module.exit_json(changed=changed)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.database import *
if __name__ == '__main__':
main()