Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grafana_folder: add permissions #231

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions plugins/modules/grafana_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
type: bool
default: False
version_added: "1.2.0"
permissions:
description:
- Set the permissions of the folder.
- Able to define it for a I(role), I(teamId) and I(userId)
- Values for I(permission) are C(1) = View, C(2) = Edit, C(4) = Admin
- This will remove existing permissions if they are not included.
type: list
elements: dict
extends_documentation_fragment:
- community.grafana.basic_auth
- community.grafana.api_key
Expand All @@ -66,6 +74,20 @@
title: "grafana_working_group"
state: present

- name: Create a folder and set permissions
community.grafana.grafana_folder:
url: "https://grafana.example.com"
grafana_api_key: "{{ some_api_token_value }}"
title: "grafana_working_group"
state: present
permissions:
- role: "Editor"
permission: 2
- role: "Viewer"
permission: 1
- userId: 2
permission: 4
rrey marked this conversation as resolved.
Show resolved Hide resolved

- name: Delete a folder
community.grafana.grafana_folder:
url: "https://grafana.example.com"
Expand Down Expand Up @@ -246,6 +268,15 @@ def delete_folder(self, folder_uid):
response = self._send_request(url, headers=self.headers, method="DELETE")
return response

def create_folder_permission(self, folder_uid, perm):
url = "/api/folders/{folder_uid}/permissions".format(folder_uid=folder_uid)
items = {
"items": []
}
items["items"].extend(perm)
response = self._send_request(url, data=items, headers=self.headers, method="POST")
return response


def setup_module_object():
module = AnsibleModule(
Expand All @@ -262,6 +293,7 @@ def setup_module_object():
name=dict(type='str', aliases=['title'], required=True),
state=dict(type='str', default='present', choices=['present', 'absent']),
skip_version_check=dict(type='bool', default=False),
permissions=dict(type='list', elements='dict'),
)


Expand All @@ -270,6 +302,7 @@ def main():
module = setup_module_object()
state = module.params['state']
title = module.params['name']
permissions = module.params['permissions']

grafana_iface = GrafanaFolderInterface(module)

Expand All @@ -281,6 +314,9 @@ def main():
folder = grafana_iface.get_folder(title)
changed = True
folder = grafana_iface.get_folder(title)
if permissions is not None:
rrey marked this conversation as resolved.
Show resolved Hide resolved
grafana_iface.create_folder_permission(folder.get("uid"), permissions)
changed = True
module.exit_json(changed=changed, folder=folder)
elif state == 'absent':
folder = grafana_iface.get_folder(title)
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/targets/grafana_folder/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@
- "result.changed == true"
- "result.folder.title == 'grafana_working_group'"

- name: Set permissions of a Folder
grafana_folder:
url: "{{ grafana_url }}"
url_username: "{{ grafana_username }}"
url_password: "{{ grafana_password }}"
title: "grafana_working_group"
state: present
permissions:
- role: "Editor"
permission: 2
- role: "Viewer"
permission: 1
register: result

- assert:
that:
- "result.changed == true"
- "result.folder.title == 'grafana_working_group'"

rrey marked this conversation as resolved.
Show resolved Hide resolved
- name: Test folder creation idempotency
grafana_folder:
url: "{{ grafana_url }}"
Expand Down