Browse Source

Fix duplication of save_config_to_disk and load_config_to_runtime function (#78)

* Refactor to de-duplicate save_config_to_disk and load_config_to_runtime

* Refactor function arguments to handle any query shape

* Add changelog fragment

* Update changelogs/fragments/68-refactor-save-load-config.yml

Fix sanity test

Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>

* Update changelogs/fragments/68-refactor-save-load-config.yml

Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>

* Add link to the PR in the changelog fragment

Co-authored-by: Markus Bergholz <git@osuv.de>

Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>
Co-authored-by: Markus Bergholz <git@osuv.de>
local_integrationtest
Laurent Indermühle 9 months ago committed by GitHub
parent
commit
7405c12473
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      changelogs/fragments/68-refactor-save-load-config.yml
  2. 26
      plugins/module_utils/mysql.py
  3. 18
      plugins/modules/proxysql_backend_servers.py
  4. 24
      plugins/modules/proxysql_global_variables.py
  5. 18
      plugins/modules/proxysql_mysql_users.py
  6. 18
      plugins/modules/proxysql_query_rules.py
  7. 16
      plugins/modules/proxysql_query_rules_fast_routing.py
  8. 18
      plugins/modules/proxysql_replication_hostgroups.py
  9. 18
      plugins/modules/proxysql_scheduler.py

3
changelogs/fragments/68-refactor-save-load-config.yml

@ -0,0 +1,3 @@
---
minor_changes:
- module_utils - Refactor save_config_to_disk and load_config_to_runtime (https://github.com/ansible-collections/community.proxysql/pull/78).

26
plugins/module_utils/mysql.py

@ -141,3 +141,29 @@ def proxysql_common_argument_spec():
login_unix_socket=dict(type='str'),
config_file=dict(type='path', default=''),
)
def save_config_to_disk(cursor, save_what, variable=None):
if variable and variable.startswith("admin"):
config_type = "ADMIN"
elif save_what == "SCHEDULER":
config_type = ""
else:
config_type = "MYSQL"
cursor.execute("SAVE {0} {1} TO DISK".format(config_type, save_what))
return True
def load_config_to_runtime(cursor, save_what, variable=None):
if variable and variable.startswith("admin"):
config_type = "ADMIN"
elif save_what == "SCHEDULER":
config_type = ""
else:
config_type = "MYSQL"
cursor.execute("LOAD {0} {1} TO RUNTIME".format(config_type, save_what))
return True

18
plugins/modules/proxysql_backend_servers.py

@ -162,7 +162,9 @@ from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.proxysql.plugins.module_utils.mysql import (
mysql_connect,
mysql_driver,
proxysql_common_argument_spec
proxysql_common_argument_spec,
save_config_to_disk,
load_config_to_runtime,
)
from ansible.module_utils.six import iteritems
from ansible.module_utils._text import to_native
@ -194,16 +196,6 @@ def perform_checks(module):
)
def save_config_to_disk(cursor):
cursor.execute("SAVE MYSQL SERVERS TO DISK")
return True
def load_config_to_runtime(cursor):
cursor.execute("LOAD MYSQL SERVERS TO RUNTIME")
return True
class ProxySQLServer(object):
def __init__(self, module):
@ -361,9 +353,9 @@ class ProxySQLServer(object):
def manage_config(self, cursor, state):
if state:
if self.save_to_disk:
save_config_to_disk(cursor)
save_config_to_disk(cursor, "SERVERS")
if self.load_to_runtime:
load_config_to_runtime(cursor)
load_config_to_runtime(cursor, "SERVERS")
def create_server(self, check_mode, result, cursor):
if not check_mode:

24
plugins/modules/proxysql_global_variables.py

