From d4cadbf431c6939ef0cd0fb8d7fccfe6db66614d Mon Sep 17 00:00:00 2001 From: Aleksandr Vagachev Date: Mon, 15 Apr 2024 11:55:52 +0300 Subject: [PATCH] Added new role management features (#59) * Added new role management features * Fixed test --- plugins/modules/clickhouse_role.py | 35 +++++++++++++++++++ plugins/modules/clickhouse_user.py | 1 + .../targets/clickhouse_role/tasks/initial.yml | 5 ++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/plugins/modules/clickhouse_role.py b/plugins/modules/clickhouse_role.py index 069f0f4..3fd8fe9 100644 --- a/plugins/modules/clickhouse_role.py +++ b/plugins/modules/clickhouse_role.py @@ -26,6 +26,7 @@ author: - Don Naro (@oranod) + - Aleksandr Vagachev (@aleksvagachev) extends_documentation_fragment: - community.clickhouse.client_inst_opts @@ -44,6 +45,17 @@ - Role name to add or remove. type: str required: true + cluster: + description: + - Run the command on all cluster hosts. + - If the cluster is not configured, the command will crash with an error. + type: str + settings: + description: + - Settings with their limitations that apply to the role. + - You can also specify the profile from which the settings will be inherited. + type: list + elements: str """ EXAMPLES = r""" @@ -52,6 +64,15 @@ name: test_role state: present +- name: Create a role with settings + community.clickhouse.clickhouse_role: + name: test_role + state: present + settings: + - max_memory_usage = 15000 MIN 15000 MAX 16000 READONLY + - PROFILE restricted + cluster: test_cluster + - name: Remove role community.clickhouse.clickhouse_role: name: test_role @@ -95,6 +116,18 @@ def check_exists(self): def create(self): if not self.exists: query = "CREATE ROLE %s" % self.name + + if self.module.params['cluster']: + query += " ON CLUSTER %s" % self.module.params['cluster'] + + list_settings = self.module.params['settings'] + if list_settings: + query += " SETTINGS" + for index, value in enumerate(list_settings): + query += " %s" % value + if index < len(list_settings) - 1: + query += "," + executed_statements.append(query) if not self.module.check_mode: @@ -128,6 +161,8 @@ def main(): argument_spec.update( state=dict(type="str", choices=["present", "absent"], default="present"), name=dict(type="str", required=True), + cluster=dict(type='str', default=None), + settings=dict(type='list', elements='str'), ) # Instantiate an object of module class diff --git a/plugins/modules/clickhouse_user.py b/plugins/modules/clickhouse_user.py index 3f9dd9b..8c82c11 100644 --- a/plugins/modules/clickhouse_user.py +++ b/plugins/modules/clickhouse_user.py @@ -81,6 +81,7 @@ - You can also specify the profile from which the settings will be inherited. type: list elements: str + version_added: '0.5.0' ''' EXAMPLES = r''' diff --git a/tests/integration/targets/clickhouse_role/tasks/initial.yml b/tests/integration/targets/clickhouse_role/tasks/initial.yml index 1a87815..573877a 100644 --- a/tests/integration/targets/clickhouse_role/tasks/initial.yml +++ b/tests/integration/targets/clickhouse_role/tasks/initial.yml @@ -33,12 +33,15 @@ community.clickhouse.clickhouse_role: state: present name: test_role + settings: + - max_memory_usage = 15000 READONLY + - max_memory_usage_for_all_queries = 15000 MIN 15000 MAX 16000 WRITABLE - name: Test 2 - Check the return values ansible.builtin.assert: that: - result is changed - - result.executed_statements == ['CREATE ROLE test_role'] + - result.executed_statements == ['CREATE ROLE test_role SETTINGS max_memory_usage = 15000 READONLY, max_memory_usage_for_all_queries = 15000 MIN 15000 MAX 16000 WRITABLE'] - name: Test 2 - Check the role was created register: result