postgresql_info: removed deprecated checks, added doc strings (#57005)

This commit is contained in:
Andrey Klychkov 2019-05-27 14:13:22 +03:00 committed by ansibot
parent 19a78bdc7b
commit c0fd1bef7c

View file

@ -486,36 +486,33 @@ from ansible.module_utils.six import iteritems
#
class PgDbConn(object):
"""Auxiliary class for working with PostgreSQL connection objects.
Arguments:
module (AnsibleModule): Object of AnsibleModule class that
contains connection parameters.
"""
def __init__(self, module):
self.module = module
self.db_conn = None
self.cursor = None
self.session_role = self.module.params.get('session_role')
def connect(self):
try:
self.db_conn = connect_to_db(self.module, warn_db_default=False)
self.cursor = self.db_conn.cursor(cursor_factory=DictCursor)
"""Connect to a PostgreSQL database and return a cursor object.
# Switch role, if specified:
if self.session_role:
try:
self.cursor.execute('SET ROLE %s' % self.session_role)
except Exception as e:
self.module.fail_json(msg="Could not switch role: %s" % to_native(e))
return self.cursor
except TypeError as e:
if 'sslrootcert' in e.args[0]:
self.module.fail_json(msg='PostgreSQL server must be at least version 8.4 '
'to support sslrootcert')
self.module.fail_json(msg="Unable to connect to database: %s" % to_native(e))
except Exception as e:
self.module.fail_json(msg="Unable to connect to database: %s" % to_native(e))
Note: connection parameters are passed by self.module object.
"""
self.db_conn = connect_to_db(self.module, warn_db_default=False)
return self.db_conn.cursor(cursor_factory=DictCursor)
def reconnect(self, dbname):
"""Reconnect to another database and return a PostgreSQL cursor object.
Arguments:
dbname (string): Database name to connect to.
"""
self.db_conn.close()
self.module.params['database'] = dbname
@ -523,6 +520,14 @@ class PgDbConn(object):
class PgClusterInfo(object):
"""Class for collection information about a PostgreSQL instance.
Arguments:
module (AnsibleModule): Object of AnsibleModule class.
db_conn_obj (psycopg2.connect): PostgreSQL connection object.
"""
def __init__(self, module, db_conn_obj):
self.module = module
self.db_obj = db_conn_obj
@ -539,6 +544,9 @@ class PgClusterInfo(object):
}
def collect(self, val_list=False):
"""
Collect information based on 'filter' option.
"""
subset_map = {
"version": self.get_pg_version,
"tablespaces": self.get_tablespaces,
@ -850,6 +858,9 @@ class PgClusterInfo(object):
return nsp_dict
def get_pg_version(self):
"""
Get major and minor PostgreSQL server version.
"""
query = "SELECT version()"
raw = self.__exec_sql(query)[0][0]
raw = raw.split()[1].split('.')
@ -859,6 +870,10 @@ class PgClusterInfo(object):
)
def get_db_info(self):
"""
Get information about the current database.
"""
# Following query returns:
# Name, Owner, Encoding, Collate, Ctype, Access Priv, Size
query = ("SELECT d.datname, "
@ -897,9 +912,15 @@ class PgClusterInfo(object):
self.pg_info["databases"] = db_dict
def __get_pretty_val(self, setting):
"""
Get setting's value represented by SHOW command.
"""
return self.__exec_sql("SHOW %s" % setting)[0][0]
def __exec_sql(self, query):
"""
Execute SQL and return the result.
"""
try:
self.cursor.execute(query)
res = self.cursor.fetchall()