mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-24 19:31:26 -07:00
Convert all databases modules to python3 and 2.4 syntax (#3688)
This commit is contained in:
parent
f7b29ba8fd
commit
09066f1518
6 changed files with 54 additions and 28 deletions
|
@ -280,7 +280,8 @@ def main():
|
|||
.ISOLATION_LEVEL_AUTOCOMMIT)
|
||||
cursor = db_connection.cursor(
|
||||
cursor_factory=psycopg2.extras.DictCursor)
|
||||
except Exception, e:
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
module.fail_json(msg="unable to connect to database: %s" % e)
|
||||
|
||||
try:
|
||||
|
@ -295,21 +296,25 @@ def main():
|
|||
if state == "absent":
|
||||
try:
|
||||
changed = db_delete(cursor, db)
|
||||
except SQLParseError, e:
|
||||
except SQLParseError:
|
||||
e = get_exception()
|
||||
module.fail_json(msg=str(e))
|
||||
|
||||
elif state == "present":
|
||||
try:
|
||||
changed = db_create(cursor, db, owner, template, encoding,
|
||||
lc_collate, lc_ctype)
|
||||
except SQLParseError, e:
|
||||
except SQLParseError:
|
||||
e = get_exception()
|
||||
module.fail_json(msg=str(e))
|
||||
except NotSupportedError, e:
|
||||
except NotSupportedError:
|
||||
e = get_exception()
|
||||
module.fail_json(msg=str(e))
|
||||
except SystemExit:
|
||||
# Avoid catching this on Python 2.4
|
||||
raise
|
||||
except Exception, e:
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
module.fail_json(msg="Database query failed: %s" % e)
|
||||
|
||||
module.exit_json(changed=changed, db=db)
|
||||
|
|
|
@ -573,7 +573,8 @@ def main():
|
|||
module.fail_json(msg='Python module "psycopg2" must be installed.')
|
||||
try:
|
||||
conn = Connection(p)
|
||||
except psycopg2.Error, e:
|
||||
except psycopg2.Error:
|
||||
e = get_exception()
|
||||
module.fail_json(msg='Could not connect to database: %s' % e)
|
||||
|
||||
try:
|
||||
|
@ -613,11 +614,13 @@ def main():
|
|||
schema_qualifier=p.schema
|
||||
)
|
||||
|
||||
except Error, e:
|
||||
except Error:
|
||||
e = get_exception()
|
||||
conn.rollback()
|
||||
module.fail_json(msg=e.message)
|
||||
|
||||
except psycopg2.Error, e:
|
||||
except psycopg2.Error:
|
||||
e = get_exception()
|
||||
conn.rollback()
|
||||
# psycopg2 errors come in connection encoding, reencode
|
||||
msg = e.message.decode(conn.encoding).encode(sys.getdefaultencoding(),
|
||||
|
|
|
@ -290,7 +290,8 @@ def user_alter(cursor, module, user, password, role_attr_flags, encrypted, expir
|
|||
|
||||
try:
|
||||
cursor.execute(' '.join(alter), query_password_data)
|
||||
except psycopg2.InternalError, e:
|
||||
except psycopg2.InternalError:
|
||||
e = get_exception()
|
||||
if e.pgcode == '25006':
|
||||
# Handle errors due to read-only transactions indicated by pgcode 25006
|
||||
# ERROR: cannot execute ALTER ROLE in a read-only transaction
|
||||
|
@ -298,7 +299,7 @@ def user_alter(cursor, module, user, password, role_attr_flags, encrypted, expir
|
|||
module.fail_json(msg=e.pgerror)
|
||||
return changed
|
||||
else:
|
||||
raise psycopg2.InternalError, e
|
||||
raise psycopg2.InternalError(e)
|
||||
|
||||
# Grab new role attributes.
|
||||
cursor.execute(select, {"user": user})
|
||||
|
@ -551,7 +552,8 @@ def main():
|
|||
no_password_changes = module.params["no_password_changes"]
|
||||
try:
|
||||
role_attr_flags = parse_role_attrs(module.params["role_attr_flags"])
|
||||
except InvalidFlagsError, e:
|
||||
except InvalidFlagsError:
|
||||
e = get_exception()
|
||||
module.fail_json(msg=str(e))
|
||||
if module.params["encrypted"]:
|
||||
encrypted = "ENCRYPTED"
|
||||
|
@ -583,7 +585,8 @@ def main():
|
|||
try:
|
||||
db_connection = psycopg2.connect(**kw)
|
||||
cursor = db_connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
|
||||
except Exception, e:
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
module.fail_json(msg="unable to connect to database: %s" % e)
|
||||
|
||||
kw = dict(user=user)
|
||||
|
@ -594,16 +597,19 @@ def main():
|
|||
if user_exists(cursor, user):
|
||||
try:
|
||||
changed = user_alter(cursor, module, user, password, role_attr_flags, encrypted, expires, no_password_changes)
|
||||
except SQLParseError, e:
|
||||
except SQLParseError:
|
||||
e = get_exception()
|
||||
module.fail_json(msg=str(e))
|
||||
else:
|
||||
try:
|
||||
changed = user_add(cursor, user, password, role_attr_flags, encrypted, expires)
|
||||
except SQLParseError, e:
|
||||
except SQLParseError:
|
||||
e = get_exception()
|
||||
module.fail_json(msg=str(e))
|
||||
try:
|
||||
changed = grant_privileges(cursor, user, privs) or changed
|
||||
except SQLParseError, e:
|
||||
except SQLParseError:
|
||||
e = get_exception()
|
||||
module.fail_json(msg=str(e))
|
||||
else:
|
||||
if user_exists(cursor, user):
|
||||
|
@ -614,7 +620,8 @@ def main():
|
|||
try:
|
||||
changed = revoke_privileges(cursor, user, privs)
|
||||
user_removed = user_delete(cursor, user)
|
||||
except SQLParseError, e:
|
||||
except SQLParseError:
|
||||
e = get_exception()
|
||||
module.fail_json(msg=str(e))
|
||||
changed = changed or user_removed
|
||||
if fail_on_user and not user_removed:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue