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: cloud_api_key
|
2022-08-09 08:37:47 +02:00
|
|
|
author:
|
|
|
|
- Ishan Jain (@ishanjainn)
|
|
|
|
version_added: "0.0.1"
|
|
|
|
short_description: Manage Grafana Cloud API keys
|
|
|
|
description:
|
|
|
|
- Create and delete Grafana Cloud API keys 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:
|
|
|
|
name:
|
|
|
|
description:
|
2022-08-30 08:46:55 +02:00
|
|
|
- Sets the name of the Grafana Cloud API key.
|
2022-08-09 08:37:47 +02:00
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
role:
|
|
|
|
description:
|
2022-10-20 12:53:16 +02:00
|
|
|
- Sets the role to be associated with the Cloud API key.
|
2022-08-09 08:37:47 +02:00
|
|
|
type: str
|
|
|
|
required: true
|
2022-08-11 11:40:32 +02:00
|
|
|
choices: [Admin, Viewer, Editor, MetricsPublisher]
|
2022-08-09 08:37:47 +02:00
|
|
|
org_slug:
|
|
|
|
description:
|
2022-08-30 08:46:55 +02:00
|
|
|
- Name of the Grafana Cloud organization in which Cloud API key will be created.
|
2022-08-09 08:37:47 +02:00
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
existing_cloud_api_key:
|
|
|
|
description:
|
2022-08-30 08:46:55 +02:00
|
|
|
- Cloud API Key to authenticate with Grafana Cloud.
|
2022-08-09 08:37:47 +02:00
|
|
|
type: str
|
|
|
|
required : true
|
|
|
|
fail_if_already_created:
|
|
|
|
description:
|
2022-10-20 12:53:16 +02:00
|
|
|
- If set to C(true), the task will fail if the API key with same name already exists in the Organization.
|
2022-08-09 08:37:47 +02:00
|
|
|
type: bool
|
|
|
|
default: 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
|
|
|
type: str
|
|
|
|
default: present
|
|
|
|
choices: [ present, absent ]
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Create Grafana Cloud API key
|
2022-08-11 07:09:04 +02:00
|
|
|
grafana.grafana.cloud_api_key:
|
2022-08-09 08:37:47 +02:00
|
|
|
name: key_name
|
|
|
|
role: Admin
|
|
|
|
org_slug: "{{ org_slug }}"
|
|
|
|
existing_cloud_api_key: "{{ grafana_cloud_api_key }}"
|
|
|
|
fail_if_already_created: False
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Delete Grafana Cloud API key
|
2022-08-11 07:09:04 +02:00
|
|
|
grafana.grafana.cloud_api_key:
|
2022-08-09 08:37:47 +02:00
|
|
|
name: key_name
|
|
|
|
org_slug: "{{ org_slug }}"
|
|
|
|
existing_cloud_api_key: "{{ grafana_cloud_api_key }}"
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
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_api_key(module):
|
|
|
|
body = {
|
|
|
|
'name': module.params['name'],
|
|
|
|
'role': module.params['role']
|
|
|
|
}
|
|
|
|
|
|
|
|
api_url = 'https://grafana.com/api/orgs/' + module.params['org_slug'] + '/api-keys'
|
|
|
|
|
|
|
|
result = requests.post(api_url, json=body,
|
|
|
|
headers={"Authorization": 'Bearer ' + module.params['existing_cloud_api_key']})
|
|
|
|
|
|
|
|
if result.status_code == 200:
|
|
|
|
return False, True, result.json()
|
|
|
|
elif result.status_code == 409:
|
|
|
|
return module.params['fail_if_already_created'], False, "A Cloud API key with the same name already exists"
|
|
|
|
else:
|
|
|
|
return True, False, {"status": result.status_code, 'response': result.json()['message']}
|
|
|
|
|
|
|
|
|
|
|
|
def absent_cloud_api_key(module):
|
|
|
|
api_url = 'https://grafana.com/api/orgs/' + module.params['org_slug'] + '/api-keys/' + module.params['name']
|
|
|
|
|
|
|
|
result = requests.delete(api_url, headers={"Authorization": 'Bearer ' + module.params['existing_cloud_api_key']})
|
|
|
|
|
|
|
|
if result.status_code == 200:
|
|
|
|
return False, True, "Cloud API key is deleted"
|
|
|
|
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(
|
|
|
|
name=dict(type='str', required=True),
|
|
|
|
role=dict(type='str', required=True, choices=['Admin', 'Viewer', 'Editor', 'MetricsPublisher']),
|
|
|
|
org_slug=dict(type='str', required=True),
|
2022-08-11 11:40:32 +02:00
|
|
|
existing_cloud_api_key=dict(type='str', required=True, no_log=True),
|
2022-08-09 08:37:47 +02:00
|
|
|
fail_if_already_created=dict(type='bool', required=False, default='True'),
|
|
|
|
state=dict(type='str', required=False, default='present', choices=['present', 'absent'])
|
|
|
|
)
|
|
|
|
|
|
|
|
choice_map = {
|
|
|
|
"present": present_cloud_api_key,
|
|
|
|
"absent": absent_cloud_api_key,
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|