-
Notifications
You must be signed in to change notification settings - Fork 92
/
datasource.py
205 lines (173 loc) · 6.15 KB
/
datasource.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 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)
DOCUMENTATION = '''
---
module: datasource
author:
- Ishan Jain (@ishanjainn)
version_added: "0.0.1"
short_description: Manage Data sources in Grafana
description:
- Create, Update and delete Data sources using Ansible.
requirements: [ "requests >= 1.0.0" ]
notes:
- Does not support C(check_mode).
- Does not support C(Idempotency).
options:
dataSource:
description:
- JSON source code for the Data source.
type: dict
required: true
grafana_url:
description:
- URL of the Grafana instance.
type: str
required: true
grafana_api_key:
description:
- Grafana API Key to authenticate with Grafana Cloud.
type: str
required : true
state:
description:
- State for the Grafana Datasource.
choices: [ present, absent ]
default: present
type: str
'''
EXAMPLES = '''
- name: Create/Update Data sources
grafana.grafana.datasource:
dataSource: |
{
"name": "Prometheus",
"type": "prometheus",
"access": "proxy",
"url": "http://localhost:9090",
"jsonData": {
"httpMethod": "POST",
"manageAlerts": true,
"prometheusType": "Prometheus",
"cacheLevel": "High"
}
}
grafana_url: "{{ grafana_url }}"
grafana_api_key: "{{ grafana_api_key }}"
state: present
- name: Delete Data sources
grafana.grafana.datasource:
dataSource: "{{ lookup('ansible.builtin.file', 'datasource.json') }}"
grafana_url: "{{ grafana_url }}"
grafana_api_key: "{{ grafana_api_key }}"
state: absent
'''
RETURN = r'''
output:
description: Dict object containing Data source information.
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
}
id:
description: The ID assigned to the data source.
returned: on success
type: int
sample: 20
name:
description: The name of the data source defined in the JSON source code.
returned: state is present and on success
type: str
sample: "ansible-integration"
message:
description: The message returned after the operation on the Data source.
returned: on success
type: str
sample: "Datasource added"
'''
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
try:
import requests
HAS_REQUESTS = True
except ImportError:
HAS_REQUESTS = False
__metaclass__ = type
def present_datasource(module):
if module.params['grafana_url'][-1] == '/':
module.params['grafana_url'] = module.params['grafana_url'][:-1]
api_url = module.params['grafana_url'] + '/api/datasources'
result = requests.post(api_url, json=module.params['dataSource'], headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
if result.status_code == 200:
return False, True, result.json()
elif result.status_code == 409:
get_id_url = requests.get(module.params['grafana_url'] + '/api/datasources/id/' + module.params['dataSource']['name'],
headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
api_url = module.params['grafana_url'] + '/api/datasources/' + str(get_id_url.json()['id'])
result = requests.put(api_url, json=module.params['dataSource'], headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
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):
if module.params['grafana_url'][-1] == '/':
module.params['grafana_url'] = module.params['grafana_url'][:-1]
api_url = module.params['grafana_url'] + '/api/datasources/name/' + module.params['dataSource']['name']
result = requests.delete(api_url, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
if result.status_code == 200:
return False, True, {"status": result.status_code, 'response': result.json()['message']}
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}
def main():
module_args = dict(
dataSource=dict(type='dict', required=True),
grafana_url=dict(type='str', required=True),
grafana_api_key=dict(type='str', required=True, no_log=True),
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
)
if not HAS_REQUESTS:
module.fail_json(msg=missing_required_lib('requests'))
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()