From 3d606dd2b447b0ed425a248825c615fc2f6d17cd Mon Sep 17 00:00:00 2001 From: Tobias Wolf Date: Tue, 26 Jan 2016 15:05:49 +0100 Subject: [PATCH] Add supports_check_mode=True to mysql_db module The most important cases are state=present and state=absent. Future work: import and dump could be simulated and hence checked. --- database/mysql/mysql_db.py | 55 +++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/database/mysql/mysql_db.py b/database/mysql/mysql_db.py index 24bcf40ed84..369de390696 100644 --- a/database/mysql/mysql_db.py +++ b/database/mysql/mysql_db.py @@ -225,7 +225,8 @@ def main(): ssl_key=dict(default=None), ssl_ca=dict(default=None), config_file=dict(default="~/.my.cnf"), - ) + ), + supports_check_mode=True ) if not mysqldb_found: @@ -277,32 +278,44 @@ def main(): config_file = None if db_exists(cursor, db): if state == "absent": - try: - changed = db_delete(cursor, db) - except Exception, e: - module.fail_json(msg="error deleting database: " + str(e)) + if module.check_mode: + changed = True + else: + try: + changed = db_delete(cursor, db) + except Exception, e: + module.fail_json(msg="error deleting database: " + str(e)) elif state == "dump": - rc, stdout, stderr = db_dump(module, login_host, login_user, - login_password, db, target, all_databases, - login_port, config_file, socket, ssl_cert, ssl_key, ssl_ca) - if rc != 0: - module.fail_json(msg="%s" % stderr) + if module.check_mode: + module.exit_json(changed=True, db=db) else: - module.exit_json(changed=True, db=db, msg=stdout) + rc, stdout, stderr = db_dump(module, login_host, login_user, + login_password, db, target, all_databases, + login_port, config_file, socket, ssl_cert, ssl_key, ssl_ca) + if rc != 0: + module.fail_json(msg="%s" % stderr) + else: + module.exit_json(changed=True, db=db, msg=stdout) elif state == "import": - rc, stdout, stderr = db_import(module, login_host, login_user, - login_password, db, target, all_databases, - login_port, config_file, socket, ssl_cert, ssl_key, ssl_ca) - if rc != 0: - module.fail_json(msg="%s" % stderr) + if module.check_mode: + module.exit_json(changed=True, db=db) else: - module.exit_json(changed=True, db=db, msg=stdout) + rc, stdout, stderr = db_import(module, login_host, login_user, + login_password, db, target, all_databases, + login_port, config_file, socket, ssl_cert, ssl_key, ssl_ca) + if rc != 0: + module.fail_json(msg="%s" % stderr) + else: + module.exit_json(changed=True, db=db, msg=stdout) else: if state == "present": - try: - changed = db_create(cursor, db, encoding, collation) - except Exception, e: - module.fail_json(msg="error creating database: " + str(e)) + if module.check_mode: + changed = True + else: + try: + changed = db_create(cursor, db, encoding, collation) + except Exception, e: + module.fail_json(msg="error creating database: " + str(e)) module.exit_json(changed=changed, db=db)