fix returned value since get_resource_limit is used by other methods

This commit is contained in:
Laurent Indermuehle 2023-09-14 18:40:07 +02:00
parent 367a528058
commit 9f34b926bf
No known key found for this signature in database
GPG key ID: 93FA944C9F34DD09
3 changed files with 41 additions and 10 deletions

View file

@ -799,11 +799,31 @@ def get_resource_limits(module, cursor, user, host):
if not res:
return None
# If all resources are at zero, then the user as no resources limits
if all(value == 0 for value in res.values()):
return None
if isinstance(res, tuple):
current_limits = {
'MAX_QUERIES_PER_HOUR': res[0],
'MAX_UPDATES_PER_HOUR': res[1],
'MAX_CONNECTIONS_PER_HOUR': res[2],
'MAX_USER_CONNECTIONS': res[3],
}
return res
if srv_type == 'mariadb':
current_limits['MAX_STATEMENT_TIME'] = res[4]
# I'm not sure this is useful. Maybe fetchone() always return a
# tuple? But I go so many KeyError 0 errors that I haded this.
if isinstance(res, dict):
current_limits = {
'MAX_QUERIES_PER_HOUR': res.get('MAX_QUERIES_PER_HOUR'),
'MAX_UPDATES_PER_HOUR': res.get('MAX_UPDATES_PER_HOUR'),
'MAX_CONNECTIONS_PER_HOUR': res.get('MAX_CONNECTIONS_PER_HOUR'),
'MAX_USER_CONNECTIONS': res.get('MAX_USER_CONNECTIONS'),
}
if srv_type == 'mariadb':
current_limits['MAX_STATEMENT_TIME'] = res.get('MAX_STATEMENT_TIME')
return current_limits
def match_resource_limits(module, current, desired):
@ -859,12 +879,11 @@ def limit_resources(module, cursor, user, host, resource_limits, check_mode):
module.fail_json(msg="The server version does not match the requirements "
"for resource_limits parameter. See module's documentation.")
cursor.execute("SELECT VERSION()")
if 'mariadb' not in cursor.fetchone()[0].lower():
if get_server_type(cursor) != 'mariadb':
if 'MAX_STATEMENT_TIME' in resource_limits:
module.fail_json(msg="MAX_STATEMENT_TIME resource limit is only supported by MariaDB.")
current_limits = get_resource_limits(cursor, user, host)
current_limits = get_resource_limits(module, cursor, user, host)
needs_to_change = match_resource_limits(module, current_limits, resource_limits)

View file

@ -552,15 +552,21 @@ class MySQL_Info(object):
resource_limits = get_resource_limits(self.module, self.cursor, user, host)
copy_ressource_limits = dict.copy(resource_limits)
output_dict = {
'user': user,
'host': host,
'privs': '/'.join(priv_string),
'resource_limits': resource_limits
'resource_limits': copy_ressource_limits
}
if not resource_limits:
del output_dict['resource_limits']
# Prevent returning a resource limit if there is no value
if resource_limits:
for key, value in resource_limits.items():
if value == 0:
del output_dict['resource_limits'][key]
if len(output_dict['resource_limits']) == 0:
del output_dict['resource_limits']
output.append(output_dict)

View file

@ -47,6 +47,7 @@
host: "users_privs.com"
password: "msandbox"
priv: "{{ item.privs }}"
resource_limits: "{{ item.resource_limits | default(omit) }}"
state: present
loop:
- user: users_privs_adm
@ -61,9 +62,14 @@
- user: users_privs_col
privs:
'users_privs_db.t1': 'SELECT (id)'
resource_limits:
MAX_USER_CONNECTIONS: 100
- user: users_privs_proc
privs:
'PROCEDURE users_privs_db.get_all_items': 'EXECUTE'
resource_limits:
MAX_USER_CONNECTIONS: 2
MAX_CONNECTIONS_PER_HOUR: 60
- user: users_privs_multi
privs:
'mysql.*': 'SELECT'