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

187 lines
5.7 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: datasource
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 Data sources in Grafana Cloud
2022-08-09 08:37:47 +02:00
description:
- Create, Update and delete Data sources using Ansible.
requirements: [ "requests >= 1.0.0" ]
notes:
- Does not support C(check_mode).
2022-08-09 08:37:47 +02:00
options:
2022-08-30 08:50:26 +02:00
dataSource:
2022-08-09 08:37:47 +02:00
description:
2022-08-30 08:46:55 +02:00
- JSON source code for the Data source.
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 data source will be added.
2022-08-09 08:37:47 +02:00
type: str
required: true
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 Data sources
2022-08-11 07:09:04 +02:00
grafana.grafana.datasource:
2022-08-30 08:50:26 +02:00
dataSource: "{{ lookup('ansible.builtin.file', 'datasource.json') }}"
2022-08-09 08:37:47 +02:00
stack_slug: "{{ stack_slug }}"
grafana_api_key: "{{ grafana_api_key }}"
2022-08-09 08:37:47 +02:00
state: present
- name: Delete Data sources
2022-08-11 07:09:04 +02:00
grafana.grafana.datasource:
2022-08-30 08:50:26 +02:00
dataSource: "{{ lookup('ansible.builtin.file', 'datasource.json') }}"
2022-08-09 08:37:47 +02:00
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 Data source information.
2022-08-09 08:37:47 +02:00
returned: On success
type: dict
contains:
datasource:
description: The response body content for the data source configuration.
returned: state is present and on success
type: dict
sample: {
"access": "proxy",
"basicAuth": false,
"basicAuthUser": "",
"database": "db-name",
"id": 20,
"isDefault": false,
"jsonData": {},
"name": "ansible-integration",
"orgId": 1,
"readOnly": false,
"secureJsonFields": {
"password": true
},
"type": "influxdb",
"typeLogoUrl": "",
"uid": "ansibletest",
"url": "https://grafana.github.com/grafana-ansible-collection",
"user": "user",
"version": 1,
"withCredentials": false
}
2022-08-09 08:37:47 +02:00
id:
description: The ID assigned to the data source.
2022-08-09 08:37:47 +02:00
returned: on success
type: int
sample: 20
2022-08-09 08:37:47 +02:00
name:
description: The name of the data source defined in the JSON source code.
2022-08-09 08:37:47 +02:00
returned: state is present and on success
type: str
sample: "ansible-integration"
2022-08-09 08:37:47 +02:00
message:
description: The message returned after the operation on the Data source.
2022-08-09 08:37:47 +02:00
returned: on success
type: str
sample: "Datasource added"
2022-08-09 08:37:47 +02:00
'''
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
try:
import requests
HAS_REQUESTS = True
except ImportError:
HAS_REQUESTS = False
__metaclass__ = type
2022-08-09 08:37:47 +02:00
def present_datasource(module):
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/datasources'
2022-08-30 08:50:26 +02:00
result = requests.post(api_url, json=module.params['dataSource'], 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 == 409:
2022-08-30 08:50:26 +02:00
get_id_url = requests.get('https://' + module.params['stack_slug'] + '.grafana.net/api/datasources/id/' + module.params['dataSource']['name'],
headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
2022-08-09 08:37:47 +02:00
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/datasources/' + str(get_id_url.json()['id'])
2022-08-30 08:50:26 +02:00
result = requests.put(api_url, json=module.params['dataSource'], 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_datasource(module):
2022-08-30 08:50:26 +02:00
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/datasources/' + module.params['dataSource']['name']
2022-08-09 08:37:47 +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-08-09 08:37:47 +02:00
module_args = dict(
2022-08-30 08:50:26 +02:00
dataSource=dict(type='dict', required=True),
2022-08-09 08:37:47 +02:00
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_datasource,
"absent": absent_datasource,
}
module = AnsibleModule(
argument_spec=module_args
2022-08-09 08:37:47 +02:00
)
if not HAS_REQUESTS:
module.fail_json(msg=missing_required_lib('requests'))
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()