mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
Bulk autopep8 (modules)
As agreed in 2017-12-07 Core meeting bulk fix pep8 issues Generated using: autopep8 1.3.3 (pycodestyle: 2.3.1) autopep8 -r --max-line-length 160 --in-place --ignore E305,E402,E722,E741 lib/ansible/modules Manually fix issues that autopep8 has introduced
This commit is contained in:
committed by
John R Barker
parent
d13d7e9404
commit
c57a7f05e1
@@ -147,9 +147,11 @@ def parse_plugin_repo(string):
|
||||
|
||||
return repo
|
||||
|
||||
|
||||
def is_plugin_present(plugin_dir, working_dir):
|
||||
return os.path.isdir(os.path.join(working_dir, plugin_dir))
|
||||
|
||||
|
||||
def parse_error(string):
|
||||
reason = "reason: "
|
||||
try:
|
||||
@@ -157,6 +159,7 @@ def parse_error(string):
|
||||
except ValueError:
|
||||
return string
|
||||
|
||||
|
||||
def install_plugin(module, plugin_bin, plugin_name, url, timeout):
|
||||
cmd_args = [plugin_bin, "plugin", PACKAGE_STATE_MAP["present"], plugin_name]
|
||||
|
||||
@@ -178,6 +181,7 @@ def install_plugin(module, plugin_bin, plugin_name, url, timeout):
|
||||
|
||||
return True, cmd, out, err
|
||||
|
||||
|
||||
def remove_plugin(module, plugin_bin, plugin_name):
|
||||
cmd_args = [plugin_bin, "plugin", PACKAGE_STATE_MAP["absent"], plugin_name]
|
||||
|
||||
@@ -193,6 +197,7 @@ def remove_plugin(module, plugin_bin, plugin_name):
|
||||
|
||||
return True, cmd, out, err
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
@@ -208,14 +213,14 @@ def main():
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
name = module.params["name"]
|
||||
state = module.params["state"]
|
||||
url = module.params["url"]
|
||||
timeout = module.params["timeout"]
|
||||
plugin_bin = module.params["plugin_bin"]
|
||||
plugin_dir = module.params["plugin_dir"]
|
||||
version = module.params["version"]
|
||||
force = module.params["force"]
|
||||
name = module.params["name"]
|
||||
state = module.params["state"]
|
||||
url = module.params["url"]
|
||||
timeout = module.params["timeout"]
|
||||
plugin_bin = module.params["plugin_bin"]
|
||||
plugin_dir = module.params["plugin_dir"]
|
||||
version = module.params["version"]
|
||||
force = module.params["force"]
|
||||
|
||||
present = is_plugin_present(parse_plugin_repo(name), plugin_dir)
|
||||
|
||||
|
||||
@@ -102,6 +102,7 @@ def ring_check(module, riak_admin_bin):
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
module = AnsibleModule(
|
||||
@@ -115,10 +116,9 @@ def main():
|
||||
wait_for_ring=dict(default=False, type='int'),
|
||||
wait_for_service=dict(
|
||||
required=False, default=None, choices=['kv']),
|
||||
validate_certs = dict(default='yes', type='bool'))
|
||||
validate_certs=dict(default='yes', type='bool'))
|
||||
)
|
||||
|
||||
|
||||
command = module.params.get('command')
|
||||
http_conn = module.params.get('http_conn')
|
||||
target_node = module.params.get('target_node')
|
||||
@@ -126,8 +126,7 @@ def main():
|
||||
wait_for_ring = module.params.get('wait_for_ring')
|
||||
wait_for_service = module.params.get('wait_for_service')
|
||||
|
||||
|
||||
#make sure riak commands are on the path
|
||||
# make sure riak commands are on the path
|
||||
riak_bin = module.get_bin_path('riak')
|
||||
riak_admin_bin = module.get_bin_path('riak-admin')
|
||||
|
||||
@@ -150,16 +149,16 @@ def main():
|
||||
node_name = stats['nodename']
|
||||
nodes = stats['ring_members']
|
||||
ring_size = stats['ring_creation_size']
|
||||
rc, out, err = module.run_command([riak_bin, 'version'] )
|
||||
rc, out, err = module.run_command([riak_bin, 'version'])
|
||||
version = out.strip()
|
||||
|
||||
result = dict(node_name=node_name,
|
||||
nodes=nodes,
|
||||
ring_size=ring_size,
|
||||
version=version)
|
||||
nodes=nodes,
|
||||
ring_size=ring_size,
|
||||
version=version)
|
||||
|
||||
if command == 'ping':
|
||||
cmd = '%s ping %s' % ( riak_bin, target_node )
|
||||
cmd = '%s ping %s' % (riak_bin, target_node)
|
||||
rc, out, err = module.run_command(cmd)
|
||||
if rc == 0:
|
||||
result['ping'] = out
|
||||
@@ -219,7 +218,7 @@ def main():
|
||||
module.fail_json(msg='Timeout waiting for handoffs.')
|
||||
|
||||
if wait_for_service:
|
||||
cmd = [riak_admin_bin, 'wait_for_service', 'riak_%s' % wait_for_service, node_name ]
|
||||
cmd = [riak_admin_bin, 'wait_for_service', 'riak_%s' % wait_for_service, node_name]
|
||||
rc, out, err = module.run_command(cmd)
|
||||
result['service'] = out
|
||||
|
||||
|
||||
@@ -223,5 +223,5 @@ def main():
|
||||
after=value)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
@@ -270,8 +270,8 @@ def user_find(client, user, db_name):
|
||||
|
||||
|
||||
def user_add(module, client, db_name, user, password, roles):
|
||||
#pymongo's user_add is a _create_or_update_user so we won't know if it was changed or updated
|
||||
#without reproducing a lot of the logic in database.py of pymongo
|
||||
# pymongo's user_add is a _create_or_update_user so we won't know if it was changed or updated
|
||||
# without reproducing a lot of the logic in database.py of pymongo
|
||||
db = client[db_name]
|
||||
|
||||
if roles is None:
|
||||
@@ -279,6 +279,7 @@ def user_add(module, client, db_name, user, password, roles):
|
||||
else:
|
||||
db.add_user(user, password, None, roles=roles)
|
||||
|
||||
|
||||
def user_remove(module, client, db_name, user):
|
||||
exists = user_find(client, user, db_name)
|
||||
if exists:
|
||||
@@ -289,6 +290,7 @@ def user_remove(module, client, db_name, user):
|
||||
else:
|
||||
module.exit_json(changed=False, user=user)
|
||||
|
||||
|
||||
def load_mongocnf():
|
||||
config = configparser.RawConfigParser()
|
||||
mongocnf = os.path.expanduser('~/.mongodb.cnf')
|
||||
@@ -305,7 +307,6 @@ def load_mongocnf():
|
||||
return creds
|
||||
|
||||
|
||||
|
||||
def check_if_roles_changed(uinfo, roles, db_name):
|
||||
# We must be aware of users which can read the oplog on a replicaset
|
||||
# Such users must have access to the local DB, but since this DB does not store users credentials
|
||||
@@ -327,7 +328,7 @@ def check_if_roles_changed(uinfo, roles, db_name):
|
||||
output = list()
|
||||
for role in roles:
|
||||
if isinstance(role, (binary_type, text_type)):
|
||||
new_role = { "role": role, "db": db_name }
|
||||
new_role = {"role": role, "db": db_name}
|
||||
output.append(new_role)
|
||||
else:
|
||||
output.append(role)
|
||||
@@ -341,14 +342,13 @@ def check_if_roles_changed(uinfo, roles, db_name):
|
||||
return True
|
||||
|
||||
|
||||
|
||||
# =========================================
|
||||
# Module execution.
|
||||
#
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec = dict(
|
||||
argument_spec=dict(
|
||||
login_user=dict(default=None),
|
||||
login_password=dict(default=None, no_log=True),
|
||||
login_host=dict(default='localhost'),
|
||||
@@ -417,7 +417,7 @@ def main():
|
||||
elif LooseVersion(PyMongoVersion) >= LooseVersion('3.0'):
|
||||
if db_name != "admin":
|
||||
module.fail_json(msg='The localhost login exception only allows the first admin account to be created')
|
||||
#else: this has to be the first admin user added
|
||||
# else: this has to be the first admin user added
|
||||
|
||||
except Exception as e:
|
||||
module.fail_json(msg='unable to connect to database: %s' % to_native(e), exception=traceback.format_exc())
|
||||
@@ -443,7 +443,7 @@ def main():
|
||||
|
||||
# Here we can check password change if mongo provide a query for that : https://jira.mongodb.org/browse/SERVER-22848
|
||||
#newuinfo = user_find(client, user, db_name)
|
||||
#if uinfo['role'] == newuinfo['role'] and CheckPasswordHere:
|
||||
# if uinfo['role'] == newuinfo['role'] and CheckPasswordHere:
|
||||
# module.exit_json(changed=False, user=user)
|
||||
|
||||
elif state == 'absent':
|
||||
|
||||
@@ -91,7 +91,7 @@ EXAMPLES = '''
|
||||
target: /tmp/dump.sql
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
RETURN = '''
|
||||
#
|
||||
'''
|
||||
|
||||
@@ -126,6 +126,7 @@ def db_delete(conn, cursor, db):
|
||||
cursor.execute("DROP DATABASE [%s]" % db)
|
||||
return not db_exists(conn, cursor, db)
|
||||
|
||||
|
||||
def db_import(conn, cursor, module, db, target):
|
||||
if os.path.isfile(target):
|
||||
backup = open(target, 'r')
|
||||
|
||||
@@ -99,6 +99,7 @@ def ext_exists(cursor, ext):
|
||||
cursor.execute(query, {'ext': ext})
|
||||
return cursor.rowcount == 1
|
||||
|
||||
|
||||
def ext_delete(cursor, ext):
|
||||
if ext_exists(cursor, ext):
|
||||
query = "DROP EXTENSION \"%s\"" % ext
|
||||
@@ -107,6 +108,7 @@ def ext_delete(cursor, ext):
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def ext_create(cursor, ext):
|
||||
if not ext_exists(cursor, ext):
|
||||
query = 'CREATE EXTENSION "%s"' % ext
|
||||
@@ -119,6 +121,7 @@ def ext_create(cursor, ext):
|
||||
# Module execution.
|
||||
#
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
@@ -130,7 +133,7 @@ def main():
|
||||
ext=dict(required=True, aliases=['name']),
|
||||
state=dict(default="present", choices=["absent", "present"]),
|
||||
),
|
||||
supports_check_mode = True
|
||||
supports_check_mode=True
|
||||
)
|
||||
|
||||
if not postgresqldb_found:
|
||||
@@ -145,13 +148,13 @@ def main():
|
||||
# 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"
|
||||
"login_host": "host",
|
||||
"login_user": "user",
|
||||
"login_password": "password",
|
||||
"port": "port"
|
||||
}
|
||||
kw = dict( (params_map[k], v) for (k, v) in module.params.items()
|
||||
if k in params_map and v != '' )
|
||||
kw = dict((params_map[k], v) for (k, v) in module.params.items()
|
||||
if k in params_map and v != '')
|
||||
try:
|
||||
db_connection = psycopg2.connect(database=db, **kw)
|
||||
# Enable autocommit so we can create databases
|
||||
|
||||
@@ -161,18 +161,21 @@ def lang_exists(cursor, lang):
|
||||
cursor.execute(query)
|
||||
return cursor.rowcount > 0
|
||||
|
||||
|
||||
def lang_istrusted(cursor, lang):
|
||||
"""Checks if language is trusted for db"""
|
||||
query = "SELECT lanpltrusted FROM pg_language WHERE lanname='%s'" % lang
|
||||
cursor.execute(query)
|
||||
return cursor.fetchone()[0]
|
||||
|
||||
|
||||
def lang_altertrust(cursor, lang, trust):
|
||||
"""Changes if language is trusted for db"""
|
||||
query = "UPDATE pg_language SET lanpltrusted = %s WHERE lanname=%s"
|
||||
cursor.execute(query, (trust, lang))
|
||||
return True
|
||||
|
||||
|
||||
def lang_add(cursor, lang, trust):
|
||||
"""Adds language for db"""
|
||||
if trust:
|
||||
@@ -182,6 +185,7 @@ def lang_add(cursor, lang, trust):
|
||||
cursor.execute(query)
|
||||
return True
|
||||
|
||||
|
||||
def lang_drop(cursor, lang, cascade):
|
||||
"""Drops language for db"""
|
||||
cursor.execute("SAVEPOINT ansible_pgsql_lang_drop")
|
||||
@@ -197,6 +201,7 @@ def lang_drop(cursor, lang, cascade):
|
||||
cursor.execute("RELEASE SAVEPOINT ansible_pgsql_lang_drop")
|
||||
return True
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
@@ -212,7 +217,7 @@ def main():
|
||||
cascade=dict(type='bool', default='no'),
|
||||
fail_on_drop=dict(type='bool', default='yes'),
|
||||
),
|
||||
supports_check_mode = True
|
||||
supports_check_mode=True
|
||||
)
|
||||
|
||||
db = module.params["db"]
|
||||
@@ -227,14 +232,14 @@ def main():
|
||||
module.fail_json(msg="the python psycopg2 module is required")
|
||||
|
||||
params_map = {
|
||||
"login_host":"host",
|
||||
"login_user":"user",
|
||||
"login_password":"password",
|
||||
"port":"port",
|
||||
"db":"database"
|
||||
"login_host": "host",
|
||||
"login_user": "user",
|
||||
"login_password": "password",
|
||||
"port": "port",
|
||||
"db": "database"
|
||||
}
|
||||
kw = dict( (params_map[k], v) for (k, v) in module.params.items()
|
||||
if k in params_map and v != "" )
|
||||
kw = dict((params_map[k], v) for (k, v) in module.params.items()
|
||||
if k in params_map and v != "")
|
||||
try:
|
||||
db_connection = psycopg2.connect(**kw)
|
||||
cursor = db_connection.cursor()
|
||||
|
||||
@@ -125,6 +125,7 @@ def set_owner(cursor, schema, owner):
|
||||
cursor.execute(query)
|
||||
return True
|
||||
|
||||
|
||||
def get_schema_info(cursor, schema):
|
||||
query = """
|
||||
SELECT schema_owner AS owner
|
||||
@@ -134,11 +135,13 @@ def get_schema_info(cursor, schema):
|
||||
cursor.execute(query, {'schema': schema})
|
||||
return cursor.fetchone()
|
||||
|
||||
|
||||
def schema_exists(cursor, schema):
|
||||
query = "SELECT schema_name FROM information_schema.schemata WHERE schema_name = %(schema)s"
|
||||
cursor.execute(query, {'schema': schema})
|
||||
return cursor.rowcount == 1
|
||||
|
||||
|
||||
def schema_delete(cursor, schema):
|
||||
if schema_exists(cursor, schema):
|
||||
query = "DROP SCHEMA %s" % pg_quote_identifier(schema, 'schema')
|
||||
@@ -147,6 +150,7 @@ def schema_delete(cursor, schema):
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def schema_create(cursor, schema, owner):
|
||||
if not schema_exists(cursor, schema):
|
||||
query_fragments = ['CREATE SCHEMA %s' % pg_quote_identifier(schema, 'schema')]
|
||||
@@ -162,6 +166,7 @@ def schema_create(cursor, schema, owner):
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def schema_matches(cursor, schema, owner):
|
||||
if not schema_exists(cursor, schema):
|
||||
return False
|
||||
@@ -176,6 +181,7 @@ def schema_matches(cursor, schema, owner):
|
||||
# Module execution.
|
||||
#
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
@@ -189,7 +195,7 @@ def main():
|
||||
database=dict(default="postgres"),
|
||||
state=dict(default="present", choices=["absent", "present"]),
|
||||
),
|
||||
supports_check_mode = True
|
||||
supports_check_mode=True
|
||||
)
|
||||
|
||||
if not postgresqldb_found:
|
||||
@@ -205,13 +211,13 @@ def main():
|
||||
# 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"
|
||||
"login_host": "host",
|
||||
"login_user": "user",
|
||||
"login_password": "password",
|
||||
"port": "port"
|
||||
}
|
||||
kw = dict( (params_map[k], v) for (k, v) in module.params.items()
|
||||
if k in params_map and v != '' )
|
||||
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"
|
||||
|
||||
@@ -86,11 +86,13 @@ from ansible.module_utils._text import to_native
|
||||
class NotSupportedError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class CannotDropError(Exception):
|
||||
pass
|
||||
|
||||
# module specific functions
|
||||
|
||||
|
||||
def get_configuration_facts(cursor, parameter_name=''):
|
||||
facts = {}
|
||||
cursor.execute("""
|
||||
@@ -110,12 +112,14 @@ def get_configuration_facts(cursor, parameter_name=''):
|
||||
'default_value': row.default_value}
|
||||
return facts
|
||||
|
||||
|
||||
def check(configuration_facts, parameter_name, current_value):
|
||||
parameter_key = parameter_name.lower()
|
||||
if current_value and current_value.lower() != configuration_facts[parameter_key]['current_value'].lower():
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def present(configuration_facts, cursor, parameter_name, current_value):
|
||||
parameter_key = parameter_name.lower()
|
||||
changed = False
|
||||
@@ -128,6 +132,7 @@ def present(configuration_facts, cursor, parameter_name, current_value):
|
||||
|
||||
# module logic
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
module = AnsibleModule(
|
||||
@@ -139,7 +144,7 @@ def main():
|
||||
port=dict(default='5433'),
|
||||
login_user=dict(default='dbadmin'),
|
||||
login_password=dict(default=None, no_log=True),
|
||||
), supports_check_mode = True)
|
||||
), supports_check_mode=True)
|
||||
|
||||
if not pyodbc_found:
|
||||
module.fail_json(msg="The python pyodbc module is required.")
|
||||
@@ -161,8 +166,8 @@ def main():
|
||||
"User={3};"
|
||||
"Password={4};"
|
||||
"ConnectionLoadBalance={5}"
|
||||
).format(module.params['cluster'], module.params['port'], db,
|
||||
module.params['login_user'], module.params['login_password'], 'true')
|
||||
).format(module.params['cluster'], module.params['port'], db,
|
||||
module.params['login_user'], module.params['login_password'], 'true')
|
||||
db_conn = pyodbc.connect(dsn, autocommit=True)
|
||||
cursor = db_conn.cursor()
|
||||
except Exception as e:
|
||||
|
||||
@@ -81,6 +81,7 @@ class NotSupportedError(Exception):
|
||||
|
||||
# module specific functions
|
||||
|
||||
|
||||
def get_schema_facts(cursor, schema=''):
|
||||
facts = {}
|
||||
cursor.execute("""
|
||||
@@ -121,6 +122,7 @@ def get_schema_facts(cursor, schema=''):
|
||||
facts[schema_key]['usage_roles'].append(row.role_name)
|
||||
return facts
|
||||
|
||||
|
||||
def get_user_facts(cursor, user=''):
|
||||
facts = {}
|
||||
cursor.execute("""
|
||||
@@ -155,6 +157,7 @@ def get_user_facts(cursor, user=''):
|
||||
facts[user_key]['default_roles'] = row.default_roles.replace(' ', '').split(',')
|
||||
return facts
|
||||
|
||||
|
||||
def get_role_facts(cursor, role=''):
|
||||
facts = {}
|
||||
cursor.execute("""
|
||||
@@ -175,6 +178,7 @@ def get_role_facts(cursor, role=''):
|
||||
facts[role_key]['assigned_roles'] = row.assigned_roles.replace(' ', '').split(',')
|
||||
return facts
|
||||
|
||||
|
||||
def get_configuration_facts(cursor, parameter=''):
|
||||
facts = {}
|
||||
cursor.execute("""
|
||||
@@ -194,6 +198,7 @@ def get_configuration_facts(cursor, parameter=''):
|
||||
'default_value': row.default_value}
|
||||
return facts
|
||||
|
||||
|
||||
def get_node_facts(cursor, schema=''):
|
||||
facts = {}
|
||||
cursor.execute("""
|
||||
@@ -216,6 +221,7 @@ def get_node_facts(cursor, schema=''):
|
||||
|
||||
# module logic
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
module = AnsibleModule(
|
||||
@@ -225,7 +231,7 @@ def main():
|
||||
db=dict(default=None),
|
||||
login_user=dict(default='dbadmin'),
|
||||
login_password=dict(default=None, no_log=True),
|
||||
), supports_check_mode = True)
|
||||
), supports_check_mode=True)
|
||||
|
||||
if not pyodbc_found:
|
||||
module.fail_json(msg="The python pyodbc module is required.")
|
||||
@@ -243,8 +249,8 @@ def main():
|
||||
"User=%s;"
|
||||
"Password=%s;"
|
||||
"ConnectionLoadBalance=%s"
|
||||
) % (module.params['cluster'], module.params['port'], db,
|
||||
module.params['login_user'], module.params['login_password'], 'true')
|
||||
) % (module.params['cluster'], module.params['port'], db,
|
||||
module.params['login_user'], module.params['login_password'], 'true')
|
||||
db_conn = pyodbc.connect(dsn, autocommit=True)
|
||||
cursor = db_conn.cursor()
|
||||
except Exception as e:
|
||||
@@ -257,11 +263,11 @@ def main():
|
||||
configuration_facts = get_configuration_facts(cursor)
|
||||
node_facts = get_node_facts(cursor)
|
||||
module.exit_json(changed=False,
|
||||
ansible_facts={'vertica_schemas': schema_facts,
|
||||
'vertica_users': user_facts,
|
||||
'vertica_roles': role_facts,
|
||||
'vertica_configuration': configuration_facts,
|
||||
'vertica_nodes': node_facts})
|
||||
ansible_facts={'vertica_schemas': schema_facts,
|
||||
'vertica_users': user_facts,
|
||||
'vertica_roles': role_facts,
|
||||
'vertica_configuration': configuration_facts,
|
||||
'vertica_nodes': node_facts})
|
||||
except NotSupportedError as e:
|
||||
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
||||
except SystemExit:
|
||||
|
||||
@@ -98,11 +98,13 @@ from ansible.module_utils._text import to_native
|
||||
class NotSupportedError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class CannotDropError(Exception):
|
||||
pass
|
||||
|
||||
# module specific functions
|
||||
|
||||
|
||||
def get_role_facts(cursor, role=''):
|
||||
facts = {}
|
||||
cursor.execute("""
|
||||
@@ -123,6 +125,7 @@ def get_role_facts(cursor, role=''):
|
||||
facts[role_key]['assigned_roles'] = row.assigned_roles.replace(' ', '').split(',')
|
||||
return facts
|
||||
|
||||
|
||||
def update_roles(role_facts, cursor, role,
|
||||
existing, required):
|
||||
for assigned_role in set(existing) - set(required):
|
||||
@@ -130,6 +133,7 @@ def update_roles(role_facts, cursor, role,
|
||||
for assigned_role in set(required) - set(existing):
|
||||
cursor.execute("grant {0} to {1}".format(assigned_role, role))
|
||||
|
||||
|
||||
def check(role_facts, role, assigned_roles):
|
||||
role_key = role.lower()
|
||||
if role_key not in role_facts:
|
||||
@@ -138,6 +142,7 @@ def check(role_facts, role, assigned_roles):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def present(role_facts, cursor, role, assigned_roles):
|
||||
role_key = role.lower()
|
||||
if role_key not in role_facts:
|
||||
@@ -147,19 +152,20 @@ def present(role_facts, cursor, role, assigned_roles):
|
||||
return True
|
||||
else:
|
||||
changed = False
|
||||
if assigned_roles and (sorted(assigned_roles) != sorted(role_facts[role_key]['assigned_roles'])):
|
||||
if assigned_roles and (sorted(assigned_roles) != sorted(role_facts[role_key]['assigned_roles'])):
|
||||
update_roles(role_facts, cursor, role,
|
||||
role_facts[role_key]['assigned_roles'], assigned_roles)
|
||||
role_facts[role_key]['assigned_roles'], assigned_roles)
|
||||
changed = True
|
||||
if changed:
|
||||
role_facts.update(get_role_facts(cursor, role))
|
||||
return changed
|
||||
|
||||
|
||||
def absent(role_facts, cursor, role, assigned_roles):
|
||||
role_key = role.lower()
|
||||
if role_key in role_facts:
|
||||
update_roles(role_facts, cursor, role,
|
||||
role_facts[role_key]['assigned_roles'], [])
|
||||
role_facts[role_key]['assigned_roles'], [])
|
||||
cursor.execute("drop role {0} cascade".format(role_facts[role_key]['name']))
|
||||
del role_facts[role_key]
|
||||
return True
|
||||
@@ -168,6 +174,7 @@ def absent(role_facts, cursor, role, assigned_roles):
|
||||
|
||||
# module logic
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
module = AnsibleModule(
|
||||
@@ -180,7 +187,7 @@ def main():
|
||||
port=dict(default='5433'),
|
||||
login_user=dict(default='dbadmin'),
|
||||
login_password=dict(default=None, no_log=True),
|
||||
), supports_check_mode = True)
|
||||
), supports_check_mode=True)
|
||||
|
||||
if not pyodbc_found:
|
||||
module.fail_json(msg="The python pyodbc module is required.")
|
||||
@@ -206,8 +213,8 @@ def main():
|
||||
"User={3};"
|
||||
"Password={4};"
|
||||
"ConnectionLoadBalance={5}"
|
||||
).format(module.params['cluster'], module.params['port'], db,
|
||||
module.params['login_user'], module.params['login_password'], 'true')
|
||||
).format(module.params['cluster'], module.params['port'], db,
|
||||
module.params['login_user'], module.params['login_password'], 'true')
|
||||
db_conn = pyodbc.connect(dsn, autocommit=True)
|
||||
cursor = db_conn.cursor()
|
||||
except Exception as e:
|
||||
|
||||
@@ -122,11 +122,13 @@ from ansible.module_utils._text import to_native
|
||||
class NotSupportedError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class CannotDropError(Exception):
|
||||
pass
|
||||
|
||||
# module specific functions
|
||||
|
||||
|
||||
def get_schema_facts(cursor, schema=''):
|
||||
facts = {}
|
||||
cursor.execute("""
|
||||
@@ -167,6 +169,7 @@ def get_schema_facts(cursor, schema=''):
|
||||
facts[schema_key]['usage_roles'].append(row.role_name)
|
||||
return facts
|
||||
|
||||
|
||||
def update_roles(schema_facts, cursor, schema,
|
||||
existing, required,
|
||||
create_existing, create_required):
|
||||
@@ -180,6 +183,7 @@ def update_roles(schema_facts, cursor, schema,
|
||||
for role in set(create_required) - set(create_existing):
|
||||
cursor.execute("grant create on schema {0} to {1}".format(schema, role))
|
||||
|
||||
|
||||
def check(schema_facts, schema, usage_roles, create_roles, owner):
|
||||
schema_key = schema.lower()
|
||||
if schema_key not in schema_facts:
|
||||
@@ -192,6 +196,7 @@ def check(schema_facts, schema, usage_roles, create_roles, owner):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def present(schema_facts, cursor, schema, usage_roles, create_roles, owner):
|
||||
schema_key = schema.lower()
|
||||
if schema_key not in schema_facts:
|
||||
@@ -208,23 +213,24 @@ def present(schema_facts, cursor, schema, usage_roles, create_roles, owner):
|
||||
raise NotSupportedError((
|
||||
"Changing schema owner is not supported. "
|
||||
"Current owner: {0}."
|
||||
).format(schema_facts[schema_key]['owner']))
|
||||
).format(schema_facts[schema_key]['owner']))
|
||||
if sorted(usage_roles) != sorted(schema_facts[schema_key]['usage_roles']) or \
|
||||
sorted(create_roles) != sorted(schema_facts[schema_key]['create_roles']):
|
||||
|
||||
update_roles(schema_facts, cursor, schema,
|
||||
schema_facts[schema_key]['usage_roles'], usage_roles,
|
||||
schema_facts[schema_key]['create_roles'], create_roles)
|
||||
schema_facts[schema_key]['usage_roles'], usage_roles,
|
||||
schema_facts[schema_key]['create_roles'], create_roles)
|
||||
changed = True
|
||||
if changed:
|
||||
schema_facts.update(get_schema_facts(cursor, schema))
|
||||
return changed
|
||||
|
||||
|
||||
def absent(schema_facts, cursor, schema, usage_roles, create_roles):
|
||||
schema_key = schema.lower()
|
||||
if schema_key in schema_facts:
|
||||
update_roles(schema_facts, cursor, schema,
|
||||
schema_facts[schema_key]['usage_roles'], [], schema_facts[schema_key]['create_roles'], [])
|
||||
schema_facts[schema_key]['usage_roles'], [], schema_facts[schema_key]['create_roles'], [])
|
||||
try:
|
||||
cursor.execute("drop schema {0} restrict".format(schema_facts[schema_key]['name']))
|
||||
except pyodbc.Error:
|
||||
@@ -236,6 +242,7 @@ def absent(schema_facts, cursor, schema, usage_roles, create_roles):
|
||||
|
||||
# module logic
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
module = AnsibleModule(
|
||||
@@ -250,7 +257,7 @@ def main():
|
||||
port=dict(default='5433'),
|
||||
login_user=dict(default='dbadmin'),
|
||||
login_password=dict(default=None, no_log=True),
|
||||
), supports_check_mode = True)
|
||||
), supports_check_mode=True)
|
||||
|
||||
if not pyodbc_found:
|
||||
module.fail_json(msg="The python pyodbc module is required.")
|
||||
@@ -281,8 +288,8 @@ def main():
|
||||
"User={3};"
|
||||
"Password={4};"
|
||||
"ConnectionLoadBalance={5}"
|
||||
).format(module.params['cluster'], module.params['port'], db,
|
||||
module.params['login_user'], module.params['login_password'], 'true')
|
||||
).format(module.params['cluster'], module.params['port'], db,
|
||||
module.params['login_user'], module.params['login_password'], 'true')
|
||||
db_conn = pyodbc.connect(dsn, autocommit=True)
|
||||
cursor = db_conn.cursor()
|
||||
except Exception as e:
|
||||
|
||||
@@ -134,11 +134,13 @@ from ansible.module_utils._text import to_native
|
||||
class NotSupportedError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class CannotDropError(Exception):
|
||||
pass
|
||||
|
||||
# module specific functions
|
||||
|
||||
|
||||
def get_user_facts(cursor, user=''):
|
||||
facts = {}
|
||||
cursor.execute("""
|
||||
@@ -173,6 +175,7 @@ def get_user_facts(cursor, user=''):
|
||||
facts[user_key]['default_roles'] = row.default_roles.replace(' ', '').split(',')
|
||||
return facts
|
||||
|
||||
|
||||
def update_roles(user_facts, cursor, user,
|
||||
existing_all, existing_default, required):
|
||||
del_roles = list(set(existing_all) - set(required))
|
||||
@@ -184,6 +187,7 @@ def update_roles(user_facts, cursor, user,
|
||||
if required:
|
||||
cursor.execute("alter user {0} default role {1}".format(user, ','.join(required)))
|
||||
|
||||
|
||||
def check(user_facts, user, profile, resource_pool,
|
||||
locked, password, expired, ldap, roles):
|
||||
user_key = user.lower()
|
||||
@@ -198,13 +202,14 @@ def check(user_facts, user, profile, resource_pool,
|
||||
if password and password != user_facts[user_key]['password']:
|
||||
return False
|
||||
if (expired is not None and expired != (user_facts[user_key]['expired'] == 'True') or
|
||||
ldap is not None and ldap != (user_facts[user_key]['expired'] == 'True')):
|
||||
ldap is not None and ldap != (user_facts[user_key]['expired'] == 'True')):
|
||||
return False
|
||||
if roles and (sorted(roles) != sorted(user_facts[user_key]['roles']) or \
|
||||
sorted(roles) != sorted(user_facts[user_key]['default_roles'])):
|
||||
if roles and (sorted(roles) != sorted(user_facts[user_key]['roles']) or
|
||||
sorted(roles) != sorted(user_facts[user_key]['default_roles'])):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def present(user_facts, cursor, user, profile, resource_pool,
|
||||
locked, password, expired, ldap, roles):
|
||||
user_key = user.lower()
|
||||
@@ -267,20 +272,21 @@ def present(user_facts, cursor, user, profile, resource_pool,
|
||||
changed = True
|
||||
if changed:
|
||||
cursor.execute(' '.join(query_fragments))
|
||||
if roles and (sorted(roles) != sorted(user_facts[user_key]['roles']) or \
|
||||
sorted(roles) != sorted(user_facts[user_key]['default_roles'])):
|
||||
if roles and (sorted(roles) != sorted(user_facts[user_key]['roles']) or
|
||||
sorted(roles) != sorted(user_facts[user_key]['default_roles'])):
|
||||
update_roles(user_facts, cursor, user,
|
||||
user_facts[user_key]['roles'], user_facts[user_key]['default_roles'], roles)
|
||||
user_facts[user_key]['roles'], user_facts[user_key]['default_roles'], roles)
|
||||
changed = True
|
||||
if changed:
|
||||
user_facts.update(get_user_facts(cursor, user))
|
||||
return changed
|
||||
|
||||
|
||||
def absent(user_facts, cursor, user, roles):
|
||||
user_key = user.lower()
|
||||
if user_key in user_facts:
|
||||
update_roles(user_facts, cursor, user,
|
||||
user_facts[user_key]['roles'], user_facts[user_key]['default_roles'], [])
|
||||
user_facts[user_key]['roles'], user_facts[user_key]['default_roles'], [])
|
||||
try:
|
||||
cursor.execute("drop user {0}".format(user_facts[user_key]['name']))
|
||||
except pyodbc.Error:
|
||||
@@ -292,6 +298,7 @@ def absent(user_facts, cursor, user, roles):
|
||||
|
||||
# module logic
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
module = AnsibleModule(
|
||||
@@ -309,7 +316,7 @@ def main():
|
||||
port=dict(default='5433'),
|
||||
login_user=dict(default='dbadmin'),
|
||||
login_password=dict(default=None, no_log=True),
|
||||
), supports_check_mode = True)
|
||||
), supports_check_mode=True)
|
||||
|
||||
if not pyodbc_found:
|
||||
module.fail_json(msg="The python pyodbc module is required.")
|
||||
@@ -348,8 +355,8 @@ def main():
|
||||
"User={3};"
|
||||
"Password={4};"
|
||||
"ConnectionLoadBalance={5}"
|
||||
).format(module.params['cluster'], module.params['port'], db,
|
||||
module.params['login_user'], module.params['login_password'], 'true')
|
||||
).format(module.params['cluster'], module.params['port'], db,
|
||||
module.params['login_user'], module.params['login_password'], 'true')
|
||||
db_conn = pyodbc.connect(dsn, autocommit=True)
|
||||
cursor = db_conn.cursor()
|
||||
except Exception as e:
|
||||
@@ -359,7 +366,7 @@ def main():
|
||||
user_facts = get_user_facts(cursor)
|
||||
if module.check_mode:
|
||||
changed = not check(user_facts, user, profile, resource_pool,
|
||||
locked, password, expired, ldap, roles)
|
||||
locked, password, expired, ldap, roles)
|
||||
elif state == 'absent':
|
||||
try:
|
||||
changed = absent(user_facts, cursor, user, roles)
|
||||
@@ -368,7 +375,7 @@ def main():
|
||||
elif state in ['present', 'locked']:
|
||||
try:
|
||||
changed = present(user_facts, cursor, user, profile, resource_pool,
|
||||
locked, password, expired, ldap, roles)
|
||||
locked, password, expired, ldap, roles)
|
||||
except pyodbc.Error as e:
|
||||
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
||||
except NotSupportedError as e:
|
||||
|
||||
Reference in New Issue
Block a user