Debian Codegraph @

ansible_collections/purestorage/flasharray/plugins/modules/purefa_alert.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # 2018, Simon Dodsley ([email protected])
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: purefa_alert
20 version_added: '1.0.0'
21 short_description: Configure Pure Storage FlashArray alert email settings
22 description:
23 - Configure alert email configuration for Pure Storage FlashArrays.
24 author:
25 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
26 options:
27 state:
28 type: str
29 description:
30 - Create, delete or test alert email
31 default: present
32 choices: [ absent, present, test ]
33 address:
34 type: str
35 description:
36 - Email address (valid format required)
37 required: true
38 enabled:
39 type: bool
40 default: true
41 description:
42 - Set specified email address to be enabled or disabled
43 extends_documentation_fragment:
44 - purestorage.flasharray.purestorage.fa
45 """
46
47 EXAMPLES = r"""
48 - name: Add new email recipient and enable, or enable existing email
49 purestorage.flasharray.purefa_alert:
50 address: "[email protected]"
51 enabled: true
52 state: present
53 fa_url: 10.10.10.2
54 api_token: e31060a7-21fc-e277-6240-25983c6c4592
55 - name: Delete existing email recipient
56 purestorage.flasharray.purefa_alert:
57 state: absent
58 address: "[email protected]"
59 fa_url: 10.10.10.2
60 api_token: e31060a7-21fc-e277-6240-25983c6c4592
61 """
62
63 RETURN = r"""
64 """
65
66 import re
67 from ansible.module_utils.basic import AnsibleModule
68 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
69 get_array,
70 purefa_argument_spec,
71 )
72
73 HAS_PURESTORAGE = True
74 try:
75 from pypureclient.flasharray import AlertWatcherPost, AlertWatcherPatch
76 except ImportError:
77 HAS_PURESTORAGE = False
78
79
80 def test_alert(module, array):
81 """Test alert watchers"""
82 test_response = []
83 response = list(
84 array.get_alert_watchers_test(names=[module.params["address"]]).items
85 )
86 for component in response:
87 if component.enabled:
88 enabled = "true"
89 else:
90 enabled = "false"
91 if component.success:
92 success = "true"
93 else:
94 success = "false"
95 test_response.append(
96 {
97 "component_address": component.component_address,
98 "component_name": component.component_name,
99 "description": component.description,
100 "destination": component.destination,
101 "enabled": enabled,
102 "result_details": getattr(component, "result_details", ""),
103 "success": success,
104 "test_type": component.test_type,
105 "resource_name": component.resource.name,
106 }
107 )
108 module.exit_json(changed=True, test_response=test_response)
109
110
111 def create_alert(module, array):
112 """Create Alert Email"""
113 changed = True
114 if not module.check_mode:
115 res = array.post_alert_watchers(
116 names=[module.params["address"]],
117 alert_watcher=AlertWatcherPost(enabled=module.params["enabled"]),
118 )
119 if res.status_code != 200:
120 module.fail_json(
121 msg="Failed to create alert email: {0}. Error: {1}".format(
122 module.params["address"], res.errors[0].message
123 )
124 )
125 module.exit_json(changed=changed)
126
127
128 def update_alert(module, array, enabled):
129 """Update Alert Email State"""
130 changed = False
131 if enabled != module.params["enabled"]:
132 changed = True
133 if not module.check_mode:
134 res = array.patch_alert_watchers(
135 names=[module.params["address"]],
136 alert_watcher=AlertWatcherPatch(enabled=module.params["enabled"]),
137 )
138 if res.status_code != 200:
139 module.fail_json(
140 msg="Failed to change alert email state: {0}. Error: {1}".format(
141 module.params["address"],
142 res.errors[0].message,
143 )
144 )
145 module.exit_json(changed=changed)
146
147
148 def delete_alert(module, array):
149 """Delete Alert Email"""
150 changed = True
151 if module.params["address"] == "[email protected]":
152 module.fail_json(
153 msg="Built-in address {0} cannot be deleted.".format(
154 module.params["address"]
155 )
156 )
157 if not module.check_mode:
158 res = array.delete_alert_watchers(names=[module.params["address"]])
159 if res.status_code != 200:
160 module.fail_json(
161 msg="Failed to delete alert email: {0}. Error: {1}".format(
162 module.params["address"], res.errors[0].message
163 )
164 )
165 module.exit_json(changed=changed)
166
167
168 def main():
169 argument_spec = purefa_argument_spec()
170 argument_spec.update(
171 dict(
172 address=dict(type="str", required=True),
173 enabled=dict(type="bool", default=True),
174 state=dict(
175 type="str", default="present", choices=["absent", "present", "test"]
176 ),
177 )
178 )
179
180 module = AnsibleModule(argument_spec, supports_check_mode=True)
181 if not HAS_PURESTORAGE:
182 module.fail_json(msg="py-pure-client sdk is required for this module")
183
184 pattern = re.compile(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")
185 if not pattern.match(module.params["address"]):
186 module.fail_json(msg="Valid email address not provided.")
187
188 array = get_array(module)
189
190 exists = False
191 res = array.get_alert_watchers()
192 if res.status_code != 200:
193 module.fail_json(
194 msg="Failed to get existing email list. Error: {0}".format(
195 res.errors[0].message
196 )
197 )
198 else:
199 watchers = list(res.items)
200 for watcher in watchers:
201 if watcher.name == module.params["address"]:
202 exists = True
203 enabled = watcher.enabled
204 break
205 if module.params["state"] == "present" and not exists:
206 create_alert(module, array)
207 elif module.params["state"] == "present" and exists:
208 update_alert(module, array, enabled)
209 elif module.params["state"] == "absent" and exists:
210 delete_alert(module, array)
211 elif module.params["state"] == "test":
212 test_alert(module, array)
213
214 module.exit_json(changed=False)
215
216
217 if __name__ == "__main__":
218 main()