Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding an ability to set a certain check_type when setting up MySQL replication host groups #20

Closed
wants to merge 6 commits into from
51 changes: 42 additions & 9 deletions plugins/modules/proxysql_replication_hostgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
description:
- Text field that can be used for any purposes defined by the user.
type: str
check_type:
description:
- Which check type to use when detecting that the node is a slave.
Andersson007 marked this conversation as resolved.
Show resolved Hide resolved
type: str
choices: [ "read_only", "innodb_read_only", "super_read_only", "read_only|innodb_read_only", "read_only&innodb_read_only" ]
default: read_only
zentavr marked this conversation as resolved.
Show resolved Hide resolved
state:
description:
- When C(present) - adds the replication hostgroup, when C(absent) -
Expand Down Expand Up @@ -64,6 +70,16 @@
state: present
load_to_runtime: False

- name: Add a replication hostgroup with super_read_only check type
community.proxysql.proxysql_replication_hostgroups:
login_user: 'admin'
login_password: 'admin'
writer_hostgroup: 1
reader_hostgroup: 2
check_type: 'super_read_only'
state: present
load_to_runtime: False

# This example removes a replication hostgroup, saves the mysql server config
# to disk, and dynamically loads the mysql server config to runtime. It uses
# credentials in a supplied config file to connect to the proxysql admin
Expand All @@ -88,6 +104,7 @@
"msg": "Added server to mysql_hosts",
"repl_group": {
"comment": "",
"check_type": "read_only",
"reader_hostgroup": "1",
"writer_hostgroup": "2"
},
Expand Down Expand Up @@ -151,17 +168,20 @@ def __init__(self, module):
self.writer_hostgroup = module.params["writer_hostgroup"]
self.reader_hostgroup = module.params["reader_hostgroup"]
self.comment = module.params["comment"]
self.check_type = module.params["check_type"]

def check_repl_group_config(self, cursor, keys):
query_string = \
"""SELECT count(*) AS `repl_groups`
FROM mysql_replication_hostgroups
WHERE writer_hostgroup = %s
AND reader_hostgroup = %s"""
AND reader_hostgroup = %s
AND check_type = %s"""

query_data = \
[self.writer_hostgroup,
self.reader_hostgroup]
self.reader_hostgroup,
self.check_type]

if self.comment and not keys:
query_string += "\n AND comment = %s"
Expand All @@ -176,11 +196,13 @@ def get_repl_group_config(self, cursor):
"""SELECT *
FROM mysql_replication_hostgroups
WHERE writer_hostgroup = %s
AND reader_hostgroup = %s"""
AND reader_hostgroup = %s
AND check_type = %s"""

query_data = \
[self.writer_hostgroup,
self.reader_hostgroup]
self.reader_hostgroup,
self.check_type]

cursor.execute(query_string, query_data)
repl_group = cursor.fetchone()
Expand All @@ -191,12 +213,14 @@ def create_repl_group_config(self, cursor):
"""INSERT INTO mysql_replication_hostgroups (
writer_hostgroup,
reader_hostgroup,
check_type,
comment)
VALUES (%s, %s, %s)"""
VALUES (%s, %s, %s, %s)"""

query_data = \
[self.writer_hostgroup,
self.reader_hostgroup,
self.check_type,
self.comment or '']

cursor.execute(query_string, query_data)
Expand All @@ -207,12 +231,14 @@ def update_repl_group_config(self, cursor):
"""UPDATE mysql_replication_hostgroups
SET comment = %s
WHERE writer_hostgroup = %s
AND reader_hostgroup = %s"""
AND reader_hostgroup = %s
AND check_type = %s"""

query_data = \
[self.comment,
self.writer_hostgroup,
self.reader_hostgroup]
self.reader_hostgroup,
self.check_type]

cursor.execute(query_string, query_data)
return True
Expand All @@ -221,11 +247,13 @@ def delete_repl_group_config(self, cursor):
query_string = \
"""DELETE FROM mysql_replication_hostgroups
WHERE writer_hostgroup = %s
AND reader_hostgroup = %s"""
AND reader_hostgroup = %s
AND check_type = %s"""

query_data = \
[self.writer_hostgroup,
self.reader_hostgroup]
self.reader_hostgroup,
self.check_type]

cursor.execute(query_string, query_data)
return True
Expand Down Expand Up @@ -299,6 +327,11 @@ def main():
writer_hostgroup=dict(required=True, type='int'),
reader_hostgroup=dict(required=True, type='int'),
comment=dict(type='str'),
check_type=dict(default='read_only', choices=['read_only',
'innodb_read_only',
'super_read_only',
'read_only|innodb_read_only',
'read_only&innodb_read_only']),
state=dict(default='present', choices=['present',
'absent']),
save_to_disk=dict(default=True, type='bool'),
Expand Down