Skip to content

Commit

Permalink
Added the collection of information settings_profiles, quotas
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksvagachev committed Feb 18, 2024
1 parent e33b2e7 commit 4ca3f5a
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/0-clickhouse_info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- clickhouse_info - added the ability to collect information from system.quotas, system.settings_profiles.
100 changes: 100 additions & 0 deletions plugins/modules/clickhouse_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@
type: dict
sample: { "database": { "dictionary": "..." } }
version_added: '0.3.0'
quotas:
description:
- The content of the system.quotas table with quota names as keys.
returned: success
type: dict
sample: { "default": "..." }
version_added: '0.4.0'
settings_profiles:
description:
- The content of the system.settings_profiles table with profile names as keys.
returned: success
type: dict
sample: { "readonly": "..." }
version_added: '0.4.0'
'''

from ansible.module_utils.basic import AnsibleModule
Expand Down Expand Up @@ -421,6 +435,90 @@ def get_users(module, client):
return user_info


def get_settings_profiles(module, client):
"""Get settings profiles.
Returns a dictionary with profile names as keys.
"""
query = ("SELECT name, id, storage, num_elements, apply_to_all, apply_to_list, "
"apply_to_except FROM system.settings_profiles")
result = execute_query(module, client, query)

if result == PRIV_ERR_CODE:
return {PRIV_ERR_CODE: "Not enough privileges"}

profile_info = {}
for row in result:
profile_info[row[0]] = {
"id": str(row[1]),
"storage": row[2],
"num_elements": row[3],
"apply_to_all": row[4],
"apply_to_list": row[5],
"apply_to_except": row[6],
}

return profile_info


def get_quotas(module, client):
"""Get quotas.
Returns a dictionary with quota names as keys.
"""
query = ("SELECT name, id, storage, keys, durations, apply_to_all, "
"apply_to_list, apply_to_except FROM system.quotas")
result = execute_query(module, client, query)

if result == PRIV_ERR_CODE:
return {PRIV_ERR_CODE: "Not enough privileges"}

quota_info = {}
for row in result:
quota_info[row[0]] = {
"id": str(row[1]),
"storage": row[2],
"keys": row[3],
"durations": row[4],
"apply_to_all": row[5],
"apply_to_list": row[6],
"apply_to_except": row[7],
}

return quota_info


def get_server_version(module, client):
"""Get server version.
Returns a dictionary with server version.
"""
result = execute_query(module, client, "SELECT version()")

if result == PRIV_ERR_CODE:
return {PRIV_ERR_CODE: "Not enough privileges"}

raw = result[0][0]
split_raw = raw.split('.')

version = {}
version["raw"] = raw

version["year"] = int(split_raw[0])
version["feature"] = int(split_raw[1])
version["maintenance"] = int(split_raw[2])

if '-' in split_raw[3]:
tmp = split_raw[3].split('-')
version["build"] = int(tmp[0])
version["type"] = tmp[1]
else:
version["build"] = int(split_raw[3])
version["type"] = None

return version


def get_server_version(module, client):
"""Get server version.
Expand Down Expand Up @@ -521,6 +619,8 @@ def main():
'dictionaries': get_dictionaries,
'tables': get_tables,
'merge_tree_settings': get_merge_tree_settings,
'quotas': get_quotas,
'settings_profiles': get_settings_profiles,
}
# Check if the limit is provided, it contains correct values
limit = module.params['limit']
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/targets/clickhouse_info/tasks/initial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
- result["merge_tree_settings"]["merge_max_block_size"] != {}
- result["tables"] != {}
- result["dictionaries"] != {}
- result["quotas"]["default"] != {}
- result["settings_profiles"]["default"] != {}

- name: Debug
ansible.builtin.debug:
Expand Down

0 comments on commit 4ca3f5a

Please sign in to comment.