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