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: pingdom
13short_description: Pause/unpause Pingdom alerts
14description:
15    - This module will let you pause/unpause Pingdom alerts
16author:
17    - "Dylan Silva (@thaumos)"
18    - "Justin Johns (!UNKNOWN)"
19requirements:
20    - "This pingdom python library: https://github.com/mbabineau/pingdom-python"
21options:
22    state:
23        type: str
24        description:
25            - Define whether or not the check should be running or paused.
26        required: true
27        choices: [ "running", "paused", "started", "stopped" ]
28    checkid:
29        type: str
30        description:
31            - Pingdom ID of the check.
32        required: true
33    uid:
34        type: str
35        description:
36            - Pingdom user ID.
37        required: true
38    passwd:
39        type: str
40        description:
41            - Pingdom user password.
42        required: true
43    key:
44        type: str
45        description:
46            - Pingdom API key.
47        required: true
48notes:
49    - This module does not yet have support to add/remove checks.
50'''
51
52EXAMPLES = '''
53- name: Pause the check with the ID of 12345
54  community.general.pingdom:
55    uid: example@example.com
56    passwd: password123
57    key: apipassword123
58    checkid: 12345
59    state: paused
60
61- name: Unpause the check with the ID of 12345
62  community.general.pingdom:
63    uid: example@example.com
64    passwd: password123
65    key: apipassword123
66    checkid: 12345
67    state: running
68'''
69
70import traceback
71
72PINGDOM_IMP_ERR = None
73try:
74    import pingdom
75    HAS_PINGDOM = True
76except Exception:
77    PINGDOM_IMP_ERR = traceback.format_exc()
78    HAS_PINGDOM = False
79
80from ansible.module_utils.basic import AnsibleModule, missing_required_lib
81
82
83def pause(checkid, uid, passwd, key):
84
85    c = pingdom.PingdomConnection(uid, passwd, key)
86    c.modify_check(checkid, paused=True)
87    check = c.get_check(checkid)
88    name = check.name
89    result = check.status
90    # if result != "paused":             # api output buggy - accept raw exception for now
91    #    return (True, name, result)
92    return (False, name, result)
93
94
95def unpause(checkid, uid, passwd, key):
96
97    c = pingdom.PingdomConnection(uid, passwd, key)
98    c.modify_check(checkid, paused=False)
99    check = c.get_check(checkid)
100    name = check.name
101    result = check.status
102    # if result != "up":                 # api output buggy - accept raw exception for now
103    #    return (True, name, result)
104    return (False, name, result)
105
106
107def main():
108
109    module = AnsibleModule(
110        argument_spec=dict(
111            state=dict(required=True, choices=['running', 'paused', 'started', 'stopped']),
112            checkid=dict(required=True),
113            uid=dict(required=True),
114            passwd=dict(required=True, no_log=True),
115            key=dict(required=True, no_log=True),
116        )
117    )
118
119    if not HAS_PINGDOM:
120        module.fail_json(msg=missing_required_lib("pingdom"), exception=PINGDOM_IMP_ERR)
121
122    checkid = module.params['checkid']
123    state = module.params['state']
124    uid = module.params['uid']
125    passwd = module.params['passwd']
126    key = module.params['key']
127
128    if (state == "paused" or state == "stopped"):
129        (rc, name, result) = pause(checkid, uid, passwd, key)
130
131    if (state == "running" or state == "started"):
132        (rc, name, result) = unpause(checkid, uid, passwd, key)
133
134    if rc != 0:
135        module.fail_json(checkid=checkid, name=name, status=result)
136
137    module.exit_json(checkid=checkid, name=name, status=result)
138
139
140if __name__ == '__main__':
141    main()
142