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: alert_contact_point
|
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 Alerting Contact points in Grafana Cloud
|
2022-08-09 08:37:47 +02:00
|
|
|
description:
|
|
|
|
- Create, Update and delete Contact points 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 contact point.
|
2022-08-09 08:37:47 +02:00
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
uid:
|
|
|
|
description:
|
2022-08-10 12:43:04 +02:00
|
|
|
- Sets the UID of the Contact point.
|
2022-08-09 08:37:47 +02:00
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
type:
|
|
|
|
description:
|
2022-08-30 08:46:55 +02:00
|
|
|
- Sets Contact point type.
|
2022-08-09 08:37:47 +02:00
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
settings:
|
|
|
|
description:
|
2022-08-30 08:46:55 +02:00
|
|
|
- Sets Contact point settings.
|
2022-08-09 08:37:47 +02:00
|
|
|
type: dict
|
|
|
|
required: true
|
2022-08-30 08:46:55 +02:00
|
|
|
disableResolveMessage:
|
2022-08-09 08:37:47 +02:00
|
|
|
description:
|
2022-10-20 12:53:16 +02:00
|
|
|
- When set to C(true), Disables the resolve message [OK] that is sent when alerting state returns to C(false).
|
2022-08-09 08:37:47 +02:00
|
|
|
type: bool
|
|
|
|
default: false
|
|
|
|
grafana_api_key:
|
|
|
|
description:
|
|
|
|
- Grafana API Key used to authenticate with Grafana.
|
|
|
|
type: str
|
|
|
|
required : true
|
2022-08-11 11:40:32 +02:00
|
|
|
stack_slug:
|
|
|
|
description:
|
2022-08-30 08:46:55 +02:00
|
|
|
- Name of the Grafana Cloud stack to which the contact points will be added.
|
2022-08-11 11:40:32 +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
|
|
|
choices: [ present, absent ]
|
|
|
|
type: str
|
|
|
|
default: present
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Create/Update Alerting contact point
|
2022-08-11 07:09:04 +02:00
|
|
|
grafana.grafana.alert_contact_point:
|
2022-08-09 08:37:47 +02:00
|
|
|
name: ops-email
|
|
|
|
uid: opsemail
|
|
|
|
type: email
|
|
|
|
settings: {
|
|
|
|
addresses: "ops@mydomain.com,devs@mydomain.com"
|
|
|
|
}
|
|
|
|
stack_slug: "{{ stack_slug }}"
|
|
|
|
grafana_api_key: "{{ grafana_api_key }}"
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Delete Alerting contact point
|
2022-08-11 07:09:04 +02:00
|
|
|
grafana.grafana.alert_contact_point:
|
2022-08-09 08:37:47 +02:00
|
|
|
name: ops-email
|
|
|
|
uid: opsemail
|
|
|
|
type: email
|
|
|
|
settings: {
|
|
|
|
addresses: "ops@mydomain.com,devs@mydomain.com"
|
|
|
|
}
|
|
|
|
stack_slug: "{{ stack_slug }}"
|
|
|
|
grafana_api_key: "{{ grafana_api_key }}"
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = r'''
|
2022-08-10 12:43:04 +02:00
|
|
|
output:
|
2022-10-20 12:53:16 +02:00
|
|
|
description: Dict object containing Contact point information information.
|
2022-08-09 08:37:47 +02:00
|
|
|
returned: On success
|
|
|
|
type: dict
|
|
|
|
contains:
|
|
|
|
disableResolveMessage:
|
2022-10-20 12:53:16 +02:00
|
|
|
description: When set to True, Disables the resolve message [OK] that is sent when alerting state returns to false.
|
2022-08-09 08:37:47 +02:00
|
|
|
returned: state is present and on success
|
|
|
|
type: bool
|
2022-10-20 12:53:16 +02:00
|
|
|
sample: false
|
2022-08-09 08:37:47 +02:00
|
|
|
name:
|
2022-10-20 12:53:16 +02:00
|
|
|
description: The name for the contact point.
|
2022-08-09 08:37:47 +02:00
|
|
|
returned: state is present and on success
|
|
|
|
type: str
|
2022-10-20 12:53:16 +02:00
|
|
|
sample: ops-email
|
2022-08-09 08:37:47 +02:00
|
|
|
settings:
|
2022-10-20 12:53:16 +02:00
|
|
|
description: Contains contact point settings.
|
2022-08-09 08:37:47 +02:00
|
|
|
returned: state is present and on success
|
|
|
|
type: dict
|
2022-10-20 12:53:16 +02:00
|
|
|
sample: {
|
|
|
|
addresses: "ops@mydomain.com,devs@mydomain.com"
|
|
|
|
}
|
2022-08-09 08:37:47 +02:00
|
|
|
uid:
|
2022-10-20 12:53:16 +02:00
|
|
|
description: The UID for the contact point.
|
2022-08-09 08:37:47 +02:00
|
|
|
returned: state is present and on success
|
|
|
|
type: str
|
2022-10-20 12:53:16 +02:00
|
|
|
sample: opsemail
|
2022-08-09 08:37:47 +02:00
|
|
|
type:
|
2022-10-20 12:53:16 +02:00
|
|
|
description: The type of contact point.
|
2022-08-09 08:37:47 +02:00
|
|
|
returned: state is present and on success
|
|
|
|
type: str
|
2022-10-20 12:53:16 +02:00
|
|
|
sample: email
|
2022-08-09 08:37:47 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
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_alert_contact_point(module):
|
|
|
|
body = {
|
|
|
|
'Name': module.params['name'],
|
|
|
|
'UID': module.params['uid'],
|
|
|
|
'type': module.params['type'],
|
|
|
|
'settings': module.params['settings'],
|
2022-08-30 08:46:55 +02:00
|
|
|
'DisableResolveMessage': module.params['disableResolveMessage']
|
2022-08-09 08:37:47 +02:00
|
|
|
}
|
|
|
|
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/contact-points'
|
|
|
|
|
|
|
|
result = requests.post(api_url, json=body, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
|
|
|
|
|
|
|
|
if result.status_code == 202:
|
|
|
|
return False, True, result.json()
|
|
|
|
elif result.status_code == 500:
|
|
|
|
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/contact-points/' + \
|
|
|
|
module.params['uid']
|
|
|
|
|
|
|
|
result = requests.put(api_url, json=body, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
|
|
|
|
|
|
|
|
if result.status_code == 202:
|
|
|
|
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/contact-points'
|
|
|
|
|
|
|
|
result = requests.get(api_url, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
|
|
|
|
|
|
|
|
for contact_points in result.json():
|
|
|
|
if contact_points['uid'] == module.params['uid']:
|
|
|
|
return False, True, contact_points
|
|
|
|
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_alert_contact_point(module):
|
|
|
|
already_exists = False
|
|
|
|
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/contact-points'
|
|
|
|
|
|
|
|
result = requests.get(api_url, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
|
|
|
|
|
|
|
|
for contact_points in result.json():
|
|
|
|
if contact_points['uid'] == module.params['uid']:
|
|
|
|
already_exists = True
|
|
|
|
if already_exists:
|
|
|
|
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/contact-points/' + \
|
|
|
|
module.params['uid']
|
|
|
|
|
|
|
|
result = requests.delete(api_url, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
|
|
|
|
|
|
|
|
if result.status_code == 202:
|
|
|
|
return False, True, result.json()
|
|
|
|
else:
|
|
|
|
return True, False, {"status": result.status_code, 'response': result.json()['message']}
|
|
|
|
else:
|
|
|
|
return True, False, "Alert Contact point does not exist"
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module_args = dict(
|
|
|
|
name=dict(type='str', required=True),
|
|
|
|
uid=dict(type='str', required=True),
|
|
|
|
type=dict(type='str', required=True),
|
|
|
|
settings=dict(type='dict', required=True),
|
2022-08-30 08:46:55 +02:00
|
|
|
disableResolveMessage=dict(type='bool', required=False, default=False),
|
2022-08-09 08:37:47 +02:00
|
|
|
stack_slug=dict(type='str', required=True),
|
2022-08-11 11:40:32 +02:00
|
|
|
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_alert_contact_point,
|
|
|
|
"absent": absent_alert_contact_point,
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|