grafana-ansible-collection/plugins/modules/folder.py

242 lines
6.8 KiB
Python
Raw Permalink Normal View History

2022-08-09 08:37:47 +02:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
2022-08-11 12:17:51 +02:00
# Copyright: (c) 2021, Ishan Jain (@ishanjainn)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import (absolute_import, division, print_function)
2022-08-09 08:37:47 +02:00
DOCUMENTATION = '''
---
module: folder
2022-08-09 08:37:47 +02:00
author:
- Ishan Jain (@ishanjainn)
version_added: "0.0.1"
2022-08-30 08:46:55 +02:00
short_description: Manage Folders in Grafana Cloud
2022-08-09 08:37:47 +02:00
description:
- Create, Update and delete Folders via Ansible.
requirements: [ "requests >= 1.0.0" ]
notes:
- Does not support C(check_mode).
2022-08-09 08:37:47 +02:00
options:
title:
description:
2022-08-30 08:46:55 +02:00
- Sets the title of the folder.
2022-08-09 08:37:47 +02:00
type: str
required: true
uid:
description:
2022-08-30 08:46:55 +02:00
- Sets the UID for your folder.
2022-08-09 08:37:47 +02:00
type: str
required: true
overwrite:
description:
- Set to C(false) if you dont want to overwrite existing folder with newer version.
type: bool
2022-08-09 08:37:47 +02:00
required: false
default: true
grafana_api_key:
2022-08-09 08:37:47 +02:00
description:
- Grafana API Key to authenticate with Grafana.
2022-08-09 08:37:47 +02:00
type: str
required : true
stack_slug:
description:
2022-08-30 08:46:55 +02:00
- Name of the Grafana Cloud stack to which the folder will be added.
type: str
required: true
2022-08-09 08:37:47 +02:00
state:
description:
2022-08-30 08:46:55 +02:00
- State for the Grafana Cloud stack.
2022-08-09 08:37:47 +02:00
choices: [ present, absent ]
default: present
type: str
'''
EXAMPLES = '''
- name: Create/Update a Folder in Grafana
2022-08-11 07:09:04 +02:00
grafana.grafana.folder:
2022-08-09 08:37:47 +02:00
title: folder_name
uid: folder_name
overwrite: true
stack_slug: "{{ stack_slug }}"
grafana_api_key: "{{ grafana_api_key }}"
2022-08-09 08:37:47 +02:00
state: present
- name: Delete a Folder in Grafana
2022-08-11 07:09:04 +02:00
grafana.grafana.folder:
2022-08-09 08:37:47 +02:00
uid: folder_name
stack_slug: "{{ stack_slug }}"
grafana_api_key: "{{ grafana_api_key }}"
2022-08-09 08:37:47 +02:00
state: absent
'''
RETURN = r'''
2022-08-10 12:43:04 +02:00
output:
description: Dict object containing folder information.
2022-08-09 08:37:47 +02:00
returned: On success
type: dict
contains:
canAdmin:
description: Boolean value specifying if current user can admin in folder.
2022-08-09 08:37:47 +02:00
returned: state is present and on success
type: bool
sample: true
2022-08-09 08:37:47 +02:00
canDelete:
description: Boolean value specifying if current user can delete the folder.
2022-08-09 08:37:47 +02:00
returned: state is present and on success
type: bool
sample: true
2022-08-09 08:37:47 +02:00
canEdit:
description: Boolean value specifying if current user can edit in folder.
2022-08-09 08:37:47 +02:00
returned: state is present and on success
type: bool
sample: true
2022-08-09 08:37:47 +02:00
canSave:
description: Boolean value specifying if current user can save in folder.
2022-08-09 08:37:47 +02:00
returned: state is present and on success
type: bool
sample: true
2022-08-09 08:37:47 +02:00
created:
description: The date when folder was created.
2022-08-09 08:37:47 +02:00
returned: state is present and on success
type: str
sample: "2022-10-20T09:31:53Z"
2022-08-09 08:37:47 +02:00
createdBy:
description: The name of the user who created the folder.
2022-08-09 08:37:47 +02:00
returned: state is present and on success
type: str
sample: "Anonymous"
2022-08-09 08:37:47 +02:00
hasAcl:
description: Boolean value specifying if folder has acl.
2022-08-09 08:37:47 +02:00
returned: state is present and on success
type: bool
sample: true
2022-08-09 08:37:47 +02:00
id:
description: The ID for the folder.
2022-08-09 08:37:47 +02:00
returned: on success
type: int
sample: 18
2022-08-09 08:37:47 +02:00
title:
description: The name of the folder.
2022-08-09 08:37:47 +02:00
returned: on success
type: str
sample: foldername
2022-08-09 08:37:47 +02:00
uid:
description: The UID for the folder.
2022-08-09 08:37:47 +02:00
returned: state is present and on success
type: str
sample: foldername
2022-08-09 08:37:47 +02:00
updated:
description: The date when the folder was last updated.
2022-08-09 08:37:47 +02:00
returned: state is present and on success
type: str
sample: "2022-10-20T09:31:53Z"
2022-08-09 08:37:47 +02:00
updatedBy:
description: The name of the user who last updated the folder.
2022-08-09 08:37:47 +02:00
returned: state is present and on success
type: str
sample: "Anonymous"
2022-08-09 08:37:47 +02:00
url:
description: The URl for the folder.
2022-08-09 08:37:47 +02:00
returned: state is present and on success
type: str
sample: "/dashboards/f/foldername/foldername"
2022-08-09 08:37:47 +02:00
version:
description: The version of the folder.
2022-08-09 08:37:47 +02:00
returned: state is present and on success
type: int
sample: 1
2022-08-09 08:37:47 +02:00
message:
description: The message returned after the operation on the folder.
2022-08-09 08:37:47 +02:00
returned: state is absent and on success
type: str
sample: "Folder foldername deleted"
2022-08-09 08:37:47 +02:00
'''
from ansible.module_utils.basic import AnsibleModule
try:
import requests
HAS_REQUESTS = True
except ImportError:
HAS_REQUESTS = False
__metaclass__ = type
2022-08-09 08:37:47 +02:00
def present_folder(module):
body = {
'uid': module.params['uid'],
'title': module.params['title'],
}
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/folders'
result = requests.post(api_url, json=body, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
2022-08-09 08:37:47 +02:00
if result.status_code == 200:
return False, True, result.json()
elif result.status_code == 412:
body = {
'uid': module.params['uid'],
'title': module.params['title'],
'overwrite': module.params['overwrite']
}
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/folders/' + module.params['uid']
result = requests.put(api_url, json=body, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
2022-08-09 08:37:47 +02:00
if result.status_code == 200:
return False, True, result.json()
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}
def absent_folder(module):
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/folders/' + module.params['uid']
result = requests.delete(api_url, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
2022-08-09 08:37:47 +02:00
if result.status_code == 200:
return False, True, result.json()
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}
def main():
2022-08-09 08:37:47 +02:00
module_args = dict(
title=dict(type='str', required=True),
uid=dict(type='str', required=True),
overwrite=dict(type='bool', required=False, default=True),
stack_slug=dict(type='str', required=True),
grafana_api_key=dict(type='str', required=True, no_log=True),
2022-08-09 08:37:47 +02:00
state=dict(type='str', required=False, default='present', choices=['present', 'absent'])
)
choice_map = {
"present": present_folder,
"absent": absent_folder,
}
module = AnsibleModule(
argument_spec=module_args
2022-08-09 08:37:47 +02:00
)
if not HAS_REQUESTS:
module.fail_json("Missing package - `request` ")
2022-08-09 08:37:47 +02:00
is_error, has_changed, result = choice_map.get(
module.params['state'])(module)
if not is_error:
module.exit_json(changed=has_changed, output=result)
else:
module.fail_json(msg=result)
if __name__ == '__main__':
main()