Skip to content

Commit

Permalink
clickhouse_info: add roles info per user (ansible-collections#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andersson007 authored Aug 19, 2024
1 parent 78abf5d commit 8435902
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
Empty file added changelogs/fragments/.keep
Empty file.
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 - add the ``roles`` field to user information.
16 changes: 15 additions & 1 deletion plugins/modules/clickhouse_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ def get_users(module, client):

user_info = {}
for row in result:
user_info[row[0]] = {
user_name = row[0]
user_info[user_name] = {
"id": str(row[1]),
"storage": row[2],
"auth_type": row[3],
Expand All @@ -449,9 +450,22 @@ def get_users(module, client):
"default_roles_except": row[11],
}

user_info[user_name]["roles"] = get_user_roles(module, client, user_name)

return user_info


def get_user_roles(module, client, user_name):
"""Get user roles.
Returns a list of roles.
"""
query = ("SELECT granted_role_name FROM system.role_grants "
"WHERE user_name = '%s'" % user_name)
result = execute_query(module, client, query)
return [row[0] for row in result]


def get_settings_profiles(module, client):
"""Get settings profiles.
Expand Down
19 changes: 17 additions & 2 deletions tests/integration/targets/clickhouse_info/tasks/initial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@
# and should not be used as examples of how to write Ansible roles #
####################################################################

- name: Create role
- name: Create roles
community.clickhouse.clickhouse_client:
execute: "CREATE ROLE IF NOT EXISTS accountant"
execute: "CREATE ROLE IF NOT EXISTS {{ item }}"
loop:
- accountant
- sales

- name: Create user
community.clickhouse.clickhouse_user:
name: bob

- name: Grant role
community.clickhouse.clickhouse_client:
execute: "GRANT {{ item }} TO bob"
loop:
- accountant
- sales

- name: Get info
register: result
Expand All @@ -19,6 +33,7 @@
that:
- result is not changed
- result["users"]["default"] != {}
- result["users"]["bob"]["roles"] == ["accountant", "sales"] or result["users"]["bob"]["roles"] == ["sales", "accountant"]
- result["roles"]["accountant"] != {}
- result["databases"]["default"]["engine"] == "Atomic"
- result["version"] != {}
Expand Down

0 comments on commit 8435902

Please sign in to comment.