Merge pull request #1001 from ospii/devel
Validate variable, return only the found variable value instead of tuple
This commit is contained in:
commit
769b81cae1
1 changed files with 15 additions and 12 deletions
|
@ -73,6 +73,7 @@ EXAMPLES = '''
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
import os
|
import os
|
||||||
import warnings
|
import warnings
|
||||||
|
from re import match
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import MySQLdb
|
import MySQLdb
|
||||||
|
@ -109,10 +110,12 @@ def typedvalue(value):
|
||||||
|
|
||||||
|
|
||||||
def getvariable(cursor, mysqlvar):
|
def getvariable(cursor, mysqlvar):
|
||||||
cursor.execute("SHOW VARIABLES LIKE %s", (mysqlvar,))
|
cursor.execute("SHOW VARIABLES WHERE Variable_name = %s", (mysqlvar,))
|
||||||
mysqlvar_val = cursor.fetchall()
|
mysqlvar_val = cursor.fetchall()
|
||||||
return mysqlvar_val
|
if len(mysqlvar_val) is 1:
|
||||||
|
return mysqlvar_val[0][1]
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
def setvariable(cursor, mysqlvar, value):
|
def setvariable(cursor, mysqlvar, value):
|
||||||
""" Set a global mysql variable to a given value
|
""" Set a global mysql variable to a given value
|
||||||
|
@ -122,11 +125,9 @@ def setvariable(cursor, mysqlvar, value):
|
||||||
should be passed as numeric literals.
|
should be passed as numeric literals.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
query = ["SET GLOBAL %s" % mysql_quote_identifier(mysqlvar, 'vars') ]
|
query = "SET GLOBAL %s = " % mysql_quote_identifier(mysqlvar, 'vars')
|
||||||
query.append(" = %s")
|
|
||||||
query = ' '.join(query)
|
|
||||||
try:
|
try:
|
||||||
cursor.execute(query, (value,))
|
cursor.execute(query + "%s", (value,))
|
||||||
cursor.fetchall()
|
cursor.fetchall()
|
||||||
result = True
|
result = True
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
|
@ -212,6 +213,10 @@ def main():
|
||||||
port = module.params["login_port"]
|
port = module.params["login_port"]
|
||||||
mysqlvar = module.params["variable"]
|
mysqlvar = module.params["variable"]
|
||||||
value = module.params["value"]
|
value = module.params["value"]
|
||||||
|
if mysqlvar is None:
|
||||||
|
module.fail_json(msg="Cannot run without variable to operate with")
|
||||||
|
if match('^[0-9a-z_]+$', mysqlvar) is None:
|
||||||
|
module.fail_json(msg="invalid variable name \"%s\"" % mysqlvar)
|
||||||
if not mysqldb_found:
|
if not mysqldb_found:
|
||||||
module.fail_json(msg="the python mysqldb module is required")
|
module.fail_json(msg="the python mysqldb module is required")
|
||||||
else:
|
else:
|
||||||
|
@ -240,17 +245,15 @@ def main():
|
||||||
cursor = db_connection.cursor()
|
cursor = db_connection.cursor()
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
module.fail_json(msg="unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials")
|
module.fail_json(msg="unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials")
|
||||||
if mysqlvar is None:
|
|
||||||
module.fail_json(msg="Cannot run without variable to operate with")
|
|
||||||
mysqlvar_val = getvariable(cursor, mysqlvar)
|
mysqlvar_val = getvariable(cursor, mysqlvar)
|
||||||
|
if mysqlvar_val is None:
|
||||||
|
module.fail_json(msg="Variable not available \"%s\"" % mysqlvar, changed=False)
|
||||||
if value is None:
|
if value is None:
|
||||||
module.exit_json(msg=mysqlvar_val)
|
module.exit_json(msg=mysqlvar_val)
|
||||||
else:
|
else:
|
||||||
if len(mysqlvar_val) < 1:
|
|
||||||
module.fail_json(msg="Variable not available", changed=False)
|
|
||||||
# Type values before using them
|
# Type values before using them
|
||||||
value_wanted = typedvalue(value)
|
value_wanted = typedvalue(value)
|
||||||
value_actual = typedvalue(mysqlvar_val[0][1])
|
value_actual = typedvalue(mysqlvar_val)
|
||||||
if value_wanted == value_actual:
|
if value_wanted == value_actual:
|
||||||
module.exit_json(msg="Variable already set to requested value", changed=False)
|
module.exit_json(msg="Variable already set to requested value", changed=False)
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in a new issue