Skip to content

Commit

Permalink
Added the collection of information functions (#44)
Browse files Browse the repository at this point in the history
* Added the collection of information functions

* Update plugins/modules/clickhouse_info.py

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

---------

Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>
  • Loading branch information
aleksvagachev and Andersson007 authored Feb 28, 2024
1 parent ed30582 commit e9da8b0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
Empty file removed changelogs/fragments/.keep
Empty file.
2 changes: 2 additions & 0 deletions changelogs/fragments/2-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.functions.
38 changes: 38 additions & 0 deletions plugins/modules/clickhouse_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@
type: dict
sample: { "readonly": "..." }
version_added: '0.4.0'
functions:
description:
- The content of the system.functions table with function names as keys.
- Works only for clickhouse-server versions >= 22.
- Does not output functions on the 'System' origin.
returned: success
type: dict
sample: { "test_function": "..." }
version_added: '0.4.0'
'''

from ansible.module_utils.basic import AnsibleModule
Expand Down Expand Up @@ -489,6 +498,34 @@ def get_quotas(module, client):
return quota_info


def get_functions(module, client):
"""Get functions.
Returns a dictionary with function names as keys.
"""
srv_version = get_server_version(module, client)
function_info = {}
if srv_version['year'] >= 22:
query = ("SELECT name, is_aggregate, case_insensitive, alias_to, "
"create_query, origin FROM system.functions "
"WHERE origin != 'System'")
result = execute_query(module, client, query)

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

for row in result:
function_info[row[0]] = {
"is_aggregate": str(row[1]),
"case_insensitive": row[2],
"alias_to": row[3],
"create_query": row[4],
"origin": row[5],
}

return function_info


def get_driver(module, client):
"""Gets driver information.
Expand Down Expand Up @@ -560,6 +597,7 @@ def main():
'merge_tree_settings': get_merge_tree_settings,
'quotas': get_quotas,
'settings_profiles': get_settings_profiles,
'functions': get_functions,
}
# Check if the limit is provided, it contains correct values
limit = module.params['limit']
Expand Down
20 changes: 20 additions & 0 deletions tests/integration/targets/clickhouse_info/tasks/initial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,23 @@
- result is not changed
- result["version"] != {}
- result["driver"]["version"] != {}

- name: Check function
when: result['version']['year'] >= 22
block:
- name: Create function
community.clickhouse.clickhouse_client:
execute: "CREATE FUNCTION linear_equation AS (x, k, b) -> k*x + b"

- name: Get info
register: result_func
community.clickhouse.clickhouse_info:
login_host: localhost
limit:
- functions

- name: Check result
ansible.builtin.assert:
that:
- result_func is not changed
- result_func["functions"] != {}

0 comments on commit e9da8b0

Please sign in to comment.