2022-08-09 08:37:47 +02:00
|
|
|
#!/usr/bin/python
|
2022-08-11 11:40:32 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-11 12:17:51 +02:00
|
|
|
# Copyright: (c) 2021, Ishan Jain (@ishanjainn)
|
2022-08-11 11:40:32 +02:00
|
|
|
# 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 = '''
|
|
|
|
---
|
2022-08-11 11:40:32 +02:00
|
|
|
module: dashboard
|
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 Dashboards in Grafana Cloud
|
2022-08-09 08:37:47 +02:00
|
|
|
description:
|
|
|
|
- Create, Update and delete Dashboards using Ansible.
|
2022-08-11 11:40:32 +02:00
|
|
|
requirements: [ "requests >= 1.0.0" ]
|
2022-10-20 12:53:16 +02:00
|
|
|
notes:
|
|
|
|
- Does not support C(check_mode).
|
2022-08-09 08:37:47 +02:00
|
|
|
options:
|
|
|
|
dashboard:
|
|
|
|
description:
|
2022-08-30 08:46:55 +02:00
|
|
|
- JSON source code for dashboard.
|
2022-08-09 08:37:47 +02:00
|
|
|
type: dict
|
|
|
|
required: true
|
|
|
|
stack_slug:
|
|
|
|
description:
|
2022-08-30 08:46:55 +02:00
|
|
|
- Name of the Grafana Cloud stack to which the dashboard will be added.
|
2022-08-09 08:37:47 +02:00
|
|
|
type: str
|
|
|
|
required: true
|
2022-08-11 11:40:32 +02:00
|
|
|
grafana_api_key:
|
2022-08-09 08:37:47 +02:00
|
|
|
description:
|
2022-08-30 08:46:55 +02:00
|
|
|
- Grafana API Key to authenticate with Grafana Cloud.
|
2022-08-09 08:37:47 +02:00
|
|
|
type: str
|
|
|
|
required : true
|
|
|
|
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 dashboard
|
2022-08-11 07:09:04 +02:00
|
|
|
grafana.grafana.dashboard:
|
2022-08-23 09:00:35 +02:00
|
|
|
dashboard: "{{ lookup('ansible.builtin.file', 'dashboard.json') }}"
|
2022-08-09 08:37:47 +02:00
|
|
|
stack_slug: "{{ stack_slug }}"
|
2022-08-11 11:40:32 +02:00
|
|
|
grafana_api_key: "{{ grafana_api_key }}"
|
2022-08-09 08:37:47 +02:00
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Delete dashboard
|
2022-08-11 07:09:04 +02:00
|
|
|
grafana.grafana.dashboard:
|
2022-08-23 09:00:35 +02:00
|
|
|
dashboard: "{{ lookup('ansible.builtin.file', 'dashboard.json') }}"
|
2022-08-09 08:37:47 +02:00
|
|
|
stack_slug: "{{ stack_slug }}"
|
2022-08-11 11:40:32 +02:00
|
|
|
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:
|
2022-10-20 12:53:16 +02:00
|
|
|
description: Dict object containing folder information.
|
2022-08-09 08:37:47 +02:00
|
|
|
returned: On success
|
|
|
|
type: dict
|
|
|
|
contains:
|
|
|
|
id:
|
2022-10-20 12:53:16 +02:00
|
|
|
description: The ID for the dashboard.
|
2022-08-09 08:37:47 +02:00
|
|
|
returned: on success
|
|
|
|
type: int
|
2022-10-20 12:53:16 +02:00
|
|
|
sample: 17
|
2022-08-09 08:37:47 +02:00
|
|
|
slug:
|
2022-10-20 12:53:16 +02:00
|
|
|
description: The slug for the dashboard.
|
2022-08-09 08:37:47 +02:00
|
|
|
returned: state is present and on success
|
|
|
|
type: str
|
2022-10-20 12:53:16 +02:00
|
|
|
sample: ansible-integration-test
|
2022-08-09 08:37:47 +02:00
|
|
|
status:
|
2022-10-20 12:53:16 +02:00
|
|
|
description: The status of the dashboard.
|
2022-08-09 08:37:47 +02:00
|
|
|
returned: state is present and on success
|
|
|
|
type: str
|
2022-10-20 12:53:16 +02:00
|
|
|
sample: success
|
2022-08-09 08:37:47 +02:00
|
|
|
uid:
|
2022-10-20 12:53:16 +02:00
|
|
|
description: The UID for the dashboard.
|
2022-08-09 08:37:47 +02:00
|
|
|
returned: state is present and on success
|
|
|
|
type: str
|
2022-10-20 12:53:16 +02:00
|
|
|
sample: "test1234"
|
2022-08-09 08:37:47 +02:00
|
|
|
url:
|
2022-10-20 12:53:16 +02:00
|
|
|
description: The endpoint for the dashboard.
|
2022-08-09 08:37:47 +02:00
|
|
|
returned: state is present and on success
|
|
|
|
type: str
|
2022-10-20 12:53:16 +02:00
|
|
|
sample: "/d/test1234/ansible-integration-test"
|
2022-08-09 08:37:47 +02:00
|
|
|
version:
|
2022-10-20 12:53:16 +02:00
|
|
|
description: The version of the dashboard.
|
2022-08-09 08:37:47 +02:00
|
|
|
returned: state is present and on success
|
|
|
|
type: int
|
2022-10-20 12:53:16 +02:00
|
|
|
sample: 2
|
2022-08-09 08:37:47 +02:00
|
|
|
message:
|
2022-10-20 12:53:16 +02:00
|
|
|
description: The message returned after the operation on the dashboard.
|
2022-08-09 08:37:47 +02:00
|
|
|
returned: state is absent and on success
|
|
|
|
type: str
|
2022-10-20 12:53:16 +02:00
|
|
|
sample: "Dashboard Ansible Integration Test deleted"
|
2022-08-09 08:37:47 +02:00
|
|
|
title:
|
2022-10-20 12:53:16 +02:00
|
|
|
description: The name of the dashboard.
|
2022-08-09 08:37:47 +02:00
|
|
|
returned: state is absent and on success
|
|
|
|
type: str
|
2022-10-20 12:53:16 +02:00
|
|
|
sample: "Ansible Integration Test"
|
2022-08-09 08:37:47 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2022-08-11 11:40:32 +02:00
|
|
|
try:
|
|
|
|
import requests
|
|
|
|
HAS_REQUESTS = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_REQUESTS = False
|
|
|
|
|
|
|
|
|
|
|
|
__metaclass__ = type
|
2022-08-09 08:37:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
def present_dashboard(module):
|
|
|
|
|
|
|
|
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/dashboards/db'
|
|
|
|
|
2022-08-11 11:40:32 +02:00
|
|
|
result = requests.post(api_url, json=module.params['dashboard'], 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 absent_dashboard(module):
|
|
|
|
if 'uid' not in module.params['dashboard']['dashboard']:
|
|
|
|
return True, False, "UID is not defined in the the Dashboard configuration"
|
|
|
|
|
|
|
|
api_url = api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/dashboards/uid/' + module.params['dashboard']['dashboard']['uid']
|
|
|
|
|
2022-08-11 11:40:32 +02:00
|
|
|
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-10-20 12:53:16 +02:00
|
|
|
|
2022-08-09 08:37:47 +02:00
|
|
|
module_args = dict(
|
|
|
|
dashboard=dict(type='dict', required=True),
|
|
|
|
stack_slug=dict(type='str', required=True),
|
2022-08-11 11:40:32 +02:00
|
|
|
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_dashboard,
|
|
|
|
"absent": absent_dashboard,
|
|
|
|
}
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2022-10-20 12:53:16 +02:00
|
|
|
argument_spec=module_args
|
2022-08-09 08:37:47 +02:00
|
|
|
)
|
|
|
|
|
2022-10-20 12:53:16 +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__':
|
2022-08-11 11:40:32 +02:00
|
|
|
main()
|