Debian Codegraph @

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