@ -76,7 +76,9 @@ from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.proxysql.plugins.module_utils.mysql import (
mysql_connect,
mysql_driver,
proxysql_common_argument_spec
proxysql_common_argument_spec,
save_config_to_disk,
load_config_to_runtime,
)
from ansible.module_utils._text import to_native
@ -85,22 +87,6 @@ from ansible.module_utils._text import to_native
#
def save_config_to_disk(variable, cursor):
if variable.startswith("admin"):
cursor.execute("SAVE ADMIN VARIABLES TO DISK")
else:
cursor.execute("SAVE MYSQL VARIABLES TO DISK")
return True
def load_config_to_runtime(variable, cursor):
if variable.startswith("admin"):
cursor.execute("LOAD ADMIN VARIABLES TO RUNTIME")
else:
cursor.execute("LOAD MYSQL VARIABLES TO RUNTIME")
return True
def check_config(variable, value, cursor):
query_string = \
"""SELECT count(*) AS `variable_count`
@ -156,9 +142,9 @@ def set_config(variable, value, cursor):
def manage_config(variable, save_to_disk, load_to_runtime, cursor, state):
if state:
if save_to_disk:
save_config_to_disk(variable, cursor)
save_config_to_disk(cursor, "VARIABLES", variable)
if load_to_runtime:
load_config_to_runtime(variable, cursor)
load_config_to_runtime(cursor, "VARIABLES", variable)
# ===========================================
# Module execution.

18
plugins/modules/proxysql_mysql_users.py

@ -163,7 +163,9 @@ from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.proxysql.plugins.module_utils.mysql import (
mysql_connect,
mysql_driver,
proxysql_common_argument_spec
proxysql_common_argument_spec,
save_config_to_disk,
load_config_to_runtime,
)
from ansible.module_utils.six import iteritems
from ansible.module_utils._text import to_native, to_bytes
@ -174,16 +176,6 @@ from hashlib import sha1
#
def save_config_to_disk(cursor):
cursor.execute("SAVE MYSQL USERS TO DISK")
return True
def load_config_to_runtime(cursor):
cursor.execute("LOAD MYSQL USERS TO RUNTIME")
return True
def _mysql_native_password(cleartext_password):
mysql_native_encrypted_password = "*" + sha1(sha1(to_bytes(cleartext_password)).digest()).hexdigest().upper()
return mysql_native_encrypted_password
@ -357,9 +349,9 @@ class ProxySQLUser(object):
def manage_config(self, cursor, state):
if state:
if self.save_to_disk:
save_config_to_disk(cursor)
save_config_to_disk(cursor, "USERS")
if self.load_to_runtime:
load_config_to_runtime(cursor)
load_config_to_runtime(cursor, "USERS")
def create_user(self, check_mode, result, cursor):
if not check_mode:

18
plugins/modules/proxysql_query_rules.py

@ -358,7 +358,9 @@ from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.proxysql.plugins.module_utils.mysql import (
mysql_connect,
mysql_driver,
proxysql_common_argument_spec
proxysql_common_argument_spec,
save_config_to_disk,
load_config_to_runtime,
)
from ansible.module_utils.six import iteritems
from ansible.module_utils._text import to_native
@ -368,16 +370,6 @@ from ansible.module_utils._text import to_native
#
def save_config_to_disk(cursor):
cursor.execute("SAVE MYSQL QUERY RULES TO DISK")
return True
def load_config_to_runtime(cursor):
cursor.execute("LOAD MYSQL QUERY RULES TO RUNTIME")
return True
class ProxyQueryRule(object):
def __init__(self, module, version):
@ -568,9 +560,9 @@ class ProxyQueryRule(object):
def manage_config(self, cursor, state):
if state:
if self.save_to_disk:
save_config_to_disk(cursor)
save_config_to_disk(cursor, "QUERY RULES")
if self.load_to_runtime:
load_config_to_runtime(cursor)
load_config_to_runtime(cursor, "QUERY RULES")
def create_rule(self, check_mode, result, cursor):
if not check_mode:

16
plugins/modules/proxysql_query_rules_fast_routing.py

@ -111,6 +111,8 @@ from ansible_collections.community.proxysql.plugins.module_utils.mysql import (
mysql_connect,
mysql_driver,
proxysql_common_argument_spec,
save_config_to_disk,
load_config_to_runtime,
)
from ansible.module_utils.six import iteritems
from ansible.module_utils._text import to_native
@ -120,16 +122,6 @@ from ansible.module_utils._text import to_native
#
def save_config_to_disk(cursor):
cursor.execute("SAVE MYSQL QUERY RULES TO DISK")
return True
def load_config_to_runtime(cursor):
cursor.execute("LOAD MYSQL QUERY RULES TO RUNTIME")
return True
class ProxyQueryRuleFastRouting(object):
def __init__(self, module):
@ -292,9 +284,9 @@ class ProxyQueryRuleFastRouting(object):
return
if self.save_to_disk:
save_config_to_disk(cursor)
save_config_to_disk(cursor, "QUERY RULES")
if self.load_to_runtime:
load_config_to_runtime(cursor)
load_config_to_runtime(cursor, "QUERY RULES")
def create_rule(self, check_mode, result, cursor):
if not check_mode:

18
plugins/modules/proxysql_replication_hostgroups.py

@ -121,7 +121,9 @@ from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.proxysql.plugins.module_utils.mysql import (
mysql_connect,
mysql_driver,
proxysql_common_argument_spec
proxysql_common_argument_spec,
save_config_to_disk,
load_config_to_runtime,
)
from ansible.module_utils._text import to_native
@ -146,16 +148,6 @@ def perform_checks(module):
msg="reader_hostgroup and writer_hostgroup must be different integer values")
def save_config_to_disk(cursor):
cursor.execute("SAVE MYSQL SERVERS TO DISK")
return True
def load_config_to_runtime(cursor):
cursor.execute("LOAD MYSQL SERVERS TO RUNTIME")
return True
class ProxySQLReplicationHostgroup(object):
def __init__(self, module, version):
@ -229,9 +221,9 @@ class ProxySQLReplicationHostgroup(object):
def manage_config(self, cursor, state):
if state and not self.check_mode:
if self.save_to_disk:
save_config_to_disk(cursor)
save_config_to_disk(cursor, "SERVERS")
if self.load_to_runtime:
load_config_to_runtime(cursor)
load_config_to_runtime(cursor, "SERVERS")
def create_repl_group(self, result, cursor):
if not self.check_mode:

18
plugins/modules/proxysql_scheduler.py

@ -138,7 +138,9 @@ from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.proxysql.plugins.module_utils.mysql import (
mysql_connect,
mysql_driver,
proxysql_common_argument_spec
proxysql_common_argument_spec,
save_config_to_disk,
load_config_to_runtime,
)
from ansible.module_utils.six import iteritems
from ansible.module_utils._text import to_native
@ -156,16 +158,6 @@ def perform_checks(module):
)
def save_config_to_disk(cursor):
cursor.execute("SAVE SCHEDULER TO DISK")
return True
def load_config_to_runtime(cursor):
cursor.execute("LOAD SCHEDULER TO RUNTIME")
return True
class ProxySQLSchedule(object):
def __init__(self, module):
@ -283,9 +275,9 @@ class ProxySQLSchedule(object):
def manage_config(self, cursor, state):
if state:
if self.save_to_disk:
save_config_to_disk(cursor)
save_config_to_disk(cursor, "SCHEDULER")
if self.load_to_runtime:
load_config_to_runtime(cursor)
load_config_to_runtime(cursor, "SCHEDULER")
def create_schedule(self, check_mode, result, cursor):
if not check_mode:

Loading…
Cancel
Save