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

@@ -203,6 +203,7 @@ user:
import os
import ssl as ssl_lib
import traceback
from distutils.version import LooseVersion
try:
@@ -221,8 +222,9 @@ else:
pymongo_found = True
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils.six import binary_type, text_type
from ansible.module_utils.six.moves import configparser
from ansible.module_utils._text import to_native
# =========================================
@@ -333,7 +335,7 @@ def check_if_roles_changed(uinfo, roles, db_name):
def make_sure_roles_are_a_list_of_dict(roles, db_name):
output = list()
for role in roles:
if isinstance(role, basestring):
if isinstance(role, (binary_type, text_type)):
new_role = { "role": role, "db": db_name }
output.append(new_role)
else:
@@ -427,9 +429,8 @@ def main():
module.fail_json(msg='The localhost login exception only allows the first admin account to be created')
#else: this has to be the first admin user added
except Exception:
e = get_exception()
module.fail_json(msg='unable to connect to database: %s' % str(e))
except Exception as e:
module.fail_json(msg='unable to connect to database: %s' % to_native(e), exception=traceback.format_exc())
if state == 'present':
if password is None and update_password == 'always':
@@ -447,9 +448,8 @@ def main():
module.exit_json(changed=True, user=user)
user_add(module, client, db_name, user, password, roles)
except Exception:
e = get_exception()
module.fail_json(msg='Unable to add or update user: %s' % str(e))
except Exception as e:
module.fail_json(msg='Unable to add or update user: %s' % to_native(e), exception=traceback.format_exc())
# Here we can check password change if mongo provide a query for that : https://jira.mongodb.org/browse/SERVER-22848
#newuinfo = user_find(client, user, db_name)
@@ -459,9 +459,8 @@ def main():
elif state == 'absent':
try:
user_remove(module, client, db_name, user)
except Exception:
e = get_exception()
module.fail_json(msg='Unable to remove user: %s' % str(e))
except Exception as e:
module.fail_json(msg='Unable to remove user: %s' % to_native(e), exception=traceback.format_exc())
module.exit_json(changed=True, user=user)

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()