Debian Codegraph @

ansible_collections/community/general/plugins/modules/uptimerobot.py

raw
1 #!/usr/bin/python
2 # Copyright Ansible Project
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 module: uptimerobot
10 short_description: Pause and start Uptime Robot monitoring
11 deprecated:
12 removed_in: 15.0.0
13 why: The module uses the Uptime Robot API v1, which was retired and is no longer available.
14 alternative: Use the Uptime Robot API v3 directly with the M(ansible.builtin.uri) module.
15 description:
16 - This module lets you start and pause Uptime Robot Monitoring.
17 author: "Nate Kingsley (@nate-kingsley)"
18 requirements:
19 - Valid Uptime Robot API Key
20 extends_documentation_fragment:
21 - community.general._attributes
22 attributes:
23 check_mode:
24 support: none
25 diff_mode:
26 support: none
27 options:
28 state:
29 type: str
30 description:
31 - Define whether or not the monitor should be running or paused.
32 required: true
33 choices: ["started", "paused"]
34 monitorid:
35 type: str
36 description:
37 - ID of the monitor to check.
38 required: true
39 apikey:
40 type: str
41 description:
42 - Uptime Robot API key.
43 required: true
44 notes:
45 - Support for adding and removing monitors and alert contacts has not yet been implemented.
46 """
47
48 EXAMPLES = r"""
49 - name: Pause the monitor with an ID of 12345
50 community.general.uptimerobot:
51 monitorid: 12345
52 apikey: 12345-1234512345
53 state: paused
54
55 - name: Start the monitor with an ID of 12345
56 community.general.uptimerobot:
57 monitorid: 12345
58 apikey: 12345-1234512345
59 state: started
60 """
61
62 import json
63 from urllib.parse import urlencode
64
65 from ansible.module_utils.basic import AnsibleModule
66 from ansible.module_utils.common.text.converters import to_text
67 from ansible.module_utils.urls import fetch_url
68
69 API_BASE = "https://api.uptimerobot.com/"
70
71 API_ACTIONS = dict(status="getMonitors?", editMonitor="editMonitor?")
72
73 API_FORMAT = "json"
74 API_NOJSONCALLBACK = 1
75 CHANGED_STATE = False
76 SUPPORTS_CHECK_MODE = False
77
78
79 def checkID(module, params):
80 data = urlencode(params)
81 full_uri = API_BASE + API_ACTIONS["status"] + data
82 req, info = fetch_url(module, full_uri)
83 result = to_text(req.read())
84 jsonresult = json.loads(result)
85 req.close()
86 return jsonresult
87
88
89 def startMonitor(module, params):
90 params["monitorStatus"] = 1
91 data = urlencode(params)
92 full_uri = API_BASE + API_ACTIONS["editMonitor"] + data
93 req, info = fetch_url(module, full_uri)
94 result = to_text(req.read())
95 jsonresult = json.loads(result)
96 req.close()
97 return jsonresult["stat"]
98
99
100 def pauseMonitor(module, params):
101 params["monitorStatus"] = 0
102 data = urlencode(params)
103 full_uri = API_BASE + API_ACTIONS["editMonitor"] + data
104 req, info = fetch_url(module, full_uri)
105 result = to_text(req.read())
106 jsonresult = json.loads(result)
107 req.close()
108 return jsonresult["stat"]
109
110
111 def main():
112 module = AnsibleModule(
113 argument_spec=dict(
114 state=dict(required=True, choices=["started", "paused"]),
115 apikey=dict(required=True, no_log=True),
116 monitorid=dict(required=True),
117 ),
118 supports_check_mode=SUPPORTS_CHECK_MODE,
119 )
120
121 params = dict(
122 apiKey=module.params["apikey"],
123 monitors=module.params["monitorid"],
124 monitorID=module.params["monitorid"],
125 format=API_FORMAT,
126 noJsonCallback=API_NOJSONCALLBACK,
127 )
128
129 check_result = checkID(module, params)
130
131 if check_result["stat"] != "ok":
132 module.fail_json(msg="failed", result=check_result["message"])
133
134 if module.params["state"] == "started":
135 monitor_result = startMonitor(module, params)
136 else:
137 monitor_result = pauseMonitor(module, params)
138
139 module.exit_json(msg="success", result=monitor_result)
140
141
142 if __name__ == "__main__":
143 main()