| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# Copyright: (c) 2017, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
DOCUMENTATION = ''' |
| 10 |
--- |
| 11 |
module: proxysql_pgsql_query_rules |
| 12 |
author: "Janos Ruszo (@jruszo)" |
| 13 |
short_description: Modifies pgsql query rules using the proxysql admin interface |
| 14 |
description: |
| 15 |
- The M(community.proxysql.proxysql_pgsql_query_rules) module modifies pgsql query rules |
| 16 |
using the proxysql admin interface. |
| 17 |
version_added: '1.8.0' |
| 18 |
options: |
| 19 |
rule_id: |
| 20 |
description: |
| 21 |
- The unique id of the rule. Rules are processed in rule_id order. |
| 22 |
type: int |
| 23 |
active: |
| 24 |
description: |
| 25 |
- A rule with O(active) set to V(False) will be tracked in the database, |
| 26 |
but will be never loaded in the in-memory data structures. |
| 27 |
If omitted the proxysql database default for I(active) is C(False). |
| 28 |
type: bool |
| 29 |
username: |
| 30 |
description: |
| 31 |
- Filtering criteria matching username. If I(username) is non-NULL, a |
| 32 |
query will match only if the connection is made with the correct |
| 33 |
username. |
| 34 |
type: str |
| 35 |
database: |
| 36 |
description: |
| 37 |
- Filtering criteria matching database. If I(database) is non-NULL, a |
| 38 |
query will match only if the connection uses database as its default |
| 39 |
schema. |
| 40 |
type: str |
| 41 |
flagIN: |
| 42 |
description: |
| 43 |
- Used in combination with I(flagOUT) and I(apply) to create chains of |
| 44 |
rules. |
| 45 |
If omitted the proxysql database default for I(flagIN) is C(0). |
| 46 |
type: int |
| 47 |
client_addr: |
| 48 |
description: |
| 49 |
- Match traffic from a specific source. |
| 50 |
type: str |
| 51 |
proxy_addr: |
| 52 |
description: |
| 53 |
- Match incoming traffic on a specific local IP. |
| 54 |
type: str |
| 55 |
proxy_port: |
| 56 |
description: |
| 57 |
- Match incoming traffic on a specific local port. |
| 58 |
type: int |
| 59 |
digest: |
| 60 |
description: |
| 61 |
- Match queries with a specific digest, as returned by |
| 62 |
stats_pgsql_query_digest.digest. |
| 63 |
type: str |
| 64 |
match_digest: |
| 65 |
description: |
| 66 |
- Regular expression that matches the query digest. The dialect of |
| 67 |
regular expressions used is that of re2 - U(https://github.com/google/re2). |
| 68 |
type: str |
| 69 |
match_pattern: |
| 70 |
description: |
| 71 |
- Regular expression that matches the query text. The dialect of regular |
| 72 |
expressions used is that of re2 - U(https://github.com/google/re2). |
| 73 |
type: str |
| 74 |
negate_match_pattern: |
| 75 |
description: |
| 76 |
- If I(negate_match_pattern) is set to C(True), only queries not matching |
| 77 |
the query text will be considered as a match. This acts as a NOT |
| 78 |
operator in front of the regular expression matching against |
| 79 |
match_pattern. |
| 80 |
If omitted the proxysql database default for |
| 81 |
I(negate_match_pattern) is C(False). |
| 82 |
type: bool |
| 83 |
re_modifiers: |
| 84 |
description: |
| 85 |
- Comma separated list of options to modify the behavior of the RE engine. |
| 86 |
With C(CASELESS) the match is case insensitive. With C(GLOBAL) the replace |
| 87 |
is global (replaces all matches and not just the first). |
| 88 |
For backward compatibility, only C(CASELESS) is enabled by default. |
| 89 |
type: str |
| 90 |
flagOUT: |
| 91 |
description: |
| 92 |
- Used in combination with I(flagIN) and apply to create chains of rules. |
| 93 |
When set, I(flagOUT) signifies the I(flagIN) to be used in the next |
| 94 |
chain of rules. |
| 95 |
type: int |
| 96 |
replace_pattern: |
| 97 |
description: |
| 98 |
- This is the pattern with which to replace the matched pattern. Note |
| 99 |
that this is optional, and when omitted, the query processor will only |
| 100 |
cache, route, or set other parameters without rewriting. |
| 101 |
type: str |
| 102 |
destination_hostgroup: |
| 103 |
description: |
| 104 |
- Route matched queries to this hostgroup. |
| 105 |
type: int |
| 106 |
cache_ttl: |
| 107 |
description: |
| 108 |
- The number of milliseconds for which to cache the result of the query. |
| 109 |
type: int |
| 110 |
cache_empty_result: |
| 111 |
description: |
| 112 |
- Controls if resultset without rows will be cached or not. |
| 113 |
type: bool |
| 114 |
cache_timeout: |
| 115 |
description: |
| 116 |
- The maximum timeout in milliseconds for caching the result of the query. |
| 117 |
type: int |
| 118 |
reconnect: |
| 119 |
description: |
| 120 |
- Not yet implemented in ProxySQL. |
| 121 |
type: bool |
| 122 |
multiplex: |
| 123 |
description: |
| 124 |
- If C(0), multiplex will be disabled. |
| 125 |
- If C(1), try to enable multiplex. There can be other conditions preventing this. |
| 126 |
- If C(2), multiplexing is not disabled for just the current query. |
| 127 |
- By default, does not change multiplexing policies. |
| 128 |
type: int |
| 129 |
choices: [0, 1, 2] |
| 130 |
timeout: |
| 131 |
description: |
| 132 |
- The maximum timeout in milliseconds with which the matched or rewritten |
| 133 |
query should be executed. If a query run for longer than the specific |
| 134 |
threshold, the query is automatically killed. |
| 135 |
type: int |
| 136 |
retries: |
| 137 |
description: |
| 138 |
- The maximum number of times a query needs to be re-executed in case of |
| 139 |
detected failure during the execution of the query. |
| 140 |
type: int |
| 141 |
delay: |
| 142 |
description: |
| 143 |
- Number of milliseconds to delay the execution of the query. This is |
| 144 |
essentially a throttling mechanism and QoS, and allows a way to give |
| 145 |
priority to queries over others. |
| 146 |
type: int |
| 147 |
next_query_flagIN: |
| 148 |
description: |
| 149 |
- When is set, its value will become the I(flagIN) value for the next queries. |
| 150 |
type: int |
| 151 |
mirror_flagOUT: |
| 152 |
description: |
| 153 |
- Enables query mirroring. If set I(mirror_flagOUT) can be used to |
| 154 |
evaluates the mirrored query against the specified chain of rules. |
| 155 |
type: int |
| 156 |
mirror_hostgroup: |
| 157 |
description: |
| 158 |
- Enables query mirroring. If set I(mirror_hostgroup) can be used to |
| 159 |
mirror queries to the same or different hostgroup. |
| 160 |
type: int |
| 161 |
OK_msg: |
| 162 |
description: |
| 163 |
- The specified message will be returned for a query that uses the |
| 164 |
defined rule. |
| 165 |
type: str |
| 166 |
error_msg: |
| 167 |
description: |
| 168 |
- Query will be blocked, and the specified error_msg will be returned to |
| 169 |
the client. |
| 170 |
type: str |
| 171 |
sticky_conn: |
| 172 |
description: |
| 173 |
- Not yet implemented in ProxySQL. |
| 174 |
type: bool |
| 175 |
log: |
| 176 |
description: |
| 177 |
- Query will be logged. |
| 178 |
type: bool |
| 179 |
apply: |
| 180 |
description: |
| 181 |
- Used in combination with I(flagIN) and I(flagOUT) to create chains of |
| 182 |
rules. Setting apply to True signifies the last rule to be applied. |
| 183 |
If omitted the proxysql database default for I(apply) is C(False). |
| 184 |
type: bool |
| 185 |
attributes: |
| 186 |
description: |
| 187 |
- A JSON field that can be used to specify load balancing between |
| 188 |
hostgroups based on query rules. |
| 189 |
If omitted the proxysql database default for I(attributes) is C(''). |
| 190 |
type: str |
| 191 |
comment: |
| 192 |
description: |
| 193 |
- Free form text field, usable for a descriptive comment of the query |
| 194 |
rule. |
| 195 |
type: str |
| 196 |
state: |
| 197 |
description: |
| 198 |
- When C(present) - adds the rule, when C(absent) - removes the rule. |
| 199 |
type: str |
| 200 |
choices: [ "present", "absent" ] |
| 201 |
default: present |
| 202 |
force_delete: |
| 203 |
description: |
| 204 |
- By default we avoid deleting more than one query rule in a single batch, |
| 205 |
however if you need this behaviour and you are not concerned about the |
| 206 |
query rules deleted, you can set I(force_delete) to C(True). |
| 207 |
type: bool |
| 208 |
default: false |
| 209 |
extends_documentation_fragment: |
| 210 |
- community.proxysql.proxysql.managing_config |
| 211 |
- community.proxysql.proxysql.connectivity |
| 212 |
attributes: |
| 213 |
check_mode: |
| 214 |
description: Do not make any changes to memory, disk, or runtime. |
| 215 |
support: full |
| 216 |
''' |
| 217 |
|
| 218 |
EXAMPLES = ''' |
| 219 |
--- |
| 220 |
# This example adds a rule to redirect queries from a specific user to another |
| 221 |
# hostgroup, it saves the pgsql query rule config to disk, but avoids loading |
| 222 |
# the pgsql query config to runtime (this might be because several rules are |
| 223 |
# being added and the user wants to push the config to runtime in a single |
| 224 |
# batch using the community.proxysql.proxysql_manage_config module). It uses |
| 225 |
# supplied credentials to connect to the proxysql admin interface. |
| 226 |
|
| 227 |
- name: Add a rule |
| 228 |
community.proxysql.proxysql_pgsql_query_rules: |
| 229 |
login_user: admin |
| 230 |
login_password: admin |
| 231 |
username: 'guest_ro' |
| 232 |
match_pattern: "^SELECT.*" |
| 233 |
destination_hostgroup: 1 |
| 234 |
active: 1 |
| 235 |
retries: 3 |
| 236 |
state: present |
| 237 |
load_to_runtime: false |
| 238 |
|
| 239 |
# This example removes all rules that use the username 'guest_ro', saves the |
| 240 |
# pgsql query rule config to disk, and dynamically loads the pgsql query rule |
| 241 |
# config to runtime. It uses credentials in a supplied config file to connect |
| 242 |
# to the proxysql admin interface. |
| 243 |
|
| 244 |
- name: Remove rules |
| 245 |
community.proxysql.proxysql_pgsql_query_rules: |
| 246 |
config_file: '~/proxysql.cnf' |
| 247 |
username: 'guest_ro' |
| 248 |
state: absent |
| 249 |
force_delete: true |
| 250 |
''' |
| 251 |
|
| 252 |
RETURN = ''' |
| 253 |
stdout: |
| 254 |
description: The pgsql query rule modified or removed from proxysql. |
| 255 |
returned: On create/update will return the newly modified rule, in all |
| 256 |
other cases will return a list of rules that match the supplied |
| 257 |
criteria. |
| 258 |
type: dict |
| 259 |
"sample": { |
| 260 |
"changed": true, |
| 261 |
"msg": "Added rule to pgsql_query_rules", |
| 262 |
"rules": [ |
| 263 |
{ |
| 264 |
"active": "0", |
| 265 |
"apply": "0", |
| 266 |
"cache_ttl": null, |
| 267 |
"cache_empty_result": null, |
| 268 |
"cache_timeout": null, |
| 269 |
"multiplex": null, |
| 270 |
"client_addr": null, |
| 271 |
"comment": null, |
| 272 |
"delay": null, |
| 273 |
"destination_hostgroup": 1, |
| 274 |
"digest": null, |
| 275 |
"OK_msg": null, |
| 276 |
"error_msg": null, |
| 277 |
"flagIN": "0", |
| 278 |
"flagOUT": null, |
| 279 |
"log": null, |
| 280 |
"match_digest": null, |
| 281 |
"match_pattern": null, |
| 282 |
"mirror_flagOUT": null, |
| 283 |
"mirror_hostgroup": null, |
| 284 |
"negate_match_pattern": "0", |
| 285 |
"proxy_addr": null, |
| 286 |
"proxy_port": null, |
| 287 |
"reconnect": null, |
| 288 |
"replace_pattern": null, |
| 289 |
"retries": null, |
| 290 |
"rule_id": "1", |
| 291 |
"database": null, |
| 292 |
"timeout": null, |
| 293 |
"username": "guest_ro" |
| 294 |
} |
| 295 |
], |
| 296 |
"state": "present" |
| 297 |
} |
| 298 |
''' |
| 299 |
|
| 300 |
from ansible.module_utils.basic import AnsibleModule |
| 301 |
from ansible_collections.community.proxysql.plugins.module_utils.mysql import ( |
| 302 |
mysql_connect, |
| 303 |
mysql_driver, |
| 304 |
proxysql_common_argument_spec, |
| 305 |
save_config_to_disk, |
| 306 |
load_config_to_runtime, |
| 307 |
) |
| 308 |
from ansible.module_utils._text import to_native |
| 309 |
|
| 310 |
# =========================================== |
| 311 |
# proxysql module specific support methods. |
| 312 |
# |
| 313 |
|
| 314 |
|
| 315 |
class ProxySQLPgSQLQueryRule(object): |
| 316 |
|
| 317 |
def __init__(self, module): |
| 318 |
self.state = module.params["state"] |
| 319 |
self.force_delete = module.params["force_delete"] |
| 320 |
self.save_to_disk = module.params["save_to_disk"] |
| 321 |
self.load_to_runtime = module.params["load_to_runtime"] |
| 322 |
|
| 323 |
config_data_keys = ["rule_id", |
| 324 |
"active", |
| 325 |
"username", |
| 326 |
"database", |
| 327 |
"flagIN", |
| 328 |
"client_addr", |
| 329 |
"proxy_addr", |
| 330 |
"proxy_port", |
| 331 |
"digest", |
| 332 |
"match_digest", |
| 333 |
"match_pattern", |
| 334 |
"negate_match_pattern", |
| 335 |
"re_modifiers", |
| 336 |
"flagOUT", |
| 337 |
"replace_pattern", |
| 338 |
"destination_hostgroup", |
| 339 |
"cache_ttl", |
| 340 |
"cache_empty_result", |
| 341 |
"cache_timeout", |
| 342 |
"reconnect", |
| 343 |
"multiplex", |
| 344 |
"timeout", |
| 345 |
"retries", |
| 346 |
"delay", |
| 347 |
"next_query_flagIN", |
| 348 |
"mirror_flagOUT", |
| 349 |
"mirror_hostgroup", |
| 350 |
"error_msg", |
| 351 |
"OK_msg", |
| 352 |
"sticky_conn", |
| 353 |
"log", |
| 354 |
"apply", |
| 355 |
"attributes", |
| 356 |
"comment"] |
| 357 |
|
| 358 |
self.config_data = dict((k, module.params[k]) |
| 359 |
for k in config_data_keys) |
| 360 |
|
| 361 |
def check_rule_pk_exists(self, cursor): |
| 362 |
query_string = ("SELECT count(*) AS `rule_count`" |
| 363 |
" FROM pgsql_query_rules" |
| 364 |
" WHERE rule_id = %s") |
| 365 |
|
| 366 |
query_data = \ |
| 367 |
[self.config_data["rule_id"]] |
| 368 |
|
| 369 |
cursor.execute(query_string, query_data) |
| 370 |
check_count = cursor.fetchone() |
| 371 |
return (int(check_count['rule_count']) > 0) |
| 372 |
|
| 373 |
def check_rule_cfg_exists(self, cursor): |
| 374 |
query_string = ("SELECT count(*) AS `rule_count`" |
| 375 |
" FROM pgsql_query_rules") |
| 376 |
|
| 377 |
cols = 0 |
| 378 |
query_data = [] |
| 379 |
|
| 380 |
for col, val in self.config_data.items(): |
| 381 |
if val is not None: |
| 382 |
cols += 1 |
| 383 |
query_data.append(val) |
| 384 |
if cols == 1: |
| 385 |
query_string += "\n WHERE " + col + " = %s" |
| 386 |
else: |
| 387 |
query_string += "\n AND " + col + " = %s" |
| 388 |
|
| 389 |
if cols > 0: |
| 390 |
cursor.execute(query_string, query_data) |
| 391 |
else: |
| 392 |
cursor.execute(query_string) |
| 393 |
check_count = cursor.fetchone() |
| 394 |
return int(check_count['rule_count']) |
| 395 |
|
| 396 |
def get_rule_config(self, cursor, created_rule_id=None): |
| 397 |
query_string = ("SELECT *" |
| 398 |
" FROM pgsql_query_rules") |
| 399 |
|
| 400 |
if created_rule_id: |
| 401 |
query_data = [created_rule_id, ] |
| 402 |
query_string += "\nWHERE rule_id = %s" |
| 403 |
|
| 404 |
cursor.execute(query_string, query_data) |
| 405 |
rule = cursor.fetchone() |
| 406 |
else: |
| 407 |
cols = 0 |
| 408 |
query_data = [] |
| 409 |
|
| 410 |
for col, val in self.config_data.items(): |
| 411 |
if val is not None: |
| 412 |
cols += 1 |
| 413 |
query_data.append(val) |
| 414 |
if cols == 1: |
| 415 |
query_string += "\n WHERE " + col + " = %s" |
| 416 |
else: |
| 417 |
query_string += "\n AND " + col + " = %s" |
| 418 |
|
| 419 |
if cols > 0: |
| 420 |
cursor.execute(query_string, query_data) |
| 421 |
else: |
| 422 |
cursor.execute(query_string) |
| 423 |
rule = cursor.fetchall() |
| 424 |
|
| 425 |
return rule |
| 426 |
|
| 427 |
def create_rule_config(self, cursor): |
| 428 |
query_string = ("INSERT INTO pgsql_query_rules (") |
| 429 |
|
| 430 |
cols = 0 |
| 431 |
query_data = [] |
| 432 |
|
| 433 |
for col, val in self.config_data.items(): |
| 434 |
if val is not None: |
| 435 |
cols += 1 |
| 436 |
query_data.append(val) |
| 437 |
query_string += "\n" + col + "," |
| 438 |
|
| 439 |
query_string = query_string[:-1] |
| 440 |
|
| 441 |
query_string += \ |
| 442 |
(")\n" + |
| 443 |
"VALUES (" + |
| 444 |
"%s ," * cols) |
| 445 |
|
| 446 |
query_string = query_string[:-2] |
| 447 |
query_string += ")" |
| 448 |
|
| 449 |
cursor.execute(query_string, query_data) |
| 450 |
new_rule_id = cursor.lastrowid |
| 451 |
return True, new_rule_id |
| 452 |
|
| 453 |
def update_rule_config(self, cursor): |
| 454 |
query_string = ("UPDATE pgsql_query_rules") |
| 455 |
|
| 456 |
cols = 0 |
| 457 |
query_data = [] |
| 458 |
|
| 459 |
for col, val in self.config_data.items(): |
| 460 |
if val is not None and col != "rule_id": |
| 461 |
cols += 1 |
| 462 |
query_data.append(val) |
| 463 |
if cols == 1: |
| 464 |
query_string += "\nSET " + col + "= %s," |
| 465 |
else: |
| 466 |
query_string += "\n " + col + " = %s," |
| 467 |
|
| 468 |
query_string = query_string[:-1] |
| 469 |
query_string += "\nWHERE rule_id = %s" |
| 470 |
|
| 471 |
query_data.append(self.config_data["rule_id"]) |
| 472 |
|
| 473 |
cursor.execute(query_string, query_data) |
| 474 |
return True |
| 475 |
|
| 476 |
def delete_rule_config(self, cursor): |
| 477 |
query_string = ("DELETE FROM pgsql_query_rules") |
| 478 |
|
| 479 |
cols = 0 |
| 480 |
query_data = [] |
| 481 |
|
| 482 |
for col, val in self.config_data.items(): |
| 483 |
if val is not None: |
| 484 |
cols += 1 |
| 485 |
query_data.append(val) |
| 486 |
if cols == 1: |
| 487 |
query_string += "\n WHERE " + col + " = %s" |
| 488 |
else: |
| 489 |
query_string += "\n AND " + col + " = %s" |
| 490 |
|
| 491 |
if cols > 0: |
| 492 |
cursor.execute(query_string, query_data) |
| 493 |
else: |
| 494 |
cursor.execute(query_string) |
| 495 |
check_count = cursor.rowcount |
| 496 |
return True, int(check_count) |
| 497 |
|
| 498 |
def manage_config(self, cursor, state): |
| 499 |
if state: |
| 500 |
if self.save_to_disk: |
| 501 |
save_config_to_disk(cursor, "QUERY RULES", |
| 502 |
variable="pgsql") |
| 503 |
if self.load_to_runtime: |
| 504 |
load_config_to_runtime(cursor, "QUERY RULES", |
| 505 |
variable="pgsql") |
| 506 |
|
| 507 |
def create_rule(self, check_mode, result, cursor): |
| 508 |
if not check_mode: |
| 509 |
result['changed'], new_rule_id = \ |
| 510 |
self.create_rule_config(cursor) |
| 511 |
result['msg'] = "Added rule to pgsql_query_rules" |
| 512 |
self.manage_config(cursor, |
| 513 |
result['changed']) |
| 514 |
result['rules'] = \ |
| 515 |
self.get_rule_config(cursor, new_rule_id) |
| 516 |
else: |
| 517 |
result['changed'] = True |
| 518 |
result['msg'] = ("Rule would have been added to" + |
| 519 |
" pgsql_query_rules, however" + |
| 520 |
" check_mode is enabled.") |
| 521 |
|
| 522 |
def update_rule(self, check_mode, result, cursor): |
| 523 |
if not check_mode: |
| 524 |
result['changed'] = \ |
| 525 |
self.update_rule_config(cursor) |
| 526 |
result['msg'] = "Updated rule in pgsql_query_rules" |
| 527 |
self.manage_config(cursor, |
| 528 |
result['changed']) |
| 529 |
result['rules'] = \ |
| 530 |
self.get_rule_config(cursor) |
| 531 |
else: |
| 532 |
result['changed'] = True |
| 533 |
result['msg'] = ("Rule would have been updated in" + |
| 534 |
" pgsql_query_rules, however" + |
| 535 |
" check_mode is enabled.") |
| 536 |
|
| 537 |
def delete_rule(self, check_mode, result, cursor): |
| 538 |
if not check_mode: |
| 539 |
result['rules'] = \ |
| 540 |
self.get_rule_config(cursor) |
| 541 |
result['changed'], result['rows_affected'] = \ |
| 542 |
self.delete_rule_config(cursor) |
| 543 |
result['msg'] = "Deleted rule from pgsql_query_rules" |
| 544 |
self.manage_config(cursor, |
| 545 |
result['changed']) |
| 546 |
else: |
| 547 |
result['changed'] = True |
| 548 |
result['msg'] = ("Rule would have been deleted from" + |
| 549 |
" pgsql_query_rules, however" + |
| 550 |
" check_mode is enabled.") |
| 551 |
|
| 552 |
# =========================================== |
| 553 |
# Module execution. |
| 554 |
# |
| 555 |
|
| 556 |
|
| 557 |
def main(): |
| 558 |
argument_spec = proxysql_common_argument_spec() |
| 559 |
argument_spec.update( |
| 560 |
rule_id=dict(type='int'), |
| 561 |
active=dict(type='bool'), |
| 562 |
username=dict(type='str'), |
| 563 |
database=dict(type='str'), |
| 564 |
flagIN=dict(type='int'), |
| 565 |
client_addr=dict(type='str'), |
| 566 |
proxy_addr=dict(type='str'), |
| 567 |
proxy_port=dict(type='int'), |
| 568 |
digest=dict(type='str'), |
| 569 |
match_digest=dict(type='str'), |
| 570 |
match_pattern=dict(type='str'), |
| 571 |
negate_match_pattern=dict(type='bool'), |
| 572 |
re_modifiers=dict(type='str'), |
| 573 |
flagOUT=dict(type='int'), |
| 574 |
replace_pattern=dict(type='str'), |
| 575 |
destination_hostgroup=dict(type='int'), |
| 576 |
cache_ttl=dict(type='int'), |
| 577 |
cache_empty_result=dict(type='bool'), |
| 578 |
cache_timeout=dict(type='int'), |
| 579 |
reconnect=dict(type='bool'), |
| 580 |
multiplex=dict(type='int', choices=[0, 1, 2]), |
| 581 |
timeout=dict(type='int'), |
| 582 |
retries=dict(type='int'), |
| 583 |
delay=dict(type='int'), |
| 584 |
next_query_flagIN=dict(type='int'), |
| 585 |
mirror_flagOUT=dict(type='int'), |
| 586 |
mirror_hostgroup=dict(type='int'), |
| 587 |
OK_msg=dict(type='str'), |
| 588 |
error_msg=dict(type='str'), |
| 589 |
sticky_conn=dict(type='bool'), |
| 590 |
log=dict(type='bool'), |
| 591 |
apply=dict(type='bool'), |
| 592 |
attributes=dict(type='str'), |
| 593 |
comment=dict(type='str'), |
| 594 |
state=dict(default='present', choices=['present', |
| 595 |
'absent']), |
| 596 |
force_delete=dict(default=False, type='bool'), |
| 597 |
save_to_disk=dict(default=True, type='bool'), |
| 598 |
load_to_runtime=dict(default=True, type='bool') |
| 599 |
) |
| 600 |
|
| 601 |
module = AnsibleModule( |
| 602 |
supports_check_mode=True, |
| 603 |
argument_spec=argument_spec |
| 604 |
) |
| 605 |
|
| 606 |
login_user = module.params["login_user"] |
| 607 |
login_password = module.params["login_password"] |
| 608 |
config_file = module.params["config_file"] |
| 609 |
|
| 610 |
cursor = None |
| 611 |
try: |
| 612 |
cursor, db_conn, version = mysql_connect(module, |
| 613 |
login_user, |
| 614 |
login_password, |
| 615 |
config_file, |
| 616 |
cursor_class='DictCursor') |
| 617 |
except mysql_driver.Error as e: |
| 618 |
module.fail_json( |
| 619 |
msg="unable to connect to ProxySQL Admin Module.. %s" % to_native(e) |
| 620 |
) |
| 621 |
|
| 622 |
proxysql_query_rule = ProxySQLPgSQLQueryRule(module) |
| 623 |
result = {} |
| 624 |
|
| 625 |
result['state'] = proxysql_query_rule.state |
| 626 |
|
| 627 |
if proxysql_query_rule.state == "present": |
| 628 |
try: |
| 629 |
if not proxysql_query_rule.check_rule_cfg_exists(cursor): |
| 630 |
if proxysql_query_rule.config_data["rule_id"] and \ |
| 631 |
proxysql_query_rule.check_rule_pk_exists(cursor): |
| 632 |
proxysql_query_rule.update_rule(module.check_mode, |
| 633 |
result, |
| 634 |
cursor) |
| 635 |
else: |
| 636 |
proxysql_query_rule.create_rule(module.check_mode, |
| 637 |
result, |
| 638 |
cursor) |
| 639 |
else: |
| 640 |
result['changed'] = False |
| 641 |
result['msg'] = ("The rule already exists in" + |
| 642 |
" pgsql_query_rules and doesn't need to be" + |
| 643 |
" updated.") |
| 644 |
result['rules'] = \ |
| 645 |
proxysql_query_rule.get_rule_config(cursor) |
| 646 |
|
| 647 |
except mysql_driver.Error as e: |
| 648 |
module.fail_json( |
| 649 |
msg="unable to modify rule.. %s" % to_native(e) |
| 650 |
) |
| 651 |
|
| 652 |
elif proxysql_query_rule.state == "absent": |
| 653 |
try: |
| 654 |
existing_rules = proxysql_query_rule.check_rule_cfg_exists(cursor) |
| 655 |
if existing_rules > 0: |
| 656 |
if existing_rules == 1 or \ |
| 657 |
proxysql_query_rule.force_delete: |
| 658 |
proxysql_query_rule.delete_rule(module.check_mode, |
| 659 |
result, |
| 660 |
cursor) |
| 661 |
else: |
| 662 |
module.fail_json( |
| 663 |
msg=("Operation would delete multiple rules" + |
| 664 |
" use force_delete to override this") |
| 665 |
) |
| 666 |
else: |
| 667 |
result['changed'] = False |
| 668 |
result['msg'] = ("The rule is already absent from the" + |
| 669 |
" pgsql_query_rules memory configuration") |
| 670 |
except mysql_driver.Error as e: |
| 671 |
module.fail_json( |
| 672 |
msg="unable to remove rule.. %s" % to_native(e) |
| 673 |
) |
| 674 |
|
| 675 |
module.exit_json(**result) |
| 676 |
|
| 677 |
|
| 678 |
if __name__ == '__main__': |
| 679 |
main() |