Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2
3 # Copyright: (c) 2024, 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 replication pairs on Dell Technologies (Dell) PowerFlex"""
7
8 from __future__ import (absolute_import, division, print_function)
9
10 __metaclass__ = type
11
12 DOCUMENTATION = r'''
13 module: replication_pair
14 version_added: '1.6.0'
15 short_description: Manage replication pairs on Dell PowerFlex
16 description:
17 - Managing replication pairs on PowerFlex storage system includes
18 getting details, creating, pause, resume initial copy and deleting a replication pair.
19 author:
20 - Jennifer John (@Jennifer-John) <[email protected]>
21 extends_documentation_fragment:
22 - dellemc.powerflex.powerflex
23 options:
24 pair_id:
25 description:
26 - The ID of the replication pair.
27 - Mutually exclusive with I(pair_name).
28 type: str
29 pair_name:
30 description:
31 - The name of the replication pair.
32 - Mutually exclusive with I(pair_id).
33 type: str
34 rcg_name:
35 description:
36 - The name of the replication consistency group.
37 - Mutually exclusive with I(rcg_id).
38 type: str
39 rcg_id:
40 description:
41 - The ID of the replication consistency group.
42 - Mutually exclusive with I(rcg_name).
43 type: str
44 pause:
45 description:
46 - Pause or resume the initial copy of replication pair.
47 type: bool
48 pairs:
49 description:
50 - List of replication pairs to add to rcg.
51 type: list
52 elements: dict
53 suboptions:
54 source_volume_id:
55 description:
56 - Source volume ID.
57 - Mutually exclusive with I(source_volume_name).
58 type: str
59 source_volume_name:
60 description:
61 - Source volume name.
62 - Mutually exclusive with I(source_volume_id).
63 type: str
64 target_volume_id:
65 description:
66 - Target volume ID.
67 - Mutually exclusive with I(target_volume_name).
68 type: str
69 target_volume_name:
70 description:
71 - Target volume name.
72 - If specified, I(remote_peer) details should also be specified.
73 - Mutually exclusive with I(target_volume_id).
74 type: str
75 copy_type:
76 description:
77 - Copy type.
78 choices: ['Identical', 'OnlineCopy', 'OnlineHashCopy', 'OfflineCopy']
79 type: str
80 required: true
81 name:
82 description:
83 - Name of replication pair.
84 type: str
85 remote_peer:
86 description:
87 - Remote peer system.
88 type: dict
89 suboptions:
90 hostname:
91 required: true
92 description:
93 - IP or FQDN of the remote peer gateway host.
94 type: str
95 aliases:
96 - gateway_host
97 username:
98 type: str
99 required: true
100 description:
101 - The username of the remote peer gateway host.
102 password:
103 type: str
104 required: true
105 description:
106 - The password of the remote peer gateway host.
107 validate_certs:
108 type: bool
109 default: true
110 aliases:
111 - verifycert
112 description:
113 - Boolean variable to specify whether or not to validate SSL
114 certificate.
115 - C(true) - Indicates that the SSL certificate should be verified.
116 - C(false) - Indicates that the SSL certificate should not be verified.
117 port:
118 description:
119 - Port number through which communication happens with remote peer
120 gateway host.
121 type: int
122 default: 443
123 timeout:
124 description:
125 - Time after which connection will get terminated.
126 - It is to be mentioned in seconds.
127 type: int
128 default: 120
129 state:
130 description:
131 - State of the replication pair.
132 choices: ['present', 'absent']
133 default: present
134 type: str
135 notes:
136 - The I(check_mode) is supported.
137 - In 4.0 the creation of replication pair fails when I(copy_type) is specified as C(OfflineCopy).
138 '''
139
140 EXAMPLES = r'''
141 - name: Get replication pair details
142 dellemc.powerflex.replication_pair:
143 hostname: "{{hostname}}"
144 username: "{{username}}"
145 password: "{{password}}"
146 validate_certs: "{{validate_certs}}"
147 port: "{{port}}"
148 pair_id: "123"
149
150 - name: Create a replication pair
151 dellemc.powerflex.replication_pair:
152 hostname: "{{hostname}}"
153 username: "{{username}}"
154 password: "{{password}}"
155 validate_certs: "{{validate_certs}}"
156 port: "{{port}}"
157 rcg_name: "test_rcg"
158 pairs:
159 - source_volume_id: "002"
160 target_volume_id: "001"
161 copy_type: "OnlineCopy"
162 name: "pair1"
163
164 - name: Create a replication pair with target volume name
165 dellemc.powerflex.replication_pair:
166 hostname: "{{hostname}}"
167 username: "{{username}}"
168 password: "{{password}}"
169 validate_certs: "{{validate_certs}}"
170 port: "{{port}}"
171 rcg_name: "test_rcg"
172 pairs:
173 - source_volume_name: "src_vol"
174 target_volume_name: "dest_vol"
175 copy_type: "OnlineCopy"
176 name: "pair1"
177 remote_peer:
178 hostname: "{{hostname}}"
179 username: "{{username}}"
180 password: "{{password}}"
181 validate_certs: "{{validate_certs}}"
182 port: "{{port}}"
183
184 - name: Pause replication pair
185 dellemc.powerflex.replication_pair:
186 hostname: "{{hostname}}"
187 username: "{{username}}"
188 password: "{{password}}"
189 validate_certs: "{{validate_certs}}"
190 port: "{{port}}"
191 pair_name: "pair1"
192 pause: true
193
194 - name: Resume replication pair
195 dellemc.powerflex.replication_pair:
196 hostname: "{{hostname}}"
197 username: "{{username}}"
198 password: "{{password}}"
199 validate_certs: "{{validate_certs}}"
200 port: "{{port}}"
201 pair_name: "pair1"
202 pause: false
203
204 - name: Delete replication pair
205 dellemc.powerflex.replication_pair:
206 hostname: "{{hostname}}"
207 username: "{{username}}"
208 password: "{{password}}"
209 validate_certs: "{{validate_certs}}"
210 port: "{{port}}"
211 pair_name: "pair1"
212 state: "absent"
213 '''
214
215 RETURN = r'''
216 changed:
217 description: Whether or not the resource has changed.
218 returned: always
219 type: bool
220 sample: 'false'
221 replication_pair_details:
222 description: Details of the replication pair.
223 returned: When replication pair exists
224 type: dict
225 contains:
226 id:
227 description: The ID of the replication pair.
228 type: str
229 name:
230 description: The name of the replication pair.
231 type: str
232 remoteId:
233 description: The ID of the remote replication pair.
234 type: str
235 localVolumeId:
236 description: The ID of the local volume.
237 type: str
238 localVolumeName:
239 description: The name of the local volume.
240 type: str
241 replicationConsistencyGroupId:
242 description: The ID of the replication consistency group.
243 type: str
244 copyType:
245 description: The copy type of the replication pair.
246 type: str
247 initialCopyState:
248 description: The inital copy state of the replication pair.
249 type: str
250 localActivityState:
251 description: The state of activity of the local replication pair.
252 type: str
253 remoteActivityState:
254 description: The state of activity of the remote replication pair.
255 type: str
256 initialCopyPriority:
257 description: Initial copy priority.
258 type: int
259 lifetimeState:
260 description: Lifetime state of replication pair.
261 type: int
262 peerSystemName:
263 description: Peer system name.
264 type: int
265 remoteCapacityInMB:
266 description: Remote Capacity in MB.
267 type: int
268 userRequestedPauseTransmitInitCopy:
269 description: Value of user requested pause transmit initial copy.
270 type: int
271 remoteVolumeId:
272 description: Remote Volume ID.
273 type: int
274 remoteVolumeName:
275 description: Remote Volume Name.
276 type: int
277 sample: {
278 "copyType": "OnlineCopy",
279 "id": "23aa0bc900000001",
280 "initialCopyPriority": -1,
281 "initialCopyState": "Done",
282 "lifetimeState": "Normal",
283 "localActivityState": "RplEnabled",
284 "localVolumeId": "e2bc1fab00000008",
285 "localVolumeName": "vol1",
286 "name": null,
287 "peerSystemName": null,
288 "remoteActivityState": "RplEnabled",
289 "remoteCapacityInMB": 8192,
290 "remoteId": "a058446700000001",
291 "remoteVolumeId": "1cda7af20000000d",
292 "remoteVolumeName": "vol",
293 "replicationConsistencyGroupId": "e2ce036b00000002",
294 "userRequestedPauseTransmitInitCopy": false
295 }
296 rcg_replication_pairs:
297 description: Details of the replication pairs of rcg.
298 returned: When rcg exists
299 type: list
300 contains:
301 id:
302 description: The ID of the replication pair.
303 type: str
304 name:
305 description: The name of the replication pair.
306 type: str
307 remoteId:
308 description: The ID of the remote replication pair.
309 type: str
310 localVolumeId:
311 description: The ID of the local volume.
312 type: str
313 localVolumeName:
314 description: The name of the local volume.
315 type: str
316 replicationConsistencyGroupId:
317 description: The ID of the replication consistency group.
318 type: str
319 copyType:
320 description: The copy type of the replication pair.
321 type: str
322 initialCopyState:
323 description: The inital copy state of the replication pair.
324 type: str
325 localActivityState:
326 description: The state of activity of the local replication pair.
327 type: str
328 remoteActivityState:
329 description: The state of activity of the remote replication pair.
330 type: str
331 initialCopyPriority:
332 description: Initial copy priority.
333 type: int
334 lifetimeState:
335 description: Lifetime state of replication pair.
336 type: int
337 peerSystemName:
338 description: Peer system name.
339 type: int
340 remoteCapacityInMB:
341 description: Remote Capacity in MB.
342 type: int
343 userRequestedPauseTransmitInitCopy:
344 description: Value of user requested pause transmit initial copy.
345 type: int
346 remoteVolumeId:
347 description: Remote Volume ID.
348 type: int
349 remoteVolumeName:
350 description: Remote Volume Name.
351 type: int
352 sample: [{
353 "copyType": "OnlineCopy",
354 "id": "23aa0bc900000001",
355 "initialCopyPriority": -1,
356 "initialCopyState": "Done",
357 "lifetimeState": "Normal",
358 "localActivityState": "RplEnabled",
359 "localVolumeId": "e2bc1fab00000008",
360 "localVolumeName": "vol1",
361 "name": null,
362 "peerSystemName": null,
363 "remoteActivityState": "RplEnabled",
364 "remoteCapacityInMB": 8192,
365 "remoteId": "a058446700000001",
366 "remoteVolumeId": "1cda7af20000000d",
367 "remoteVolumeName": "vol",
368 "replicationConsistencyGroupId": "e2ce036b00000002",
369 "userRequestedPauseTransmitInitCopy": false
370 }]
371 '''
372
373 from ansible.module_utils.basic import AnsibleModule
374 from ansible_collections.dellemc.powerflex.plugins.module_utils.storage.dell \
375 import utils
376 from ansible_collections.dellemc.powerflex.plugins.module_utils.storage.dell.libraries.powerflex_base \
377 import powerflex_compatibility
378 from ansible_collections.dellemc.powerflex.plugins.module_utils.storage.dell.libraries.powerflex_base \
379 import PowerFlexBase
380
381 LOG = utils.get_logger('replication_pair')
382
383
384 @powerflex_compatibility(min_ver='3.6', max_ver='5.0')
385 class PowerFlexReplicationPair(PowerFlexBase):
386 """Class with replication pair operations"""
387
388 def __init__(self):
389 """ Define all parameters required by this module"""
390 argument_spec = get_powerflex_replication_pair_parameters()
391 mut_ex_args = [['rcg_name', 'rcg_id'], ['pair_id', 'pair_name']]
392
393 ansible_module_params = {
394 'argument_spec': argument_spec,
395 'supports_check_mode': True,
396 'mutually_exclusive': mut_ex_args
397 }
398 super().__init__(AnsibleModule, ansible_module_params)
399 super().check_module_compatibility()
400
401 def get_replication_pair(self, pair_name=None, pair_id=None):
402 """Get replication pair details
403 :param pair_name: Name of the replication pair
404 :param pair_id: ID of the replication pair
405 :return: Replication pair details
406 """
407 name_or_id = pair_id if pair_id else pair_name
408 try:
409 pair_details = []
410 if pair_id:
411 pair_details = self.powerflex_conn.replication_pair.get(
412 filter_fields={'id': pair_id})
413
414 if pair_name:
415 pair_details = self.powerflex_conn.replication_pair.get(
416 filter_fields={'name': pair_name})
417
418 if pair_details:
419 pair_details[0].pop('links', None)
420 pair_details[0]['localVolumeName'] = self.get_volume(pair_details[0]['localVolumeId'], filter_by_name=False)[0]['name']
421 pair_details[0]['statistics'] = \
422 self.powerflex_conn.replication_pair.get_statistics(pair_details[0]['id'])
423 return pair_details[0]
424 return pair_details
425 except Exception as e:
426 errormsg = "Failed to get the replication pair {0} with" \
427 " error {1}".format(name_or_id, str(e))
428 LOG.error(errormsg)
429 self.module.fail_json(msg=errormsg)
430
431 def get_volume(self, vol_name_or_id, filter_by_name=True, is_remote=False):
432 """Get volume details
433 :param vol_name: ID or name of the volume
434 :param filter_by_name: If filter details by name or id
435 :param is_remote: Specifies if source or target volume
436 :return: Details of volume if exist.
437 """
438 try:
439 volume_details = []
440 filter_field = {'id': vol_name_or_id}
441 if filter_by_name:
442 filter_field = {'name': vol_name_or_id}
443 if is_remote:
444 self.remote_powerflex_conn = utils.get_powerflex_gateway_host_connection(
445 self.module.params['remote_peer'])
446 volume_details = self.remote_powerflex_conn.volume.get(
447 filter_fields=filter_field)
448 else:
449 volume_details = self.powerflex_conn.volume.get(
450 filter_fields=filter_field)
451
452 if not volume_details:
453 vol_type = 'Target' if is_remote else 'Source'
454 self.module.fail_json("%s volume %s does not exist" % (vol_type, vol_name_or_id))
455 return volume_details
456 except Exception as e:
457 errormsg = "Failed to retrieve volume {0}".format(str(e))
458 LOG.error(errormsg)
459 self.module.fail_json(msg=errormsg)
460
461 def get_rcg(self, rcg_name=None, rcg_id=None):
462 """Get rcg details
463 :param rcg_name: Name of the rcg
464 :param rcg_id: ID of the rcg
465 :return: RCG details
466 """
467 name_or_id = rcg_id if rcg_id else rcg_name
468 try:
469 rcg_details = {}
470 if rcg_id:
471 rcg_details = self.powerflex_conn.replication_consistency_group.get(
472 filter_fields={'id': rcg_id})
473
474 if rcg_name:
475 rcg_details = self.powerflex_conn.replication_consistency_group.get(
476 filter_fields={'name': rcg_name})
477
478 if not rcg_details:
479 self.module.fail_json("RCG %s does not exist" % rcg_name)
480
481 return rcg_details[0]
482 except Exception as e:
483 errormsg = "Failed to get the replication consistency group {0} with" \
484 " error {1}".format(name_or_id, str(e))
485 LOG.error(errormsg)
486 self.module.fail_json(msg=errormsg)
487
488 def get_rcg_replication_pairs(self, rcg_id):
489 """Get rcg replication pair details
490 :param rcg_id: ID of the rcg
491 :return: RCG replication pair details
492 """
493 try:
494 rcg_pairs = self.powerflex_conn.replication_consistency_group.get_replication_pairs(rcg_id)
495 for rcg_pair in rcg_pairs:
496 rcg_pair.pop('links', None)
497 rcg_pair['localVolumeName'] = self.get_volume(rcg_pair['localVolumeId'], filter_by_name=False)[0]['name']
498 rcg_pair['replicationConsistencyGroupName'] = self.get_rcg(rcg_id=rcg_pair['replicationConsistencyGroupId'])['name']
499 return rcg_pairs
500 except Exception as e:
501 errormsg = "Failed to get the replication pairs for replication consistency group {0} with" \
502 " error {1}".format(rcg_id, str(e))
503 LOG.error(errormsg)
504 self.module.fail_json(msg=errormsg)
505
506 def create_replication_pairs(self, rcg_id, rcg_pairs, input_pairs):
507 """Create replication pairs"""
508 try:
509 for pair in input_pairs:
510 if pair['source_volume_name'] is not None:
511 pair['source_volume_id'] = self.get_volume(pair['source_volume_name'])[0]['id']
512 if pair['target_volume_name'] is not None:
513 pair['target_volume_id'] = self.get_volume(pair['target_volume_name'], is_remote=True)[0]['id']
514 pairs = find_non_existing_pairs(rcg_pairs, input_pairs)
515 if not pairs:
516 return False
517 if not self.module.check_mode:
518 for pair in pairs:
519 self.powerflex_conn.replication_pair.add(
520 source_vol_id=pair['source_volume_id'],
521 dest_vol_id=pair['target_volume_id'],
522 rcg_id=rcg_id,
523 copy_type=pair['copy_type'],
524 name=pair['name'])
525 return True
526 except Exception as e:
527 errormsg = "Create replication pairs failed with error {0}".format(str(e))
528 LOG.error(errormsg)
529 self.module.fail_json(msg=errormsg)
530
531 def pause(self, pair_id):
532 """Pause replication pair
533 :param pair_id: ID of the replication pair
534 :return: True if paused
535 """
536 try:
537 if not self.module.check_mode:
538 self.powerflex_conn.replication_pair.pause(pair_id)
539 return True
540 except Exception as e:
541 errormsg = "Pause replication pair {0} failed with error {1}".format(pair_id, str(e))
542 LOG.error(errormsg)
543 self.module.fail_json(msg=errormsg)
544
545 def resume(self, pair_id):
546 """Resume replication pair
547 :param pair_id: ID of the replication pair
548 :return: True if resumed
549 """
550 try:
551 if not self.module.check_mode:
552 self.powerflex_conn.replication_pair.resume(pair_id)
553 return True
554 except Exception as e:
555 errormsg = "Resume replication pair {0} failed with error {1}".format(pair_id, str(e))
556 LOG.error(errormsg)
557 self.module.fail_json(msg=errormsg)
558
559 def delete_pair(self, pair_id):
560 """Delete replication pair
561 :param pair_id: Replication pair id.
562 :return: Boolean indicates if delete pair operation is successful
563 """
564 try:
565 if not self.module.check_mode:
566 self.powerflex_conn.replication_pair.remove(pair_id)
567 return True
568
569 except Exception as e:
570 errormsg = "Delete replication pair {0} failed with " \
571 "error {1}".format(pair_id, str(e))
572 LOG.error(errormsg)
573 self.module.fail_json(msg=errormsg)
574
575 def validate_input(self, params):
576 if params['pairs'] is not None:
577 self.validate_pairs(params)
578 if not params['rcg_id'] and not params['rcg_name']:
579 self.module.fail_json(msg="Specify either rcg_id or rcg_name to create replication pair")
580 self.validate_pause(params)
581
582 def validate_pairs(self, params):
583 for pair in params['pairs']:
584 if pair['source_volume_id'] and pair['source_volume_name']:
585 self.module.fail_json(msg='Specify either source_volume_id or source_volume_name')
586 if pair['target_volume_id'] and pair['target_volume_name']:
587 self.module.fail_json(msg='Specify either target_volume_id or target_volume_name')
588 if pair['target_volume_name'] and params['remote_peer'] is None:
589 self.module.fail_json(msg='Specify remote_peer with target_volume_name')
590
591 def validate_pause(self, params):
592 if params['pause'] is not None and (not params['pair_id'] and not params['pair_name']):
593 self.module.fail_json(msg='Specify either pair_id or pair_name to perform pause or resume of initial copy')
594
595 def validate_pause_or_resume(self, pause, replication_pair_details, pair_id):
596 if not replication_pair_details:
597 self.module.fail_json(msg="Specify a valid pair_name or pair_id to perform pause or resume")
598 return self.perform_pause_or_resume(pause, replication_pair_details, pair_id)
599
600 def perform_pause_or_resume(self, pause, replication_pair_details, pair_id):
601 changed = False
602 if pause and replication_pair_details['initialCopyState'] not in ('Paused', 'Done'):
603 changed = self.pause(pair_id)
604 elif not pause and replication_pair_details['initialCopyState'] == 'Paused':
605 changed = self.resume(pair_id)
606 return changed
607
608 def perform_module_operation(self):
609 """
610 Perform different actions on replication pair based on parameters passed in
611 the playbook
612 """
613 self.validate_input(self.module.params)
614 rcg_name = self.module.params['rcg_name']
615 rcg_id = self.module.params['rcg_id']
616 pair_name = self.module.params['pair_name']
617 pair_id = self.module.params['pair_id']
618 pairs = self.module.params['pairs']
619 pause = self.module.params['pause']
620 state = self.module.params['state']
621
622 changed = False
623 result = dict(
624 changed=False,
625 replication_pair_details=[],
626 rcg_replication_pairs=[]
627 )
628
629 if pair_id or pair_name:
630 result['replication_pair_details'] = self.get_replication_pair(pair_name, pair_id)
631 if result['replication_pair_details']:
632 pair_id = result['replication_pair_details']['id']
633 if pairs:
634 rcg_id = self.get_rcg(rcg_name, rcg_id)['id']
635 result['rcg_replication_pairs'] = self.get_rcg_replication_pairs(rcg_id)
636 changed = self.create_replication_pairs(rcg_id, result['rcg_replication_pairs'], pairs)
637 if changed:
638 result['rcg_replication_pairs'] = self.get_rcg_replication_pairs(rcg_id)
639 if pause is not None:
640 changed = self.validate_pause_or_resume(pause, result['replication_pair_details'], pair_id)
641 if state == 'absent' and result['replication_pair_details']:
642 changed = self.delete_pair(pair_id)
643 if changed and (pair_id or pair_name):
644 result['replication_pair_details'] = self.get_replication_pair(pair_name, pair_id)
645 result['changed'] = changed
646 self.module.exit_json(**result)
647
648
649 def find_non_existing_pairs(rcg_pairs, input_pairs):
650 for pair in rcg_pairs:
651 for input_pair in list(input_pairs):
652 if input_pair['source_volume_id'] == pair['localVolumeId'] and \
653 input_pair['target_volume_id'] == pair['remoteVolumeId']:
654 input_pairs.remove(input_pair)
655 return input_pairs
656
657
658 def get_powerflex_replication_pair_parameters():
659 """This method provide parameter required for the replication_consistency_group
660 module on PowerFlex"""
661 return dict(pair_id=dict(), pair_name=dict(), pause=dict(type='bool'),
662 state=dict(choices=['absent', 'present'], default='present'), rcg_id=dict(), rcg_name=dict(),
663 remote_peer=dict(type='dict',
664 options=dict(hostname=dict(type='str', aliases=['gateway_host'], required=True),
665 username=dict(type='str', required=True),
666 password=dict(type='str', required=True, no_log=True),
667 validate_certs=dict(type='bool', aliases=['verifycert'], default=True),
668 port=dict(type='int', default=443),
669 timeout=dict(type='int', default=120))),
670 pairs=dict(
671 type='list', elements='dict',
672 options=dict(source_volume_name=dict(),
673 source_volume_id=dict(),
674 target_volume_name=dict(),
675 target_volume_id=dict(),
676 copy_type=dict(required=True, choices=['Identical', 'OnlineCopy', 'OnlineHashCopy', 'OfflineCopy']),
677 name=dict(),)
678 ))
679
680
681 def main():
682 """ Create PowerFlex Replication Consistency Group object and perform actions on it
683 based on user input from playbook"""
684 obj = PowerFlexReplicationPair()
685 obj.perform_module_operation()
686
687
688 if __name__ == '__main__':
689 main()