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

209 lines
6.3 KiB
Python
Raw 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: cloud_stack
2022-08-09 08:37:47 +02:00
author:
- Ishan Jain (@ishanjainn)
version_added: "0.0.1"
short_description: Manage Grafana Cloud stack
description:
- Create and delete Grafana Cloud stacks using Ansible.
requirements: [ "requests >= 1.0.0" ]
2022-08-09 08:37:47 +02:00
options:
name:
description:
2022-08-30 08:46:55 +02:00
- Sets the name of stack. Conventionally matches the URL of the instance. For example, "<stack_slug>.grafana.net".
2022-08-09 08:37:47 +02:00
type: str
required: true
stack_slug:
description:
2022-08-30 08:54:53 +02:00
- Sets the subdomain of the Grafana instance. For example, if slug is <stack_slug>, the instance URL will be `https://<stack_slug>.grafana.net`.
2022-08-09 08:37:47 +02:00
type: str
required: true
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
region:
description:
2022-08-30 08:46:55 +02:00
- Sets the region for the Grafana Cloud stack.
2022-08-09 08:37:47 +02:00
type: str
default: us
choices: [ us, us-azure, eu, au, eu-azure, prod-ap-southeast-0, prod-gb-south-0, prod-eu-west-3]
url:
description:
2022-08-30 08:46:55 +02:00
- If you use a custom domain for the instance, you can provide it here. If not provided, Will be set to `https://<stack_slug>.grafana.net`.
2022-08-09 08:37:47 +02:00
type: str
org_slug:
description:
2022-08-10 12:43:04 +02:00
- Name of the organization under which Cloud stack is created.
2022-08-09 08:37:47 +02:00
type: str
required: true
2022-08-09 08:37:47 +02:00
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 a Grafana Cloud stack
2022-08-11 07:09:04 +02:00
grafana.grafana.cloud_stack:
2022-08-23 09:00:35 +02:00
name: stack_name
stack_slug: stack_name
2022-08-09 08:37:47 +02:00
cloud_api_key: "{{ grafana_cloud_api_key }}"
region: eu
url: https://grafana.company_name.com
2022-08-23 09:00:35 +02:00
org_slug: org_name
2022-08-09 08:37:47 +02:00
state: present
- name: Delete a Grafana Cloud stack
2022-08-11 07:09:04 +02:00
grafana.grafana.cloud_stack:
2022-08-23 09:00:35 +02:00
name: stack_name
slug: stack_name
2022-08-09 08:37:47 +02:00
cloud_api_key: "{{ grafana_cloud_api_key }}"
2022-08-23 09:00:35 +02:00
org_slug: org_name
2022-08-09 08:37:47 +02:00
state: absent
'''
RETURN = r'''
alertmanager_name:
description: Name of the alertmanager instance
returned: always
type: str
alertmanager_url:
description: URL of the alertmanager instance
returned: always
type: str
cluster_slug:
description: Slug for the cluster where the Grafana stack is deployed
returned: always
type: str
id:
description: ID of the Grafana Cloud stack
returned: always
type: int
loki_url:
description: URl for the Loki instance
returned: always
type: str
orgID:
description: ID of the Grafana Cloud organization
returned: always
type: int
prometheus_url:
description: URl for the Prometheus instance
returned: always
2022-08-10 12:17:39 +02:00
type: str
2022-08-09 08:37:47 +02:00
tempo_url:
description: URl for the Tempo instance
returned: always
2022-08-10 12:17:39 +02:00
type: str
2022-08-09 08:37:47 +02:00
url:
description: URL of the Grafana Cloud stack
returned: always
type: str
'''
from ansible.module_utils.basic import AnsibleModule
try:
import requests
HAS_REQUESTS = True
except ImportError:
HAS_REQUESTS = False
__metaclass__ = type
2022-08-09 08:37:47 +02:00
def present_cloud_stack(module):
if not module.params['url']:
module.params['url'] = 'https://' + module.params['stack_slug'] + '.grafana.net'
body = {
'name': module.params['name'],
'slug': module.params['stack_slug'],
'region': module.params['region'],
'url': module.params['url']
}
api_url = 'https://grafana.com/api/instances'
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 in [409, 403] and result.json()['message'] in ["That url is not available", "Hosted instance limit reached"]:
2022-08-09 08:37:47 +02:00
api_url = 'https://grafana.com/api/orgs/' + module.params['org_slug'] + '/instances'
result = requests.get(api_url, headers={"Authorization": 'Bearer ' + module.params['cloud_api_key']})
for stack in result.json()['items']:
if stack['slug'] == module.params['stack_slug']:
return False, False, stack
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}
def absent_cloud_stack(module):
api_url = 'https://grafana.com/api/instances/' + module.params['stack_slug']
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),
stack_slug=dict(type='str', required=True),
cloud_api_key=dict(type='str', required=True, no_log=True),
2022-08-09 08:37:47 +02:00
region=dict(type='str', required=False, default='us',
choices=['us', 'us-azure', 'eu', 'au', 'eu-azure', 'prod-ap-southeast-0', 'prod-gb-south-0',
'prod-eu-west-3']),
url=dict(type='str', required=False),
org_slug=dict(type='str', required=True),
state=dict(type='str', required=False, default='present', choices=['present', 'absent'])
)
choice_map = {
"present": present_cloud_stack,
"absent": absent_cloud_stack,
}
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,
alertmanager_name=result['amInstanceName'],
url=result['url'], id=result['id'],
cluster_slug=result['clusterName'],
orgID=result['orgId'],
loki_url=result['hlInstanceUrl'],
prometheus_url=result['hmInstancePromUrl'],
tempo_url=result['htInstanceUrl'],
alertmanager_url=result['amInstanceUrl'])
2022-08-09 08:37:47 +02:00
else:
module.fail_json(msg=result)
if __name__ == '__main__':
main()