1#!/usr/local/bin/python3.8
2# -*- coding: utf-8 -*-
3# Copyright: Ansible Project
4# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6from __future__ import absolute_import, division, print_function
7__metaclass__ = type
8
9
10DOCUMENTATION = '''
11
12module: uptimerobot
13short_description: Pause and start Uptime Robot monitoring
14description:
15    - This module will let you start and pause Uptime Robot Monitoring
16author: "Nate Kingsley (@nate-kingsley)"
17requirements:
18    - Valid Uptime Robot API Key
19options:
20    state:
21        type: str
22        description:
23            - Define whether or not the monitor should be running or paused.
24        required: true
25        choices: [ "started", "paused" ]
26    monitorid:
27        type: str
28        description:
29            - ID of the monitor to check.
30        required: true
31    apikey:
32        type: str
33        description:
34            - Uptime Robot API key.
35        required: true
36notes:
37    - Support for adding and removing monitors and alert contacts has not yet been implemented.
38'''
39
40EXAMPLES = '''
41- name: Pause the monitor with an ID of 12345
42  community.general.uptimerobot:
43    monitorid: 12345
44    apikey: 12345-1234512345
45    state: paused
46
47- name: Start the monitor with an ID of 12345
48  community.general.uptimerobot:
49    monitorid: 12345
50    apikey: 12345-1234512345
51    state: started
52'''
53
54import json
55
56from ansible.module_utils.basic import AnsibleModule
57from ansible.module_utils.six.moves.urllib.parse import urlencode
58from ansible.module_utils.urls import fetch_url
59from ansible.module_utils.common.text.converters import to_text
60
61
62API_BASE = "https://api.uptimerobot.com/"
63
64API_ACTIONS = dict(
65    status='getMonitors?',
66    editMonitor='editMonitor?'
67)
68
69API_FORMAT = 'json'
70API_NOJSONCALLBACK = 1
71CHANGED_STATE = False
72SUPPORTS_CHECK_MODE = False
73
74
75def checkID(module, params):
76
77    data = urlencode(params)
78    full_uri = API_BASE + API_ACTIONS['status'] + data
79    req, info = fetch_url(module, full_uri)
80    result = to_text(req.read())
81    jsonresult = json.loads(result)
82    req.close()
83    return jsonresult
84
85
86def startMonitor(module, params):
87
88    params['monitorStatus'] = 1
89    data = urlencode(params)
90    full_uri = API_BASE + API_ACTIONS['editMonitor'] + data
91    req, info = fetch_url(module, full_uri)
92    result = to_text(req.read())
93    jsonresult = json.loads(result)
94    req.close()
95    return jsonresult['stat']
96
97
98def pauseMonitor(module, params):
99
100    params['monitorStatus'] = 0
101    data = urlencode(params)
102    full_uri = API_BASE + API_ACTIONS['editMonitor'] + data
103    req, info = fetch_url(module, full_uri)
104    result = to_text(req.read())
105    jsonresult = json.loads(result)
106    req.close()
107    return jsonresult['stat']
108
109
110def main():
111
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(
133            msg="failed",
134            result=check_result['message']
135        )
136
137    if module.params['state'] == 'started':
138        monitor_result = startMonitor(module, params)
139    else:
140        monitor_result = pauseMonitor(module, params)
141
142    module.exit_json(
143        msg="success",
144        result=monitor_result
145    )
146
147
148if __name__ == '__main__':
149    main()
150