Debian Codegraph @

ansible_collections/dellemc/powerflex/plugins/modules/mdm_cluster.py

raw symbols SCIP
1 #!/usr/bin/python
2
3 # Copyright: (c) 2022, Dell Technologies
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 """ Ansible module for managing MDM Cluster on PowerFlex"""
7
8 from __future__ import (absolute_import, division, print_function)
9
10 __metaclass__ = type
11
12 DOCUMENTATION = r'''
13 module: mdm_cluster
14 version_added: '1.3.0'
15 short_description: Manage MDM cluster on Dell PowerFlex
16 description:
17 - Managing MDM cluster and MDMs on PowerFlex storage system includes
18 adding/removing standby MDM, modify MDM name and virtual interface.
19 - It also includes getting details of MDM cluster, modify MDM cluster
20 ownership, cluster mode, and performance profile.
21 author:
22 - Bhavneet Sharma (@sharmb5) <[email protected]>
23 extends_documentation_fragment:
24 - dellemc.powerflex.powerflex
25 options:
26 mdm_name:
27 description:
28 - The name of the MDM. It is unique across the PowerFlex array.
29 - Mutually exclusive with I(mdm_id).
30 - If mdm_name passed in add standby operation, then same name will be
31 assigned to the new standby mdm.
32 type: str
33 mdm_id:
34 description:
35 - The ID of the MDM.
36 - Mutually exclusive with I(mdm_name).
37 type: str
38 mdm_new_name:
39 description:
40 - To rename the MDM.
41 type: str
42 standby_mdm:
43 description:
44 - Specifies add standby MDM parameters.
45 type: dict
46 suboptions:
47 mdm_ips:
48 description:
49 - List of MDM IPs that will be assigned to new MDM. It can contain
50 IPv4 addresses.
51 required: true
52 type: list
53 elements: str
54 role:
55 description:
56 - Role of new MDM.
57 required: true
58 choices: ['Manager', 'TieBreaker']
59 type: str
60 management_ips:
61 description:
62 - List of management IPs to manage MDM. It can contain IPv4
63 addresses.
64 type: list
65 elements: str
66 port:
67 description:
68 - Specifies the port of new MDM.
69 type: int
70 allow_multiple_ips:
71 description:
72 - Allow the added node to have different number of IPs from the
73 primary node.
74 type: bool
75 virtual_interfaces:
76 description:
77 - List of NIC interfaces that will be used for virtual IP addresses.
78 type: list
79 elements: str
80 is_primary:
81 description:
82 - Set I(is_primary) as C(true) to change MDM cluster ownership from the current
83 master MDM to different MDM.
84 - Set I(is_primary) as C(false), will return MDM cluster details.
85 - New owner MDM must be an MDM with a manager role.
86 type: bool
87 cluster_mode:
88 description:
89 - Mode of the cluster.
90 choices: ['OneNode', 'ThreeNodes', 'FiveNodes']
91 type: str
92 mdm:
93 description:
94 - Specifies parameters to add/remove MDMs to/from the MDM cluster.
95 type: list
96 elements: dict
97 suboptions:
98 mdm_id:
99 description:
100 - ID of MDM that will be added/removed to/from the cluster.
101 type: str
102 mdm_name:
103 description:
104 - Name of MDM that will be added/removed to/from the cluster.
105 type: str
106 mdm_type:
107 description:
108 - Type of the MDM.
109 - Either I(mdm_id) or I(mdm_name) must be passed with mdm_type.
110 required: true
111 choices: ['Secondary', 'TieBreaker']
112 type: str
113 mdm_state:
114 description:
115 - Mapping state of MDM.
116 choices: ['present-in-cluster', 'absent-in-cluster']
117 type: str
118 virtual_ip_interfaces:
119 description:
120 - List of interfaces to be used for virtual IPs.
121 - The order of interfaces must be matched with virtual IPs assigned to the
122 cluster.
123 - Interfaces of the primary and secondary type MDMs are allowed to modify.
124 - The I(virtual_ip_interfaces) is mutually exclusive with I(clear_interfaces).
125 type: list
126 elements: str
127 clear_interfaces:
128 description:
129 - Clear all virtual IP interfaces.
130 - The I(clear_interfaces) is mutually exclusive with I(virtual_ip_interfaces).
131 type: bool
132 performance_profile:
133 description:
134 - Apply performance profile to cluster MDMs.
135 choices: ['Compact', 'HighPerformance']
136 type: str
137 state:
138 description:
139 - State of the MDM cluster.
140 choices: ['present', 'absent']
141 required: true
142 type: str
143 notes:
144 - Parameters I(mdm_name) or I(mdm_id) are mandatory for rename and modify virtual IP
145 interfaces.
146 - Parameters I(mdm_name) or I(mdm_id) are not required while modifying performance
147 profile.
148 - For change MDM cluster ownership operation, only changed as true will be
149 returned and for idempotency case MDM cluster details will be returned.
150 - Reinstall all SDC after changing ownership to some newly added MDM.
151 - To add manager standby MDM, MDM package must be installed with manager
152 role.
153 - The I(check_mode) is supported.
154 '''
155
156 EXAMPLES = r'''
157 - name: Add a standby MDM
158 dellemc.powerflex.mdm_cluster:
159 hostname: "{{hostname}}"
160 username: "{{username}}"
161 password: "{{password}}"
162 validate_certs: "{{validate_certs}}"
163 port: "{{port}}"
164 mdm_name: "mdm_1"
165 standby_mdm:
166 mdm_ips:
167 - "10.x.x.x"
168 role: "TieBreaker"
169 management_ips:
170 - "10.x.y.z"
171 state: "present"
172
173 - name: Remove a standby MDM
174 dellemc.powerflex.mdm_cluster:
175 hostname: "{{hostname}}"
176 username: "{{username}}"
177 password: "{{password}}"
178 validate_certs: "{{validate_certs}}"
179 port: "{{port}}"
180 mdm_name: "mdm_1"
181 state: "absent"
182
183 - name: Switch cluster mode from 3 node to 5 node MDM cluster
184 dellemc.powerflex.mdm_cluster:
185 hostname: "{{hostname}}"
186 username: "{{username}}"
187 password: "{{password}}"
188 validate_certs: "{{validate_certs}}"
189 port: "{{port}}"
190 cluster_mode: "FiveNodes"
191 mdm:
192 - mdm_id: "5f091a8a013f1100"
193 mdm_type: "Secondary"
194 - mdm_name: "mdm_1"
195 mdm_type: "TieBreaker"
196 sdc_state: "present-in-cluster"
197 state: "present"
198
199 - name: Switch cluster mode from 5 node to 3 node MDM cluster
200 dellemc.powerflex.mdm_cluster:
201 hostname: "{{hostname}}"
202 username: "{{username}}"
203 password: "{{password}}"
204 validate_certs: "{{validate_certs}}"
205 port: "{{port}}"
206 cluster_mode: "ThreeNodes"
207 mdm:
208 - mdm_id: "5f091a8a013f1100"
209 mdm_type: "Secondary"
210 - mdm_name: "mdm_1"
211 mdm_type: "TieBreaker"
212 sdc_state: "absent-in-cluster"
213 state: "present"
214
215 - name: Get the details of the MDM cluster
216 dellemc.powerflex.mdm_cluster:
217 hostname: "{{hostname}}"
218 username: "{{username}}"
219 password: "{{password}}"
220 validate_certs: "{{validate_certs}}"
221 port: "{{port}}"
222 state: "present"
223
224 - name: Change ownership of MDM cluster
225 dellemc.powerflex.mdm_cluster:
226 hostname: "{{hostname}}"
227 username: "{{username}}"
228 password: "{{password}}"
229 validate_certs: "{{validate_certs}}"
230 port: "{{port}}"
231 mdm_name: "mdm_2"
232 is_primary: true
233 state: "present"
234
235 - name: Modify performance profile
236 dellemc.powerflex.mdm_cluster:
237 hostname: "{{hostname}}"
238 username: "{{username}}"
239 password: "{{password}}"
240 validate_certs: "{{validate_certs}}"
241 port: "{{port}}"
242 performance_profile: "HighPerformance"
243 state: "present"
244
245 - name: Rename the MDM
246 dellemc.powerflex.mdm_cluster:
247 hostname: "{{hostname}}"
248 username: "{{username}}"
249 password: "{{password}}"
250 validate_certs: "{{validate_certs}}"
251 port: "{{port}}"
252 mdm_name: "mdm_1"
253 mdm_new_name: "new_mdm_1"
254 state: "present"
255
256 - name: Modify virtual IP interface of the MDM
257 dellemc.powerflex.mdm_cluster:
258 hostname: "{{hostname}}"
259 username: "{{username}}"
260 password: "{{password}}"
261 validate_certs: "{{validate_certs}}"
262 port: "{{port}}"
263 mdm_name: "mdm_1"
264 virtual_ip_interface:
265 - "ens224"
266 state: "present"
267
268 - name: Clear virtual IP interface of the MDM
269 dellemc.powerflex.mdm_cluster:
270 hostname: "{{hostname}}"
271 username: "{{username}}"
272 password: "{{password}}"
273 validate_certs: "{{validate_certs}}"
274 port: "{{port}}"
275 mdm_name: "mdm_1"
276 clear_interfaces: true
277 state: "present"
278 '''
279
280 RETURN = r'''
281 changed:
282 description: Whether or not the resource has changed.
283 returned: always
284 type: bool
285 sample: 'false'
286 mdm_cluster_details:
287 description: Details of the MDM cluster.
288 returned: When MDM cluster exists
289 type: dict
290 contains:
291 id:
292 description: The ID of the MDM cluster.
293 type: str
294 name:
295 description: Name of MDM cluster.
296 type: str
297 clusterMode:
298 description: Mode of the MDM cluster.
299 type: str
300 master:
301 description: The details of the master MDM.
302 type: dict
303 contains:
304 id:
305 description: ID of the MDM.
306 type: str
307 name:
308 description: Name of the MDM.
309 type: str
310 port:
311 description: Port of the MDM.
312 type: str
313 ips:
314 description: List of IPs for master MDM.
315 type: list
316 managementIPs:
317 description: List of management IPs for master MDM.
318 type: list
319 role:
320 description: Role of MDM.
321 type: str
322 status:
323 description: Status of MDM.
324 type: str
325 versionInfo:
326 description: Version of MDM.
327 type: str
328 virtualInterfaces:
329 description: List of virtual interfaces
330 type: list
331 opensslVersion:
332 description: OpenSSL version.
333 type: str
334 slaves:
335 description: The list of the secondary MDMs.
336 type: list
337 elements: dict
338 contains:
339 id:
340 description: ID of the MDM.
341 type: str
342 name:
343 description: Name of the MDM.
344 type: str
345 port:
346 description: Port of the MDM.
347 type: str
348 ips:
349 description: List of IPs for secondary MDM.
350 type: list
351 managementIPs:
352 description: List of management IPs for secondary MDM.
353 type: list
354 role:
355 description: Role of MDM.
356 type: str
357 status:
358 description: Status of MDM.
359 type: str
360 versionInfo:
361 description: Version of MDM.
362 type: str
363 virtualInterfaces:
364 description: List of virtual interfaces
365 type: list
366 opensslVersion:
367 description: OpenSSL version.
368 type: str
369 tieBreakers:
370 description: The list of the TieBreaker MDMs.
371 type: list
372 elements: dict
373 contains:
374 id:
375 description: ID of the MDM.
376 type: str
377 name:
378 description: Name of the MDM.
379 type: str
380 port:
381 description: Port of the MDM.
382 type: str
383 ips:
384 description: List of IPs for tie-breaker MDM.
385 type: list
386 managementIPs:
387 description: List of management IPs for tie-breaker MDM.
388 type: list
389 role:
390 description: Role of MDM.
391 type: str
392 status:
393 description: Status of MDM.
394 type: str
395 versionInfo:
396 description: Version of MDM.
397 type: str
398 opensslVersion:
399 description: OpenSSL version.
400 type: str
401 standbyMDMs:
402 description: The list of the standby MDMs.
403 type: list
404 elements: dict
405 contains:
406 id:
407 description: ID of the MDM.
408 type: str
409 name:
410 description: Name of the MDM.
411 type: str
412 port:
413 description: Port of the MDM.
414 type: str
415 ips:
416 description: List of IPs for MDM.
417 type: list
418 managementIPs:
419 description: List of management IPs for MDM.
420 type: list
421 role:
422 description: Role of MDM.
423 type: str
424 status:
425 description: Status of MDM.
426 type: str
427 versionInfo:
428 description: Version of MDM.
429 type: str
430 virtualInterfaces:
431 description: List of virtual interfaces.
432 type: list
433 opensslVersion:
434 description: OpenSSL version.
435 type: str
436 clusterState:
437 description: State of the MDM cluster.
438 type: str
439 goodNodesNum:
440 description: Number of Nodes in MDM cluster.
441 type: int
442 goodReplicasNum:
443 description: Number of nodes for Replication.
444 type: int
445 virtualIps:
446 description: List of virtual IPs.
447 type: list
448 sample: {
449 "clusterState": "ClusteredNormal",
450 "clusterMode": "ThreeNodes",
451 "goodNodesNum": 3,
452 "master": {
453 "virtualInterfaces": [
454 "ens1"
455 ],
456 "managementIPs": [
457 "10.x.y.z"
458 ],
459 "ips": [
460 "10.x.y.z"
461 ],
462 "versionInfo": "R3_6.0.0",
463 "opensslVersion": "OpenSSL 1.0.2k-fips 26 Jan 2017",
464 "role": "Manager",
465 "status": "Normal",
466 "name": "sample_mdm",
467 "id": "5908d328581d1400",
468 "port": 9011
469 },
470 "perfProfile": "HighPerformance",
471 "slaves": [
472 {
473 "virtualInterfaces": [
474 "ens1"
475 ],
476 "managementIPs": [
477 "10.x.x.z"
478 ],
479 "ips": [
480 "10.x.x.z"
481 ],
482 "versionInfo": "R3_6.0.0",
483 "opensslVersion": "OpenSSL 1.0.2k-fips 26 Jan 2017",
484 "role": "Manager",
485 "status": "Normal",
486 "name": "sample_mdm1",
487 "id": "5908d328581d1401",
488 "port": 9011
489 }
490 ],
491 "tieBreakers": [
492 {
493 "virtualInterfaces": [],
494 "managementIPs": [],
495 "ips": [
496 "10.x.y.y"
497 ],
498 "versionInfo": "R3_6.0.0",
499 "opensslVersion": "N/A",
500 "role": "TieBreaker",
501 "status": "Normal",
502 "id": "5908d328581d1402",
503 "port": 9011
504 }
505 ],
506 "standbyMDMs": [
507 {
508 "virtualInterfaces": [],
509 "managementIPs": [
510 "10.x.z.z"
511 ],
512 "ips": [
513 "10.x.z.z"
514 ],
515 "versionInfo": "R3_6.0.0",
516 "opensslVersion": "N/A",
517 "role": "TieBreaker",
518 "status": "Normal",
519 "id": "5908d328581d1403",
520 "port": 9011
521 }
522 ],
523 "goodReplicasNum": 2,
524 "id": "cdd883cf00000002"
525 }
526 '''
527
528 from ansible.module_utils.basic import AnsibleModule
529 from ansible_collections.dellemc.powerflex.plugins.module_utils.storage.dell \
530 import utils
531 import copy
532
533 LOG = utils.get_logger('mdm_cluster')
534
535
536 class PowerFlexMdmCluster(object):
537 """Class with MDM cluster operations"""
538
539 def __init__(self):
540 """ Define all parameters required by this module"""
541 self.module_params = utils.get_powerflex_gateway_host_parameters()
542 self.module_params.update(get_powerflex_mdm_cluster_parameters())
543
544 mut_ex_args = [['mdm_name', 'mdm_id'],
545 ['virtual_ip_interfaces', 'clear_interfaces']]
546
547 required_together_args = [['cluster_mode', 'mdm', 'mdm_state']]
548
549 # initialize the Ansible module
550 self.module = AnsibleModule(
551 argument_spec=self.module_params,
552 supports_check_mode=True,
553 mutually_exclusive=mut_ex_args,
554 required_together=required_together_args)
555
556 utils.ensure_required_libs(self.module)
557
558 self.not_exist_msg = "MDM {0} does not exists in MDM cluster."
559 self.exist_msg = "MDM already exists in the MDM cluster"
560 try:
561 self.powerflex_conn = utils.get_powerflex_gateway_host_connection(
562 self.module.params)
563 LOG.info("Got the PowerFlex system connection object instance")
564 LOG.info('Check Mode Flag %s', self.module.check_mode)
565 except Exception as e:
566 LOG.error(str(e))
567 self.module.fail_json(msg=str(e))
568
569 def set_mdm_virtual_interface(self, mdm_id=None, mdm_name=None,
570 virtual_ip_interfaces=None,
571 clear_interfaces=None,
572 mdm_cluster_details=None):
573 """Modify the MDM virtual IP interface.
574 :param mdm_id: ID of MDM
575 :param mdm_name: Name of MDM
576 :param virtual_ip_interfaces: List of virtual IP interfaces
577 :param clear_interfaces: clear virtual IP interfaces of MDM.
578 :param mdm_cluster_details: Details of MDM cluster
579 :return: True if modification of virtual interface or clear operation
580 successful
581 """
582
583 name_or_id = mdm_id if mdm_id else mdm_name
584 if mdm_name is None and mdm_id is None:
585 err_msg = "Please provide mdm_name/mdm_id to modify virtual IP" \
586 " interfaces the MDM."
587 LOG.error(err_msg)
588 self.module.fail_json(msg=err_msg)
589 mdm_details = self.\
590 is_mdm_name_id_exists(mdm_name=mdm_name, mdm_id=mdm_id,
591 cluster_details=mdm_cluster_details)
592 if mdm_details is None:
593 err_msg = self.not_exist_msg.format(name_or_id)
594 self.module.fail_json(msg=err_msg)
595
596 mdm_id = mdm_details['id']
597 modify_list = []
598 modify_list, clear = is_modify_mdm_virtual_interface(
599 virtual_ip_interfaces, clear_interfaces, mdm_details)
600
601 if modify_list is None and not clear:
602 LOG.info("No change required in MDM virtual IP interfaces.")
603 return False
604
605 try:
606 log_msg = "Modifying MDM virtual interfaces to %s " \
607 "or %s" % (str(modify_list), clear)
608 LOG.debug(log_msg)
609 if not self.module.check_mode:
610 self.powerflex_conn.system.modify_virtual_ip_interface(
611 mdm_id=mdm_id, virtual_ip_interfaces=modify_list,
612 clear_interfaces=clear)
613 return True
614 except Exception as e:
615 error_msg = "Failed to modify the virtual IP interfaces of MDM " \
616 "{0} with error {1}".format(name_or_id, str(e))
617 LOG.error(error_msg)
618 self.module.fail_json(msg=error_msg)
619
620 def set_performance_profile(self, performance_profile=None,
621 cluster_details=None):
622 """ Set the performance profile of Cluster MDMs
623 :param performance_profile: Specifies the performance profile of MDMs
624 :param cluster_details: Details of MDM cluster
625 :return: True if updated successfully
626 """
627
628 if self.module.params['state'] == 'present' and performance_profile:
629 if cluster_details['perfProfile'] != performance_profile:
630 try:
631 if not self.module.check_mode:
632 self.powerflex_conn.system.\
633 set_cluster_mdm_performance_profile(performance_profile=performance_profile)
634 return True
635 except Exception as e:
636 error_msg = "Failed to update performance profile to {0} " \
637 "with error {1}.".format(performance_profile,
638 str(e))
639 LOG.error(error_msg)
640 self.module.fail_json(msg=error_msg)
641 return False
642 return False
643
644 def rename_mdm(self, mdm_name=None, mdm_id=None, mdm_new_name=None,
645 cluster_details=None):
646 """Rename the MDM
647 :param mdm_name: Name of the MDM.
648 :param mdm_id: ID of the MDM.
649 :param mdm_new_name: New name of the MDM.
650 :param cluster_details: Details of the MDM cluster.
651 :return: True if successfully renamed.
652 """
653
654 name_or_id = mdm_id if mdm_id else mdm_name
655 if mdm_name is None and mdm_id is None:
656 err_msg = "Please provide mdm_name/mdm_id to rename the MDM."
657 self.module.fail_json(msg=err_msg)
658 mdm_details = self.\
659 is_mdm_name_id_exists(mdm_name=mdm_name, mdm_id=mdm_id,
660 cluster_details=cluster_details)
661 if mdm_details is None:
662 err_msg = self.not_exist_msg.format(name_or_id)
663 self.module.fail_json(msg=err_msg)
664
665 mdm_id = mdm_details['id']
666 try:
667 if ('name' in mdm_details and
668 mdm_new_name != mdm_details['name']) or \
669 'name' not in mdm_details:
670 log_msg = "Modifying the MDM name from %s to " \
671 "%s." % (mdm_name, mdm_new_name)
672 LOG.info(log_msg)
673 if not self.module.check_mode:
674 self.powerflex_conn.system.rename_mdm(
675 mdm_id=mdm_id, mdm_new_name=mdm_new_name)
676 return True
677 except Exception as e:
678 error_msg = "Failed to rename the MDM {0} with error {1}.".\
679 format(name_or_id, str(e))
680 LOG.error(error_msg)
681 self.module.fail_json(msg=error_msg)
682
683 def is_none_name_id_in_switch_cluster_mode(self, mdm):
684 """ Check whether mdm dict have mdm_name and mdm_id or not"""
685
686 for node in mdm:
687 if node['mdm_id'] and node['mdm_name']:
688 msg = "parameters are mutually exclusive: mdm_name|mdm_id"
689 self.module.fail_json(msg=msg)
690
691 def change_cluster_mode(self, cluster_mode, mdm, cluster_details):
692 """change the MDM cluster mode.
693 :param cluster_mode: specifies the mode of MDM cluster
694 :param mdm: A dict containing parameters to change MDM cluster mode
695 :param cluster_details: Details of MDM cluster
696 :return: True if mode changed successfully
697 """
698
699 self.is_none_name_id_in_switch_cluster_mode(mdm=mdm)
700
701 if cluster_mode == cluster_details['clusterMode']:
702 LOG.info("MDM cluster is already in required mode.")
703 return False
704
705 add_secondary = []
706 add_tb = []
707 remove_secondary = []
708 remove_tb = []
709 if self.module.params['state'] == 'present' and \
710 self.module.params['mdm_state'] == 'present-in-cluster':
711 add_secondary, add_tb = self.cluster_expand_list(mdm, cluster_details)
712 elif self.module.params['state'] == 'present' and \
713 self.module.params['mdm_state'] == 'absent-in-cluster':
714 remove_secondary, remove_tb = self.\
715 cluster_reduce_list(mdm, cluster_details)
716 try:
717 if not self.module.check_mode:
718 self.powerflex_conn.system.switch_cluster_mode(
719 cluster_mode=cluster_mode, add_secondary=add_secondary,
720 remove_secondary=remove_secondary, add_tb=add_tb,
721 remove_tb=remove_tb)
722 return True
723 except Exception as e:
724 err_msg = "Failed to change the MDM cluster mode with error " \
725 "{0}".format(str(e))
726 LOG.error(err_msg)
727 self.module.fail_json(msg=err_msg)
728
729 def gather_secondarys_ids(self, mdm, cluster_details):
730 """ Prepare a list of secondary MDMs for switch cluster mode
731 operation"""
732
733 secondarys = []
734
735 for node in mdm:
736 name_or_id = node['mdm_name'] if node['mdm_name'] else \
737 node['mdm_id']
738
739 if node['mdm_type'] == 'Secondary' and node['mdm_id'] is not None:
740 mdm_details = self. \
741 is_mdm_name_id_exists(mdm_id=node['mdm_id'],
742 cluster_details=cluster_details)
743 if mdm_details is None:
744 err_msg = self.not_exist_msg.format(name_or_id)
745 self.module.fail_json(msg=err_msg)
746 secondarys.append(node['mdm_id'])
747
748 elif node['mdm_type'] == 'Secondary' and node['mdm_name'] is not None:
749 mdm_details = self. \
750 is_mdm_name_id_exists(mdm_name=node['mdm_name'],
751 cluster_details=cluster_details)
752 if mdm_details is None:
753 err_msg = self.not_exist_msg.format(name_or_id)
754 self.module.fail_json(msg=err_msg)
755 else:
756 secondarys.append(mdm_details['id'])
757 return secondarys
758
759 def cluster_expand_list(self, mdm, cluster_details):
760 """Whether MDM cluster expansion is required or not.
761 """
762 add_secondary = []
763 add_tb = []
764
765 if 'standbyMDMs' not in cluster_details:
766 err_msg = "No Standby MDMs found. To expand cluster size, " \
767 "first add standby MDMs."
768 LOG.error(err_msg)
769 self.module.fail_json(msg=err_msg)
770
771 add_secondary = self.gather_secondarys_ids(mdm, cluster_details)
772 for node in mdm:
773 name_or_id = node['mdm_name'] if node['mdm_name'] else \
774 node['mdm_id']
775
776 if node['mdm_type'] == 'TieBreaker' and \
777 node['mdm_id'] is not None:
778 add_tb.append(node['mdm_id'])
779
780 elif node['mdm_type'] == 'TieBreaker' and \
781 node['mdm_name'] is not None:
782 mdm_details = self. \
783 is_mdm_name_id_exists(mdm_name=node['mdm_name'],
784 cluster_details=cluster_details)
785
786 if mdm_details is None:
787 err_msg = self.not_exist_msg.format(name_or_id)
788 self.module.fail_json(msg=err_msg)
789 else:
790 add_tb.append(mdm_details['id'])
791
792 log_msg = "expand List are: %s, %s" % (add_secondary, add_tb)
793 LOG.debug(log_msg)
794 return add_secondary, add_tb
795
796 def cluster_reduce_list(self, mdm, cluster_details):
797 """Whether MDM cluster reduction is required or not.
798 """
799 remove_secondary = []
800 remove_tb = []
801
802 remove_secondary = self.gather_secondarys_ids(mdm, cluster_details)
803 for node in mdm:
804 name_or_id = node['mdm_name'] if node['mdm_name'] else \
805 node['mdm_id']
806
807 if node['mdm_type'] == 'TieBreaker' and \
808 node['mdm_id'] is not None:
809 mdm_details = self. \
810 is_mdm_name_id_exists(mdm_id=node['mdm_id'],
811 cluster_details=cluster_details)
812
813 if mdm_details is not None and mdm_details.get('id'):
814 remove_tb.append(mdm_details.get('id'))
815 else:
816 err_msg = self.not_exist_msg.format(name_or_id)
817 self.module.fail_json(msg=err_msg)
818
819 elif node['mdm_type'] == 'TieBreaker' and \
820 node['mdm_name'] is not None:
821 mdm_details = self.\
822 is_mdm_name_id_exists(mdm_name=node['mdm_name'],
823 cluster_details=cluster_details)
824 if mdm_details is None:
825 err_msg = self.not_exist_msg.format(name_or_id)
826 self.module.fail_json(msg=err_msg)
827 else:
828 remove_tb.append(mdm_details['id'])
829
830 log_msg = "Reduce List are: %s, %s." % (remove_secondary, remove_tb)
831 LOG.debug(log_msg)
832 return remove_secondary, remove_tb
833
834 def perform_add_standby(self, mdm_name, standby_payload):
835 """ Perform SDK call to add a standby MDM
836
837 :param mdm_name: Name of new standby MDM
838 :param standby_payload: Parameters dict to add a standby MDM
839 :return: True if standby MDM added successfully
840 """
841 try:
842 if not self.module.check_mode:
843 self.powerflex_conn.system.add_standby_mdm(
844 mdm_ips=standby_payload['mdm_ips'],
845 role=standby_payload['role'],
846 management_ips=standby_payload['management_ips'],
847 mdm_name=mdm_name, port=standby_payload['port'],
848 allow_multiple_ips=standby_payload['allow_multiple_ips'],
849 virtual_interface=standby_payload['virtual_interfaces'])
850 return True
851 except Exception as e:
852 err_msg = "Failed to Add a standby MDM with error {0}.".format(
853 str(e))
854 LOG.error(err_msg)
855 self.module.fail_json(msg=err_msg)
856
857 def is_id_new_name_in_add_mdm(self):
858 """ Check whether mdm_id or mdm_new_name present in Add standby MDM"""
859
860 if self.module.params['mdm_id'] or self.module.params['mdm_new_name']:
861 err_msg = "Parameters mdm_id/mdm_new_name are not allowed while" \
862 " adding a standby MDM. Please try with valid " \
863 "parameters to add a standby MDM."
864 LOG.error(err_msg)
865 self.module.fail_json(msg=err_msg)
866
867 def add_standby_mdm(self, mdm_name, standby_mdm, cluster_details):
868 """ Adding a standby MDM"""
869
870 if self.module.params['state'] == 'present' and \
871 standby_mdm is not None and \
872 (self.check_mdm_exists(standby_mdm['mdm_ips'],
873 cluster_details)):
874 self.is_id_new_name_in_add_mdm()
875 mdm_details = self.\
876 is_mdm_name_id_exists(mdm_name=mdm_name,
877 cluster_details=cluster_details)
878 if mdm_details:
879 LOG.info("Standby MDM %s exits in the system", mdm_name)
880 return False, cluster_details
881
882 standby_payload = prepare_standby_payload(standby_mdm)
883 standby_add = self.perform_add_standby(mdm_name, standby_payload)
884
885 if standby_add:
886 cluster_details = self.get_mdm_cluster_details()
887 msg = "Fetched the MDM cluster details {0} after adding a " \
888 "standby MDM".format(str(cluster_details))
889 LOG.info(msg)
890 return True, cluster_details
891 return False, cluster_details
892
893 def remove_standby_mdm(self, mdm_name, mdm_id, cluster_details):
894 """ Remove the Standby MDM
895 :param mdm_id: ID of MDM that will become owner of MDM cluster
896 :param mdm_name: Name of MDM that will become owner of MDM cluster
897 :param cluster_details: Details of MDM cluster
898 :return: True if MDM removed successful
899 """
900
901 name_or_id = mdm_id if mdm_id else mdm_name
902 if mdm_id is None and mdm_name is None:
903 err_msg = "Either mdm_name or mdm_id is required while removing" \
904 " the standby MDM."
905 LOG.error(err_msg)
906 self.module.fail_json(msg=err_msg)
907 mdm_details = self. \
908 is_mdm_name_id_exists(mdm_name=mdm_name, mdm_id=mdm_id,
909 cluster_details=cluster_details)
910 if mdm_details is None:
911 LOG.info("MDM %s not exists in MDM cluster.", name_or_id)
912 return False
913 mdm_id = mdm_details['id']
914
915 try:
916 if not self.module.check_mode:
917 self.powerflex_conn.system.remove_standby_mdm(mdm_id=mdm_id)
918 return True
919 except Exception as e:
920 error_msg = "Failed to remove the standby MDM {0} from the MDM " \
921 "cluster with error {1}".format(name_or_id, str(e))
922 LOG.error(error_msg)
923 self.module.fail_json(msg=error_msg)
924
925 def change_ownership(self, mdm_id=None, mdm_name=None,
926 cluster_details=None):
927 """ Change the ownership of MDM cluster.
928 :param mdm_id: ID of MDM that will become owner of MDM cluster
929 :param mdm_name: Name of MDM that will become owner of MDM cluster
930 :param cluster_details: Details of MDM cluster
931 :return: True if Owner of MDM cluster change successful
932 """
933
934 name_or_id = mdm_id if mdm_id else mdm_name
935 if mdm_id is None and mdm_name is None:
936 err_msg = "Either mdm_name or mdm_id is required while changing" \
937 " ownership of MDM cluster."
938 LOG.error(err_msg)
939 self.module.fail_json(msg=err_msg)
940
941 mdm_details = self.\
942 is_mdm_name_id_exists(mdm_name=mdm_name, mdm_id=mdm_id,
943 cluster_details=cluster_details)
944 if mdm_details is None:
945 err_msg = self.not_exist_msg.format(name_or_id)
946 self.module.fail_json(msg=err_msg)
947
948 mdm_id = mdm_details['id']
949
950 if mdm_details['id'] == cluster_details['master']['id']:
951 LOG.info("MDM %s is already Owner of MDM cluster.", name_or_id)
952 return False
953 else:
954 try:
955 if not self.module.check_mode:
956 self.powerflex_conn.system.\
957 change_mdm_ownership(mdm_id=mdm_id)
958 return True
959 except Exception as e:
960 error_msg = "Failed to update the Owner of MDM cluster to " \
961 "MDM {0} with error {1}".format(name_or_id,
962 str(e))
963 LOG.error(error_msg)
964 self.module.fail_json(msg=error_msg)
965
966 def find_mdm_in_secondarys(self, mdm_name=None, mdm_id=None,
967 cluster_details=None, name_or_id=None):
968 """Whether MDM exists with mdm_name or id in secondary MDMs"""
969
970 if 'slaves' in cluster_details:
971 for mdm in cluster_details['slaves']:
972 if ('name' in mdm and mdm_name == mdm['name']) or \
973 mdm_id == mdm['id']:
974 LOG.info("MDM %s found in Secondarys MDM.", name_or_id)
975 return mdm
976
977 def find_mdm_in_tb(self, mdm_name=None, mdm_id=None,
978 cluster_details=None, name_or_id=None):
979 """Whether MDM exists with mdm_name or id in tie-breaker MDMs"""
980
981 if 'tieBreakers' in cluster_details:
982 for mdm in cluster_details['tieBreakers']:
983 if ('name' in mdm and mdm_name == mdm['name']) or \
984 mdm_id == mdm['id']:
985 LOG.info("MDM %s found in tieBreakers MDM.", name_or_id)
986 return mdm
987
988 def find_mdm_in_standby(self, mdm_name=None, mdm_id=None,
989 cluster_details=None, name_or_id=None):
990 """Whether MDM exists with mdm_name or id in standby MDMs"""
991
992 if 'standbyMDMs' in cluster_details:
993 for mdm in cluster_details['standbyMDMs']:
994 if ('name' in mdm and mdm_name == mdm['name']) or \
995 mdm_id == mdm['id']:
996 LOG.info("MDM %s found in standby MDM.", name_or_id)
997 return mdm
998
999 def is_mdm_name_id_exists(self, mdm_id=None, mdm_name=None,
1000 cluster_details=None):
1001 """Whether MDM exists with mdm_name or id """
1002
1003 name_or_id = mdm_id if mdm_id else mdm_name
1004 # check in master MDM
1005 if ('name' in cluster_details['master'] and mdm_name == cluster_details['master']['name']) \
1006 or mdm_id == cluster_details['master']['id']:
1007 LOG.info("MDM %s is master MDM.", name_or_id)
1008 return cluster_details['master']
1009
1010 # check in secondary MDMs
1011 secondary_mdm = []
1012 secondary_mdm = self.\
1013 find_mdm_in_secondarys(mdm_name=mdm_name, mdm_id=mdm_id,
1014 cluster_details=cluster_details,
1015 name_or_id=name_or_id)
1016 if secondary_mdm is not None:
1017 return secondary_mdm
1018
1019 # check in tie-breaker MDMs
1020 tb_mdm = []
1021 tb_mdm = self.find_mdm_in_tb(mdm_name=mdm_name, mdm_id=mdm_id,
1022 cluster_details=cluster_details,
1023 name_or_id=name_or_id)
1024 if tb_mdm is not None:
1025 return tb_mdm
1026
1027 # check in standby MDMs
1028 standby_mdm = self.find_mdm_in_standby(mdm_name=mdm_name,
1029 mdm_id=mdm_id,
1030 cluster_details=cluster_details,
1031 name_or_id=name_or_id)
1032 if standby_mdm is not None:
1033 return standby_mdm
1034
1035 LOG.info("MDM %s does not exists in MDM Cluster.", name_or_id)
1036 return None
1037
1038 def get_mdm_cluster_details(self):
1039 """Get MDM cluster details
1040 :return: Details of MDM Cluster if existed.
1041 """
1042
1043 try:
1044 mdm_cluster_details = self.powerflex_conn.system.\
1045 get_mdm_cluster_details()
1046
1047 if len(mdm_cluster_details) == 0:
1048 msg = "MDM cluster not found"
1049 LOG.error(msg)
1050 self.module.fail_json(msg=msg)
1051
1052 # Append Performance profile
1053 resp = self.get_system_details()
1054 if resp is not None:
1055 mdm_cluster_details['perfProfile'] = resp['perfProfile']
1056
1057 # Append list of configured MDM IP addresses
1058 gateway_configuration_details = self.powerflex_conn.system.\
1059 get_gateway_configuration_details()
1060 if gateway_configuration_details is not None:
1061 mdm_cluster_details['mdmAddresses'] = gateway_configuration_details['mdmAddresses']
1062
1063 return mdm_cluster_details
1064
1065 except Exception as e:
1066 error_msg = "Failed to get the MDM cluster with error {0}."
1067 error_msg = error_msg.format(str(e))
1068 LOG.error(error_msg)
1069 self.module.fail_json(msg=error_msg)
1070
1071 def check_ip_in_secondarys(self, standby_ip, cluster_details):
1072 """whether standby IPs present in secondary MDMs"""
1073
1074 if 'slaves' in cluster_details:
1075 for secondary_mdm in cluster_details['slaves']:
1076 current_secondary_ips = secondary_mdm['ips']
1077 for ips in standby_ip:
1078 if ips in current_secondary_ips:
1079 LOG.info(self.exist_msg)
1080 return False
1081 return True
1082
1083 def check_ip_in_tbs(self, standby_ip, cluster_details):
1084 """whether standby IPs present in tie-breaker MDMs"""
1085
1086 if 'tieBreakers' in cluster_details:
1087 for tb_mdm in cluster_details['tieBreakers']:
1088 current_tb_ips = tb_mdm['ips']
1089 for ips in standby_ip:
1090 if ips in current_tb_ips:
1091 LOG.info(self.exist_msg)
1092 return False
1093 return True
1094
1095 def check_ip_in_standby(self, standby_ip, cluster_details):
1096 """whether standby IPs present in standby MDMs"""
1097
1098 if 'standbyMDMs' in cluster_details:
1099 for stb_mdm in cluster_details['standbyMDMs']:
1100 current_stb_ips = stb_mdm['ips']
1101 for ips in standby_ip:
1102 if ips in current_stb_ips:
1103 LOG.info(self.exist_msg)
1104 return False
1105 return True
1106
1107 def check_mdm_exists(self, standby_ip=None, cluster_details=None):
1108 """Check whether standby MDM exists in MDM Cluster"""
1109
1110 # check in master node
1111 current_master_ips = cluster_details['master']['ips']
1112 for ips in standby_ip:
1113 if ips in current_master_ips:
1114 LOG.info(self.exist_msg)
1115 return False
1116
1117 # check in secondary nodes
1118 in_secondary = self.check_ip_in_secondarys(standby_ip=standby_ip,
1119 cluster_details=cluster_details)
1120 if not in_secondary:
1121 return False
1122
1123 # check in tie-breaker nodes
1124 in_tbs = self.check_ip_in_tbs(standby_ip=standby_ip,
1125 cluster_details=cluster_details)
1126 if not in_tbs:
1127 return False
1128
1129 # check in Standby nodes
1130 in_standby = self.check_ip_in_standby(standby_ip=standby_ip,
1131 cluster_details=cluster_details)
1132 if not in_standby:
1133 return False
1134
1135 LOG.info("New Standby MDM does not exists in MDM cluster")
1136 return True
1137
1138 def get_system_details(self):
1139 """Get system details
1140 :return: Details of PowerFlex system
1141 """
1142
1143 try:
1144 resp = self.powerflex_conn.system.get()
1145 if len(resp) == 0:
1146 self.module.fail_json(msg="No system exist on the given "
1147 "host.")
1148 if len(resp) > 1:
1149 self.module.fail_json(msg="Multiple systems exist on the "
1150 "given host.")
1151 return resp[0]
1152 except Exception as e:
1153 msg = "Failed to get system id with error %s" % str(e)
1154 LOG.error(msg)
1155 self.module.fail_json(msg=msg)
1156
1157 def validate_parameters(self):
1158 """Validate the input parameters"""
1159
1160 name_params = ['mdm_name', 'mdm_id', 'mdm_new_name']
1161 msg = "Please provide the valid {0}"
1162
1163 for n_item in name_params:
1164 if self.module.params[n_item] is not None and \
1165 (len(self.module.params[n_item].strip()) or
1166 self.module.params[n_item].count(" ") > 0) == 0:
1167 err_msg = msg.format(n_item)
1168 self.module.fail_json(msg=err_msg)
1169
1170 def perform_module_operation(self):
1171 """
1172 Perform different actions on MDM cluster based on parameters passed in
1173 the playbook
1174 """
1175 mdm_name = self.module.params['mdm_name']
1176 mdm_id = self.module.params['mdm_id']
1177 mdm_new_name = self.module.params['mdm_new_name']
1178 standby_mdm = copy.deepcopy(self.module.params['standby_mdm'])
1179 is_primary = self.module.params['is_primary']
1180 cluster_mode = self.module.params['cluster_mode']
1181 mdm = copy.deepcopy(self.module.params['mdm'])
1182 mdm_state = self.module.params['mdm_state']
1183 virtual_ip_interfaces = self.module.params['virtual_ip_interfaces']
1184 clear_interfaces = self.module.params['clear_interfaces']
1185 performance_profile = self.module.params['performance_profile']
1186 state = self.module.params['state']
1187
1188 # result is a dictionary to contain end state and MDM cluster details
1189 changed = False
1190 result = dict(
1191 changed=False,
1192 mdm_cluster_details={}
1193 )
1194 self.validate_parameters()
1195
1196 mdm_cluster_details = self.get_mdm_cluster_details()
1197 msg = "Fetched the MDM cluster details {0}".\
1198 format(str(mdm_cluster_details))
1199 LOG.info(msg)
1200
1201 standby_changed = False
1202 performance_changed = False
1203 renamed_changed = False
1204 interface_changed = False
1205 remove_changed = False
1206 mode_changed = False
1207 owner_changed = False
1208
1209 # Add standby MDM
1210 standby_changed, mdm_cluster_details = self.\
1211 add_standby_mdm(mdm_name, standby_mdm, mdm_cluster_details)
1212
1213 # Update performance profile
1214 performance_changed = self.\
1215 set_performance_profile(performance_profile, mdm_cluster_details)
1216
1217 # Rename MDM
1218 if state == 'present' and mdm_new_name:
1219 renamed_changed = self.rename_mdm(mdm_name, mdm_id, mdm_new_name,
1220 mdm_cluster_details)
1221
1222 # Change MDM virtual IP interfaces
1223 if state == 'present' and (virtual_ip_interfaces or clear_interfaces):
1224 interface_changed = self.\
1225 set_mdm_virtual_interface(mdm_id, mdm_name,
1226 virtual_ip_interfaces,
1227 clear_interfaces,
1228 mdm_cluster_details)
1229 # change cluster mode
1230 if state == 'present' and cluster_mode and mdm and mdm_state:
1231 mode_changed = self.change_cluster_mode(cluster_mode, mdm,
1232 mdm_cluster_details)
1233
1234 # Remove standby MDM
1235 if state == 'absent':
1236 remove_changed = self.remove_standby_mdm(mdm_name, mdm_id,
1237 mdm_cluster_details)
1238
1239 # change ownership of MDM cluster
1240 if state == 'present' and is_primary:
1241 owner_changed = self.change_ownership(mdm_id, mdm_name,
1242 mdm_cluster_details)
1243
1244 # Setting Changed Flag
1245 changed = update_change_flag(standby_changed, performance_changed,
1246 renamed_changed, interface_changed,
1247 mode_changed, remove_changed,
1248 owner_changed)
1249
1250 # Returning the updated MDM cluster details
1251 # Checking whether owner of MDM cluster has changed
1252 if owner_changed:
1253 mdm_cluster_details = {}
1254 else:
1255 mdm_cluster_details = self.get_mdm_cluster_details()
1256
1257 result['mdm_cluster_details'] = mdm_cluster_details
1258 result['changed'] = changed
1259 self.module.exit_json(**result)
1260
1261
1262 def update_change_flag(standby_changed, performance_changed, renamed_changed,
1263 interface_changed, mode_changed, remove_changed,
1264 owner_changed):
1265 """ Update the changed flag based on the operation performed in the task"""
1266
1267 if standby_changed or performance_changed or renamed_changed or \
1268 interface_changed or mode_changed or remove_changed or \
1269 owner_changed:
1270 return True
1271 return False
1272
1273
1274 def prepare_standby_payload(standby_mdm):
1275 """prepare the payload for add standby MDM"""
1276 payload_dict = {}
1277 for mdm_keys in standby_mdm:
1278 if standby_mdm[mdm_keys]:
1279 payload_dict[mdm_keys] = standby_mdm[mdm_keys]
1280 else:
1281 payload_dict[mdm_keys] = None
1282 return payload_dict
1283
1284
1285 def is_modify_mdm_virtual_interface(virtual_ip_interfaces, clear_interfaces,
1286 mdm_details):
1287 """Check if modification in MDM virtual IP interface required."""
1288
1289 modify_list = []
1290 clear = False
1291 existing_interfaces = mdm_details['virtualInterfaces']
1292
1293 # Idempotency check for virtual IP interface
1294 if clear_interfaces is None and \
1295 len(existing_interfaces) == len(virtual_ip_interfaces) and \
1296 set(existing_interfaces) == set(virtual_ip_interfaces):
1297 LOG.info("No changes required for virtual IP interface.")
1298 return None, False
1299
1300 # Idempotency check for clear_interfaces
1301 if clear_interfaces and len(mdm_details['virtualInterfaces']) == 0:
1302 LOG.info("No change required for clear interface.")
1303 return None, False
1304
1305 # clearing all virtual IP interfaces of MDM
1306 elif clear_interfaces and len(mdm_details['virtualInterfaces']) != 0 and \
1307 virtual_ip_interfaces is None:
1308 LOG.info("Clear all interfaces of the MDM.")
1309 clear = True
1310 return None, clear
1311
1312 if virtual_ip_interfaces and clear_interfaces is None:
1313 for interface in virtual_ip_interfaces:
1314 modify_list.append(interface)
1315 return modify_list, clear
1316
1317
1318 def get_powerflex_mdm_cluster_parameters():
1319 """This method provide parameter required for the MDM cluster
1320 module on PowerFlex"""
1321 return dict(
1322 mdm_name=dict(), mdm_id=dict(), mdm_new_name=dict(),
1323 virtual_ip_interfaces=dict(type='list', elements='str'),
1324 clear_interfaces=dict(type='bool'), is_primary=dict(type='bool'),
1325 standby_mdm=dict(type='dict', options=dict(
1326 mdm_ips=dict(type='list', elements='str', required=True),
1327 role=dict(required=True, choices=['Manager', 'TieBreaker']),
1328 management_ips=dict(type='list', elements='str'),
1329 port=dict(type='int'), allow_multiple_ips=dict(type='bool'),
1330 virtual_interfaces=dict(type='list', elements='str'))),
1331 cluster_mode=dict(choices=['OneNode', 'ThreeNodes', 'FiveNodes']),
1332 mdm=dict(type='list', elements='dict',
1333 options=dict(mdm_id=dict(), mdm_name=dict(),
1334 mdm_type=dict(required=True,
1335 choices=['Secondary', 'TieBreaker']))),
1336 mdm_state=dict(choices=['present-in-cluster', 'absent-in-cluster']),
1337 performance_profile=dict(choices=['Compact', 'HighPerformance']),
1338 state=dict(required=True, type='str', choices=['present', 'absent'])
1339 )
1340
1341
1342 def main():
1343 """ Perform actions on MDM cluster based on user input from playbook"""
1344 obj = PowerFlexMdmCluster()
1345 obj.perform_module_operation()
1346
1347
1348 if __name__ == '__main__':
1349 main()