import salt.exceptions def root_password(name, password): """ Set the mysql/mariadb root password password the password to user for root """ ret = { "name": name, "changes": {}, "result": False, "comment": "" } if __salt__["mysql.check_credentials"]("root", password): ret["comment"]="Password is in correct state" ret["result"] = True return ret result = __salt__["mysql.set_root_password"](password) if not result["result"]: ret["comment"] = result["err"] return ret ret["changes"].update({"root password": {"old": "######", "new": "******"}}) ret["result"]=True return ret def user(name, host, password): """ Set the mysql/mariadb root password password the password to user for root """ ret = { "name": name, "changes": {}, "result": False, "comment": "" } #check if user@host exists result = __salt__["mysql.execute"]("root", __pillar__['mysql']['root_password'],f'SELECT User, Host FROM mysql.user WHERE User="{name}" AND Host="{host}";') if not result["result"]: ret["comment"] = result["err"] return ret if "out" in result: #User exists check password if __salt__["mysql.check_credentials"](name, password): ret["comment"]=f"{name}@{host} is in correct state" ret["result"] = True return ret result = __salt__["mysql.execute"]("root", __pillar__['mysql']['root_password'],f'ALTER USER "{name}"@"{host}" IDENTIFIED BY "{password}";') if not result["result"]: ret["comment"] = result["err"] return ret ret["changes"].update({"Password Updated": f"{name}@{host}"}) ret["result"]=True return ret #Create user result = __salt__["mysql.execute"]("root", __pillar__['mysql']['root_password'],f'CREATE USER "{name}"@"{host}" IDENTIFIED BY "{password}";') if not result["result"]: ret["comment"] = result["err"] return ret ret["changes"].update({"Created user": f"{name}@{host}"}) ret["result"]=True return ret