Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2019, 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_snmp
20 version_added: '1.0.0'
21 short_description: Configure FlashArray SNMP Managers
22 description:
23 - Manage SNMP managers on a Pure Storage FlashArray.
24 - Changing of a named SNMP managers version is not supported.
25 - This module is not idempotent and will always modify an
26 existing SNMP manager due to hidden parameters that cannot
27 be compared to the play parameters.
28 author:
29 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
30 options:
31 name:
32 description:
33 - Name of SNMP Manager
34 required: true
35 type: str
36 state:
37 description:
38 - Create, delete or testSNMP manager
39 type: str
40 default: present
41 choices: [ absent, present, test ]
42 auth_passphrase:
43 type: str
44 description:
45 - SNMPv3 only. Passphrase of 8 - 32 characters.
46 auth_protocol:
47 type: str
48 description:
49 - SNMP v3 only. Hash algorithm to use
50 choices: [ MD5, SHA ]
51 community:
52 type: str
53 description:
54 - SNMP v2c only. Manager community ID. Between 1 and 32 characters long.
55 host:
56 type: str
57 description:
58 - IPv4 or IPv6 address or FQDN to send trap messages to.
59 user:
60 type: str
61 description:
62 - SNMP v3 only. User ID recognized by the specified SNMP manager.
63 Must be between 1 and 32 characters.
64 version:
65 type: str
66 description:
67 - Version of SNMP protocol to use for the manager.
68 choices: [ v2c, v3 ]
69 default: v2c
70 notification:
71 type: str
72 description:
73 - Action to perform on event.
74 default: trap
75 choices: [ inform, trap ]
76 privacy_passphrase:
77 type: str
78 description:
79 - SNMPv3 only. Passphrase to encrypt SNMP messages.
80 Must be between 8 and 63 non-space ASCII characters.
81 privacy_protocol:
82 type: str
83 description:
84 - SNMP v3 only. Encryption protocol to use
85 choices: [ AES, DES ]
86 extends_documentation_fragment:
87 - purestorage.flasharray.purestorage.fa
88 """
89
90 EXAMPLES = r"""
91 - name: Delete existing SNMP manager
92 purestorage.flasharray.purefa_snmp:
93 name: manager1
94 state: absent
95 fa_url: 10.10.10.2
96 api_token: e31060a7-21fc-e277-6240-25983c6c4592
97 - name: Create v2c SNMP manager
98 puretorage.flasharray.purefa_snmp:
99 name: manager1
100 community: public
101 host: 10.21.22.23
102 fa_url: 10.10.10.2
103 api_token: e31060a7-21fc-e277-6240-25983c6c4592
104 - name: Create v3 SNMP manager
105 puretorage.flasharray.purefa_snmp:
106 name: manager2
107 version: v3
108 user: manager
109 auth_protocol: MD5
110 auth_passphrase: password
111 host: 10.21.22.23
112 fa_url: 10.10.10.2
113 api_token: e31060a7-21fc-e277-6240-25983c6c4592
114 - name: Update existing SNMP manager
115 purestorage.flasharray.purefa_snmp:
116 name: manager1
117 community: private
118 fa_url: 10.10.10.2
119 api_token: e31060a7-21fc-e277-6240-25983c6c4592
120 """
121
122 RETURN = r"""
123 """
124
125
126 from ansible.module_utils.basic import AnsibleModule
127 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
128 get_array,
129 purefa_argument_spec,
130 )
131
132 HAS_PURESTORAGE = True
133 try:
134 from pypureclient.flasharray import (
135 SnmpManagerPost,
136 SnmpManagerPatch,
137 SnmpV2c,
138 SnmpV3Patch,
139 SnmpV3Post,
140 )
141 except ImportError:
142 HAS_PURESTORAGE = False
143
144
145 def test_manager(module, array):
146 """Test SNMP manager configuration"""
147 test_response = []
148 response = list(array.get_snmp_managers_test(names=[module.params["name"]]).items)
149 for component in response:
150 if component.enabled:
151 enabled = "true"
152 else:
153 enabled = "false"
154 if component.success:
155 success = "true"
156 else:
157 success = "false"
158 test_response.append(
159 {
160 "component_address": component.component_address,
161 "component_name": component.component_name,
162 "description": component.description,
163 "destination": component.destination,
164 "enabled": enabled,
165 "result_details": getattr(component, "result_details", ""),
166 "success": success,
167 "test_type": component.test_type,
168 "resource_name": component.resource.name,
169 }
170 )
171 module.exit_json(changed=True, test_response=test_response)
172
173
174 def update_manager(module, array):
175 """Update SNMP Manager"""
176 changed = False
177 res = array.get_snmp_managers(names=module.params["name"])
178 if res.status_code != 200:
179 module.fail_json(
180 msg="Failed to get configuration for SNMP manager {0}.".format(
181 module.params["name"]
182 )
183 )
184 else:
185 mgr = list(res.items)[0]
186 if mgr.version != module.params["version"]:
187 module.fail_json(msg="Changing an SNMP managers version is not supported.")
188 elif module.params["version"] == "v2c":
189 changed = True
190 if not module.check_mode:
191 res = array.patch_snmp_managers(
192 names=module.params["name"],
193 snmp_manager=SnmpManagerPatch(
194 version=module.params["version"],
195 v2c=SnmpV2c(community=module.params["community"]),
196 notification=module.params["notification"],
197 host=module.params["host"],
198 ),
199 )
200 if res.status_code != 200:
201 module.fail_json(
202 msg="Failed to update SNMP manager {0}. Error: {1}".format(
203 module.params["name"], res.errors[0].message
204 )
205 )
206 else:
207 if module.params["auth_protocol"] and module.params["privacy_protocol"]:
208 changed = True
209 if not module.check_mode:
210 res = array.patch_snmp_managers(
211 names=module.params["name"],
212 snmp_manager=SnmpManagerPatch(
213 version=module.params["version"],
214 v3=SnmpV3Patch(
215 auth_passphrase=module.params["auth_passphrase"],
216 auth_protocol=module.params["auth_protocol"],
217 privacy_passphrase=module.params["privacy_passphrase"],
218 privacy_protocol=module.params["privacy_protocol"],
219 user=module.params["user"],
220 ),
221 notification=module.params["notification"],
222 host=module.params["host"],
223 ),
224 )
225 if res.status_code != 200:
226 module.fail_json(
227 msg="Failed to update SNMP manager {0}. Error: {1}".format(
228 module.params["name"], res.errors[0].message
229 )
230 )
231 elif module.params["auth_protocol"] and not module.params["privacy_protocol"]:
232 changed = True
233 if not module.check_mode:
234 res = array.patch_snmp_managers(
235 names=module.params["name"],
236 snmp_manager=SnmpManagerPatch(
237 notification=module.params["notification"],
238 version=module.params["version"],
239 host=module.params["host"],
240 v3=SnmpV3Patch(
241 auth_passphrase=module.params["auth_passphrase"],
242 auth_protocol=module.params["auth_protocol"],
243 user=module.params["user"],
244 ),
245 ),
246 )
247 if res.status_code != 200:
248 module.fail_json(
249 msg="Failed to update SNMP manager {0}. Error: {1}".format(
250 module.params["name"], res.errors[0].message
251 )
252 )
253 elif not module.params["auth_protocol"] and module.params["privacy_protocol"]:
254 changed = True
255 if not module.check_mode:
256 res = array.patch_snmp_managers(
257 names=module.params["name"],
258 snmp_manager=SnmpManagerPatch(
259 notification=module.params["notification"],
260 version=module.params["version"],
261 host=module.params["host"],
262 v3=SnmpV3Patch(
263 privacy_passphrase=module.params["privacy_passphrase"],
264 privacy_protocol=module.params["privacy_protocol"],
265 user=module.params["user"],
266 ),
267 ),
268 )
269 if res.status_code != 200:
270 module.fail_json(
271 msg="Failed to update SNMP manager {0}. Error: {1}".format(
272 module.params["name"], res.errors[0].message
273 )
274 )
275 elif (
276 not module.params["auth_protocol"] and not module.params["privacy_protocol"]
277 ):
278 changed = True
279 if not module.check_mode:
280 res = array.patch_snmp_managers(
281 names=module.params["name"],
282 snmp_manager=SnmpManagerPatch(
283 version=module.params["version"],
284 notification=module.params["notification"],
285 host=module.params["host"],
286 v3=SnmpV3Patch(user=module.params["user"]),
287 ),
288 )
289 if res.status_code != 200:
290 module.fail_json(
291 msg="Failed to update SNMP manager {0}. Error: {1}".format(
292 module.params["name"], res.errors[0].name
293 )
294 )
295 else:
296 module.fail_json(
297 msg="Invalid parameters selected in update. Please raise issue in Ansible GitHub"
298 )
299
300 module.exit_json(changed=changed)
301
302
303 def delete_manager(module, array):
304 """Delete SNMP Manager"""
305 changed = True
306 if not module.check_mode:
307 res = array.delete_snmp_managers(names=[module.params["name"]])
308 if res.status_code != 200:
309 module.fail_json(
310 msg="Delete SNMP manager {0} failed. Error: {1}".format(
311 module.params["name"], res.errors[0].message
312 )
313 )
314 module.exit_json(changed=changed)
315
316
317 def create_manager(module, array):
318 """Create SNMP Manager"""
319 changed = True
320 if not module.check_mode:
321 if module.params["version"] == "v2c":
322 res = array.post_snmp_managers(
323 names=module.params["name"],
324 snmp_manager=SnmpManagerPost(
325 version=module.params["version"],
326 notification=module.params["notification"],
327 host=module.params["host"],
328 v2c=SnmpV2c(
329 community=module.params["community"],
330 ),
331 ),
332 )
333 if res.status_code != 200:
334 module.fail_json(
335 msg="Failed to create SNMP manager {0}. Error: {1}".format(
336 module.params["name"], res.errors[0].message
337 )
338 )
339 else:
340 if module.params["auth_protocol"] and module.params["privacy_protocol"]:
341 res = array.post_snmp_managers(
342 names=module.params["name"],
343 snmp_manager=SnmpManagerPost(
344 host=module.params["host"],
345 notification=module.params["notification"],
346 version=module.params["version"],
347 v3=SnmpV3Post(
348 auth_passphrase=module.params["auth_passphrase"],
349 auth_protocol=module.params["auth_protocol"],
350 privacy_passphrase=module.params["privacy_passphrase"],
351 privacy_protocol=module.params["privacy_protocol"],
352 user=module.params["user"],
353 ),
354 ),
355 )
356 if res.status_code != 200:
357 module.fail_json(
358 msg="Failed to create SNMP manager {0}. Error: {1}".format(
359 module.params["name"], res.errors[0].message
360 )
361 )
362 elif (
363 module.params["auth_protocol"] and not module.params["privacy_protocol"]
364 ):
365 res = array.post_snmp_managers(
366 names=module.params["name"],
367 snmp_manager=SnmpManagerPost(
368 version=module.params["version"],
369 notification=module.params["notification"],
370 host=module.params["host"],
371 v3=SnmpV3Post(
372 auth_passphrase=module.params["auth_passphrase"],
373 auth_protocol=module.params["auth_protocol"],
374 user=module.params["user"],
375 ),
376 ),
377 )
378 if res.status_code != 200:
379 module.fail_json(
380 msg="Failed to create SNMP manager {0}. Error: {1}".format(
381 module.params["name"], res.errors[0].message
382 )
383 )
384 elif (
385 not module.params["auth_protocol"] and module.params["privacy_protocol"]
386 ):
387 res = array.post_snmp_managers(
388 names=module.params["name"],
389 host=module.params["host"],
390 snmp_managers=SnmpManagerPost(
391 notification=module.params["notification"],
392 version=module.params["version"],
393 v3=SnmpV3Post(
394 privacy_passphrase=module.params["privacy_passphrase"],
395 privacy_protocol=module.params["privacy_protocol"],
396 user=module.params["user"],
397 ),
398 ),
399 )
400 if res.status_code != 200:
401 module.fail_json(
402 msg="Failed to create SNMP manager {0}. Error: {1}".format(
403 module.params["name"], res.errors[0].message
404 )
405 )
406 elif (
407 not module.params["auth_protocol"]
408 and not module.params["privacy_protocol"]
409 ):
410 res = array.post_snmp_managers(
411 names=module.params["name"],
412 host=module.params["host"],
413 snmp_manager=SnmpManagerPost(
414 notification=module.params["notification"],
415 version=module.params["version"],
416 v3=SnmpV3Post(
417 user=module.params["user"],
418 ),
419 ),
420 )
421 if res.status_code != 200:
422 module.fail_json(
423 msg="Failed to create SNMP manager {0}. Error: {1}".format(
424 module.params["name"], res.errors[0].message
425 )
426 )
427 else:
428 module.fail_json(
429 msg="Invalid parameters selected in create. Please raise issue in Ansible GitHub"
430 )
431 module.exit_json(changed=changed)
432
433
434 def main():
435 argument_spec = purefa_argument_spec()
436 argument_spec.update(
437 dict(
438 name=dict(type="str", required=True),
439 host=dict(type="str"),
440 state=dict(
441 type="str", default="present", choices=["absent", "present", "test"]
442 ),
443 user=dict(type="str"),
444 notification=dict(type="str", choices=["inform", "trap"], default="trap"),
445 auth_passphrase=dict(type="str", no_log=True),
446 auth_protocol=dict(type="str", choices=["MD5", "SHA"]),
447 privacy_passphrase=dict(type="str", no_log=True),
448 privacy_protocol=dict(type="str", choices=["AES", "DES"]),
449 version=dict(type="str", default="v2c", choices=["v2c", "v3"]),
450 community=dict(type="str"),
451 )
452 )
453
454 required_together = [
455 ["auth_passphrase", "auth_protocol"],
456 ["privacy_passphrase", "privacy_protocol"],
457 ]
458
459 module = AnsibleModule(
460 argument_spec,
461 required_together=required_together,
462 supports_check_mode=True,
463 )
464
465 if not HAS_PURESTORAGE:
466 module.fail_json(msg="py-pure-client sdk is required for this module")
467
468 state = module.params["state"]
469 array = get_array(module)
470 mgr_configured = False
471 mgrs = list(array.get_snmp_managers().items)
472 for mgr in mgrs:
473 if mgr.name == module.params["name"]:
474 mgr_configured = True
475 break
476 if not module.params["host"]:
477 module.fail_json(msg="The following parameter is required: host")
478 if module.params["version"] == "v3":
479 if not module.params["user"]:
480 module.fail_json(msg="version is v3 but the following is missing: user")
481 if module.params["auth_passphrase"] and (
482 8 > len(module.params["auth_passphrase"]) > 32
483 ):
484 module.fail_json(msg="auth_password must be between 8 and 32 characters")
485 if (
486 module.params["privacy_passphrase"]
487 and 8 > len(module.params["privacy_passphrase"]) > 63
488 ):
489 module.fail_json(msg="privacy_password must be between 8 and 63 characters")
490 else:
491 if not module.params["community"]:
492 module.fail_json(
493 msg="version is v2c but the following is missing: community"
494 )
495 if state == "absent" and mgr_configured:
496 delete_manager(module, array)
497 elif mgr_configured and state == "present":
498 update_manager(module, array)
499 elif mgr_configured and state == "test":
500 test_manager(module, array)
501 elif not mgr_configured and state == "present":
502 create_manager(module, array)
503 else:
504 module.exit_json(changed=False)
505
506
507 if __name__ == "__main__":
508 main()