|
|
|
@ -170,19 +170,16 @@ state:
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from psycopg2 import __version__ as PSYCOPG2_VERSION
|
|
|
|
|
from psycopg2.extras import DictCursor
|
|
|
|
|
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT as AUTOCOMMIT
|
|
|
|
|
from psycopg2.extensions import ISOLATION_LEVEL_READ_COMMITTED as READ_COMMITTED
|
|
|
|
|
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, pg_quote_identifier
|
|
|
|
|
from ansible.module_utils.postgres import connect_to_db, postgres_common_argument_spec
|
|
|
|
|
from ansible.module_utils._text import to_native
|
|
|
|
|
from ansible.module_utils.six import iteritems
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PgTablespace(object):
|
|
|
|
@ -306,7 +303,9 @@ class PgTablespace(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
|
|
|
|
|
|
|
|
|
@ -335,26 +334,62 @@ def main():
|
|
|
|
|
supports_check_mode=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not HAS_PSYCOPG2:
|
|
|
|
|
module.fail_json(msg=missing_required_lib('psycopg2'))
|
|
|
|
|
|
|
|
|
|
tablespace = module.params["tablespace"]
|
|
|
|
|
state = module.params["state"]
|
|
|
|
|
location = module.params["location"]
|
|
|
|
|
owner = module.params["owner"]
|
|
|
|
|
rename_to = module.params["rename_to"]
|
|
|
|
|
settings = module.params["set"]
|
|
|
|
|
sslrootcert = module.params["ca_cert"]
|
|
|
|
|
session_role = module.params["session_role"]
|
|
|
|
|
|
|
|
|
|
if state == 'absent' and (location or owner or rename_to or settings):
|
|
|
|
|
module.fail_json(msg="state=absent is mutually exclusive location, "
|
|
|
|
|
"owner, rename_to, and set")
|
|
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
|
"login_host": "host",
|
|
|
|
|
"login_user": "user",
|
|
|
|
|
"login_password": "password",
|
|
|
|
|
"port": "port",
|
|
|
|
|
"db": "database",
|
|
|
|
|
"ssl_mode": "sslmode",
|
|
|
|
|
"ca_cert": "sslrootcert"
|
|
|
|
|
}
|
|
|
|
|
kw = dict((params_map[k], v) for (k, v) in iteritems(module.params)
|
|
|
|
|
if k in params_map and v != '' and v is not None)
|
|
|
|
|
|
|
|
|
|
# If a login_unix_socket is specified, incorporate it here.
|
|
|
|
|
is_localhost = "host" not in kw or kw["host"] is None 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 sslrootcert:
|
|
|
|
|
module.fail_json(msg='psycopg2 must be at least 2.4.3 '
|
|
|
|
|
'in order to user the ca_cert 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))
|
|
|
|
|
|
|
|
|
|
# Change autocommit to False if check_mode:
|
|
|
|
|
if module.check_mode:
|
|
|
|
|
if PSYCOPG2_VERSION >= '2.4.2':
|
|
|
|
|
if psycopg2.__version__ >= '2.4.2':
|
|
|
|
|
db_connection.set_session(autocommit=False)
|
|
|
|
|
else:
|
|
|
|
|
db_connection.set_isolation_level(READ_COMMITTED)
|
|
|
|
|
db_connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED)
|
|
|
|
|
|
|
|
|
|
# Set defaults:
|
|
|
|
|
autocommit = False
|
|
|
|
@ -379,10 +414,10 @@ def main():
|
|
|
|
|
|
|
|
|
|
# Because CREATE TABLESPACE can not be run inside the transaction block:
|
|
|
|
|
autocommit = True
|
|
|
|
|
if PSYCOPG2_VERSION >= '2.4.2':
|
|
|
|
|
if psycopg2.__version__ >= '2.4.2':
|
|
|
|
|
db_connection.set_session(autocommit=True)
|
|
|
|
|
else:
|
|
|
|
|
db_connection.set_isolation_level(AUTOCOMMIT)
|
|
|
|
|
db_connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
|
|
|
|
|
|
|
|
|
|
changed = tblspace.create(location)
|
|
|
|
|
|
|
|
|
@ -395,10 +430,10 @@ def main():
|
|
|
|
|
elif tblspace.exists and state == 'absent':
|
|
|
|
|
# Because DROP TABLESPACE can not be run inside the transaction block:
|
|
|
|
|
autocommit = True
|
|
|
|
|
if PSYCOPG2_VERSION >= '2.4.2':
|
|
|
|
|
if psycopg2.__version__ >= '2.4.2':
|
|
|
|
|
db_connection.set_session(autocommit=True)
|
|
|
|
|
else:
|
|
|
|
|
db_connection.set_isolation_level(AUTOCOMMIT)
|
|
|
|
|
db_connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
|
|
|
|
|
|
|
|
|
|
changed = tblspace.drop()
|
|
|
|
|
|
|
|
|
|