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.
This commit is contained in:
Tobias Wolf 2016-01-26 15:05:49 +01:00 committed by Matt Clay
parent a2a3d2fc06
commit 75e8b365b4

View file

@ -225,7 +225,8 @@ def main():
ssl_key=dict(default=None), ssl_key=dict(default=None),
ssl_ca=dict(default=None), ssl_ca=dict(default=None),
config_file=dict(default="~/.my.cnf"), config_file=dict(default="~/.my.cnf"),
) ),
supports_check_mode=True
) )
if not mysqldb_found: if not mysqldb_found:
@ -277,32 +278,44 @@ def main():
config_file = None config_file = None
if db_exists(cursor, db): if db_exists(cursor, db):
if state == "absent": if state == "absent":
try: if module.check_mode:
changed = db_delete(cursor, db) changed = True
except Exception, e: else:
module.fail_json(msg="error deleting database: " + str(e)) try:
changed = db_delete(cursor, db)
except Exception, e:
module.fail_json(msg="error deleting database: " + str(e))
elif state == "dump": elif state == "dump":
rc, stdout, stderr = db_dump(module, login_host, login_user, if module.check_mode:
login_password, db, target, all_databases, module.exit_json(changed=True, db=db)
login_port, config_file, socket, ssl_cert, ssl_key, ssl_ca)
if rc != 0:
module.fail_json(msg="%s" % stderr)
else: 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": elif state == "import":
rc, stdout, stderr = db_import(module, login_host, login_user, if module.check_mode:
login_password, db, target, all_databases, module.exit_json(changed=True, db=db)
login_port, config_file, socket, ssl_cert, ssl_key, ssl_ca)
if rc != 0:
module.fail_json(msg="%s" % stderr)
else: 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: else:
if state == "present": if state == "present":
try: if module.check_mode:
changed = db_create(cursor, db, encoding, collation) changed = True
except Exception, e: else:
module.fail_json(msg="error creating database: " + str(e)) 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) module.exit_json(changed=changed, db=db)