Revert "postgres modules: move params mapping from main to connect_to_db (#55549)"

This reverts commit 2250257809.
This commit is contained in:
Matt Clay 2019-04-25 09:25:27 -07:00
parent 7b86208fcd
commit 6fe57846d0
16 changed files with 694 additions and 219 deletions

View file

@ -142,15 +142,14 @@ queries:
'''
try:
from psycopg2.extras import DictCursor
import psycopg2
HAS_PSYCOPG2 = True
except ImportError:
# psycopg2 is checked by connect_to_db()
# from ansible.module_utils.postgres
pass
HAS_PSYCOPG2 = False
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.database import SQLParseError
from ansible.module_utils.postgres import connect_to_db, postgres_common_argument_spec
from ansible.module_utils.postgres import connect_to_db, get_pg_version, postgres_common_argument_spec
from ansible.module_utils._text import to_native
@ -183,7 +182,8 @@ class PgSlot(object):
if kind == 'physical':
# Check server version (needs for immedately_reserverd needs 9.6+):
if self.cursor.connection.server_version < 96000:
ver = get_pg_version(self.cursor)
if ver < 96000:
query = "SELECT pg_create_physical_replication_slot('%s')" % self.name
else:
@ -219,7 +219,9 @@ class PgSlot(object):
res = self.cursor.fetchall()
return res
return True
except Exception as e:
except SQLParseError as e:
self.module.fail_json(msg=to_native(e))
except psycopg2.ProgrammingError as e:
self.module.fail_json(msg="Cannot execute SQL '%s': %s" % (query, to_native(e)))
return False
@ -246,17 +248,53 @@ def main():
supports_check_mode=True,
)
if not HAS_PSYCOPG2:
module.fail_json(msg=missing_required_lib('psycopg2'))
db = module.params["db"]
name = module.params["name"]
slot_type = module.params["slot_type"]
immediately_reserve = module.params["immediately_reserve"]
state = module.params["state"]
ssl_rootcert = module.params["ca_cert"]
output_plugin = module.params["output_plugin"]
session_role = module.params["session_role"]
if immediately_reserve and slot_type == 'logical':
module.fail_json(msg="Module parameters immediately_reserve and slot_type=logical are mutually exclusive")
db_connection = connect_to_db(module, autocommit=True)
cursor = db_connection.cursor(cursor_factory=DictCursor)
# To use defaults values, keyword arguments must be absent, so
# check which values are empty and don't include in the **kw
# dictionary
params_map = {
"db": "database",
"login_host": "host",
"login_user": "user",
"login_password": "password",
"port": "port",
"sslmode": "ssl_mode",
"ca_cert": "ssl_rootcert"
}
kw = dict((params_map[k], v) for (k, v) in module.params.items()
if k in params_map and v != '')
# if a login_unix_socket is specified, incorporate it here
is_localhost = "host" not in kw or kw["host"] == "" or kw["host"] == "localhost"
if is_localhost and module.params["login_unix_socket"] != "":
kw["host"] = module.params["login_unix_socket"]
if psycopg2.__version__ < '2.4.3' and ssl_rootcert is not None:
module.fail_json(msg='psycopg2 must be at least 2.4.3 in order to use the ssl_rootcert parameter')
db_connection = connect_to_db(module, kw, autocommit=True)
cursor = db_connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
# Switch role, if specified:
if session_role:
try:
cursor.execute('SET ROLE %s' % session_role)
except Exception as e:
module.fail_json(msg="Could not switch role: %s" % to_native(e))
##################################
# Create an object and do main job
@ -283,7 +321,6 @@ def main():
changed = pg_slot.changed
db_connection.close()
module.exit_json(changed=changed, name=name, queries=pg_slot.executed_queries)