2022-08-09 08:37:47 +02:00
|
|
|
#!/usr/bin/python
|
2022-08-11 11:40:32 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright: (c) 2021, Rainer Leber <rainerleber@gmail.com> <rainer.leber@sva.de>
|
|
|
|
# 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: cloud_plugin
|
2022-08-09 08:37:47 +02:00
|
|
|
author:
|
|
|
|
- Ishan Jain (@ishanjainn)
|
|
|
|
version_added: "0.0.1"
|
|
|
|
short_description: Manage Grafana Cloud Plugins
|
|
|
|
description:
|
|
|
|
- Create, Update and delete Grafana Cloud stacks using Ansible.
|
2022-08-11 11:40:32 +02:00
|
|
|
requirements: [ "requests >= 1.0.0" ]
|
2022-08-09 08:37:47 +02:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name of the plugin, e.g. grafana-github-datasource .
|
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
version:
|
|
|
|
description:
|
|
|
|
- Version of the plugin to install. Defaults to latest.
|
|
|
|
type: str
|
|
|
|
default: latest
|
|
|
|
stack_slug:
|
|
|
|
description:
|
|
|
|
- Name of the Grafana Cloud stack to which the plugin will be added
|
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
cloud_api_key:
|
|
|
|
description:
|
|
|
|
- CLoud API Key to authenticate with Grafana Cloud.
|
|
|
|
type: str
|
|
|
|
required : true
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- State for the Grafana CLoud stack.
|
|
|
|
type: str
|
|
|
|
default: present
|
|
|
|
choices: [ present, absent ]
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Create/Update a plugin
|
2022-08-11 07:09:04 +02:00
|
|
|
grafana.grafana.cloud_plugin:
|
2022-08-09 08:37:47 +02:00
|
|
|
name: grafana-github-datasource
|
|
|
|
version: 1.0.14
|
|
|
|
stack_slug: "{{ stack_slug }}"
|
|
|
|
cloud_api_key: "{{ grafana_cloud_api_key }}"
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Delete a Grafana Cloud stack
|
2022-08-11 07:09:04 +02:00
|
|
|
grafana.grafana.cloud_plugin:
|
2022-08-09 08:37:47 +02:00
|
|
|
name: grafana-github-datasource
|
|
|
|
stack_slug: "{{ stack_slug }}"
|
|
|
|
cloud_api_key: "{{ grafana_cloud_api_key }}"
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = r'''
|
|
|
|
current_version:
|
|
|
|
description: Current version of the plugin
|
|
|
|
returned: On success
|
|
|
|
type: str
|
|
|
|
latest_version:
|
|
|
|
description: Latest version available for the plugin
|
|
|
|
returned: On success
|
|
|
|
type: str
|
|
|
|
pluginId:
|
|
|
|
description: Id for the Plugin
|
|
|
|
returned: On success
|
|
|
|
type: int
|
|
|
|
pluginName:
|
|
|
|
description: Name of the plugin
|
|
|
|
returned: On success
|
|
|
|
type: str
|
|
|
|
pluginSlug:
|
|
|
|
description: Slug for the Plugin
|
|
|
|
returned: On success
|
|
|
|
type: str
|
|
|
|
'''
|
|
|
|
|
|
|
|
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_cloud_plugin(module):
|
|
|
|
body = {
|
|
|
|
'plugin': module.params['name'],
|
|
|
|
'version': module.params['version']
|
|
|
|
}
|
|
|
|
|
|
|
|
api_url = 'https://grafana.com/api/instances/' + module.params['stack_slug'] + '/plugins'
|
|
|
|
|
|
|
|
result = requests.post(api_url, json=body, headers={"Authorization": 'Bearer ' + module.params['cloud_api_key']})
|
|
|
|
|
|
|
|
if result.status_code == 200:
|
|
|
|
return False, True, result.json()
|
|
|
|
elif result.status_code == 409:
|
|
|
|
api_url = 'https://grafana.com/api/instances/' + module.params['stack_slug'] + '/plugins/' + module.params[
|
|
|
|
'name']
|
|
|
|
result = requests.post(api_url, json={'version': module.params['version']},
|
|
|
|
headers={"Authorization": 'Bearer ' + module.params['cloud_api_key']})
|
|
|
|
|
|
|
|
return False, True, result.json()
|
|
|
|
else:
|
|
|
|
return True, False, {"status": result.status_code, 'response': result.json()['message']}
|
|
|
|
|
|
|
|
|
|
|
|
def absent_cloud_plugin(module):
|
|
|
|
api_url = 'https://grafana.com/api/instances/' + module.params['stack_slug'] + '/plugins/' + module.params['name']
|
|
|
|
|
|
|
|
result = requests.delete(api_url, headers={"Authorization": 'Bearer ' + module.params['cloud_api_key']})
|
|
|
|
|
|
|
|
if result.status_code == 200:
|
|
|
|
return False, True, result.json()
|
|
|
|
else:
|
|
|
|
return True, False, {"status": result.status_code, 'response': result.json()['message']}
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module_args = dict(
|
|
|
|
name=dict(type='str', required=True),
|
|
|
|
version=dict(type='str', required=False, default='latest'),
|
|
|
|
stack_slug=dict(type='str', required=True),
|
2022-08-11 11:40:32 +02:00
|
|
|
cloud_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_cloud_plugin,
|
|
|
|
"absent": absent_cloud_plugin,
|
|
|
|
}
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=module_args,
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
is_error, has_changed, result = choice_map.get(
|
|
|
|
module.params['state'])(module)
|
|
|
|
|
|
|
|
if not is_error:
|
2022-08-10 12:43:04 +02:00
|
|
|
module.exit_json(changed=has_changed,
|
|
|
|
pluginId=result['pluginId'],
|
|
|
|
pluginName=result['pluginName'],
|
|
|
|
pluginSlug=result['pluginSlug'],
|
|
|
|
current_version=result['version'],
|
|
|
|
latest_version=result['latestVersion'])
|
2022-08-09 08:37:47 +02:00
|
|
|
else:
|
|
|
|
module.fail_json(msg=result)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-08-11 11:40:32 +02:00
|
|
|
main()
|