From 5a949dcafc000454f0715b11119dc2829513c8c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Bompard?= Date: Fri, 6 Sep 2013 00:06:26 +0200 Subject: [PATCH] Module postgresql_privs: fix default arguments Defaults arguments must not be added to the connection keywords, as the other postgresql modules already do. Closes #4043 --- library/database/postgresql_privs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/library/database/postgresql_privs b/library/database/postgresql_privs index 89648acad8..adbad0209a 100644 --- a/library/database/postgresql_privs +++ b/library/database/postgresql_privs @@ -249,12 +249,21 @@ def partial(f, *args, **kwargs): class Connection(object): """Wrapper around a psycopg2 connection with some convenience methods""" - def __init__(self, host, port, login, password, database): - self.database = database - self.connection = psycopg2.connect( - host=host, port=port, user=login, - password=password, database=database - ) + def __init__(self, params): + self.database = params.database + # 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 = { + "host":"host", + "login":"user", + "password":"password", + "port":"port", + "database": "database", + } + kw = dict( (params_map[k], getattr(params, k)) for k in params_map + if getattr(params, k) != '' ) + self.connection = psycopg2.connect(**kw) self.cursor = self.connection.cursor() @@ -503,10 +512,10 @@ def main(): roles=dict(required=True, aliases=['role']), grant_option=dict(required=False, type='bool', aliases=['admin_option']), - host=dict(required=False, aliases=['login_host']), + host=dict(default='', aliases=['login_host']), port=dict(type='int', default=5432), login=dict(default='postgres', aliases=['login_user']), - password=dict(required=False, aliases=['login_password']) + password=dict(default='', aliases=['login_password']) ), supports_check_mode = True ) @@ -541,7 +550,7 @@ def main(): if not psycopg2: module.fail_json(msg='Python module "psycopg2" must be installed.') try: - conn = Connection(p.host, p.port, p.login, p.password, p.database) + conn = Connection(p) except psycopg2.Error, e: module.fail_json(msg='Could not connect to database: %s' % e)