From e9da8b0c08b07c3193ff33821b0a708e2fb9e62f Mon Sep 17 00:00:00 2001 From: Aleksandr Vagachev Date: Wed, 28 Feb 2024 11:17:51 +0300 Subject: [PATCH] Added the collection of information functions (#44) * Added the collection of information functions * Update plugins/modules/clickhouse_info.py Co-authored-by: Andrew Klychkov --------- Co-authored-by: Andrew Klychkov --- changelogs/fragments/.keep | 0 changelogs/fragments/2-clickhouse_info.yml | 2 + plugins/modules/clickhouse_info.py | 38 +++++++++++++++++++ .../targets/clickhouse_info/tasks/initial.yml | 20 ++++++++++ 4 files changed, 60 insertions(+) delete mode 100644 changelogs/fragments/.keep create mode 100644 changelogs/fragments/2-clickhouse_info.yml diff --git a/changelogs/fragments/.keep b/changelogs/fragments/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/changelogs/fragments/2-clickhouse_info.yml b/changelogs/fragments/2-clickhouse_info.yml new file mode 100644 index 0000000..ccb37f6 --- /dev/null +++ b/changelogs/fragments/2-clickhouse_info.yml @@ -0,0 +1,2 @@ +minor_changes: +- clickhouse_info - added the ability to collect information from system.functions. diff --git a/plugins/modules/clickhouse_info.py b/plugins/modules/clickhouse_info.py index 0607778..3a6d027 100644 --- a/plugins/modules/clickhouse_info.py +++ b/plugins/modules/clickhouse_info.py @@ -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 @@ -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. @@ -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'] diff --git a/tests/integration/targets/clickhouse_info/tasks/initial.yml b/tests/integration/targets/clickhouse_info/tasks/initial.yml index 534fceb..f4b4f09 100644 --- a/tests/integration/targets/clickhouse_info/tasks/initial.yml +++ b/tests/integration/targets/clickhouse_info/tasks/initial.yml @@ -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"] != {}