Skip to content

Commit e376805

Browse files
authored
betteruptime_monitor_sla :: init module (#9)
1 parent 0c9c457 commit e376805

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This Ansible collection includes a variety of Ansible content to help automate t
99
| Name | Description |
1010
|--------------------------------------------------------|--------------------------------------------------|
1111
| toucantoco.toucantoco.betteruptime_monitor | Create & manage betteruptime monitors |
12+
| toucantoco.toucantoco.betteruptime_monitor_sla | Retrieve SLA of monitors |
1213
| toucantoco.toucantoco.betteruptime_status_page | Create & manage betteruptime status pages |
1314
| toucantoco.toucantoco.betteruptime_status_page_report | Create & manage betteruptime status page reports |
1415

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# toucantoco.toucantoco.betteruptime_monitor
2+
3+
### Purpose
4+
5+
Retrieve SLA of a monitor
6+
7+
### Parameters
8+
9+
| Parameters | Required | Type | Choices/Default | Comments |
10+
|------------|----------|------|-----------------|-------------------|
11+
| api_key | True | str | | |
12+
| url | True | str | | |
13+
| from | False | str | | format:YYYY-MM-DD |
14+
| to | False | str | | format:YYYY-MM-DD |
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
Fetch SLA for monitors
3+
"""
4+
5+
#!/usr/bin/python
6+
7+
import urllib
8+
9+
import requests
10+
from ansible.module_utils.basic import AnsibleModule
11+
from requests.models import PreparedRequest
12+
13+
from ..module_utils.payload import sanitize_payload
14+
15+
API_MONITORS_BASE_URL = "https://betteruptime.com/api/v2/monitors"
16+
17+
MONITOR_SLA_FIELDS = {
18+
"api_key": {"required": True, "type": "str", "no_log": True},
19+
"url": {"required": True, "type": "str"},
20+
"from": {"required": False, "type": "str"},
21+
"to": {"required": False, "type": "str"},
22+
}
23+
24+
25+
26+
class BetterUptimeMonitorSLA:
27+
def __init__(self, module):
28+
self.module = module
29+
self.payload = module.params
30+
self.headers = {"Authorization": f"Bearer {self.payload.pop('api_key')}"}
31+
32+
self.monitor_url = self.payload.pop('url')
33+
self.monitor_id = None
34+
self.monitor_attributes = None
35+
self.monitor_sla_attributes = None
36+
37+
self.payload = sanitize_payload(self.payload)
38+
39+
def retrieve_monitor_id(self, api_url):
40+
""" Retrieve the id of a monitor if it exists """
41+
response = requests.get(api_url, headers=self.headers)
42+
json_object = response.json()
43+
44+
for item in json_object["data"]:
45+
if item["attributes"] and item["attributes"]["url"] == self.monitor_url:
46+
self.monitor_id = item["id"]
47+
self.monitor_attributes = item["attributes"]
48+
return
49+
50+
if json_object["pagination"]["next"] is not None:
51+
self.retrieve_monitor_id(json_object["pagination"]["next"])
52+
53+
54+
def get_sla(self):
55+
""" Retrieve the SLA"""
56+
req = PreparedRequest()
57+
req.prepare_url(f"{API_MONITORS_BASE_URL}/{self.monitor_id}/sla", self.payload)
58+
59+
response = requests.get(req.url, headers=self.headers)
60+
if response.status_code != 200:
61+
self.module.fail_json(msg=response.json())
62+
63+
self.monitor_sla_attributes = response.json()["data"]["attributes"]
64+
65+
def manage(self):
66+
""" Manage monitor SLA retrieval """
67+
self.retrieve_monitor_id(f"{API_MONITORS_BASE_URL}?url={urllib.parse.quote(self.monitor_url)}")
68+
69+
if self.monitor_id is None:
70+
self.module.fail_json(msg="Monitor no found")
71+
72+
self.get_sla()
73+
result = {**self.monitor_sla_attributes, "monitor_creation_date": self.monitor_attributes["created_at"]}
74+
75+
self.module.exit_json(**result)
76+
77+
78+
79+
def main():
80+
module = AnsibleModule(
81+
argument_spec=MONITOR_SLA_FIELDS,
82+
supports_check_mode=True,
83+
)
84+
85+
monitor_sla_object = BetterUptimeMonitorSLA(module)
86+
87+
if not module.check_mode:
88+
monitor_sla_object.manage()
89+
else:
90+
return module.exit_json(changed=False)
91+
92+
93+
if __name__ == "__main__":
94+
main()

0 commit comments

Comments
 (0)