Debian Codegraph @

ansible_collections/dellemc/unity/plugins/modules/replication_session.py

raw symbols SCIP
1 #!/usr/bin/python
2 # Copyright: (c) 2023-2025, Dell Technologies
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 """Ansible module for managing replication session on Unity"""
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 DOCUMENTATION = r"""
12
13 module: replication_session
14 version_added: '1.7.0'
15 short_description: Manage replication session on Unity storage system
16 description:
17 - Managing replication session on Unity storage system includes getting details, pause,
18 resume, sync, failover, failback and deleting the replication session.
19
20 extends_documentation_fragment:
21 - dellemc.unity.unity
22
23 author:
24 - Jennifer John (@Jennifer-John) <[email protected]>
25
26 options:
27 session_id:
28 description:
29 - ID of replication session.
30 type: str
31 session_name:
32 description:
33 - Name of replication session.
34 type: str
35 pause:
36 description:
37 - Pause or resume replication session.
38 type: bool
39 sync:
40 description:
41 - Sync a replication session.
42 type: bool
43 failover_with_sync:
44 description:
45 - If C(true), Sync the source and destination resources before failing over the asynchronous
46 replication session or keep them in sync after failing over the synchronous
47 replication session.
48 - If C(false), Failover a replication session.
49 type: bool
50 failback:
51 description:
52 - Failback a replication session.
53 type: bool
54 force_full_copy:
55 description:
56 - Indicates whether to sync back all data from the destination SP to the source
57 SP during the failback session. Needed during resume operation when replication
58 session goes out of sync due to a fault.
59 type: bool
60 force:
61 description:
62 - Skip pre-checks on file system(s) replication sessions of a NAS server when a
63 replication failover is issued from the source NAS server.
64 type: bool
65 state:
66 description:
67 - State variable to determine whether replication session will exist or not.
68 choices: ['absent', 'present']
69 default: present
70 type: str
71
72 notes:
73 - The I(check_mode) is supported.
74 """
75
76 EXAMPLES = r"""
77 - name: Get replication session details
78 replication_session:
79 unispherehost: "{{unispherehost}}"
80 username: "{{username}}"
81 password: "{{password}}"
82 validate_certs: "{{validate_certs}}"
83 session_name: "fs_replication"
84
85 - name: Get replication session details based on session_id
86 replication_session:
87 unispherehost: "{{unispherehost}}"
88 username: "{{username}}"
89 password: "{{password}}"
90 validate_certs: "{{validate_certs}}"
91 session_id: "103079215114_APM00213404195_0000_103079215274_APM00213404194_0000"
92
93 - name: Pause a replication session
94 replication_session:
95 unispherehost: "{{unispherehost}}"
96 username: "{{username}}"
97 password: "{{password}}"
98 validate_certs: "{{validate_certs}}"
99 session_name: "fs_replication"
100 pause: true
101
102 - name: Resume a replication session
103 replication_session:
104 unispherehost: "{{unispherehost}}"
105 username: "{{username}}"
106 password: "{{password}}"
107 validate_certs: "{{validate_certs}}"
108 session_name: "fs_replication"
109 pause: false
110 force_full_copy: true
111
112 - name: Sync a replication session
113 replication_session:
114 unispherehost: "{{unispherehost}}"
115 username: "{{username}}"
116 password: "{{password}}"
117 validate_certs: "{{validate_certs}}"
118 session_name: "fs_replication"
119 sync: true
120
121 - name: Failover with sync a replication session
122 replication_session:
123 unispherehost: "{{unispherehost}}"
124 username: "{{username}}"
125 password: "{{password}}"
126 validate_certs: "{{validate_certs}}"
127 session_name: "fs_replication"
128 failover_with_sync: true
129 force: true
130
131 - name: Failover a replication session
132 replication_session:
133 unispherehost: "{{unispherehost}}"
134 username: "{{username}}"
135 password: "{{password}}"
136 validate_certs: "{{validate_certs}}"
137 session_name: "fs_replication"
138 failover_with_sync: false
139
140 - name: Failback a replication session
141 replication_session:
142 unispherehost: "{{unispherehost}}"
143 username: "{{username}}"
144 password: "{{password}}"
145 validate_certs: "{{validate_certs}}"
146 session_name: "fs_replication"
147 failback: true
148 force_full_copy: true
149
150 - name: Delete a replication session
151 replication_session:
152 unispherehost: "{{unispherehost}}"
153 username: "{{username}}"
154 password: "{{password}}"
155 validate_certs: "{{validate_certs}}"
156 session_name: "fs_replication"
157 state: "absent"
158 """
159
160 RETURN = r'''
161
162 changed:
163 description: Whether or not the resource has changed.
164 returned: always
165 type: bool
166 sample: true
167
168 replication_session_details:
169 description: Details of the replication session.
170 returned: When replication session exists.
171 type: dict
172 contains:
173 id:
174 description: Unique identifier of the replicationSession instance.
175 type: str
176 name:
177 description: User-specified replication session name.
178 type: str
179 replicationResourceType:
180 description: Replication resource type of replication session endpoints.
181 type: str
182 status:
183 description: Replication status of the replication session.
184 type: str
185 remoteSystem:
186 description: Specifies the remote system to use as the destination for the replication session.
187 type: dict
188 contains:
189 UnityRemoteSystem:
190 description: Information about remote storage system.
191 type: dict
192 contains:
193 id:
194 description: Unique identifier of the remote system instance.
195 type: str
196 serialNumber:
197 description: Serial number of the remote system.
198 type: str
199 maxTimeOutOfSync:
200 description: Maximum time to wait before the system syncs the source and destination resources.
201 type: int
202 srcStatus:
203 description: Status of the source end of the session.
204 type: str
205 networkStatus:
206 description: Status of the network connection used by the replication session.
207 type: str
208 dstStatus:
209 description: Status of the destination end of the replication session.
210 type: str
211 lastSyncTime:
212 description: Date and time of the last replication synchronization.
213 type: str
214 syncState:
215 description: Synchronization state between source and destination resource of the replication session.
216 type: str
217 syncProgress:
218 description: Synchronization completion percentage between source and destination resources of the replication session.
219 type: int
220 dstResourceId:
221 description: Identifier of the destination resource.
222 type: str
223 currentTransferEstRemainTime:
224 description: Estimated time left for the replication synchronization to complete.
225 type: int
226 sample: {
227 "current_transfer_est_remain_time": 0,
228 "daily_snap_replication_policy": null,
229 "dst_resource_id": "nas_8",
230 "dst_spa_interface": {
231 "UnityRemoteInterface": {
232 "hash": 8771253398547,
233 "id": "APM00213404195:if_181"
234 }
235 },
236 "dst_spb_interface": {
237 "UnityRemoteInterface": {
238 "hash": 8771253424144,
239 "id": "APM00213404195:if_180"
240 }
241 },
242 "dst_status": "ReplicationSessionStatusEnum.OK",
243 "existed": true,
244 "hash": 8771259012271,
245 "health": {
246 "UnityHealth": {
247 "hash": 8771253424168
248 }
249 },
250 "hourly_snap_replication_policy": null,
251 "id": "103079215114_APM00213404195_0000_103079215274_APM00213404194_0000",
252 "last_sync_time": "2023-04-18 10:35:25+00:00",
253 "local_role": "ReplicationSessionReplicationRoleEnum.DESTINATION",
254 "max_time_out_of_sync": 0,
255 "members": null,
256 "name": "rep_sess_nas",
257 "network_status": "ReplicationSessionNetworkStatusEnum.OK",
258 "remote_system": {
259 "UnityRemoteSystem": {
260 "hash": 8771253380142
261 }
262 },
263 "replication_resource_type": "ReplicationEndpointResourceTypeEnum.NASSERVER",
264 "src_resource_id": "nas_213",
265 "src_spa_interface": {
266 "UnityRemoteInterface": {
267 "hash": 8771253475010,
268 "id": "APM00213404194:if_195"
269 }
270 },
271 "src_spb_interface": {
272 "UnityRemoteInterface": {
273 "hash": 8771253374169,
274 "id": "APM00213404194:if_194"
275 }
276 },
277 "src_status": "ReplicationSessionStatusEnum.OK",
278 "status": "ReplicationOpStatusEnum.ACTIVE",
279 "sync_progress": 0,
280 "sync_state": "ReplicationSessionSyncStateEnum.IN_SYNC"
281 }
282 '''
283
284 from ansible.module_utils.basic import AnsibleModule
285 from ansible_collections.dellemc.unity.plugins.module_utils.storage.dell \
286 import utils
287
288 LOG = utils.get_logger('replication_session')
289
290 application_type = "Ansible/1.7.0"
291
292
293 class ReplicationSession(object):
294
295 """Class with replication session operations"""
296
297 def __init__(self):
298 """Define all parameters required by this module"""
299 self.module_params = utils.get_unity_management_host_parameters()
300 self.module_params.update(get_replication_session_parameters())
301
302 mutually_exclusive = [['session_id', 'session_name']]
303
304 required_one_of = [['session_id', 'session_name']]
305
306 # initialize the Ansible module
307 self.module = AnsibleModule(
308 argument_spec=self.module_params,
309 supports_check_mode=True,
310 mutually_exclusive=mutually_exclusive,
311 required_one_of=required_one_of)
312 utils.ensure_required_libs(self.module)
313 self.result = dict(
314 changed=False,
315 replication_session_details={}
316 )
317
318 self.unity_conn = utils.get_unity_unisphere_connection(
319 self.module.params, application_type)
320 LOG.info('Check Mode Flag %s', self.module.check_mode)
321
322 def get_replication_session(self, id=None, name=None):
323 """Get the details of a replication session.
324 :param id: The id of the replication session
325 :param name: The name of the replication session
326 :return: instance of the replication session if exist.
327 """
328
329 id_or_name = id if id else name
330 errormsg = f"Retrieving details of replication session {id_or_name} failed with error"
331
332 try:
333 obj_replication_session = self.unity_conn.get_replication_session(name=name, _id=id)
334
335 LOG.info("Successfully retrieved the replication session object %s ", obj_replication_session)
336 if obj_replication_session.existed:
337 return obj_replication_session
338 except utils.HttpError as e:
339 if e.http_status == 401:
340 self.module.fail_json(msg=f"Incorrect username or password {str(e)}")
341 else:
342 msg = f"{errormsg} {str(e)}"
343 self.module.fail_json(msg=msg)
344 except utils.UnityResourceNotFoundError as e:
345 msg = f"{errormsg} {str(e)}"
346 LOG.error(msg)
347 return None
348 except Exception as e:
349 msg = f"{errormsg} {str(e)}"
350 LOG.error(msg)
351 self.module.fail_json(msg=msg)
352
353 def pause(self, session_obj):
354 """Pause the replication session.
355 :param session_obj: Replication session object
356 :return: True if pause is successful.
357 """
358 try:
359 LOG.info("Pause replication session %s", session_obj.name)
360 if session_obj.status.name != utils.ReplicationOpStatusEnum.PAUSED.name:
361 if not self.module.check_mode:
362 session_obj.pause()
363 return True
364 except Exception as e:
365 msg = f"Pause replication session {session_obj.name} failed with error {str(e)}"
366 LOG.error(msg)
367 self.module.fail_json(msg=msg)
368
369 def resume(self, session_obj, force_full_copy):
370 """Resume the replication session.
371 :param session_obj: Replication session object
372 :param force_full_copy: needed when replication session goes out of sync due to a fault.
373 :return: True if resume is successful.
374 """
375 try:
376 LOG.info("Resume replication session %s", session_obj.name)
377 if session_obj.status.name in (utils.ReplicationOpStatusEnum.PAUSED.name,
378 utils.ReplicationOpStatusEnum.FAILED_OVER.name,
379 utils.ReplicationOpStatusEnum.FAILED_OVER_WITH_SYNC.name):
380 if not self.module.check_mode:
381 session_obj.resume(force_full_copy=force_full_copy)
382 return True
383 except Exception as e:
384 msg = f"Resume replication session {session_obj.name} failed with error {str(e)}"
385 LOG.error(msg)
386 self.module.fail_json(msg=msg)
387
388 def sync(self, session_obj):
389 """Sync the replication session.
390 :param session_obj: Replication session object
391 :return: True if sync is successful.
392 """
393 try:
394 LOG.info("Sync replication session %s", session_obj.name)
395 if not self.module.check_mode:
396 session_obj.sync()
397 return True
398 except Exception as e:
399 msg = f"Sync replication session {session_obj.name} failed with error {str(e)}"
400 LOG.error(msg)
401 self.module.fail_json(msg=msg)
402
403 def failover(self, session_obj, sync_failover, force):
404 """Failover the replication session.
405 :param session_obj: Replication session object
406 :param sync_failover: To sync the source and destination resources
407 :param force: Skip pre-checks on file system(s) replication sessions of a NAS server
408 :return: True if failover is successful.
409 """
410 try:
411 LOG.info("Failover replication session %s", session_obj.name)
412 if (sync_failover and session_obj.status.name != utils.ReplicationOpStatusEnum.FAILED_OVER_WITH_SYNC.name) or \
413 (not sync_failover and session_obj.status.name != utils.ReplicationOpStatusEnum.FAILED_OVER.name):
414 if not self.module.check_mode:
415 session_obj.failover(sync=sync_failover, force=force)
416 return True
417 except Exception as e:
418 msg = f"Failover replication session {session_obj.name} failed with error {str(e)}"
419 LOG.error(msg)
420 self.module.fail_json(msg=msg)
421
422 def failback(self, session_obj, force_full_copy):
423 """Failback the replication session.
424 :param session_obj: Replication session object
425 :param force_full_copy: needed when replication session goes out of sync due to a fault.
426 :return: True if failback is successful.
427 """
428 try:
429 LOG.info("Failback replication session %s", session_obj.name)
430 if session_obj.status.name in (utils.ReplicationOpStatusEnum.FAILED_OVER.name,
431 utils.ReplicationOpStatusEnum.FAILED_OVER_WITH_SYNC.name,
432 utils.ReplicationOpStatusEnum.PAUSED.name):
433 if not self.module.check_mode:
434 session_obj.failback(force_full_copy=force_full_copy)
435 return True
436 except Exception as e:
437 msg = f"Failback replication session {session_obj.name} failed with error {str(e)}"
438 LOG.error(msg)
439 self.module.fail_json(msg=msg)
440
441 def delete(self, session_obj):
442 """Delete the replication session.
443 :param session_obj: Replication session object
444 :return: True if delete is successful.
445 """
446 try:
447 LOG.info("Delete replication session %s", session_obj.name)
448 if not self.module.check_mode:
449 session_obj.delete()
450 return True
451 except Exception as e:
452 msg = f"Deleting replication session {session_obj.name} failed with error {str(e)}"
453 LOG.error(msg)
454 self.module.fail_json(msg=msg)
455
456
457 def get_replication_session_parameters():
458 """This method provide parameters required for the ansible replication session
459 module on Unity"""
460 return dict(
461 session_id=dict(type='str'), session_name=dict(type='str'),
462 pause=dict(type='bool'), sync=dict(type='bool'),
463 force=dict(type='bool'), failover_with_sync=dict(type='bool'),
464 failback=dict(type='bool'), force_full_copy=dict(type='bool'),
465 state=dict(type='str', choices=['present', 'absent'], default='present')
466 )
467
468
469 class ReplicationSessionFailoverHandler():
470 def handle(self, session_object, session_params, replication_session_obj):
471 if replication_session_obj and session_params['state'] == 'present' and session_params['failover_with_sync'] is not None:
472 session_object.result['changed'] = \
473 session_object.failover(replication_session_obj, session_params['failover_with_sync'], session_params['force']) or False
474 if session_object.result['changed']:
475 replication_session_obj = session_object.get_replication_session(session_params['session_id'], session_params['session_name'])
476 ReplicationSessionFailbackHandler().handle(session_object, session_params, replication_session_obj)
477
478
479 class ReplicationSessionFailbackHandler():
480 def handle(self, session_object, session_params, replication_session_obj):
481 if replication_session_obj and session_params['state'] == 'present' and session_params['failback']:
482 session_object.result['changed'] = \
483 session_object.failback(replication_session_obj, session_params['force_full_copy']) or False
484 if session_object.result['changed']:
485 replication_session_obj = session_object.get_replication_session(session_params['session_id'], session_params['session_name'])
486 ReplicationSessionDeleteHandler().handle(session_object, session_params, replication_session_obj)
487
488
489 class ReplicationSessionSyncHandler():
490 def handle(self, session_object, session_params, replication_session_obj):
491 if replication_session_obj and session_params['state'] == 'present' and session_params['sync']:
492 session_object.result['changed'] = session_object.sync(replication_session_obj)
493 if session_object.result['changed']:
494 replication_session_obj = session_object.get_replication_session(session_params['session_id'], session_params['session_name'])
495 ReplicationSessionFailoverHandler().handle(session_object, session_params, replication_session_obj)
496
497
498 class ReplicationSessionDeleteHandler():
499 def handle(self, session_object, session_params, replication_session_obj):
500 if replication_session_obj and session_params['state'] == 'absent':
501 session_object.result['changed'] = session_object.delete(replication_session_obj)
502 if session_object.result['changed']:
503 replication_session_obj = session_object.get_replication_session(session_params['session_id'], session_params['session_name'])
504 ReplicationSessionExitHandler().handle(session_object, replication_session_obj)
505
506
507 class ReplicationSessionExitHandler():
508 def handle(self, session_object, replication_session_obj):
509 if replication_session_obj:
510 session_object.result['replication_session_details'] = replication_session_obj._get_properties()
511 session_object.module.exit_json(**session_object.result)
512
513
514 class ReplicationSessionResumeHandler():
515 def handle(self, session_object, session_params, replication_session_obj):
516 if replication_session_obj and session_params['state'] == 'present' and session_params['pause'] is False:
517 session_object.result['changed'] = \
518 session_object.resume(replication_session_obj, session_params['force_full_copy']) or False
519 if session_object.result['changed']:
520 replication_session_obj = session_object.get_replication_session(session_params['session_id'], session_params['session_name'])
521 ReplicationSessionSyncHandler().handle(session_object, session_params, replication_session_obj)
522
523
524 class ReplicationSessionPauseHandler():
525 def handle(self, session_object, session_params, replication_session_obj):
526 if replication_session_obj and session_params['state'] == 'present' and session_params['pause']:
527 session_object.result['changed'] = \
528 session_object.pause(replication_session_obj) or False
529 if session_object.result['changed']:
530 replication_session_obj = session_object.get_replication_session(session_params['session_id'], session_params['session_name'])
531 ReplicationSessionResumeHandler().handle(session_object, session_params, replication_session_obj)
532
533
534 class ReplicationSessionHandler():
535 def handle(self, session_object, session_params):
536 replication_session_obj = session_object.get_replication_session(session_params['session_id'], session_params['session_name'])
537 if session_params['state'] == 'present' and not replication_session_obj:
538 session_object.module.fail_json(msg=f"Replication session {session_params['session_id'] or session_params['session_name']} is invalid.")
539 ReplicationSessionPauseHandler().handle(session_object, session_params, replication_session_obj)
540
541
542 def main():
543 """ Create Unity replication session object and perform action on it
544 based on user input from playbook"""
545 obj = ReplicationSession()
546 ReplicationSessionHandler().handle(obj, obj.module.params)
547
548
549 if __name__ == '__main__':
550 main()