| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2022, Christian Wollinger <@cwollinger> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: alerta_customer |
| 11 |
short_description: Manage customers in Alerta |
| 12 |
version_added: 4.8.0 |
| 13 |
description: |
| 14 |
- Create or delete customers in Alerta with the REST API. |
| 15 |
author: Christian Wollinger (@cwollinger) |
| 16 |
seealso: |
| 17 |
- name: API documentation |
| 18 |
description: Documentation for Alerta API. |
| 19 |
link: https://docs.alerta.io/api/reference.html#customers |
| 20 |
extends_documentation_fragment: |
| 21 |
- community.general._attributes |
| 22 |
attributes: |
| 23 |
check_mode: |
| 24 |
support: full |
| 25 |
diff_mode: |
| 26 |
support: none |
| 27 |
options: |
| 28 |
customer: |
| 29 |
description: |
| 30 |
- Name of the customer. |
| 31 |
required: true |
| 32 |
type: str |
| 33 |
match: |
| 34 |
description: |
| 35 |
- The matching logged in user for the customer. |
| 36 |
required: true |
| 37 |
type: str |
| 38 |
alerta_url: |
| 39 |
description: |
| 40 |
- The Alerta API endpoint. |
| 41 |
required: true |
| 42 |
type: str |
| 43 |
api_username: |
| 44 |
description: |
| 45 |
- The username for the API using basic auth. |
| 46 |
type: str |
| 47 |
api_password: |
| 48 |
description: |
| 49 |
- The password for the API using basic auth. |
| 50 |
type: str |
| 51 |
api_key: |
| 52 |
description: |
| 53 |
- The access token for the API. |
| 54 |
type: str |
| 55 |
state: |
| 56 |
description: |
| 57 |
- Whether the customer should exist or not. |
| 58 |
- Both O(customer) and O(match) identify a customer that should be added or removed. |
| 59 |
type: str |
| 60 |
choices: [absent, present] |
| 61 |
default: present |
| 62 |
""" |
| 63 |
|
| 64 |
EXAMPLES = r""" |
| 65 |
- name: Create customer |
| 66 |
community.general.alerta_customer: |
| 67 |
alerta_url: https://alerta.example.com |
| 68 |
api_username: [email protected] |
| 69 |
api_password: password |
| 70 |
customer: Developer |
| 71 |
match: [email protected] |
| 72 |
|
| 73 |
- name: Delete customer |
| 74 |
community.general.alerta_customer: |
| 75 |
alerta_url: https://alerta.example.com |
| 76 |
api_username: [email protected] |
| 77 |
api_password: password |
| 78 |
customer: Developer |
| 79 |
match: [email protected] |
| 80 |
state: absent |
| 81 |
""" |
| 82 |
|
| 83 |
RETURN = r""" |
| 84 |
msg: |
| 85 |
description: |
| 86 |
- Success or failure message. |
| 87 |
returned: always |
| 88 |
type: str |
| 89 |
sample: Customer customer1 created |
| 90 |
response: |
| 91 |
description: |
| 92 |
- The response from the API. |
| 93 |
returned: always |
| 94 |
type: dict |
| 95 |
""" |
| 96 |
|
| 97 |
from ansible.module_utils.basic import AnsibleModule |
| 98 |
from ansible.module_utils.urls import basic_auth_header, fetch_url |
| 99 |
|
| 100 |
|
| 101 |
class AlertaInterface: |
| 102 |
def __init__(self, module): |
| 103 |
self.module = module |
| 104 |
self.state = module.params["state"] |
| 105 |
self.customer = module.params["customer"] |
| 106 |
self.match = module.params["match"] |
| 107 |
self.alerta_url = module.params["alerta_url"] |
| 108 |
self.headers = {"Content-Type": "application/json"} |
| 109 |
|
| 110 |
if module.params.get("api_key", None): |
| 111 |
self.headers["Authorization"] = f"Key {module.params['api_key']}" |
| 112 |
else: |
| 113 |
self.headers["Authorization"] = basic_auth_header( |
| 114 |
module.params["api_username"], module.params["api_password"] |
| 115 |
) |
| 116 |
|
| 117 |
def send_request(self, url, data=None, method="GET"): |
| 118 |
response, info = fetch_url(self.module, url, data=data, headers=self.headers, method=method) |
| 119 |
|
| 120 |
status_code = info["status"] |
| 121 |
if status_code == 401: |
| 122 |
self.module.fail_json(failed=True, response=info, msg=f"Unauthorized to request '{method}' on '{url}'") |
| 123 |
elif status_code == 403: |
| 124 |
self.module.fail_json(failed=True, response=info, msg=f"Permission Denied for '{method}' on '{url}'") |
| 125 |
elif status_code == 404: |
| 126 |
self.module.fail_json(failed=True, response=info, msg=f"Not found for request '{method}' on '{url}'") |
| 127 |
elif status_code in (200, 201): |
| 128 |
return self.module.from_json(response.read()) |
| 129 |
self.module.fail_json(failed=True, response=info, msg=f"Alerta API error with HTTP {status_code} for {url}") |
| 130 |
|
| 131 |
def get_customers(self): |
| 132 |
url = f"{self.alerta_url}/api/customers" |
| 133 |
response = self.send_request(url) |
| 134 |
pages = response["pages"] |
| 135 |
if pages > 1: |
| 136 |
for page in range(2, pages + 1): |
| 137 |
page_url = f"{url}?page={page}" |
| 138 |
new_results = self.send_request(page_url) |
| 139 |
response.update(new_results) |
| 140 |
return response |
| 141 |
|
| 142 |
def create_customer(self): |
| 143 |
url = f"{self.alerta_url}/api/customer" |
| 144 |
|
| 145 |
payload = { |
| 146 |
"customer": self.customer, |
| 147 |
"match": self.match, |
| 148 |
} |
| 149 |
|
| 150 |
payload = self.module.jsonify(payload) |
| 151 |
response = self.send_request(url, payload, "POST") |
| 152 |
return response |
| 153 |
|
| 154 |
def delete_customer(self, id): |
| 155 |
url = f"{self.alerta_url}/api/customer/{id}" |
| 156 |
|
| 157 |
response = self.send_request(url, None, "DELETE") |
| 158 |
return response |
| 159 |
|
| 160 |
def find_customer_id(self, customer): |
| 161 |
for i in customer["customers"]: |
| 162 |
if self.customer == i["customer"] and self.match == i["match"]: |
| 163 |
return i["id"] |
| 164 |
return None |
| 165 |
|
| 166 |
|
| 167 |
def main(): |
| 168 |
module = AnsibleModule( |
| 169 |
argument_spec=dict( |
| 170 |
state=dict(choices=["present", "absent"], default="present"), |
| 171 |
customer=dict(type="str", required=True), |
| 172 |
match=dict(type="str", required=True), |
| 173 |
alerta_url=dict(type="str", required=True), |
| 174 |
api_username=dict(type="str"), |
| 175 |
api_password=dict(type="str", no_log=True), |
| 176 |
api_key=dict(type="str", no_log=True), |
| 177 |
), |
| 178 |
required_together=[["api_username", "api_password"]], |
| 179 |
mutually_exclusive=[["api_username", "api_key"]], |
| 180 |
supports_check_mode=True, |
| 181 |
) |
| 182 |
|
| 183 |
alerta_iface = AlertaInterface(module) |
| 184 |
|
| 185 |
if alerta_iface.state == "present": |
| 186 |
response = alerta_iface.get_customers() |
| 187 |
if alerta_iface.find_customer_id(response): |
| 188 |
module.exit_json(changed=False, response=response, msg=f"Customer {alerta_iface.customer} already exists") |
| 189 |
else: |
| 190 |
if not module.check_mode: |
| 191 |
response = alerta_iface.create_customer() |
| 192 |
module.exit_json(changed=True, response=response, msg=f"Customer {alerta_iface.customer} created") |
| 193 |
else: |
| 194 |
response = alerta_iface.get_customers() |
| 195 |
id = alerta_iface.find_customer_id(response) |
| 196 |
if id: |
| 197 |
if not module.check_mode: |
| 198 |
alerta_iface.delete_customer(id) |
| 199 |
module.exit_json( |
| 200 |
changed=True, response=response, msg=f"Customer {alerta_iface.customer} with id {id} deleted" |
| 201 |
) |
| 202 |
else: |
| 203 |
module.exit_json(changed=False, response=response, msg=f"Customer {alerta_iface.customer} does not exists") |
| 204 |
|
| 205 |
|
| 206 |
if __name__ == "__main__": |
| 207 |
main() |